blob: 20f3c6040cdf159db15566e2f1c3636069c54e5c [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
24
Dean Troyer490430d2015-01-30 14:38:35 -060025# Python Functions
26# ================
27
28# Get the path to the pip command.
29# get_pip_command
30function get_pip_command {
Doug Hellmannddc38392015-05-07 21:06:24 +000031 local version="$1"
Tom Barron4db9d562019-01-09 08:43:52 -050032 if [ -z "$version" ]; then
33 die $LINENO "pip python version is not set."
34 fi
35
Doug Hellmannddc38392015-05-07 21:06:24 +000036 # 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 Troyer490430d2015-01-30 14:38:35 -060039
40 if [ $? -ne 0 ]; then
Doug Hellmannddc38392015-05-07 21:06:24 +000041 die $LINENO "Unable to find pip${version}; cannot continue"
Dean Troyer490430d2015-01-30 14:38:35 -060042 fi
43}
44
Atsushi SAKAI5509ed52015-11-30 20:20:21 +090045# Get the path to the directory where python executables are installed.
Dean Troyer490430d2015-01-30 14:38:35 -060046# get_python_exec_prefix
47function get_python_exec_prefix {
Ian Wienand433a9b12015-10-07 13:29:31 +110048 local xtrace
49 xtrace=$(set +o | grep xtrace)
Dean Troyer2b564762015-02-11 17:01:02 -060050 set +o xtrace
51 if [[ -z "$os_PACKAGE" ]]; then
52 GetOSVersion
53 fi
54 $xtrace
55
Lenny Verkhovskya30dd1c2019-03-14 13:19:36 +020056 local PYTHON_PATH=/usr/local/bin
57 ( is_fedora && ! python3_enabled ) || is_suse && PYTHON_PATH=/usr/bin
58 echo $PYTHON_PATH
Dean Troyer490430d2015-01-30 14:38:35 -060059}
60
Sean Dague60996b12015-04-08 09:06:49 -040061# 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
67function pip_install_gr {
68 local name=$1
Ian Wienandada886d2015-10-07 14:06:26 +110069 local clean_name
70 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -040071 pip_install $clean_name
72}
73
Mehdi Abaakouk52b10742016-12-01 16:11:17 +010074# 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,...
80function 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 Hellmann36377f62018-12-04 11:33:03 -050088# enable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +000089#
Doug Hellmann94129c72017-01-09 21:24:24 +000090# enable_python3_package dir [dir ...]
91function enable_python3_package {
92 local xtrace
93 xtrace=$(set +o | grep xtrace)
94 set +o xtrace
95
Doug Hellmann36377f62018-12-04 11:33:03 -050096 echo "It is no longer necessary to call enable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +000097
98 $xtrace
99}
100
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100101# disable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000102#
Doug Hellmann94129c72017-01-09 21:24:24 +0000103# disable_python3_package dir [dir ...]
104function disable_python3_package {
105 local xtrace
106 xtrace=$(set +o | grep xtrace)
107 set +o xtrace
108
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100109 echo "It is no longer possible to call disable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000110
111 $xtrace
112}
113
Monty Taylorc26dfb02020-03-25 08:32:26 -0500114
115function pip_check {
116 time_start "pip_check"
117
118 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
119 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
120 else
121 local cmd_pip
122 if python3_enabled; then
123 echo "Using python $PYTHON3_VERSION to check pip install because python3_enabled=True"
124 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
125 else
126 echo "Using python $PYTHON2_VERSION to check pip install because python3_enabled=False"
127 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
128 fi
129 fi
130
131 $cmd_pip check
132 result=$?
133
134 time_stop "pip_check"
135 return $result
136}
137
Dean Troyer490430d2015-01-30 14:38:35 -0600138# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500139# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Ian Wienandbcb2c302020-01-13 16:31:20 +1100140# ``PIP_UPGRADE``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400141# Usage:
142# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600143function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000144 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100145 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600146 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000147 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600148 local offline=${OFFLINE:-False}
149 if [[ "$offline" == "True" || -z "$@" ]]; then
150 $xtrace
151 return
152 fi
153
Sean Daguecb658fa2015-10-08 17:12:03 -0400154 time_start "pip_install"
155
Chris Dentebdd9ac2015-03-04 12:35:14 +0000156 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
157 if [[ "$PIP_UPGRADE" = "True" ]] ; then
158 upgrade="--upgrade"
159 fi
160
Dean Troyer490430d2015-01-30 14:38:35 -0600161 if [[ -z "$os_PACKAGE" ]]; then
162 GetOSVersion
163 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400164
165 # Try to extract the path of the package we are installing into
166 # package_dir. We need this to check for test-requirements.txt,
167 # at least.
168 #
169 # ${!#} expands to the last positional argument to this function.
170 # With "extras" syntax included, our arguments might be something
171 # like:
172 # -e /path/to/fooproject[extra]
173 # Thus this magic line grabs just the path without extras
174 #
175 # Note that this makes no sense if this is a pypi (rather than
176 # local path) install; ergo you must check this path exists before
177 # use. Also, if we had multiple or mixed installs, we would also
178 # likely break. But for historical reasons, it's basically only
179 # the other wrapper functions in here calling this to install
180 # local packages, and they do so with single call per install. So
181 # this works (for now...)
182 local package_dir=${!#%\[*\]}
183
Ian Wienandbcb2c302020-01-13 16:31:20 +1100184 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
185 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
Dean Troyer490430d2015-01-30 14:38:35 -0600186 local sudo_pip="env"
187 else
Ian Wienandbcb2c302020-01-13 16:31:20 +1100188 local cmd_pip
Ian Wienandbcb2c302020-01-13 16:31:20 +1100189 local sudo_pip="sudo -H"
190 if python3_enabled; then
Radosław Piliszek29bf8522020-01-24 11:52:13 +0100191 echo "Using python $PYTHON3_VERSION to install $package_dir because python3_enabled=True"
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100192 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
193 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
Radosław Piliszek29bf8522020-01-24 11:52:13 +0100194 else
195 echo "Using python $PYTHON2_VERSION to install $package_dir because python3_enabled=False"
196 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600197 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600198 fi
199
Robert Collins635a5ba2015-06-10 08:48:06 +1200200 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700201 # Always apply constraints
202 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200203
Dean Troyer490430d2015-01-30 14:38:35 -0600204 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700205
206 # Also install test requirements
207 local install_test_reqs=""
Zane Bitter9e7ead92017-10-05 16:51:09 -0400208 local test_req="${package_dir}/test-requirements.txt"
Clark Boylanf266a2d2017-06-12 14:57:59 -0700209 if [[ -e "$test_req" ]]; then
210 install_test_reqs="-r $test_req"
211 fi
212
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200213 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
214 # the same behaviour of setuptools before version 25.0.0.
215 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600216 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800217 http_proxy="${http_proxy:-}" \
218 https_proxy="${https_proxy:-}" \
219 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800220 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200221 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Clark Boylanf266a2d2017-06-12 14:57:59 -0700222 $cmd_pip $upgrade $install_test_reqs \
Dean Troyer490430d2015-01-30 14:38:35 -0600223 $@
Federico Ressie208d062015-11-21 11:15:39 +0000224 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600225
Sean Daguecb658fa2015-10-08 17:12:03 -0400226 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000227 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600228}
229
Sean Daguef28e7ef2017-05-07 22:02:10 -0400230function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900231 # Skip uninstall if offline
232 [[ "${OFFLINE}" = "True" ]] && return
233
Sean Daguef28e7ef2017-05-07 22:02:10 -0400234 local name=$1
235 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
236 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
237 local sudo_pip="env"
238 else
239 local cmd_pip
Sean Daguef28e7ef2017-05-07 22:02:10 -0400240 local sudo_pip="sudo -H"
Radosław Piliszek29bf8522020-01-24 11:52:13 +0100241 if python3_enabled; then
242 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
243 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
244 else
245 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
246 fi
Sean Daguef28e7ef2017-05-07 22:02:10 -0400247 fi
248 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400249 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400250}
251
Joe Gordond5ac7852015-02-06 19:29:23 -0800252# get version of a package from global requirements file
253# get_from_global_requirements <package>
254function get_from_global_requirements {
255 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100256 local required_pkg
257 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800258 if [[ $required_pkg == "" ]]; then
259 die $LINENO "Can't find package $package in requirements"
260 fi
261 echo $required_pkg
262}
263
Dean Troyer490430d2015-01-30 14:38:35 -0600264# should we use this library from their git repo, or should we let it
265# get pulled in via pip dependencies.
266function use_library_from_git {
267 local name=$1
268 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200269 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600270 return $enabled
271}
272
Sean Daguec71973e2015-09-08 07:12:48 -0400273# determine if a package was installed from git
274function lib_installed_from_git {
275 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800276 local safe_name
277 safe_name=$(python -c "from pkg_resources import safe_name; \
278 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000279 # Note "pip freeze" doesn't always work here, because it tries to
280 # be smart about finding the remote of the git repo the package
281 # was installed from. This doesn't work with zuul which clones
282 # repos with no remote.
283 #
284 # The best option seems to be to use "pip list" which will tell
285 # you the path an editable install was installed from; for example
286 # in response to something like
Matt Riedemann9b6d2f22019-06-18 10:43:16 -0400287 # pip install -e 'git+https://opendev.org/openstack/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500288 # pip list --format columns shows
289 # bashate 0.5.2.dev19 /tmp/env/src/bashate
290 # Thus we check the third column to see if we're installed from
291 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800292 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400293}
294
Dean Troyer490430d2015-01-30 14:38:35 -0600295# setup a library by name. If we are trying to use the library from
296# git, we'll do a git based install, otherwise we'll punt and the
297# library should be installed by a requirements pull from another
298# project.
299function setup_lib {
300 local name=$1
301 local dir=${GITDIR[$name]}
302 setup_install $dir
303}
304
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900305# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600306# the library from git, we'll do a git based install, otherwise we'll
307# punt and the library should be installed by a requirements pull from
308# another project.
309#
310# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100311#
312# setup_dev_lib [-bindep] <name>
Dean Troyer490430d2015-01-30 14:38:35 -0600313function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100314 local bindep
315 if [[ $1 == -bindep* ]]; then
316 bindep="${1}"
317 shift
318 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600319 local name=$1
320 local dir=${GITDIR[$name]}
Ian Wienand58243f62018-12-13 14:05:53 +1100321 setup_develop $bindep $dir
Dean Troyer490430d2015-01-30 14:38:35 -0600322}
323
324# this should be used if you want to install globally, all libraries should
325# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500326#
327# setup_install project_dir [extras]
328# project_dir: directory of project repo (e.g., /opt/stack/keystone)
329# extras: comma-separated list of optional dependencies to install
330# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900331# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Ian Wienand58243f62018-12-13 14:05:53 +1100332# bindep: Set "-bindep" as first argument to install bindep.txt packages
Brant Knudson0842b812015-08-03 13:31:25 -0500333# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600334function setup_install {
Ian Wienand58243f62018-12-13 14:05:53 +1100335 local bindep
336 if [[ $1 == -bindep* ]]; then
337 bindep="${1}"
338 shift
339 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600340 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500341 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100342 _setup_package_with_constraints_edit $bindep $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600343}
344
345# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500346#
347# setup_develop project_dir [extras]
348# project_dir: directory of project repo (e.g., /opt/stack/keystone)
349# extras: comma-separated list of optional dependencies to install
350# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900351# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500352# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600353function setup_develop {
Ian Wienand58243f62018-12-13 14:05:53 +1100354 local bindep
355 if [[ $1 == -bindep* ]]; then
356 bindep="${1}"
357 shift
358 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600359 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500360 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100361 _setup_package_with_constraints_edit $bindep $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600362}
363
Dean Troyer490430d2015-01-30 14:38:35 -0600364# ``pip install -e`` the package, which processes the dependencies
365# using pip before running `setup.py develop`
366#
Clark Boylan05aa3842015-08-03 11:14:13 -0700367# Updates the constraints from REQUIREMENTS_DIR to reflect the
368# future installed state of this package. This ensures when we
369# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600370#
Clark Boylan05aa3842015-08-03 11:14:13 -0700371# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500372# _setup_package_with_constraints_edit project_dir flags [extras]
373# project_dir: directory of project repo (e.g., /opt/stack/keystone)
374# flags: pip CLI options/flags
375# extras: comma-separated list of optional dependencies to install
376# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900377# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500378# The command is like "pip install <flags> <project_dir>[<extras>]"
379function _setup_package_with_constraints_edit {
Ian Wienand58243f62018-12-13 14:05:53 +1100380 local bindep
381 if [[ $1 == -bindep* ]]; then
382 bindep="${1}"
383 shift
384 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600385 local project_dir=$1
386 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500387 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600388
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900389 # Normalize the directory name to avoid
390 # "installation from path or url cannot be constrained to a version"
391 # error.
392 # REVISIT(yamamoto): Remove this when fixed in pip.
393 # https://github.com/pypa/pip/pull/3582
394 project_dir=$(cd $project_dir && pwd)
395
Robert Collins635a5ba2015-06-10 08:48:06 +1200396 if [ -n "$REQUIREMENTS_DIR" ]; then
397 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100398 local name
399 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200400 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
401 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
402 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200403 fi
404
Ian Wienand58243f62018-12-13 14:05:53 +1100405 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600406
James E. Blaire1edde32018-03-02 15:05:14 +0000407 # If this project is in LIBS_FROM_GIT, verify it was actually installed
408 # correctly. This helps catch errors caused by constraints mismatches.
409 if use_library_from_git "$project_dir"; then
410 if ! lib_installed_from_git "$project_dir"; then
411 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
412 fi
413 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600414}
415
416# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100417# using pip before running `setup.py develop`. The command is like
418# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500419#
Dean Troyer490430d2015-01-30 14:38:35 -0600420# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100421#
422# Usage:
423# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
424#
425# -bindep : Use bindep to install dependencies; select extra profiles
426# as comma separated arguments after "="
427# project_dir : directory of project repo (e.g., /opt/stack/keystone)
428# flags : pip CLI options/flags
429# extras : comma-separated list of optional dependencies to install
430# (e.g., ldap,memcache).
431# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600432function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100433 local bindep=0
434 local bindep_flag=""
435 local bindep_profiles=""
436 if [[ $1 == -bindep* ]]; then
437 bindep=1
438 IFS="=" read bindep_flag bindep_profiles <<< ${1}
439 shift
440 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600441 local project_dir=$1
442 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500443 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600444
Brant Knudson0842b812015-08-03 13:31:25 -0500445 # if the flags variable exists, and it doesn't look like a flag,
446 # assume it's actually the extras list.
447 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
448 extras=$flags
449 flags=""
450 fi
451
452 if [[ ! -z "$extras" ]]; then
453 extras="[$extras]"
454 fi
455
Ian Wienand58243f62018-12-13 14:05:53 +1100456 # install any bindep packages
457 if [[ $bindep == 1 ]]; then
458 install_bindep $project_dir/bindep.txt $bindep_profiles
459 fi
460
Brant Knudson0842b812015-08-03 13:31:25 -0500461 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600462 # ensure that further actions can do things like setup.py sdist
463 if [[ "$flags" == "-e" ]]; then
464 safe_chown -R $STACK_USER $1/*.egg-info
465 fi
466}
467
Doug Hellmannddc38392015-05-07 21:06:24 +0000468# Report whether python 3 should be used
469function python3_enabled {
470 if [[ $USE_PYTHON3 == "True" ]]; then
471 return 0
472 else
473 return 1
474 fi
475}
476
Federico Ressi21a10d32020-01-31 07:43:30 +0100477# Provide requested python version and sets PYTHON variable
478function install_python {
479 # NOTE: install_python function should finally just do what install_python3
480 # does as soon Python 2 support has been dropped
481 if python3_enabled; then
482 install_python3
483 export PYTHON=$(which python${PYTHON3_VERSION} 2>/dev/null ||
484 which python3 2>/dev/null)
485 if [[ "${DISTRO}" =~ (rhel8) ]]; then
486 # Use Python 3 as default python command so that we have only one
487 # python alternative to use on the system for either python and
488 # python3
489 sudo alternatives --set python "${PYTHON}"
490 else
491 # Install anyway Python 2 for legacy scripts that still requires
492 # python instead of python3 command
493 install_package python
494 fi
495 else
496 echo "WARNING - Python 2 support has been deprecated in favor of Python 3"
497 install_package python
498 export PYTHON=$(which python 2>/dev/null)
499 fi
500}
501
Doug Hellmannddc38392015-05-07 21:06:24 +0000502# Install python3 packages
503function install_python3 {
504 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500505 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700506 elif is_suse; then
507 install_package python3-devel python3-dbm
Terry Wilson78cf6f62019-10-15 19:45:09 +0000508 elif is_fedora; then
Federico Ressi2dcbc282020-02-05 11:29:51 +0100509 if [ "$os_VENDOR" = "Fedora" ]; then
510 install_package python${PYTHON3_VERSION//.}
511 else
512 install_package python${PYTHON3_VERSION//.} python${PYTHON3_VERSION//.}-devel
513 fi
Doug Hellmannddc38392015-05-07 21:06:24 +0000514 fi
515}
Dean Troyer490430d2015-01-30 14:38:35 -0600516
Sean Daguef80e2cf2017-01-18 15:42:32 -0500517function install_devstack_tools {
518 # intentionally old to ensure devstack-gate has control
519 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
520 install_python3
521 sudo pip3 install -U devstack-tools==${dstools_version}
522}
523
Dean Troyer490430d2015-01-30 14:38:35 -0600524# Restore xtrace
525$INC_PY_TRACE
526
527# Local variables:
528# mode: shell-script
529# End: