From e793cc4038c6b131b801e854b233fd07ab766bcf Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Fri, 29 May 2020 12:22:01 +0200 Subject: [PATCH] Remove virtualenv activation Since the original merge of this code (04208e7c79e52e2db905b973ab018deec6782260) several things have changed; particularly now we ship dib-run-parts as part of dib, not as a separate package. We setup $_LIB to point to the shipped library diretory via pkg_resources lookups. We now call dib-run-parts (as mentioned, shipped as a dib library now), source scripts, etc. via $_LIB and thus do not rely on $PATH. Consequently we don't need this activation part. Which is helpful, because "venv" (as opposed to virtualenv) doesn't have activate_this.py. So this fixes installation under that for Python 3. We update the functional tests to use the virtualenv_command exported by the ensure-pip role, which will test the venv path. There is no need for dib_python as we are Python 3 only now. Change-Id: Iede929ea2d278008220aac8b1d678ba41eba0d8a --- diskimage_builder/disk_image_create.py | 46 +------------------ playbooks/dib-functests/run.yaml | 2 +- ...irtualenv-activation-6de5738c9db8241d.yaml | 6 +++ roles/dib-functests/defaults/main.yaml | 3 +- roles/dib-functests/tasks/main.yaml | 2 +- 5 files changed, 10 insertions(+), 49 deletions(-) create mode 100644 releasenotes/notes/virtualenv-activation-6de5738c9db8241d.yaml diff --git a/diskimage_builder/disk_image_create.py b/diskimage_builder/disk_image_create.py index cd164d5c..589ccd87 100644 --- a/diskimage_builder/disk_image_create.py +++ b/diskimage_builder/disk_image_create.py @@ -12,58 +12,14 @@ # License for the specific language governing permissions and limitations # under the License. -from __future__ import print_function - import os import os.path -import runpy import sys import diskimage_builder.paths -# borrowed from pip:locations.py -def running_under_virtualenv(): - """Return True if we're running inside a virtualenv, False otherwise.""" - if hasattr(sys, 'real_prefix'): - return True - elif sys.prefix != getattr(sys, "base_prefix", sys.prefix): - return True - return False - - -def activate_venv(): - if running_under_virtualenv(): - activate_this = os.path.join(sys.prefix, "bin", "activate_this.py") - try: - globs = runpy.run_path(activate_this, globals()) - globals().update(globs) - del globs - # TODO(dtantsur): replace with FileNotFoundError when Python 2 is no - # longer supported. - except OSError: - print("WARNING: A virtual environment was detected, but the " - "activate_this.py script was not found. You may need to set " - "PATH manually", file=sys.stderr) - - def main(): - # If we are called directly from a venv install - # (/path/venv/bin/disk-image-create) then nothing has added the - # virtualenv bin/ dir to $PATH. the exec'd script below will be - # unable to find call other dib tools like dib-run-parts. - # - # One solution is to say that you should only ever run - # disk-image-create in a shell that has already sourced - # bin/activate.sh (all this really does is add /path/venv/bin to - # $PATH). That's not a great interface as resulting errors will - # be very non-obvious. - # - # We can detect if we are running in a virtualenv and use - # virtualenv's "activate_this.py" script to activate it ourselves - # before we call the script. This ensures we have the path setting - activate_venv() - environ = os.environ # pre-seed some paths for the shell script @@ -74,7 +30,7 @@ def main(): os.environ['DIB_PYTHON_EXEC'] = sys.executable # we have to handle being called as "disk-image-create" or - # "ramdisk-image-create". ramdisk-iamge-create is just a symlink + # "ramdisk-image-create". ramdisk-image-create is just a symlink # to disk-image-create # XXX: we could simplify things by removing the symlink, and # just setting IS_RAMDISK in environ here depending on sys.argv[1] diff --git a/playbooks/dib-functests/run.yaml b/playbooks/dib-functests/run.yaml index aa2f3c02..ca624b24 100644 --- a/playbooks/dib-functests/run.yaml +++ b/playbooks/dib-functests/run.yaml @@ -1,7 +1,7 @@ - hosts: all name: Run dib functional tests roles: - - ensure-virtualenv + - ensure-pip - bindep - dib-setup-gate-mirrors - dib-functests diff --git a/releasenotes/notes/virtualenv-activation-6de5738c9db8241d.yaml b/releasenotes/notes/virtualenv-activation-6de5738c9db8241d.yaml new file mode 100644 index 00000000..5f6e205c --- /dev/null +++ b/releasenotes/notes/virtualenv-activation-6de5738c9db8241d.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + This removes automatic detection and activation of virtualenv + environments, which is no longer necessary. This fixes installation + under Python 3's inbuilt ``venv`` module. diff --git a/roles/dib-functests/defaults/main.yaml b/roles/dib-functests/defaults/main.yaml index 56cb5b11..501d349c 100644 --- a/roles/dib-functests/defaults/main.yaml +++ b/roles/dib-functests/defaults/main.yaml @@ -1,8 +1,7 @@ --- -dib_python: python3 dib_virtualenv: "{{ ansible_user_dir }}/dib-venv" dib_functests: [] # This is a bit of hack; it allows us to template out jobs where the # base tests are defined in dib_functests, but then add a few extra # tests to run in this variable for a specific test. -dib_functests_extra: [] \ No newline at end of file +dib_functests_extra: [] diff --git a/roles/dib-functests/tasks/main.yaml b/roles/dib-functests/tasks/main.yaml index be7e253a..fecbf889 100644 --- a/roles/dib-functests/tasks/main.yaml +++ b/roles/dib-functests/tasks/main.yaml @@ -25,7 +25,7 @@ pip: name: "file://{{ ansible_user_dir }}/{{ zuul.project.src_dir }}" virtualenv: "{{ dib_virtualenv }}" - virtualenv_python: "{{ dib_python }}" + virtualenv_command: "{{ ensure_pip_virtualenv_command }}" extra_args: "-c {{ upper_constraints }}" - name: Setup log output