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