Comment 2 for bug 1745626

Revision history for this message
Jeremy Audet (ichimonji10) wrote :

> if there's some way we can know the canonical path to dib-block-device and others without this that would be good

Yes, it's possible to discover the canonical path to `dib-block-device` and other executables without using `activate_this.py`. In fact, the current code base already assumes that it can find the bin directory:

    activate_this = os.path.join(sys.prefix, "bin", "activate_this.py")

The big question is: what happens next?

The current code updates `os.environ['PATH']` by using runpy to execute `activate_this.py` and then injecting its namespace dictionary into the current namespace dictionary, overwriting any existing keys:

    globs = runpy.run_path(activate_this, globals())
    globals().update(globs)
    del globs

I see two problems with this approach:

* This doesn't work for virtualenvs created by `python3 -m venv`, as no `activate_this.py` script is present.
* Overwriting the current namespace dictionary with all of the values from another module's namespace dictionary is, uh... not how I'd do it.

Here's a different approach:

    bin_dir = os.path.abspath(os.path.join(sys.prefix, 'bin'))
    paths = os.environ['PATH'].split(':')
    if bin_dir not in paths:
        # If a dependent executable (like dib-block-device) is installed in
        # several locations, we want to find the one in the virtualenv's
        # bin directory. Thus, prepend to PATH, instead of appending.
        os.environ['PATH'] = bin_dir + os.pathsep + os.environ['PATH']

I've attached a patch that implements this change, along with some other nice changes. Let me know if the patch is in an inappropriate format. I generated the patch with a simple `git show`, but I'm unsure if this is the correct approach.

I tested the changes with `tox`. I've also tested the changes by executing the following:

    python3 -m venv ~/.venvs/diskimage-builder-py3/
    source ~/.venvs/diskimage-builder-py3/bin/activate
    pip install -e .
    mkdir ~/diskimage-builder
    cd ~/diskimage-builder
    disk-image-create -a amd64 -o ubuntu-amd64 vm ubuntu
    sudo cp ubuntu-amd64.qcow2 /var/lib/libvirt/images/

I successfully booted the image.