tools/install_pip.sh
62d1d698
 #!/usr/bin/env bash
 
 # **install_pip.sh**
 
 # Update pip and friends to a known common version
 
 # Assumptions:
ddc3839b
 # - if USE_PYTHON3=True, PYTHON3_VERSION refers to a version already installed
62d1d698
 
c85ade77
 set -o errexit
 
62d1d698
 # Keep track of the current directory
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
 TOP_DIR=`cd $TOOLS_DIR/..; pwd`
 
dc97cb71
 # Change dir to top of DevStack
62d1d698
 cd $TOP_DIR
 
 # Import common functions
05aa3846
 source $TOP_DIR/stackrc
62d1d698
 
646085d7
 # don't start tracing until after we've sourced the world
 set -o xtrace
 
62d1d698
 FILES=$TOP_DIR/files
 
0063495b
 # The URL from where the get-pip.py file gets downloaded. If a local
 # get-pip.py mirror is available, PIP_GET_PIP_URL can be set to that
 # mirror in local.conf to avoid download timeouts.
 # Example:
 #  PIP_GET_PIP_URL="http://local-server/get-pip.py"
 #
 # Note that if get-pip.py already exists in $FILES this script will
 # not re-download or check for a new version.  For example, this is
 # done by openstack-infra diskimage-builder elements as part of image
 # preparation [1].  This prevents any network access, which can be
 # unreliable in CI situations.
 # [1] http://git.openstack.org/cgit/openstack-infra/project-config/tree/nodepool/elements/cache-devstack/source-repository-pip
 
 PIP_GET_PIP_URL=${PIP_GET_PIP_URL:-"https://bootstrap.pypa.io/get-pip.py"}
f625ffe2
 LOCAL_PIP="$FILES/$(basename $PIP_GET_PIP_URL)"
62d1d698
 
 GetDistro
 echo "Distro: $DISTRO"
 
aee18c74
 function get_versions {
ddc3839b
     # FIXME(dhellmann): Deal with multiple python versions here? This
     # is just used for reporting, so maybe not?
46ea7238
     PIP=$(which pip 2>/dev/null || which pip-python 2>/dev/null || true)
62d1d698
     if [[ -n $PIP ]]; then
         PIP_VERSION=$($PIP --version | awk '{ print $2}')
dace92f5
         echo "pip: $PIP_VERSION"
46ea7238
     else
         echo "pip: Not Installed"
62d1d698
     fi
 }
 
 
aee18c74
 function install_get_pip {
0280f6f6
     # If get-pip.py isn't python, delete it. This was probably an
     # outage on the server.
     if [[ -r $LOCAL_PIP ]]; then
         if ! head -1 $LOCAL_PIP | grep -q '#!/usr/bin/env python'; then
             echo "WARNING: Corrupt $LOCAL_PIP found removing"
             rm $LOCAL_PIP
         fi
     fi
 
dc97cb71
     # The OpenStack gate and others put a cached version of get-pip.py
7c4ce9ed
     # for this to find, explicitly to avoid download issues.
     #
dc97cb71
     # However, if DevStack *did* download the file, we want to check
     # for updates; people can leave their stacks around for a long
7c4ce9ed
     # time and in the mean-time pip might get upgraded.
     #
     # Thus we use curl's "-z" feature to always check the modified
     # since and only download if a new version is out -- but only if
     # it seems we downloaded the file originally.
     if [[ ! -r $LOCAL_PIP || -r $LOCAL_PIP.downloaded ]]; then
a0cc2918
         # only test freshness if LOCAL_PIP is actually there,
         # otherwise we generate a scary warning.
         local timecond=""
         if [[ -r $LOCAL_PIP ]]; then
             timecond="-z $LOCAL_PIP"
         fi
 
fa41b5b4
         curl -f --retry 6 --retry-delay 5 \
a0cc2918
             $timecond -o $LOCAL_PIP $PIP_GET_PIP_URL || \
f625ffe2
             die $LINENO "Download of get-pip.py failed"
7c4ce9ed
         touch $LOCAL_PIP.downloaded
62d1d698
     fi
1d27155f
     sudo -H -E python $LOCAL_PIP -c $TOOLS_DIR/cap-pip.txt
ddc3839b
     if python3_enabled; then
1d27155f
         sudo -H -E python${PYTHON3_VERSION} $LOCAL_PIP -c $TOOLS_DIR/cap-pip.txt
ddc3839b
     fi
62d1d698
 }
 
 
683ff42d
 function configure_pypi_alternative_url {
     PIP_ROOT_FOLDER="$HOME/.pip"
     PIP_CONFIG_FILE="$PIP_ROOT_FOLDER/pip.conf"
     if [[ ! -d $PIP_ROOT_FOLDER ]]; then
         echo "Creating $PIP_ROOT_FOLDER"
         mkdir $PIP_ROOT_FOLDER
     fi
     if [[ ! -f $PIP_CONFIG_FILE ]]; then
         echo "Creating $PIP_CONFIG_FILE"
         touch $PIP_CONFIG_FILE
     fi
     if ! ini_has_option "$PIP_CONFIG_FILE" "global" "index-url"; then
dc97cb71
         # It means that the index-url does not exist
683ff42d
         iniset "$PIP_CONFIG_FILE" "global" "index-url" "$PYPI_OVERRIDE"
     fi
 
 }
 
35b5283a
 # Setuptools 8 implements PEP 440, and 8.0.4 adds a warning triggered any time
 # pkg_resources inspects the list of installed Python packages if there are
 # non-compliant version numbers in the egg-info (for example, from distro
 # system packaged Python libraries). This is off by default after 8.2 but can
 # be enabled by uncommenting the lines below.
 #PYTHONWARNINGS=$PYTHONWARNINGS,always::RuntimeWarning:pkg_resources
 #export PYTHONWARNINGS
683ff42d
 
62d1d698
 # Show starting versions
 get_versions
 
 # Do pip
 
dace92f5
 # Eradicate any and all system packages
9127c1a5
 
4404f680
 # Python in fedora/suse depends on the python-pip package so removing it
bd4048a3
 # results in a nonfunctional system. pip on fedora installs to /usr so pip
 # can safely override the system pip for all versions of fedora
4404f680
 if ! is_fedora  && ! is_suse; then
9127c1a5
     uninstall_package python-pip
ddc3839b
     uninstall_package python3-pip
9127c1a5
 fi
62d1d698
 
7b63c5ec
 install_get_pip
dace92f5
 
683ff42d
 if [[ -n $PYPI_ALTERNATIVE_URL ]]; then
     configure_pypi_alternative_url
 fi
 
ddc3839b
 set -x
1e7f738f
 
 # Note setuptools is part of requirements.txt and we want to make sure
 # we obey any versioning as described there.
 pip_install_gr setuptools
76ed427c
 
dace92f5
 get_versions