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