blob: dd77296049a2e7574dc415c3c20c68e4f51c83af [file] [log] [blame]
Dean Troyer490430d2015-01-30 14:38:35 -06001#!/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
14INC_PY_TRACE=$(set +o | grep xtrace)
15set +o xtrace
16
17
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060018# Global Config Variables
19
Atsushi SAKAI5509ed52015-11-30 20:20:21 +090020# PROJECT_VENV contains the name of the virtual environment for each
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060021# project. A null value installs to the system Python directories.
Sean Dagueafef8bf2017-03-06 14:07:23 -050022declare -A -g PROJECT_VENV
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060023
Radosław Piliszekbe263062020-03-30 09:56:53 +020024# Utility Functions
25# =================
26
27# Joins bash array of extras with commas as expected by other functions
28function join_extras {
29 local IFS=","
30 echo "$*"
31}
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060032
Dean Troyer490430d2015-01-30 14:38:35 -060033# Python Functions
34# ================
35
36# Get the path to the pip command.
37# get_pip_command
38function get_pip_command {
Doug Hellmannddc38392015-05-07 21:06:24 +000039 local version="$1"
Tom Barron4db9d562019-01-09 08:43:52 -050040 if [ -z "$version" ]; then
41 die $LINENO "pip python version is not set."
42 fi
43
Doug Hellmannddc38392015-05-07 21:06:24 +000044 # 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 Troyer490430d2015-01-30 14:38:35 -060047
48 if [ $? -ne 0 ]; then
Doug Hellmannddc38392015-05-07 21:06:24 +000049 die $LINENO "Unable to find pip${version}; cannot continue"
Dean Troyer490430d2015-01-30 14:38:35 -060050 fi
51}
52
Atsushi SAKAI5509ed52015-11-30 20:20:21 +090053# Get the path to the directory where python executables are installed.
Dean Troyer490430d2015-01-30 14:38:35 -060054# get_python_exec_prefix
55function get_python_exec_prefix {
Ian Wienand433a9b12015-10-07 13:29:31 +110056 local xtrace
57 xtrace=$(set +o | grep xtrace)
Dean Troyer2b564762015-02-11 17:01:02 -060058 set +o xtrace
59 if [[ -z "$os_PACKAGE" ]]; then
60 GetOSVersion
61 fi
62 $xtrace
63
Lenny Verkhovskya30dd1c2019-03-14 13:19:36 +020064 local PYTHON_PATH=/usr/local/bin
65 ( is_fedora && ! python3_enabled ) || is_suse && PYTHON_PATH=/usr/bin
66 echo $PYTHON_PATH
Dean Troyer490430d2015-01-30 14:38:35 -060067}
68
Sean Dague60996b12015-04-08 09:06:49 -040069# 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
75function pip_install_gr {
76 local name=$1
Ian Wienandada886d2015-10-07 14:06:26 +110077 local clean_name
78 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -040079 pip_install $clean_name
80}
81
Mehdi Abaakouk52b10742016-12-01 16:11:17 +010082# 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,...
88function pip_install_gr_extras {
89 local name=$1
90 local extras=$2
Radosław Piliszekbe263062020-03-30 09:56:53 +020091 local version_constraints
92 version_constraints=$(get_version_constraints_from_global_requirements $name)
93 pip_install $name[$extras]$version_constraints
Mehdi Abaakouk52b10742016-12-01 16:11:17 +010094}
95
Doug Hellmann36377f62018-12-04 11:33:03 -050096# enable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +000097#
Doug Hellmann94129c72017-01-09 21:24:24 +000098# enable_python3_package dir [dir ...]
99function enable_python3_package {
100 local xtrace
101 xtrace=$(set +o | grep xtrace)
102 set +o xtrace
103
Doug Hellmann36377f62018-12-04 11:33:03 -0500104 echo "It is no longer necessary to call enable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000105
106 $xtrace
107}
108
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100109# disable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000110#
Doug Hellmann94129c72017-01-09 21:24:24 +0000111# disable_python3_package dir [dir ...]
112function disable_python3_package {
113 local xtrace
114 xtrace=$(set +o | grep xtrace)
115 set +o xtrace
116
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100117 echo "It is no longer possible to call disable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000118
119 $xtrace
120}
121
Dean Troyer490430d2015-01-30 14:38:35 -0600122# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500123# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Ian Wienandbcb2c302020-01-13 16:31:20 +1100124# ``PIP_UPGRADE``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400125# Usage:
126# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600127function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000128 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100129 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600130 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000131 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600132 local offline=${OFFLINE:-False}
133 if [[ "$offline" == "True" || -z "$@" ]]; then
134 $xtrace
135 return
136 fi
137
Sean Daguecb658fa2015-10-08 17:12:03 -0400138 time_start "pip_install"
139
Chris Dentebdd9ac2015-03-04 12:35:14 +0000140 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
141 if [[ "$PIP_UPGRADE" = "True" ]] ; then
142 upgrade="--upgrade"
143 fi
144
Dean Troyer490430d2015-01-30 14:38:35 -0600145 if [[ -z "$os_PACKAGE" ]]; then
146 GetOSVersion
147 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400148
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 Wienandbcb2c302020-01-13 16:31:20 +1100168 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
169 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
Dean Troyer490430d2015-01-30 14:38:35 -0600170 local sudo_pip="env"
171 else
Ian Wienandbcb2c302020-01-13 16:31:20 +1100172 local cmd_pip
Ian Wienandbcb2c302020-01-13 16:31:20 +1100173 local sudo_pip="sudo -H"
174 if python3_enabled; then
Radosław Piliszek29bf8522020-01-24 11:52:13 +0100175 echo "Using python $PYTHON3_VERSION to install $package_dir because python3_enabled=True"
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100176 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
177 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
Radosław Piliszek29bf8522020-01-24 11:52:13 +0100178 else
179 echo "Using python $PYTHON2_VERSION to install $package_dir because python3_enabled=False"
180 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600181 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600182 fi
183
Robert Collins635a5ba2015-06-10 08:48:06 +1200184 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700185 # Always apply constraints
186 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200187
Dean Troyer490430d2015-01-30 14:38:35 -0600188 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700189
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200190 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
191 # the same behaviour of setuptools before version 25.0.0.
192 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600193 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800194 http_proxy="${http_proxy:-}" \
195 https_proxy="${https_proxy:-}" \
196 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800197 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200198 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Monty Taylor09b5b052020-03-27 11:22:39 -0500199 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600200 $@
Federico Ressie208d062015-11-21 11:15:39 +0000201 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600202
Sean Daguecb658fa2015-10-08 17:12:03 -0400203 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000204 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600205}
206
Sean Daguef28e7ef2017-05-07 22:02:10 -0400207function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900208 # Skip uninstall if offline
209 [[ "${OFFLINE}" = "True" ]] && return
210
Sean Daguef28e7ef2017-05-07 22:02:10 -0400211 local name=$1
212 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
213 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
214 local sudo_pip="env"
215 else
216 local cmd_pip
Sean Daguef28e7ef2017-05-07 22:02:10 -0400217 local sudo_pip="sudo -H"
Radosław Piliszek29bf8522020-01-24 11:52:13 +0100218 if python3_enabled; then
219 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
220 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
221 else
222 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
223 fi
Sean Daguef28e7ef2017-05-07 22:02:10 -0400224 fi
225 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400226 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400227}
228
Joe Gordond5ac7852015-02-06 19:29:23 -0800229# get version of a package from global requirements file
230# get_from_global_requirements <package>
231function get_from_global_requirements {
232 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100233 local required_pkg
234 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800235 if [[ $required_pkg == "" ]]; then
236 die $LINENO "Can't find package $package in requirements"
237 fi
238 echo $required_pkg
239}
240
Radosław Piliszekbe263062020-03-30 09:56:53 +0200241# get only version constraints of a package from global requirements file
242# get_version_constraints_from_global_requirements <package>
243function get_version_constraints_from_global_requirements {
244 local package=$1
245 local required_pkg_version_constraint
246 # drop the package name from output (\K)
247 required_pkg_version_constraint=$(grep -i -h -o -P "^${package}\K.*" $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
248 if [[ $required_pkg_version_constraint == "" ]]; then
249 die $LINENO "Can't find package $package in requirements"
250 fi
251 echo $required_pkg_version_constraint
252}
253
Dean Troyer490430d2015-01-30 14:38:35 -0600254# should we use this library from their git repo, or should we let it
255# get pulled in via pip dependencies.
256function use_library_from_git {
257 local name=$1
258 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200259 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600260 return $enabled
261}
262
Sean Daguec71973e2015-09-08 07:12:48 -0400263# determine if a package was installed from git
264function lib_installed_from_git {
265 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800266 local safe_name
267 safe_name=$(python -c "from pkg_resources import safe_name; \
268 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000269 # Note "pip freeze" doesn't always work here, because it tries to
270 # be smart about finding the remote of the git repo the package
271 # was installed from. This doesn't work with zuul which clones
272 # repos with no remote.
273 #
274 # The best option seems to be to use "pip list" which will tell
275 # you the path an editable install was installed from; for example
276 # in response to something like
Matt Riedemann9b6d2f22019-06-18 10:43:16 -0400277 # pip install -e 'git+https://opendev.org/openstack/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500278 # pip list --format columns shows
279 # bashate 0.5.2.dev19 /tmp/env/src/bashate
280 # Thus we check the third column to see if we're installed from
281 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800282 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400283}
284
Dean Troyer490430d2015-01-30 14:38:35 -0600285# setup a library by name. If we are trying to use the library from
286# git, we'll do a git based install, otherwise we'll punt and the
287# library should be installed by a requirements pull from another
288# project.
289function setup_lib {
290 local name=$1
291 local dir=${GITDIR[$name]}
292 setup_install $dir
293}
294
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900295# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600296# the library from git, we'll do a git based install, otherwise we'll
297# punt and the library should be installed by a requirements pull from
298# another project.
299#
300# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100301#
Radosław Piliszekbe263062020-03-30 09:56:53 +0200302# setup_dev_lib [-bindep] <name> [<extras>]
Dean Troyer490430d2015-01-30 14:38:35 -0600303function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100304 local bindep
305 if [[ $1 == -bindep* ]]; then
306 bindep="${1}"
307 shift
308 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600309 local name=$1
310 local dir=${GITDIR[$name]}
Radosław Piliszekbe263062020-03-30 09:56:53 +0200311 local extras=$2
312 setup_develop $bindep $dir $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600313}
314
315# this should be used if you want to install globally, all libraries should
316# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500317#
318# setup_install project_dir [extras]
319# project_dir: directory of project repo (e.g., /opt/stack/keystone)
320# extras: comma-separated list of optional dependencies to install
321# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900322# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Ian Wienand58243f62018-12-13 14:05:53 +1100323# bindep: Set "-bindep" as first argument to install bindep.txt packages
Brant Knudson0842b812015-08-03 13:31:25 -0500324# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600325function setup_install {
Ian Wienand58243f62018-12-13 14:05:53 +1100326 local bindep
327 if [[ $1 == -bindep* ]]; then
328 bindep="${1}"
329 shift
330 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600331 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500332 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100333 _setup_package_with_constraints_edit $bindep $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600334}
335
336# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500337#
338# setup_develop project_dir [extras]
339# project_dir: directory of project repo (e.g., /opt/stack/keystone)
340# extras: comma-separated list of optional dependencies to install
341# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900342# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500343# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600344function setup_develop {
Ian Wienand58243f62018-12-13 14:05:53 +1100345 local bindep
346 if [[ $1 == -bindep* ]]; then
347 bindep="${1}"
348 shift
349 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600350 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500351 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100352 _setup_package_with_constraints_edit $bindep $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600353}
354
Dean Troyer490430d2015-01-30 14:38:35 -0600355# ``pip install -e`` the package, which processes the dependencies
356# using pip before running `setup.py develop`
357#
Clark Boylan05aa3842015-08-03 11:14:13 -0700358# Updates the constraints from REQUIREMENTS_DIR to reflect the
359# future installed state of this package. This ensures when we
360# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600361#
Clark Boylan05aa3842015-08-03 11:14:13 -0700362# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500363# _setup_package_with_constraints_edit project_dir flags [extras]
364# project_dir: directory of project repo (e.g., /opt/stack/keystone)
365# flags: pip CLI options/flags
366# extras: comma-separated list of optional dependencies to install
367# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900368# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500369# The command is like "pip install <flags> <project_dir>[<extras>]"
370function _setup_package_with_constraints_edit {
Ian Wienand58243f62018-12-13 14:05:53 +1100371 local bindep
372 if [[ $1 == -bindep* ]]; then
373 bindep="${1}"
374 shift
375 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600376 local project_dir=$1
377 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500378 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600379
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900380 # Normalize the directory name to avoid
381 # "installation from path or url cannot be constrained to a version"
382 # error.
383 # REVISIT(yamamoto): Remove this when fixed in pip.
384 # https://github.com/pypa/pip/pull/3582
385 project_dir=$(cd $project_dir && pwd)
386
Robert Collins635a5ba2015-06-10 08:48:06 +1200387 if [ -n "$REQUIREMENTS_DIR" ]; then
388 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100389 local name
390 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200391 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
392 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
393 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200394 fi
395
Ian Wienand58243f62018-12-13 14:05:53 +1100396 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600397
James E. Blaire1edde32018-03-02 15:05:14 +0000398 # If this project is in LIBS_FROM_GIT, verify it was actually installed
399 # correctly. This helps catch errors caused by constraints mismatches.
400 if use_library_from_git "$project_dir"; then
401 if ! lib_installed_from_git "$project_dir"; then
402 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
403 fi
404 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600405}
406
407# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100408# using pip before running `setup.py develop`. The command is like
409# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500410#
Dean Troyer490430d2015-01-30 14:38:35 -0600411# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100412#
413# Usage:
414# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
415#
416# -bindep : Use bindep to install dependencies; select extra profiles
417# as comma separated arguments after "="
418# project_dir : directory of project repo (e.g., /opt/stack/keystone)
419# flags : pip CLI options/flags
420# extras : comma-separated list of optional dependencies to install
421# (e.g., ldap,memcache).
422# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600423function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100424 local bindep=0
425 local bindep_flag=""
426 local bindep_profiles=""
427 if [[ $1 == -bindep* ]]; then
428 bindep=1
429 IFS="=" read bindep_flag bindep_profiles <<< ${1}
430 shift
431 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600432 local project_dir=$1
433 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500434 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600435
Brant Knudson0842b812015-08-03 13:31:25 -0500436 # if the flags variable exists, and it doesn't look like a flag,
437 # assume it's actually the extras list.
438 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
439 extras=$flags
440 flags=""
441 fi
442
443 if [[ ! -z "$extras" ]]; then
444 extras="[$extras]"
445 fi
446
Ian Wienand58243f62018-12-13 14:05:53 +1100447 # install any bindep packages
448 if [[ $bindep == 1 ]]; then
449 install_bindep $project_dir/bindep.txt $bindep_profiles
450 fi
451
Brant Knudson0842b812015-08-03 13:31:25 -0500452 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600453 # ensure that further actions can do things like setup.py sdist
454 if [[ "$flags" == "-e" ]]; then
455 safe_chown -R $STACK_USER $1/*.egg-info
456 fi
457}
458
Doug Hellmannddc38392015-05-07 21:06:24 +0000459# Report whether python 3 should be used
460function python3_enabled {
461 if [[ $USE_PYTHON3 == "True" ]]; then
462 return 0
463 else
464 return 1
465 fi
466}
467
Federico Ressi21a10d32020-01-31 07:43:30 +0100468# Provide requested python version and sets PYTHON variable
469function install_python {
470 # NOTE: install_python function should finally just do what install_python3
471 # does as soon Python 2 support has been dropped
472 if python3_enabled; then
473 install_python3
474 export PYTHON=$(which python${PYTHON3_VERSION} 2>/dev/null ||
475 which python3 2>/dev/null)
476 if [[ "${DISTRO}" =~ (rhel8) ]]; then
477 # Use Python 3 as default python command so that we have only one
478 # python alternative to use on the system for either python and
479 # python3
480 sudo alternatives --set python "${PYTHON}"
481 else
482 # Install anyway Python 2 for legacy scripts that still requires
483 # python instead of python3 command
484 install_package python
485 fi
486 else
487 echo "WARNING - Python 2 support has been deprecated in favor of Python 3"
488 install_package python
489 export PYTHON=$(which python 2>/dev/null)
490 fi
491}
492
Doug Hellmannddc38392015-05-07 21:06:24 +0000493# Install python3 packages
494function install_python3 {
495 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500496 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700497 elif is_suse; then
498 install_package python3-devel python3-dbm
Terry Wilson78cf6f62019-10-15 19:45:09 +0000499 elif is_fedora; then
Federico Ressi2dcbc282020-02-05 11:29:51 +0100500 if [ "$os_VENDOR" = "Fedora" ]; then
501 install_package python${PYTHON3_VERSION//.}
502 else
503 install_package python${PYTHON3_VERSION//.} python${PYTHON3_VERSION//.}-devel
504 fi
Doug Hellmannddc38392015-05-07 21:06:24 +0000505 fi
506}
Dean Troyer490430d2015-01-30 14:38:35 -0600507
Sean Daguef80e2cf2017-01-18 15:42:32 -0500508function install_devstack_tools {
509 # intentionally old to ensure devstack-gate has control
510 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
511 install_python3
512 sudo pip3 install -U devstack-tools==${dstools_version}
513}
514
Dean Troyer490430d2015-01-30 14:38:35 -0600515# Restore xtrace
516$INC_PY_TRACE
517
518# Local variables:
519# mode: shell-script
520# End: