| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 | # | 
 | 3 | # **inc/python** - Python-related functions | 
 | 4 | # | 
 | 5 | # Support for pip/setuptools interfaces and virtual environments | 
 | 6 | # | 
 | 7 | # External functions used: | 
 | 8 | # - GetOSVersion | 
 | 9 | # - is_fedora | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 10 | # - safe_chown | 
 | 11 |  | 
 | 12 | # Save trace setting | 
 | 13 | INC_PY_TRACE=$(set +o | grep xtrace) | 
 | 14 | set +o xtrace | 
 | 15 |  | 
 | 16 |  | 
| Dean Troyer | 8c2ce6e | 2015-02-18 14:47:54 -0600 | [diff] [blame] | 17 | # Global Config Variables | 
 | 18 |  | 
| Atsushi SAKAI | 5509ed5 | 2015-11-30 20:20:21 +0900 | [diff] [blame] | 19 | # PROJECT_VENV contains the name of the virtual environment for each | 
| Dean Troyer | 8c2ce6e | 2015-02-18 14:47:54 -0600 | [diff] [blame] | 20 | # project.  A null value installs to the system Python directories. | 
| Sean Dague | afef8bf | 2017-03-06 14:07:23 -0500 | [diff] [blame] | 21 | declare -A -g PROJECT_VENV | 
| Dean Troyer | 8c2ce6e | 2015-02-18 14:47:54 -0600 | [diff] [blame] | 22 |  | 
| Radosław Piliszek | be26306 | 2020-03-30 09:56:53 +0200 | [diff] [blame] | 23 | # Utility Functions | 
 | 24 | # ================= | 
 | 25 |  | 
 | 26 | # Joins bash array of extras with commas as expected by other functions | 
 | 27 | function join_extras { | 
 | 28 |     local IFS="," | 
 | 29 |     echo "$*" | 
 | 30 | } | 
| Dean Troyer | 8c2ce6e | 2015-02-18 14:47:54 -0600 | [diff] [blame] | 31 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 32 | # Python Functions | 
 | 33 | # ================ | 
 | 34 |  | 
| Clark Boylan | a40f9cb | 2018-04-04 14:02:30 -0700 | [diff] [blame] | 35 | # Setup the global devstack virtualenvs and the associated environment | 
 | 36 | # updates. | 
 | 37 | function setup_devstack_virtualenv { | 
 | 38 |     # We run devstack out of a global virtualenv. | 
 | 39 |     if [[ ! -d $DEVSTACK_VENV ]] ; then | 
 | 40 |         # Using system site packages to enable nova to use libguestfs. | 
 | 41 |         # This package is currently installed via the distro and not | 
 | 42 |         # available on pypi. | 
 | 43 |         python$PYTHON3_VERSION -m venv --system-site-packages $DEVSTACK_VENV | 
| Sean Mooney | 41d253a | 2024-06-20 19:03:37 +0100 | [diff] [blame] | 44 |         pip_install -U pip setuptools | 
| Riccardo Pittau | 6990b06 | 2024-07-24 18:01:51 +0200 | [diff] [blame] | 45 |         #NOTE(rpittau): workaround for simplejson removal in osc | 
 | 46 |         #  https://review.opendev.org/c/openstack/python-openstackclient/+/920001 | 
 | 47 |         pip_install -U simplejson | 
| Clark Boylan | a40f9cb | 2018-04-04 14:02:30 -0700 | [diff] [blame] | 48 |     fi | 
 | 49 |     if [[ ":$PATH:" != *":$DEVSTACK_VENV/bin:"* ]] ; then | 
 | 50 |         export PATH="$DEVSTACK_VENV/bin:$PATH" | 
 | 51 |         export PYTHON="$DEVSTACK_VENV/bin/python3" | 
 | 52 |     fi | 
 | 53 | } | 
 | 54 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 55 | # Get the path to the pip command. | 
 | 56 | # get_pip_command | 
 | 57 | function get_pip_command { | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 58 |     local version="$1" | 
| Tom Barron | 4db9d56 | 2019-01-09 08:43:52 -0500 | [diff] [blame] | 59 |     if [ -z "$version" ]; then | 
 | 60 |         die $LINENO "pip python version is not set." | 
 | 61 |     fi | 
 | 62 |  | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 63 |     # NOTE(dhellmann): I don't know if we actually get a pip3.4-python | 
 | 64 |     # under any circumstances. | 
 | 65 |     which pip${version} || which pip${version}-python | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 66 |  | 
 | 67 |     if [ $? -ne 0 ]; then | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 68 |         die $LINENO "Unable to find pip${version}; cannot continue" | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 69 |     fi | 
 | 70 | } | 
 | 71 |  | 
| Atsushi SAKAI | 5509ed5 | 2015-11-30 20:20:21 +0900 | [diff] [blame] | 72 | # Get the path to the directory where python executables are installed. | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 73 | # get_python_exec_prefix | 
 | 74 | function get_python_exec_prefix { | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 75 |     local xtrace | 
 | 76 |     xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame] | 77 |     set +o xtrace | 
 | 78 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 79 |         GetOSVersion | 
 | 80 |     fi | 
 | 81 |     $xtrace | 
 | 82 |  | 
| Clark Boylan | a40f9cb | 2018-04-04 14:02:30 -0700 | [diff] [blame] | 83 |     if [[ "$GLOBAL_VENV" == "True" ]] ; then | 
 | 84 |         echo "$DEVSTACK_VENV/bin" | 
 | 85 |     else | 
 | 86 |         echo "/usr/local/bin" | 
 | 87 |     fi | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 88 | } | 
 | 89 |  | 
| Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 90 | # Wrapper for ``pip install`` that only installs versions of libraries | 
 | 91 | # from the global-requirements specification. | 
 | 92 | # | 
 | 93 | # Uses globals ``REQUIREMENTS_DIR`` | 
 | 94 | # | 
 | 95 | # pip_install_gr packagename | 
 | 96 | function pip_install_gr { | 
 | 97 |     local name=$1 | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 98 |     local clean_name | 
 | 99 |     clean_name=$(get_from_global_requirements $name) | 
| Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 100 |     pip_install $clean_name | 
 | 101 | } | 
 | 102 |  | 
| Mehdi Abaakouk | 52b1074 | 2016-12-01 16:11:17 +0100 | [diff] [blame] | 103 | # Wrapper for ``pip install`` that only installs versions of libraries | 
 | 104 | # from the global-requirements specification with extras. | 
 | 105 | # | 
 | 106 | # Uses globals ``REQUIREMENTS_DIR`` | 
 | 107 | # | 
 | 108 | # pip_install_gr_extras packagename extra1,extra2,... | 
 | 109 | function pip_install_gr_extras { | 
 | 110 |     local name=$1 | 
 | 111 |     local extras=$2 | 
| Radosław Piliszek | be26306 | 2020-03-30 09:56:53 +0200 | [diff] [blame] | 112 |     local version_constraints | 
 | 113 |     version_constraints=$(get_version_constraints_from_global_requirements $name) | 
 | 114 |     pip_install $name[$extras]$version_constraints | 
| Mehdi Abaakouk | 52b1074 | 2016-12-01 16:11:17 +0100 | [diff] [blame] | 115 | } | 
 | 116 |  | 
| Doug Hellmann | 36377f6 | 2018-12-04 11:33:03 -0500 | [diff] [blame] | 117 | # enable_python3_package() -- no-op for backwards compatibility | 
| Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame] | 118 | # | 
| Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame] | 119 | # enable_python3_package dir [dir ...] | 
 | 120 | function enable_python3_package { | 
 | 121 |     local xtrace | 
 | 122 |     xtrace=$(set +o | grep xtrace) | 
 | 123 |     set +o xtrace | 
 | 124 |  | 
| Doug Hellmann | 36377f6 | 2018-12-04 11:33:03 -0500 | [diff] [blame] | 125 |     echo "It is no longer necessary to call enable_python3_package()." | 
| Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame] | 126 |  | 
 | 127 |     $xtrace | 
 | 128 | } | 
 | 129 |  | 
| Stephen Finucane | 6b6bdc7 | 2019-10-09 16:13:20 +0100 | [diff] [blame] | 130 | # disable_python3_package() -- no-op for backwards compatibility | 
| Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame] | 131 | # | 
| Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame] | 132 | # disable_python3_package dir [dir ...] | 
 | 133 | function disable_python3_package { | 
 | 134 |     local xtrace | 
 | 135 |     xtrace=$(set +o | grep xtrace) | 
 | 136 |     set +o xtrace | 
 | 137 |  | 
| Stephen Finucane | 6b6bdc7 | 2019-10-09 16:13:20 +0100 | [diff] [blame] | 138 |     echo "It is no longer possible to call disable_python3_package()." | 
| Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame] | 139 |  | 
 | 140 |     $xtrace | 
 | 141 | } | 
 | 142 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 143 | # Wrapper for ``pip install`` to set cache and proxy environment variables | 
| Dean Troyer | 41d6f85 | 2015-03-25 22:42:46 -0500 | [diff] [blame] | 144 | # Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``, | 
| Ian Wienand | bcb2c30 | 2020-01-13 16:31:20 +1100 | [diff] [blame] | 145 | # ``PIP_UPGRADE``, ``*_proxy``, | 
| Zane Bitter | 9e7ead9 | 2017-10-05 16:51:09 -0400 | [diff] [blame] | 146 | # Usage: | 
 | 147 | #  pip_install pip_arguments | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 148 | function pip_install { | 
| Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 149 |     local xtrace result | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 150 |     xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 151 |     set +o xtrace | 
| Chris Dent | ebdd9ac | 2015-03-04 12:35:14 +0000 | [diff] [blame] | 152 |     local upgrade="" | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 153 |     local offline=${OFFLINE:-False} | 
 | 154 |     if [[ "$offline" == "True" || -z "$@" ]]; then | 
 | 155 |         $xtrace | 
 | 156 |         return | 
 | 157 |     fi | 
 | 158 |  | 
| Sean Dague | cb658fa | 2015-10-08 17:12:03 -0400 | [diff] [blame] | 159 |     time_start "pip_install" | 
 | 160 |  | 
| Chris Dent | ebdd9ac | 2015-03-04 12:35:14 +0000 | [diff] [blame] | 161 |     PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE) | 
 | 162 |     if [[ "$PIP_UPGRADE" = "True" ]] ; then | 
 | 163 |         upgrade="--upgrade" | 
 | 164 |     fi | 
 | 165 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 166 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 167 |         GetOSVersion | 
 | 168 |     fi | 
| Zane Bitter | 9e7ead9 | 2017-10-05 16:51:09 -0400 | [diff] [blame] | 169 |  | 
 | 170 |     # Try to extract the path of the package we are installing into | 
 | 171 |     # package_dir.  We need this to check for test-requirements.txt, | 
 | 172 |     # at least. | 
 | 173 |     # | 
 | 174 |     # ${!#} expands to the last positional argument to this function. | 
 | 175 |     # With "extras" syntax included, our arguments might be something | 
 | 176 |     # like: | 
 | 177 |     #  -e /path/to/fooproject[extra] | 
 | 178 |     # Thus this magic line grabs just the path without extras | 
 | 179 |     # | 
 | 180 |     # Note that this makes no sense if this is a pypi (rather than | 
 | 181 |     # local path) install; ergo you must check this path exists before | 
 | 182 |     # use.  Also, if we had multiple or mixed installs, we would also | 
 | 183 |     # likely break.  But for historical reasons, it's basically only | 
 | 184 |     # the other wrapper functions in here calling this to install | 
 | 185 |     # local packages, and they do so with single call per install.  So | 
 | 186 |     # this works (for now...) | 
 | 187 |     local package_dir=${!#%\[*\]} | 
 | 188 |  | 
| Ian Wienand | bcb2c30 | 2020-01-13 16:31:20 +1100 | [diff] [blame] | 189 |     if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then | 
 | 190 |         local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 191 |         local sudo_pip="env" | 
| Clark Boylan | a40f9cb | 2018-04-04 14:02:30 -0700 | [diff] [blame] | 192 |     elif [[ "${GLOBAL_VENV}" == "True" && -d ${DEVSTACK_VENV} ]] ; then | 
 | 193 |         # We have to check that the DEVSTACK_VENV exists because early | 
 | 194 |         # devstack boostrapping needs to operate in a system context | 
 | 195 |         # too bootstrap pip. Once pip is bootstrapped we create the | 
 | 196 |         # global venv and can start to use it. | 
 | 197 |         local cmd_pip=$DEVSTACK_VENV/bin/pip | 
 | 198 |         local sudo_pip="env" | 
 | 199 |         echo "Using python $PYTHON3_VERSION to install $package_dir" | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 200 |     else | 
| Jens Harbott | d7a82f4 | 2020-06-23 10:21:09 +0200 | [diff] [blame] | 201 |         local cmd_pip="python$PYTHON3_VERSION -m pip" | 
| Ian Wienand | 18b4251 | 2020-08-31 16:22:57 +1000 | [diff] [blame] | 202 |         # See | 
 | 203 |         #  https://github.com/pypa/setuptools/issues/2232 | 
 | 204 |         #  http://lists.openstack.org/pipermail/openstack-discuss/2020-August/016905.html | 
 | 205 |         # this makes setuptools >=50 use the platform distutils. | 
 | 206 |         # We only want to do this on global pip installs, not if | 
 | 207 |         # installing in a virtualenv | 
 | 208 |         local sudo_pip="sudo -H LC_ALL=en_US.UTF-8 SETUPTOOLS_USE_DISTUTILS=stdlib " | 
| Jens Harbott | d7a82f4 | 2020-06-23 10:21:09 +0200 | [diff] [blame] | 209 |         echo "Using python $PYTHON3_VERSION to install $package_dir" | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 210 |     fi | 
 | 211 |  | 
| Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 212 |     cmd_pip="$cmd_pip install" | 
| Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 213 |     # Always apply constraints | 
 | 214 |     cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt" | 
| Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 215 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 216 |     $xtrace | 
| Clark Boylan | f266a2d | 2017-06-12 14:57:59 -0700 | [diff] [blame] | 217 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 218 |     $sudo_pip \ | 
| Eli Qiao | 6a83c42 | 2015-03-17 16:54:16 +0800 | [diff] [blame] | 219 |         http_proxy="${http_proxy:-}" \ | 
 | 220 |         https_proxy="${https_proxy:-}" \ | 
 | 221 |         no_proxy="${no_proxy:-}" \ | 
| Joe Gordon | cd8824a | 2015-03-04 16:40:19 -0800 | [diff] [blame] | 222 |         PIP_FIND_LINKS=$PIP_FIND_LINKS \ | 
| Monty Taylor | 09b5b05 | 2020-03-27 11:22:39 -0500 | [diff] [blame] | 223 |         $cmd_pip $upgrade \ | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 224 |         $@ | 
| Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 225 |     result=$? | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 226 |  | 
| Sean Dague | cb658fa | 2015-10-08 17:12:03 -0400 | [diff] [blame] | 227 |     time_stop "pip_install" | 
| Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 228 |     return $result | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 229 | } | 
 | 230 |  | 
| Sean Dague | f28e7ef | 2017-05-07 22:02:10 -0400 | [diff] [blame] | 231 | function pip_uninstall { | 
| Sampath Priyankara | 87d2396 | 2017-08-03 16:12:40 +0900 | [diff] [blame] | 232 |     # Skip uninstall if offline | 
 | 233 |     [[ "${OFFLINE}" = "True" ]] && return | 
 | 234 |  | 
| Sean Dague | f28e7ef | 2017-05-07 22:02:10 -0400 | [diff] [blame] | 235 |     local name=$1 | 
 | 236 |     if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then | 
 | 237 |         local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip | 
 | 238 |         local sudo_pip="env" | 
 | 239 |     else | 
| Jens Harbott | d7a82f4 | 2020-06-23 10:21:09 +0200 | [diff] [blame] | 240 |         local cmd_pip="python$PYTHON3_VERSION -m pip" | 
 | 241 |         local sudo_pip="sudo -H LC_ALL=en_US.UTF-8" | 
| Sean Dague | f28e7ef | 2017-05-07 22:02:10 -0400 | [diff] [blame] | 242 |     fi | 
 | 243 |     # don't error if we can't uninstall, it might not be there | 
| Brian Haley | 954fd1b | 2017-05-16 12:24:45 -0400 | [diff] [blame] | 244 |     $sudo_pip $cmd_pip uninstall -y $name || /bin/true | 
| Sean Dague | f28e7ef | 2017-05-07 22:02:10 -0400 | [diff] [blame] | 245 | } | 
 | 246 |  | 
| Joe Gordon | d5ac785 | 2015-02-06 19:29:23 -0800 | [diff] [blame] | 247 | # get version of a package from global requirements file | 
 | 248 | # get_from_global_requirements <package> | 
 | 249 | function get_from_global_requirements { | 
 | 250 |     local package=$1 | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 251 |     local required_pkg | 
 | 252 |     required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1) | 
