diskimage-builder/diskimage_builder/elements/pip-and-virtualenv/install.d/pip-and-virtualenv-source-install/04-install-pip
Ian Wienand 79d4113cbe pip-and-virtualenv : install python2 & 3, and default to 2
Recent changes in project-config have shown that we leave the system
in an inconsistent state when installing from source.  On fedora, we
will have installed the python2 packages, but then used $DIB_PYTHON to
install python3 pip from source!

This tries to clarify the situation.  As described in the document,
with package installs, we just install the $DIB_PYTHON packaged
versions.

Source installs want to take over the global namespace.  This is the
price you pay for running the latest versions outside package managers
:) The only sane thing seems to be for us to normalise python2 &
python3 versions of pip, setuptools and virtualenv and then hacking
things such that "/usr/bin/pip" and "/usr/bin/virtalenv" remain
defaulted to python2 versions.

Documentation is added

Change-Id: Ibc6572b89e256d1f48b7fe7c672b8b9524dc704f
2017-04-11 18:59:11 +10:00

116 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
set -x
fi
set -eu
set -o pipefail
if [[ $DISTRO_NAME =~ (opensuse|fedora|centos|centos7|rhel|rhel7) ]]; then
_do_py3=0
packages="python-virtualenv python-pip python-setuptools"
if [[ $DISTRO_NAME =~ (fedora) ]]; then
_do_py3=1
packages+=" python3-virtualenv python3-pip python3-setuptools"
fi
# GENERAL WARNING : mixing packaged python libraries with
# pip-installed versions always creates issues. Upstream
# openstack-infra uses this a lot (especially devstack) but be
# warned: here be dragons :)
# Firstly we want to install the system packages. Otherwise later
# on somebody does a "yum install python-virtualenv" and goes and
# overwrites the pip installed version with the packaged version,
# leading to all sorts of weird version issues.
if [[ $DISTRO_NAME = opensuse ]]; then
zypper -n install python-virtualenv python-pip python-setuptools
else
${YUM:-yum} install -y $packages
fi
# install the latest python2 pip; this overwrites packaged pip
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
# info in the egg for pip to be certain it has fully uninstalled
# the old package, for safety we clear it out by hand (this seems
# to have been a problem with very old to new updates,
# e.g. centos6 to current-era, but less so for smaller jumps).
# There is a bit of chicken-and-egg problem with pip in that it
# requires setuptools for some operations, such as wheel creation.
# But just installing setuptools shouldn't require setuptools
# itself, so we are safe for this small section.
rm -rf /usr/lib/python2.7/site-packages/setuptools*
pip install -U setuptools
if [[ $_do_py3 -eq 1 ]]; then
# Repeat above for python3
# You would think that installing python3 bits first, then
# python2 would work -- alas get-pip.py doesn't seem to leave
# python3 alone:
# https://github.com/pypa/pip/issues/4435
python3 /tmp/get-pip.py
rm -rf /usr/lib/python3.?/site-packages/setuptools*
pip3 install -U setuptools
# reclaim /usr/bin/pip back to pip2
ln -sf /usr/bin/pip2 /usr/bin/pip
fi
# now install latest virtualenv. it vendors stuff it needs so
# doesn't have issues with other system packages.
# python[2|3]-virtualenv package has installed versioned scripts
# (/usr/bin/virtualenv-[2|3]) but upstream does not! (see [2]).
# For consistency, clear them out and then reinstall so we're just
# left with python2's version
# [2] http://pkgs.fedoraproject.org/cgit/rpms/python-virtualenv.git/tree/python-virtualenv.spec#n116)
rm /usr/bin/virtualenv*
if [[ $_do_py3 -eq 1 ]]; then
pip3 install -U virtualenv
fi
pip install -U virtualenv
# at this point, we should have the latest
# pip/setuptools/virtualenv packages for python2 & 3, and
# "/usr/bin/pip" and "/usr/bin/virtualenv" should be python2
# versions.
if [[ $DISTRO_NAME = opensuse ]]; then
for pkg in virtualenv pip setuptools; do
cat - >> /etc/zypp/locks <<EOF
type: package
match_type: glob
case_sensitive: on
solvable_name: python-$pkg
EOF
done
else
# Add this to exclude so that we don't install a later package
# over it if it updates. Note that fedora-minimal, bootstrapped
# via yum, can have an old yum.conf around, so look for dnf first.
if [[ -f /etc/dnf/dnf.conf ]]; then
conf=/etc/dnf/dnf.conf
elif [[ -f /etc/yum.conf ]]; then
conf=/etc/yum.conf
else
die "No conf to modify?"
fi
echo "exclude=$packages" >> ${conf}
fi
else
# pre-install packages, we will overwrite with latest below
apt-get -y install python-pip python3-pip \
python-virtualenv python3-virtualenv
# These install into /usr/local/bin so override any packages, even
# if installed later.
python3 /tmp/get-pip.py
python2 /tmp/get-pip.py
pip3 install virtualenv
pip install virtualenv
fi