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