| Joe Gordon | d5ac785 | 2015-02-06 19:29:23 -0800 | [diff] [blame] | 253 |     if [[ $required_pkg == ""  ]]; then | 
 | 254 |         die $LINENO "Can't find package $package in requirements" | 
 | 255 |     fi | 
 | 256 |     echo $required_pkg | 
 | 257 | } | 
 | 258 |  | 
| Radosław Piliszek | be26306 | 2020-03-30 09:56:53 +0200 | [diff] [blame] | 259 | # get only version constraints of a package from global requirements file | 
 | 260 | # get_version_constraints_from_global_requirements <package> | 
 | 261 | function get_version_constraints_from_global_requirements { | 
 | 262 |     local package=$1 | 
 | 263 |     local required_pkg_version_constraint | 
 | 264 |     # drop the package name from output (\K) | 
 | 265 |     required_pkg_version_constraint=$(grep -i -h -o -P "^${package}\K.*" $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1) | 
 | 266 |     if [[ $required_pkg_version_constraint == ""  ]]; then | 
 | 267 |         die $LINENO "Can't find package $package in requirements" | 
 | 268 |     fi | 
 | 269 |     echo $required_pkg_version_constraint | 
 | 270 | } | 
 | 271 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 272 | # should we use this library from their git repo, or should we let it | 
 | 273 | # get pulled in via pip dependencies. | 
 | 274 | function use_library_from_git { | 
 | 275 |     local name=$1 | 
 | 276 |     local enabled=1 | 
| Marc Koderer | 46f8cb7 | 2016-05-13 09:08:16 +0200 | [diff] [blame] | 277 |     [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0 | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 278 |     return $enabled | 
 | 279 | } | 
 | 280 |  | 
| Sean Dague | c71973e | 2015-09-08 07:12:48 -0400 | [diff] [blame] | 281 | # determine if a package was installed from git | 
 | 282 | function lib_installed_from_git { | 
 | 283 |     local name=$1 | 
| DamonLi | 007f588 | 2017-11-23 10:05:46 +0800 | [diff] [blame] | 284 |     local safe_name | 
 | 285 |     safe_name=$(python -c "from pkg_resources import safe_name; \ | 
 | 286 |         print(safe_name('${name}'))") | 
| Ian Wienand | ae9c6ab | 2017-09-29 10:16:47 +1000 | [diff] [blame] | 287 |     # Note "pip freeze" doesn't always work here, because it tries to | 
 | 288 |     # be smart about finding the remote of the git repo the package | 
 | 289 |     # was installed from.  This doesn't work with zuul which clones | 
 | 290 |     # repos with no remote. | 
 | 291 |     # | 
 | 292 |     # The best option seems to be to use "pip list" which will tell | 
 | 293 |     # you the path an editable install was installed from; for example | 
 | 294 |     # in response to something like | 
| Matt Riedemann | 9b6d2f2 | 2019-06-18 10:43:16 -0400 | [diff] [blame] | 295 |     #  pip install -e 'git+https://opendev.org/openstack/bashate#egg=bashate' | 
| Monty Taylor | f0cd9a8 | 2017-10-06 13:11:48 -0500 | [diff] [blame] | 296 |     # pip list --format columns shows | 
 | 297 |     #  bashate 0.5.2.dev19 /tmp/env/src/bashate | 
 | 298 |     # Thus we check the third column to see if we're installed from | 
 | 299 |     # some local place. | 
| DamonLi | 007f588 | 2017-11-23 10:05:46 +0800 | [diff] [blame] | 300 |     [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]] | 
| Sean Dague | c71973e | 2015-09-08 07:12:48 -0400 | [diff] [blame] | 301 | } | 
 | 302 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 303 | # setup a library by name. If we are trying to use the library from | 
 | 304 | # git, we'll do a git based install, otherwise we'll punt and the | 
 | 305 | # library should be installed by a requirements pull from another | 
 | 306 | # project. | 
 | 307 | function setup_lib { | 
 | 308 |     local name=$1 | 
 | 309 |     local dir=${GITDIR[$name]} | 
 | 310 |     setup_install $dir | 
 | 311 | } | 
 | 312 |  | 
| Atsushi SAKAI | 5509ed5 | 2015-11-30 20:20:21 +0900 | [diff] [blame] | 313 | # setup a library by name in editable mode. If we are trying to use | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 314 | # the library from git, we'll do a git based install, otherwise we'll | 
 | 315 | # punt and the library should be installed by a requirements pull from | 
 | 316 | # another project. | 
 | 317 | # | 
 | 318 | # use this for non namespaced libraries | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 319 | # | 
| Radosław Piliszek | be26306 | 2020-03-30 09:56:53 +0200 | [diff] [blame] | 320 | # setup_dev_lib [-bindep] <name> [<extras>] | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 321 | function setup_dev_lib { | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 322 |     local bindep | 
 | 323 |     if [[ $1 == -bindep* ]]; then | 
 | 324 |         bindep="${1}" | 
 | 325 |         shift | 
 | 326 |     fi | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 327 |     local name=$1 | 
 | 328 |     local dir=${GITDIR[$name]} | 
| Radosław Piliszek | be26306 | 2020-03-30 09:56:53 +0200 | [diff] [blame] | 329 |     local extras=$2 | 
 | 330 |     setup_develop $bindep $dir $extras | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 331 | } | 
 | 332 |  | 
 | 333 | # this should be used if you want to install globally, all libraries should | 
 | 334 | # use this, especially *oslo* ones | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 335 | # | 
 | 336 | # setup_install project_dir [extras] | 
 | 337 | # project_dir: directory of project repo (e.g., /opt/stack/keystone) | 
 | 338 | # extras: comma-separated list of optional dependencies to install | 
 | 339 | #         (e.g., ldap,memcache). | 
