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