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