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. |
| 22 | declare -A PROJECT_VENV |
| 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 | |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 72 | # Determine the python versions supported by a package |
| 73 | function get_python_versions_for_package { |
| 74 | local name=$1 |
| 75 | cd $name && python setup.py --classifiers \ |
| 76 | | grep 'Language' | cut -f5 -d: | grep '\.' | tr '\n' ' ' |
| 77 | } |
| 78 | |
Davanum Srinivas | afa8a00 | 2016-12-19 09:51:01 -0500 | [diff] [blame] | 79 | # Check for python3 classifier in local directory |
| 80 | function check_python3_support_for_package_local { |
| 81 | local name=$1 |
| 82 | cd $name |
| 83 | set +e |
| 84 | classifier=$(python setup.py --classifiers \ |
| 85 | | grep 'Programming Language :: Python :: 3$') |
| 86 | set -e |
| 87 | echo $classifier |
| 88 | } |
| 89 | |
| 90 | # Check for python3 classifier on pypi |
| 91 | function check_python3_support_for_package_remote { |
| 92 | local name=$1 |
| 93 | set +e |
| 94 | classifier=$(curl -s -L "https://pypi.python.org/pypi/$name/json" \ |
| 95 | | grep '"Programming Language :: Python :: 3"') |
| 96 | set -e |
| 97 | echo $classifier |
| 98 | } |
| 99 | |
Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame^] | 100 | # python3_enabled_for() checks if the service(s) specified as arguments are |
| 101 | # enabled by the user in ``ENABLED_PYTHON3_PACKAGES``. |
| 102 | # |
| 103 | # Multiple services specified as arguments are ``OR``'ed together; the test |
| 104 | # is a short-circuit boolean, i.e it returns on the first match. |
| 105 | # |
| 106 | # Uses global ``ENABLED_PYTHON3_PACKAGES`` |
| 107 | # python3_enabled_for dir [dir ...] |
| 108 | function python3_enabled_for { |
| 109 | local xtrace |
| 110 | xtrace=$(set +o | grep xtrace) |
| 111 | set +o xtrace |
| 112 | |
| 113 | local enabled=1 |
| 114 | local dirs=$@ |
| 115 | local dir |
| 116 | for dir in ${dirs}; do |
| 117 | [[ ,${ENABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0 |
| 118 | done |
| 119 | |
| 120 | $xtrace |
| 121 | return $enabled |
| 122 | } |
| 123 | |
| 124 | # python3_disabled_for() checks if the service(s) specified as arguments are |
| 125 | # disabled by the user in ``DISABLED_PYTHON3_PACKAGES``. |
| 126 | # |
| 127 | # Multiple services specified as arguments are ``OR``'ed together; the test |
| 128 | # is a short-circuit boolean, i.e it returns on the first match. |
| 129 | # |
| 130 | # Uses global ``DISABLED_PYTHON3_PACKAGES`` |
| 131 | # python3_disabled_for dir [dir ...] |
| 132 | function python3_disabled_for { |
| 133 | local xtrace |
| 134 | xtrace=$(set +o | grep xtrace) |
| 135 | set +o xtrace |
| 136 | |
| 137 | local enabled=1 |
| 138 | local dirs=$@ |
| 139 | local dir |
| 140 | for dir in ${dirs}; do |
| 141 | [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0 |
| 142 | done |
| 143 | |
| 144 | $xtrace |
| 145 | return $enabled |
| 146 | } |
| 147 | |
| 148 | # enable_python3_package() adds the repositories passed as argument to the |
| 149 | # ``ENABLED_PYTHON3_PACKAGES`` list, if they are not already present. |
| 150 | # |
| 151 | # For example: |
| 152 | # enable_python3_package nova |
| 153 | # |
| 154 | # Uses global ``ENABLED_PYTHON3_PACKAGES`` |
| 155 | # enable_python3_package dir [dir ...] |
| 156 | function enable_python3_package { |
| 157 | local xtrace |
| 158 | xtrace=$(set +o | grep xtrace) |
| 159 | set +o xtrace |
| 160 | |
| 161 | local tmpsvcs="${ENABLED_PYTHON3_PACKAGES}" |
| 162 | local python3 |
| 163 | for dir in $@; do |
| 164 | if [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]]; then |
| 165 | warn $LINENO "Attempt to enable_python3_package ${dir} when it has been disabled" |
| 166 | continue |
| 167 | fi |
| 168 | if ! python3_enabled_for $dir; then |
| 169 | tmpsvcs+=",$dir" |
| 170 | fi |
| 171 | done |
| 172 | ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$tmpsvcs") |
| 173 | |
| 174 | $xtrace |
| 175 | } |
| 176 | |
| 177 | # disable_python3_package() prepares the services passed as argument to be |
| 178 | # removed from the ``ENABLED_PYTHON3_PACKAGES`` list, if they are present. |
| 179 | # |
| 180 | # For example: |
| 181 | # disable_python3_package swift |
| 182 | # |
| 183 | # Uses globals ``ENABLED_PYTHON3_PACKAGES`` and ``DISABLED_PYTHON3_PACKAGES`` |
| 184 | # disable_python3_package dir [dir ...] |
| 185 | function disable_python3_package { |
| 186 | local xtrace |
| 187 | xtrace=$(set +o | grep xtrace) |
| 188 | set +o xtrace |
| 189 | |
| 190 | local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}" |
| 191 | local enabled_svcs=",${ENABLED_PYTHON3_PACKAGES}," |
| 192 | local dir |
| 193 | for dir in $@; do |
| 194 | disabled_svcs+=",$dir" |
| 195 | if python3_enabled_for $dir; then |
| 196 | enabled_svcs=${enabled_svcs//,$dir,/,} |
| 197 | fi |
| 198 | done |
| 199 | DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs") |
| 200 | ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$enabled_svcs") |
| 201 | |
| 202 | $xtrace |
| 203 | } |
| 204 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 205 | # Wrapper for ``pip install`` to set cache and proxy environment variables |
Dean Troyer | 41d6f85 | 2015-03-25 22:42:46 -0500 | [diff] [blame] | 206 | # Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``, |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 207 | # ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``, |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 208 | # pip_install package [package ...] |
| 209 | function pip_install { |
Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 210 | local xtrace result |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 211 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 212 | set +o xtrace |
Chris Dent | ebdd9ac | 2015-03-04 12:35:14 +0000 | [diff] [blame] | 213 | local upgrade="" |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 214 | local offline=${OFFLINE:-False} |
| 215 | if [[ "$offline" == "True" || -z "$@" ]]; then |
| 216 | $xtrace |
| 217 | return |
| 218 | fi |
| 219 | |
Sean Dague | cb658fa | 2015-10-08 17:12:03 -0400 | [diff] [blame] | 220 | time_start "pip_install" |
| 221 | |
Chris Dent | ebdd9ac | 2015-03-04 12:35:14 +0000 | [diff] [blame] | 222 | PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE) |
| 223 | if [[ "$PIP_UPGRADE" = "True" ]] ; then |
| 224 | upgrade="--upgrade" |
| 225 | fi |
| 226 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 227 | if [[ -z "$os_PACKAGE" ]]; then |
| 228 | GetOSVersion |
| 229 | fi |
| 230 | if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then |
| 231 | # TRACK_DEPENDS=True installation creates a circular dependency when |
Atsushi SAKAI | 5509ed5 | 2015-11-30 20:20:21 +0900 | [diff] [blame] | 232 | # we attempt to install virtualenv into a virtualenv, so we must global |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 233 | # that installation. |
| 234 | source $DEST/.venv/bin/activate |
| 235 | local cmd_pip=$DEST/.venv/bin/pip |
| 236 | local sudo_pip="env" |
| 237 | else |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame] | 238 | if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then |
| 239 | local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip |
| 240 | local sudo_pip="env" |
| 241 | else |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 242 | local cmd_pip |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 243 | cmd_pip=$(get_pip_command $PYTHON2_VERSION) |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame] | 244 | local sudo_pip="sudo -H" |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 245 | if python3_enabled; then |
| 246 | # Look at the package classifiers to find the python |
| 247 | # versions supported, and if we find the version of |
| 248 | # python3 we've been told to use, use that instead of the |
| 249 | # default pip |
| 250 | local package_dir=${!#} |
| 251 | local python_versions |
Davanum Srinivas | afa8a00 | 2016-12-19 09:51:01 -0500 | [diff] [blame] | 252 | |
| 253 | # Special case some services that have experimental |
| 254 | # support for python3 in progress, but don't claim support |
| 255 | # in their classifier |
| 256 | echo "Check python version for : $package_dir" |
Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame^] | 257 | if python3_disabled_for ${package_dir##*/}; then |
| 258 | echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES" |
| 259 | elif python3_enabled_for ${package_dir##*/}; then |
| 260 | 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] | 261 | sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8" |
| 262 | cmd_pip=$(get_pip_command $PYTHON3_VERSION) |
| 263 | elif [[ -d "$package_dir" ]]; then |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 264 | python_versions=$(get_python_versions_for_package $package_dir) |
| 265 | if [[ $python_versions =~ $PYTHON3_VERSION ]]; then |
Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame^] | 266 | 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] | 267 | sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8" |
| 268 | cmd_pip=$(get_pip_command $PYTHON3_VERSION) |
| 269 | else |
| 270 | # The package may not have yet advertised python3.5 |
| 271 | # support so check for just python3 classifier and log |
| 272 | # a warning. |
| 273 | python3_classifier=$(check_python3_support_for_package_local $package_dir) |
| 274 | if [[ ! -z "$python3_classifier" ]]; then |
Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame^] | 275 | 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] | 276 | sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8" |
| 277 | cmd_pip=$(get_pip_command $PYTHON3_VERSION) |
| 278 | fi |
| 279 | fi |
| 280 | else |
| 281 | # Check pypi as we don't have the package on disk |
| 282 | package=$(echo $package_dir | grep -o '^[.a-zA-Z0-9_-]*') |
| 283 | python3_classifier=$(check_python3_support_for_package_remote $package) |
| 284 | if [[ ! -z "$python3_classifier" ]]; then |
Doug Hellmann | 94129c7 | 2017-01-09 21:24:24 +0000 | [diff] [blame^] | 285 | 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] | 286 | sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8" |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 287 | cmd_pip=$(get_pip_command $PYTHON3_VERSION) |
| 288 | fi |
| 289 | fi |
| 290 | fi |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame] | 291 | fi |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 292 | fi |
| 293 | |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 294 | cmd_pip="$cmd_pip install" |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 295 | # Always apply constraints |
| 296 | cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt" |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 297 | |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 298 | # FIXME(dhellmann): Need to force multiple versions of pip for |
| 299 | # packages like setuptools? |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 300 | local pip_version |
| 301 | pip_version=$(python -c "import pip; \ |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 302 | print(pip.__version__.strip('.')[0])") |
| 303 | if (( pip_version<6 )); then |
| 304 | die $LINENO "Currently installed pip version ${pip_version} does not" \ |
| 305 | "meet minimum requirements (>=6)." |
| 306 | fi |
| 307 | |
| 308 | $xtrace |
Spyros Trigazis | 88ccd47 | 2016-07-24 22:13:57 +0200 | [diff] [blame] | 309 | # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep |
| 310 | # the same behaviour of setuptools before version 25.0.0. |
| 311 | # related issue: https://github.com/pypa/pip/issues/3874 |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 312 | $sudo_pip \ |
Eli Qiao | 6a83c42 | 2015-03-17 16:54:16 +0800 | [diff] [blame] | 313 | http_proxy="${http_proxy:-}" \ |
| 314 | https_proxy="${https_proxy:-}" \ |
| 315 | no_proxy="${no_proxy:-}" \ |
Joe Gordon | cd8824a | 2015-03-04 16:40:19 -0800 | [diff] [blame] | 316 | PIP_FIND_LINKS=$PIP_FIND_LINKS \ |
Spyros Trigazis | 88ccd47 | 2016-07-24 22:13:57 +0200 | [diff] [blame] | 317 | SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \ |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 318 | $cmd_pip $upgrade \ |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 319 | $@ |
Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 320 | result=$? |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 321 | |
Sean Dague | eeb7bda | 2015-03-25 11:55:32 -0400 | [diff] [blame] | 322 | # Also install test requirements |
Sirushti Murugesan | 713fd2f | 2015-09-30 15:12:50 +0530 | [diff] [blame] | 323 | local test_req="${!#}/test-requirements.txt" |
Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 324 | if [[ $result == 0 ]] && [[ -e "$test_req" ]]; then |
Sean Dague | eeb7bda | 2015-03-25 11:55:32 -0400 | [diff] [blame] | 325 | echo "Installing test-requirements for $test_req" |
| 326 | $sudo_pip \ |
| 327 | http_proxy=${http_proxy:-} \ |
| 328 | https_proxy=${https_proxy:-} \ |
| 329 | no_proxy=${no_proxy:-} \ |
| 330 | PIP_FIND_LINKS=$PIP_FIND_LINKS \ |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 331 | $cmd_pip $upgrade \ |
Sean Dague | eeb7bda | 2015-03-25 11:55:32 -0400 | [diff] [blame] | 332 | -r $test_req |
Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 333 | result=$? |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 334 | fi |
Sean Dague | cb658fa | 2015-10-08 17:12:03 -0400 | [diff] [blame] | 335 | |
| 336 | time_stop "pip_install" |
Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 337 | return $result |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 338 | } |
| 339 | |
Joe Gordon | d5ac785 | 2015-02-06 19:29:23 -0800 | [diff] [blame] | 340 | # get version of a package from global requirements file |
| 341 | # get_from_global_requirements <package> |
| 342 | function get_from_global_requirements { |
| 343 | local package=$1 |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 344 | local required_pkg |
| 345 | 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] | 346 | if [[ $required_pkg == "" ]]; then |
| 347 | die $LINENO "Can't find package $package in requirements" |
| 348 | fi |
| 349 | echo $required_pkg |
| 350 | } |
| 351 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 352 | # should we use this library from their git repo, or should we let it |
| 353 | # get pulled in via pip dependencies. |
| 354 | function use_library_from_git { |
| 355 | local name=$1 |
| 356 | local enabled=1 |
Marc Koderer | 46f8cb7 | 2016-05-13 09:08:16 +0200 | [diff] [blame] | 357 | [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0 |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 358 | return $enabled |
| 359 | } |
| 360 | |
Sean Dague | c71973e | 2015-09-08 07:12:48 -0400 | [diff] [blame] | 361 | # determine if a package was installed from git |
| 362 | function lib_installed_from_git { |
| 363 | local name=$1 |
| 364 | pip freeze 2>/dev/null | grep -- "$name" | grep -q -- '-e git' |
| 365 | } |
| 366 | |
| 367 | # check that everything that's in LIBS_FROM_GIT was actually installed |
| 368 | # correctly, this helps double check issues with library fat fingering. |
| 369 | function check_libs_from_git { |
| 370 | local lib="" |
| 371 | local not_installed="" |
| 372 | for lib in $(echo ${LIBS_FROM_GIT} | tr "," " "); do |
| 373 | if ! lib_installed_from_git "$lib"; then |
| 374 | not_installed+=" $lib" |
| 375 | fi |
| 376 | done |
| 377 | # if anything is not installed, say what it is. |
| 378 | if [[ -n "$not_installed" ]]; then |
| 379 | die $LINENO "The following LIBS_FROM_GIT were not installed correct: $not_installed" |
| 380 | fi |
| 381 | } |
| 382 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 383 | # setup a library by name. If we are trying to use the library from |
| 384 | # git, we'll do a git based install, otherwise we'll punt and the |
| 385 | # library should be installed by a requirements pull from another |
| 386 | # project. |
| 387 | function setup_lib { |
| 388 | local name=$1 |
| 389 | local dir=${GITDIR[$name]} |
| 390 | setup_install $dir |
| 391 | } |
| 392 | |
Atsushi SAKAI | 5509ed5 | 2015-11-30 20:20:21 +0900 | [diff] [blame] | 393 | # 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] | 394 | # the library from git, we'll do a git based install, otherwise we'll |
| 395 | # punt and the library should be installed by a requirements pull from |
| 396 | # another project. |
| 397 | # |
| 398 | # use this for non namespaced libraries |
| 399 | function setup_dev_lib { |
| 400 | local name=$1 |
| 401 | local dir=${GITDIR[$name]} |
| 402 | setup_develop $dir |
| 403 | } |
| 404 | |
| 405 | # this should be used if you want to install globally, all libraries should |
| 406 | # use this, especially *oslo* ones |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 407 | # |
| 408 | # setup_install project_dir [extras] |
| 409 | # project_dir: directory of project repo (e.g., /opt/stack/keystone) |
| 410 | # extras: comma-separated list of optional dependencies to install |
| 411 | # (e.g., ldap,memcache). |
| 412 | # See http://docs.openstack.org/developer/pbr/#extra-requirements |
| 413 | # The command is like "pip install <project_dir>[<extras>]" |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 414 | function setup_install { |
| 415 | local project_dir=$1 |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 416 | local extras=$2 |
| 417 | _setup_package_with_constraints_edit $project_dir "" $extras |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | # this should be used for projects which run services, like all services |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 421 | # |
| 422 | # setup_develop project_dir [extras] |
| 423 | # project_dir: directory of project repo (e.g., /opt/stack/keystone) |
| 424 | # extras: comma-separated list of optional dependencies to install |
| 425 | # (e.g., ldap,memcache). |
| 426 | # See http://docs.openstack.org/developer/pbr/#extra-requirements |
| 427 | # The command is like "pip install -e <project_dir>[<extras>]" |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 428 | function setup_develop { |
| 429 | local project_dir=$1 |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 430 | local extras=$2 |
| 431 | _setup_package_with_constraints_edit $project_dir -e $extras |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | # determine if a project as specified by directory is in |
| 435 | # projects.txt. This will not be an exact match because we throw away |
| 436 | # the namespacing when we clone, but it should be good enough in all |
| 437 | # practical ways. |
| 438 | function is_in_projects_txt { |
| 439 | local project_dir=$1 |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 440 | local project_name |
| 441 | project_name=$(basename $project_dir) |
Ihar Hrachyshka | 2ba4a72 | 2015-06-26 10:45:44 +0200 | [diff] [blame] | 442 | grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | # ``pip install -e`` the package, which processes the dependencies |
| 446 | # using pip before running `setup.py develop` |
| 447 | # |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 448 | # Updates the constraints from REQUIREMENTS_DIR to reflect the |
| 449 | # future installed state of this package. This ensures when we |
| 450 | # install this package we get the from source version. |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 451 | # |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 452 | # Uses globals ``REQUIREMENTS_DIR`` |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 453 | # _setup_package_with_constraints_edit project_dir flags [extras] |
| 454 | # project_dir: directory of project repo (e.g., /opt/stack/keystone) |
| 455 | # flags: pip CLI options/flags |
| 456 | # extras: comma-separated list of optional dependencies to install |
| 457 | # (e.g., ldap,memcache). |
| 458 | # See http://docs.openstack.org/developer/pbr/#extra-requirements |
| 459 | # The command is like "pip install <flags> <project_dir>[<extras>]" |
| 460 | function _setup_package_with_constraints_edit { |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 461 | local project_dir=$1 |
| 462 | local flags=$2 |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 463 | local extras=$3 |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 464 | |
YAMAMOTO Takashi | c8c1c61 | 2016-03-22 14:29:47 +0900 | [diff] [blame] | 465 | # Normalize the directory name to avoid |
| 466 | # "installation from path or url cannot be constrained to a version" |
| 467 | # error. |
| 468 | # REVISIT(yamamoto): Remove this when fixed in pip. |
| 469 | # https://github.com/pypa/pip/pull/3582 |
| 470 | project_dir=$(cd $project_dir && pwd) |
| 471 | |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 472 | if [ -n "$REQUIREMENTS_DIR" ]; then |
| 473 | # Constrain this package to this project directory from here on out. |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 474 | local name |
| 475 | name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg) |
Robert Collins | 7c83861 | 2015-07-03 13:28:09 +1200 | [diff] [blame] | 476 | $REQUIREMENTS_DIR/.venv/bin/edit-constraints \ |
| 477 | $REQUIREMENTS_DIR/upper-constraints.txt -- $name \ |
| 478 | "$flags file://$project_dir#egg=$name" |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 479 | fi |
| 480 | |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 481 | setup_package $project_dir "$flags" $extras |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 482 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | # ``pip install -e`` the package, which processes the dependencies |
| 486 | # using pip before running `setup.py develop` |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 487 | # |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 488 | # Uses globals ``STACK_USER`` |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 489 | # setup_package project_dir [flags] [extras] |
| 490 | # project_dir: directory of project repo (e.g., /opt/stack/keystone) |
| 491 | # flags: pip CLI options/flags |
| 492 | # extras: comma-separated list of optional dependencies to install |
| 493 | # (e.g., ldap,memcache). |
| 494 | # See http://docs.openstack.org/developer/pbr/#extra-requirements |
| 495 | # The command is like "pip install <flags> <project_dir>[<extras>]" |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 496 | function setup_package { |
| 497 | local project_dir=$1 |
| 498 | local flags=$2 |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 499 | local extras=$3 |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 500 | |
Brant Knudson | 0842b81 | 2015-08-03 13:31:25 -0500 | [diff] [blame] | 501 | # if the flags variable exists, and it doesn't look like a flag, |
| 502 | # assume it's actually the extras list. |
| 503 | if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then |
| 504 | extras=$flags |
| 505 | flags="" |
| 506 | fi |
| 507 | |
| 508 | if [[ ! -z "$extras" ]]; then |
| 509 | extras="[$extras]" |
| 510 | fi |
| 511 | |
| 512 | pip_install $flags "$project_dir$extras" |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 513 | # ensure that further actions can do things like setup.py sdist |
| 514 | if [[ "$flags" == "-e" ]]; then |
| 515 | safe_chown -R $STACK_USER $1/*.egg-info |
| 516 | fi |
| 517 | } |
| 518 | |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 519 | # Report whether python 3 should be used |
| 520 | function python3_enabled { |
| 521 | if [[ $USE_PYTHON3 == "True" ]]; then |
| 522 | return 0 |
| 523 | else |
| 524 | return 1 |
| 525 | fi |
| 526 | } |
| 527 | |
| 528 | # Install python3 packages |
| 529 | function install_python3 { |
| 530 | if is_ubuntu; then |
Lubosz "diltram" Kosnik | 0a09976 | 2016-08-03 10:21:41 -0500 | [diff] [blame] | 531 | apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev |
Doug Hellmann | ddc3839 | 2015-05-07 21:06:24 +0000 | [diff] [blame] | 532 | fi |
| 533 | } |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 534 | |
| 535 | # Restore xtrace |
| 536 | $INC_PY_TRACE |
| 537 | |
| 538 | # Local variables: |
| 539 | # mode: shell-script |
| 540 | # End: |