Remove virtualenv activation

Since the original merge of this code
(04208e7c79) 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
This commit is contained in:
Dmitry Tantsur 2020-05-29 12:22:01 +02:00 committed by Ian Wienand
parent 1e1e36df64
commit e793cc4038
5 changed files with 10 additions and 49 deletions

View File

@ -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]

View File

@ -1,7 +1,7 @@
- hosts: all
name: Run dib functional tests
roles:
- ensure-virtualenv
- ensure-pip
- bindep
- dib-setup-gate-mirrors
- dib-functests

View File

@ -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.

View File

@ -1,5 +1,4 @@
---
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

View File

@ -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