| Takashi NATSUME | fa00777 | 2017-07-22 08:59:43 +0900 | [diff] [blame] | 340 | #         See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 341 | # bindep: Set "-bindep" as first argument to install bindep.txt packages | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 342 | # The command is like "pip install <project_dir>[<extras>]" | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 343 | function setup_install { | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 344 |     local bindep | 
 | 345 |     if [[ $1 == -bindep* ]]; then | 
 | 346 |         bindep="${1}" | 
 | 347 |         shift | 
 | 348 |     fi | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 349 |     local project_dir=$1 | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 350 |     local extras=$2 | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 351 |     _setup_package_with_constraints_edit $bindep $project_dir "" $extras | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 352 | } | 
 | 353 |  | 
 | 354 | # this should be used for projects which run services, like all services | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 355 | # | 
 | 356 | # setup_develop project_dir [extras] | 
 | 357 | # project_dir: directory of project repo (e.g., /opt/stack/keystone) | 
 | 358 | # extras: comma-separated list of optional dependencies to install | 
 | 359 | #         (e.g., ldap,memcache). | 
| Takashi NATSUME | fa00777 | 2017-07-22 08:59:43 +0900 | [diff] [blame] | 360 | #         See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 361 | # The command is like "pip install -e <project_dir>[<extras>]" | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 362 | function setup_develop { | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 363 |     local bindep | 
 | 364 |     if [[ $1 == -bindep* ]]; then | 
 | 365 |         bindep="${1}" | 
 | 366 |         shift | 
 | 367 |     fi | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 368 |     local project_dir=$1 | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 369 |     local extras=$2 | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 370 |     _setup_package_with_constraints_edit $bindep $project_dir -e $extras | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 371 | } | 
 | 372 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 373 | # ``pip install -e`` the package, which processes the dependencies | 
 | 374 | # using pip before running `setup.py develop` | 
 | 375 | # | 
| Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 376 | # Updates the constraints from REQUIREMENTS_DIR to reflect the | 
 | 377 | # future installed state of this package. This ensures when we | 
 | 378 | # install this package we get the from source version. | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 379 | # | 
| Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 380 | # Uses globals ``REQUIREMENTS_DIR`` | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 381 | # _setup_package_with_constraints_edit project_dir flags [extras] | 
 | 382 | # project_dir: directory of project repo (e.g., /opt/stack/keystone) | 
 | 383 | # flags: pip CLI options/flags | 
 | 384 | # extras: comma-separated list of optional dependencies to install | 
 | 385 | #         (e.g., ldap,memcache). | 
| Takashi NATSUME | fa00777 | 2017-07-22 08:59:43 +0900 | [diff] [blame] | 386 | #         See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 387 | # The command is like "pip install <flags> <project_dir>[<extras>]" | 
 | 388 | function _setup_package_with_constraints_edit { | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 389 |     local bindep | 
 | 390 |     if [[ $1 == -bindep* ]]; then | 
 | 391 |         bindep="${1}" | 
 | 392 |         shift | 
 | 393 |     fi | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 394 |     local project_dir=$1 | 
 | 395 |     local flags=$2 | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 396 |     local extras=$3 | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 397 |  | 
| YAMAMOTO Takashi | c8c1c61 | 2016-03-22 14:29:47 +0900 | [diff] [blame] | 398 |     # Normalize the directory name to avoid | 
 | 399 |     # "installation from path or url cannot be constrained to a version" | 
 | 400 |     # error. | 
 | 401 |     # REVISIT(yamamoto): Remove this when fixed in pip. | 
 | 402 |     # https://github.com/pypa/pip/pull/3582 | 
 | 403 |     project_dir=$(cd $project_dir && pwd) | 
 | 404 |  | 
| Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 405 |     if [ -n "$REQUIREMENTS_DIR" ]; then | 
| Ian Wienand | 6b9a564 | 2021-07-28 11:19:57 +1000 | [diff] [blame] | 406 |         # Remove this package from constraints before we install it. | 
 | 407 |         # That way, later installs won't "downgrade" the install from | 
 | 408 |         # source we are about to do. | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 409 |         local name | 
 | 410 |         name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg) | 
| Slawek Kaplonski | 4ddd456 | 2024-02-09 14:11:44 +0100 | [diff] [blame] | 411 |         if [ -z $name ]; then | 
 | 412 |             name=$(awk '/^name =/ {gsub(/"/, "", $3); print $3}' $project_dir/pyproject.toml) | 
 | 413 |         fi | 
| Robert Collins | 7c83861 | 2015-07-03 13:28:09 +1200 | [diff] [blame] | 414 |         $REQUIREMENTS_DIR/.venv/bin/edit-constraints \ | 
| Ian Wienand | 6b9a564 | 2021-07-28 11:19:57 +1000 | [diff] [blame] | 415 |             $REQUIREMENTS_DIR/upper-constraints.txt -- $name | 
| Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 416 |     fi | 
 | 417 |  | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 418 |     setup_package $bindep $project_dir "$flags" $extras | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 419 |  | 
| James E. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 420 |     # If this project is in LIBS_FROM_GIT, verify it was actually installed | 
 | 421 |     # correctly.  This helps catch errors caused by constraints mismatches. | 
 | 422 |     if use_library_from_git "$project_dir"; then | 
 | 423 |         if ! lib_installed_from_git "$project_dir"; then | 
 | 424 |             die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir" | 
 | 425 |         fi | 
 | 426 |     fi | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 427 | } | 
 | 428 |  | 
 | 429 | # ``pip install -e`` the package, which processes the dependencies | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 430 | # using pip before running `setup.py develop`.  The command is like | 
 | 431 | # "pip install <flags> <project_dir>[<extras>]" | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 432 | # | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 433 | # Uses globals ``STACK_USER`` | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 434 | # | 
 | 435 | # Usage: | 
 | 436 | #  setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras] | 
 | 437 | # | 
 | 438 | # -bindep     : Use bindep to install dependencies; select extra profiles | 
 | 439 | #               as comma separated arguments after "=" | 
 | 440 | # project_dir : directory of project repo (e.g., /opt/stack/keystone) | 
 | 441 | # flags       : pip CLI options/flags | 
 | 442 | # extras      : comma-separated list of optional dependencies to install | 
 | 443 | #               (e.g., ldap,memcache). | 
 | 444 | #               See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 445 | function setup_package { | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 446 |     local bindep=0 | 
 | 447 |     local bindep_flag="" | 
 | 448 |     local bindep_profiles="" | 
 | 449 |     if [[ $1 == -bindep* ]]; then | 
 | 450 |         bindep=1 | 
 | 451 |         IFS="=" read bindep_flag bindep_profiles <<< ${1} | 
 | 452 |         shift | 
 | 453 |     fi | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 454 |     local project_dir=$1 | 
 | 455 |     local flags=$2 | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 456 |     local extras=$3 | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 457 |  | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 458 |     # if the flags variable exists, and it doesn't look like a flag, | 
 | 459 |     # assume it's actually the extras list. | 
 | 460 |     if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then | 
 | 461 |         extras=$flags | 
 | 462 |         flags="" | 
 | 463 |     fi | 
 | 464 |  | 
 | 465 |     if [[ ! -z "$extras" ]]; then | 
 | 466 |         extras="[$extras]" | 
 | 467 |     fi | 
 | 468 |  | 
| Ian Wienand | 58243f6 | 2018-12-13 14:05:53 +1100 | [diff] [blame] | 469 |     # install any bindep packages | 
 | 470 |     if [[ $bindep == 1 ]]; then | 
 | 471 |         install_bindep $project_dir/bindep.txt $bindep_profiles | 
 | 472 |     fi | 
 | 473 |  | 
| Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 474 |     pip_install $flags "$project_dir$extras" | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 475 |     # ensure that further actions can do things like setup.py sdist | 
| Clark Boylan | a40f9cb | 2018-04-04 14:02:30 -0700 | [diff] [blame] | 476 |     if [[ "$flags" == "-e" && "$GLOBAL_VENV" == "False" ]]; then | 
| yatinkarel | 0ff6272 | 2024-09-02 17:29:55 +0530 | [diff] [blame] | 477 |         # egg-info is not created when project have pyproject.toml | 
 | 478 |         if [ -d $1/*.egg-info ]; then | 
 | 479 |             safe_chown -R $STACK_USER $1/*.egg-info | 
 | 480 |         fi | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 481 |     fi | 
 | 482 | } | 
 | 483 |  | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 484 | # Report whether python 3 should be used | 
| Jens Harbott | d7a82f4 | 2020-06-23 10:21:09 +0200 | [diff] [blame] | 485 | # TODO(frickler): drop this once all legacy uses are removed | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 486 | function python3_enabled { | 
| Radosław Piliszek | 3cbb33e | 2020-06-30 17:52:10 +0200 | [diff] [blame] | 487 |     return 0 | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 488 | } | 
 | 489 |  | 
| Federico Ressi | 21a10d3 | 2020-01-31 07:43:30 +0100 | [diff] [blame] | 490 | # Provide requested python version and sets PYTHON variable | 
 | 491 | function install_python { | 
| Jens Harbott | d7a82f4 | 2020-06-23 10:21:09 +0200 | [diff] [blame] | 492 |     install_python3 | 
 | 493 |     export PYTHON=$(which python${PYTHON3_VERSION} 2>/dev/null) | 
| Federico Ressi | 21a10d3 | 2020-01-31 07:43:30 +0100 | [diff] [blame] | 494 | } | 
 | 495 |  | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 496 | # Install python3 packages | 
 | 497 | function install_python3 { | 
 | 498 |     if is_ubuntu; then | 
| Lubosz "diltram" Kosnik | 0a09976 | 2016-08-03 10:21:41 -0500 | [diff] [blame] | 499 |         apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev | 
| Terry Wilson | 78cf6f6 | 2019-10-15 19:45:09 +0000 | [diff] [blame] | 500 |     elif is_fedora; then | 
| Federico Ressi | 2dcbc28 | 2020-02-05 11:29:51 +0100 | [diff] [blame] | 501 |         if [ "$os_VENDOR" = "Fedora" ]; then | 
 | 502 |             install_package python${PYTHON3_VERSION//.} | 
 | 503 |         else | 
 | 504 |             install_package python${PYTHON3_VERSION//.} python${PYTHON3_VERSION//.}-devel | 
 | 505 |         fi | 
| Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 506 |     fi | 
 | 507 | } | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 508 |  | 
| Sean Dague | f80e2cf | 2017-01-18 15:42:32 -0500 | [diff] [blame] | 509 | function install_devstack_tools { | 
 | 510 |     # intentionally old to ensure devstack-gate has control | 
 | 511 |     local dstools_version=${DSTOOLS_VERSION:-0.1.2} | 
 | 512 |     install_python3 | 
 | 513 |     sudo pip3 install -U devstack-tools==${dstools_version} | 
 | 514 | } | 
 | 515 |  | 
| Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 516 | # Restore xtrace | 
 | 517 | $INC_PY_TRACE | 
 | 518 |  | 
 | 519 | # Local variables: | 
 | 520 | # mode: shell-script | 
 | 521 | # End: |