From ffd4820d59dbd1a4000ff3536acb065326714932 Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Tue, 11 Apr 2017 10:38:22 +1000
Subject: [PATCH] Install pip with python interpreter

Currently we install pip/virtualenv with "/usr/local/bin/dib-python".
This means that every time you create a virtualenv, the python
interpreter inside it is called "dib-python" which is confusing.

Add an env var DIB_PYTHON that points directly the to interpreter
available during build, for use when running scripts.

Change-Id: I88ad3c9eb958d58db4631d9b27bc2c592f970345
---
 .../elements/dib-python/README.rst            | 22 ++++++++++++++-----
 .../environment.d/50-dib-python-version       |  5 +++++
 .../04-install-pip                            |  6 ++---
 3 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/diskimage_builder/elements/dib-python/README.rst b/diskimage_builder/elements/dib-python/README.rst
index 974a85599..2ed7b5ad9 100644
--- a/diskimage_builder/elements/dib-python/README.rst
+++ b/diskimage_builder/elements/dib-python/README.rst
@@ -2,11 +2,23 @@
 dib-python
 ==========
 
-Adds a symlink to /usr/local/bin/dib-python which points at either a python2
-or python3 executable. This is useful for creating #! lines for scripts that
-are compatible with both python2 and python3.
+Adds a symlink to `/usr/local/bin/dib-python` which points at either a
+`python2` or `python3` executable as appropriate.
+
+In-chroot scripts should use this as their interpreter
+(`#!/usr/local/bin/dib-python`) to make scripts that are compatible
+with both `python2` and `python3`.  We can not assume
+`/usr/bin/python` exists, as some platforms have started shipping with
+only Python 3.
+
+`DIB_PYTHON` will be exported as the python interpreter.  You should
+use this instead of `python script.py` (e.g. `${DIB_PYTHON}
+script.py`).  Note you can also call `/usr/local/bin/dib-python
+script.py` but in some circumstances, such as creating a `virtualenv`,
+it can create somewhat confusing references to `dib-python` that
+remain in the built image.
 
 This does not install a python if one does not exist, and instead fails.
 
-This also exports a variable DIB_PYTHON_VERSION which will either be '2' or
-'3' depending on the python version which dib-python points to.
+This also exports a variable `DIB_PYTHON_VERSION` which will either be
+'2' or '3' depending on the python version which dib-python points to.
diff --git a/diskimage_builder/elements/dib-python/environment.d/50-dib-python-version b/diskimage_builder/elements/dib-python/environment.d/50-dib-python-version
index 58a5dc7a9..6ab64d635 100644
--- a/diskimage_builder/elements/dib-python/environment.d/50-dib-python-version
+++ b/diskimage_builder/elements/dib-python/environment.d/50-dib-python-version
@@ -28,3 +28,8 @@ if [ -z "${DIB_PYTHON_VERSION:-}" ]; then
 fi
 
 export DIB_PYTHON_VERSION
+export DIB_PYTHON=python${DIB_PYTHON_VERSION}
+
+# Local variables:
+# mode: sh
+# End:
diff --git a/diskimage_builder/elements/pip-and-virtualenv/install.d/pip-and-virtualenv-source-install/04-install-pip b/diskimage_builder/elements/pip-and-virtualenv/install.d/pip-and-virtualenv-source-install/04-install-pip
index 4dbe0a084..cdeb442f7 100755
--- a/diskimage_builder/elements/pip-and-virtualenv/install.d/pip-and-virtualenv-source-install/04-install-pip
+++ b/diskimage_builder/elements/pip-and-virtualenv/install.d/pip-and-virtualenv-source-install/04-install-pip
@@ -22,8 +22,8 @@ if [[ $DISTRO_NAME =~ (opensuse|fedora|centos|centos7|rhel|rhel7) ]]; then
         ${YUM:-yum} install -y python-virtualenv python-pip python-setuptools
     fi
 
-    # install pip; this overwrites packaged pip
-    /usr/local/bin/dib-python /tmp/get-pip.py
+    # install the latest pip; this overwrites packaged pip
+    ${DIB_PYTHON} /tmp/get-pip.py
 
     # pip and setuptools are closely related; we want to ensure the
     # latest for sanity.  Because distro packages don't include enough
@@ -65,6 +65,6 @@ EOF
         echo "exclude=python-virtualenv,python-pip,python-setuptools" >> ${conf}
     fi
 else
-    /usr/local/bin/dib-python /tmp/get-pip.py
+    ${DIB_PYTHON} /tmp/get-pip.py
     pip install virtualenv
 fi