blob: 1fd414773fdb286decc464453b452f18416b0166 [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
Dean Troyer490430d2015-01-30 14:38:35 -060010# - safe_chown
11
12# Save trace setting
13INC_PY_TRACE=$(set +o | grep xtrace)
14set +o xtrace
15
16
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060017# Global Config Variables
18
Atsushi SAKAI5509ed52015-11-30 20:20:21 +090019# PROJECT_VENV contains the name of the virtual environment for each
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060020# project. A null value installs to the system Python directories.
Sean Dagueafef8bf2017-03-06 14:07:23 -050021declare -A -g PROJECT_VENV
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060022
Radosław Piliszekbe263062020-03-30 09:56:53 +020023# Utility Functions
24# =================
25
26# Joins bash array of extras with commas as expected by other functions
27function join_extras {
28 local IFS=","
29 echo "$*"
30}
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060031
Dean Troyer490430d2015-01-30 14:38:35 -060032# Python Functions
33# ================
34
Clark Boylana40f9cb2018-04-04 14:02:30 -070035# Setup the global devstack virtualenvs and the associated environment
36# updates.
37function setup_devstack_virtualenv {
38 # We run devstack out of a global virtualenv.
39 if [[ ! -d $DEVSTACK_VENV ]] ; then
40 # Using system site packages to enable nova to use libguestfs.
41 # This package is currently installed via the distro and not
42 # available on pypi.
43 python$PYTHON3_VERSION -m venv --system-site-packages $DEVSTACK_VENV
Sean Mooney41d253a2024-06-20 19:03:37 +010044 pip_install -U pip setuptools
Riccardo Pittau6990b062024-07-24 18:01:51 +020045 #NOTE(rpittau): workaround for simplejson removal in osc
46 # https://review.opendev.org/c/openstack/python-openstackclient/+/920001
47 pip_install -U simplejson
Clark Boylana40f9cb2018-04-04 14:02:30 -070048 fi
49 if [[ ":$PATH:" != *":$DEVSTACK_VENV/bin:"* ]] ; then
50 export PATH="$DEVSTACK_VENV/bin:$PATH"
51 export PYTHON="$DEVSTACK_VENV/bin/python3"
52 fi
53}
54
Dean Troyer490430d2015-01-30 14:38:35 -060055# Get the path to the pip command.
56# get_pip_command
57function get_pip_command {
Doug Hellmannddc38392015-05-07 21:06:24 +000058 local version="$1"
Tom Barron4db9d562019-01-09 08:43:52 -050059 if [ -z "$version" ]; then
60 die $LINENO "pip python version is not set."
61 fi
62
Doug Hellmannddc38392015-05-07 21:06:24 +000063 # NOTE(dhellmann): I don't know if we actually get a pip3.4-python
64 # under any circumstances.
65 which pip${version} || which pip${version}-python
Dean Troyer490430d2015-01-30 14:38:35 -060066
67 if [ $? -ne 0 ]; then
Doug Hellmannddc38392015-05-07 21:06:24 +000068 die $LINENO "Unable to find pip${version}; cannot continue"
Dean Troyer490430d2015-01-30 14:38:35 -060069 fi
70}
71
Atsushi SAKAI5509ed52015-11-30 20:20:21 +090072# Get the path to the directory where python executables are installed.
Dean Troyer490430d2015-01-30 14:38:35 -060073# get_python_exec_prefix
74function get_python_exec_prefix {
Ian Wienand433a9b12015-10-07 13:29:31 +110075 local xtrace
76 xtrace=$(set +o | grep xtrace)
Dean Troyer2b564762015-02-11 17:01:02 -060077 set +o xtrace
78 if [[ -z "$os_PACKAGE" ]]; then
79 GetOSVersion
80 fi
81 $xtrace
82
Clark Boylana40f9cb2018-04-04 14:02:30 -070083 if [[ "$GLOBAL_VENV" == "True" ]] ; then
84 echo "$DEVSTACK_VENV/bin"
85 else
86 echo "/usr/local/bin"
87 fi
Dean Troyer490430d2015-01-30 14:38:35 -060088}
89
Sean Dague60996b12015-04-08 09:06:49 -040090# Wrapper for ``pip install`` that only installs versions of libraries
91# from the global-requirements specification.
92#
93# Uses globals ``REQUIREMENTS_DIR``
94#
95# pip_install_gr packagename
96function pip_install_gr {
97 local name=$1
Ian Wienandada886d2015-10-07 14:06:26 +110098 local clean_name
99 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -0400100 pip_install $clean_name
101}
102
Mehdi Abaakouk52b10742016-12-01 16:11:17 +0100103# Wrapper for ``pip install`` that only installs versions of libraries
104# from the global-requirements specification with extras.
105#
106# Uses globals ``REQUIREMENTS_DIR``
107#
108# pip_install_gr_extras packagename extra1,extra2,...
109function pip_install_gr_extras {
110 local name=$1
111 local extras=$2
Radosław Piliszekbe263062020-03-30 09:56:53 +0200112 local version_constraints
113 version_constraints=$(get_version_constraints_from_global_requirements $name)
114 pip_install $name[$extras]$version_constraints
Mehdi Abaakouk52b10742016-12-01 16:11:17 +0100115}
116
Doug Hellmann36377f62018-12-04 11:33:03 -0500117# enable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000118#
Doug Hellmann94129c72017-01-09 21:24:24 +0000119# enable_python3_package dir [dir ...]
120function enable_python3_package {
121 local xtrace
122 xtrace=$(set +o | grep xtrace)
123 set +o xtrace
124
Doug Hellmann36377f62018-12-04 11:33:03 -0500125 echo "It is no longer necessary to call enable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000126
127 $xtrace
128}
129
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100130# disable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000131#
Doug Hellmann94129c72017-01-09 21:24:24 +0000132# disable_python3_package dir [dir ...]
133function disable_python3_package {
134 local xtrace
135 xtrace=$(set +o | grep xtrace)
136 set +o xtrace
137
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100138 echo "It is no longer possible to call disable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000139
140 $xtrace
141}
142
Dean Troyer490430d2015-01-30 14:38:35 -0600143# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500144# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Ian Wienandbcb2c302020-01-13 16:31:20 +1100145# ``PIP_UPGRADE``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400146# Usage:
147# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600148function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000149 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100150 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600151 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000152 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600153 local offline=${OFFLINE:-False}
154 if [[ "$offline" == "True" || -z "$@" ]]; then
155 $xtrace
156 return
157 fi
158
Sean Daguecb658fa2015-10-08 17:12:03 -0400159 time_start "pip_install"
160
Chris Dentebdd9ac2015-03-04 12:35:14 +0000161 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
162 if [[ "$PIP_UPGRADE" = "True" ]] ; then
163 upgrade="--upgrade"
164 fi
165
Dean Troyer490430d2015-01-30 14:38:35 -0600166 if [[ -z "$os_PACKAGE" ]]; then
167 GetOSVersion
168 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400169
170 # Try to extract the path of the package we are installing into
171 # package_dir. We need this to check for test-requirements.txt,
172 # at least.
173 #
174 # ${!#} expands to the last positional argument to this function.
175 # With "extras" syntax included, our arguments might be something
176 # like:
177 # -e /path/to/fooproject[extra]
178 # Thus this magic line grabs just the path without extras
179 #
180 # Note that this makes no sense if this is a pypi (rather than
181 # local path) install; ergo you must check this path exists before
182 # use. Also, if we had multiple or mixed installs, we would also
183 # likely break. But for historical reasons, it's basically only
184 # the other wrapper functions in here calling this to install
185 # local packages, and they do so with single call per install. So
186 # this works (for now...)
187 local package_dir=${!#%\[*\]}
188
Ian Wienandbcb2c302020-01-13 16:31:20 +1100189 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
190 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
Dean Troyer490430d2015-01-30 14:38:35 -0600191 local sudo_pip="env"
Clark Boylana40f9cb2018-04-04 14:02:30 -0700192 elif [[ "${GLOBAL_VENV}" == "True" && -d ${DEVSTACK_VENV} ]] ; then
193 # We have to check that the DEVSTACK_VENV exists because early
194 # devstack boostrapping needs to operate in a system context
195 # too bootstrap pip. Once pip is bootstrapped we create the
196 # global venv and can start to use it.
197 local cmd_pip=$DEVSTACK_VENV/bin/pip
198 local sudo_pip="env"
199 echo "Using python $PYTHON3_VERSION to install $package_dir"
Dean Troyer490430d2015-01-30 14:38:35 -0600200 else
Jens Harbottd7a82f42020-06-23 10:21:09 +0200201 local cmd_pip="python$PYTHON3_VERSION -m pip"
Ian Wienand18b42512020-08-31 16:22:57 +1000202 # See
203 # https://github.com/pypa/setuptools/issues/2232
204 # http://lists.openstack.org/pipermail/openstack-discuss/2020-August/016905.html
205 # this makes setuptools >=50 use the platform distutils.
206 # We only want to do this on global pip installs, not if
207 # installing in a virtualenv
208 local sudo_pip="sudo -H LC_ALL=en_US.UTF-8 SETUPTOOLS_USE_DISTUTILS=stdlib "
Jens Harbottd7a82f42020-06-23 10:21:09 +0200209 echo "Using python $PYTHON3_VERSION to install $package_dir"
Dean Troyer490430d2015-01-30 14:38:35 -0600210 fi
211
Robert Collins635a5ba2015-06-10 08:48:06 +1200212 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700213 # Always apply constraints
214 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200215
Dean Troyer490430d2015-01-30 14:38:35 -0600216 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700217
Dean Troyer490430d2015-01-30 14:38:35 -0600218 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800219 http_proxy="${http_proxy:-}" \
220 https_proxy="${https_proxy:-}" \
221 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800222 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Monty Taylor09b5b052020-03-27 11:22:39 -0500223 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600224 $@
Federico Ressie208d062015-11-21 11:15:39 +0000225 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600226
Sean Daguecb658fa2015-10-08 17:12:03 -0400227 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000228 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600229}
230
Sean Daguef28e7ef2017-05-07 22:02:10 -0400231function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900232 # Skip uninstall if offline
233 [[ "${OFFLINE}" = "True" ]] && return
234
Sean Daguef28e7ef2017-05-07 22:02:10 -0400235 local name=$1
236 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
237 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
238 local sudo_pip="env"
239 else
Jens Harbottd7a82f42020-06-23 10:21:09 +0200240 local cmd_pip="python$PYTHON3_VERSION -m pip"
241 local sudo_pip="sudo -H LC_ALL=en_US.UTF-8"
Sean Daguef28e7ef2017-05-07 22:02:10 -0400242 fi
243 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400244 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400245}
246
Joe Gordond5ac7852015-02-06 19:29:23 -0800247# get version of a package from global requirements file
248# get_from_global_requirements <package>
249function get_from_global_requirements {
250 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100251 local required_pkg
252 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800253 if [[ $required_pkg == "" ]]; then
254 die $LINENO "Can't find package $package in requirements"
255 fi
256 echo $required_pkg
257}
258
Radosław Piliszekbe263062020-03-30 09:56:53 +0200259# get only version constraints of a package from global requirements file
260# get_version_constraints_from_global_requirements <package>
261function get_version_constraints_from_global_requirements {
262 local package=$1
263 local required_pkg_version_constraint
264 # drop the package name from output (\K)
265 required_pkg_version_constraint=$(grep -i -h -o -P "^${package}\K.*" $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
266 if [[ $required_pkg_version_constraint == "" ]]; then
267 die $LINENO "Can't find package $package in requirements"
268 fi
269 echo $required_pkg_version_constraint
270}
271
Dean Troyer490430d2015-01-30 14:38:35 -0600272# should we use this library from their git repo, or should we let it
273# get pulled in via pip dependencies.
274function use_library_from_git {
275 local name=$1
276 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200277 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600278 return $enabled
279}
280
Sean Daguec71973e2015-09-08 07:12:48 -0400281# determine if a package was installed from git
282function lib_installed_from_git {
283 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800284 local safe_name
285 safe_name=$(python -c "from pkg_resources import safe_name; \
286 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000287 # Note "pip freeze" doesn't always work here, because it tries to
288 # be smart about finding the remote of the git repo the package
289 # was installed from. This doesn't work with zuul which clones
290 # repos with no remote.
291 #
292 # The best option seems to be to use "pip list" which will tell
293 # you the path an editable install was installed from; for example
294 # in response to something like
Matt Riedemann9b6d2f22019-06-18 10:43:16 -0400295 # pip install -e 'git+https://opendev.org/openstack/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500296 # pip list --format columns shows
297 # bashate 0.5.2.dev19 /tmp/env/src/bashate
298 # Thus we check the third column to see if we're installed from
299 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800300 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400301}
302
Dean Troyer490430d2015-01-30 14:38:35 -0600303# setup a library by name. If we are trying to use the library from
304# git, we'll do a git based install, otherwise we'll punt and the
305# library should be installed by a requirements pull from another
306# project.
307function setup_lib {
308 local name=$1
309 local dir=${GITDIR[$name]}
310 setup_install $dir
311}
312
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900313# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600314# the library from git, we'll do a git based install, otherwise we'll
315# punt and the library should be installed by a requirements pull from
316# another project.
317#
318# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100319#
Radosław Piliszekbe263062020-03-30 09:56:53 +0200320# setup_dev_lib [-bindep] <name> [<extras>]
Dean Troyer490430d2015-01-30 14:38:35 -0600321function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100322 local bindep
323 if [[ $1 == -bindep* ]]; then
324 bindep="${1}"
325 shift
326 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600327 local name=$1
328 local dir=${GITDIR[$name]}
Radosław Piliszekbe263062020-03-30 09:56:53 +0200329 local extras=$2
330 setup_develop $bindep $dir $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600331}
332
333# this should be used if you want to install globally, all libraries should
334# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500335#
336# setup_install project_dir [extras]
337# project_dir: directory of project repo (e.g., /opt/stack/keystone)
338# extras: comma-separated list of optional dependencies to install
339# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900340# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Ian Wienand58243f62018-12-13 14:05:53 +1100341# bindep: Set "-bindep" as first argument to install bindep.txt packages
Brant Knudson0842b812015-08-03 13:31:25 -0500342# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600343function setup_install {
Ian Wienand58243f62018-12-13 14:05:53 +1100344 local bindep
345 if [[ $1 == -bindep* ]]; then
346 bindep="${1}"
347 shift
348 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600349 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500350 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100351 _setup_package_with_constraints_edit $bindep $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600352}
353
354# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500355#
356# setup_develop project_dir [extras]
357# project_dir: directory of project repo (e.g., /opt/stack/keystone)
358# extras: comma-separated list of optional dependencies to install
359# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900360# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500361# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600362function setup_develop {
Ian Wienand58243f62018-12-13 14:05:53 +1100363 local bindep
364 if [[ $1 == -bindep* ]]; then
365 bindep="${1}"
366 shift
367 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600368 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500369 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100370 _setup_package_with_constraints_edit $bindep $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600371}
372
Dean Troyer490430d2015-01-30 14:38:35 -0600373# ``pip install -e`` the package, which processes the dependencies
374# using pip before running `setup.py develop`
375#
Clark Boylan05aa3842015-08-03 11:14:13 -0700376# Updates the constraints from REQUIREMENTS_DIR to reflect the
377# future installed state of this package. This ensures when we
378# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600379#
Clark Boylan05aa3842015-08-03 11:14:13 -0700380# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500381# _setup_package_with_constraints_edit project_dir flags [extras]
382# project_dir: directory of project repo (e.g., /opt/stack/keystone)
383# flags: pip CLI options/flags
384# extras: comma-separated list of optional dependencies to install
385# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900386# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500387# The command is like "pip install <flags> <project_dir>[<extras>]"
388function _setup_package_with_constraints_edit {
Ian Wienand58243f62018-12-13 14:05:53 +1100389 local bindep
390 if [[ $1 == -bindep* ]]; then
391 bindep="${1}"
392 shift
393 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600394 local project_dir=$1
395 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500396 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600397
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900398 # Normalize the directory name to avoid
399 # "installation from path or url cannot be constrained to a version"
400 # error.
401 # REVISIT(yamamoto): Remove this when fixed in pip.
402 # https://github.com/pypa/pip/pull/3582
403 project_dir=$(cd $project_dir && pwd)
404
Robert Collins635a5ba2015-06-10 08:48:06 +1200405 if [ -n "$REQUIREMENTS_DIR" ]; then
Ian Wienand6b9a5642021-07-28 11:19:57 +1000406 # Remove this package from constraints before we install it.
407 # That way, later installs won't "downgrade" the install from
408 # source we are about to do.
Ian Wienandada886d2015-10-07 14:06:26 +1100409 local name
410 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Slawek Kaplonski4ddd4562024-02-09 14:11:44 +0100411 if [ -z $name ]; then
412 name=$(awk '/^name =/ {gsub(/"/, "", $3); print $3}' $project_dir/pyproject.toml)
413 fi
Robert Collins7c838612015-07-03 13:28:09 +1200414 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
Ian Wienand6b9a5642021-07-28 11:19:57 +1000415 $REQUIREMENTS_DIR/upper-constraints.txt -- $name
Robert Collins635a5ba2015-06-10 08:48:06 +1200416 fi
417
Ian Wienand58243f62018-12-13 14:05:53 +1100418 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600419
James E. Blaire1edde32018-03-02 15:05:14 +0000420 # If this project is in LIBS_FROM_GIT, verify it was actually installed
421 # correctly. This helps catch errors caused by constraints mismatches.
422 if use_library_from_git "$project_dir"; then
423 if ! lib_installed_from_git "$project_dir"; then
424 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
425 fi
426 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600427}
428
429# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100430# using pip before running `setup.py develop`. The command is like
431# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500432#
Dean Troyer490430d2015-01-30 14:38:35 -0600433# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100434#
435# Usage:
436# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
437#
438# -bindep : Use bindep to install dependencies; select extra profiles
439# as comma separated arguments after "="
440# project_dir : directory of project repo (e.g., /opt/stack/keystone)
441# flags : pip CLI options/flags
442# extras : comma-separated list of optional dependencies to install
443# (e.g., ldap,memcache).
444# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600445function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100446 local bindep=0
447 local bindep_flag=""
448 local bindep_profiles=""
449 if [[ $1 == -bindep* ]]; then
450 bindep=1
451 IFS="=" read bindep_flag bindep_profiles <<< ${1}
452 shift
453 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600454 local project_dir=$1
455 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500456 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600457
Brant Knudson0842b812015-08-03 13:31:25 -0500458 # if the flags variable exists, and it doesn't look like a flag,
459 # assume it's actually the extras list.
460 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
461 extras=$flags
462 flags=""
463 fi
464
465 if [[ ! -z "$extras" ]]; then
466 extras="[$extras]"
467 fi
468
Ian Wienand58243f62018-12-13 14:05:53 +1100469 # install any bindep packages
470 if [[ $bindep == 1 ]]; then
471 install_bindep $project_dir/bindep.txt $bindep_profiles
472 fi
473
Brant Knudson0842b812015-08-03 13:31:25 -0500474 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600475 # ensure that further actions can do things like setup.py sdist
Clark Boylana40f9cb2018-04-04 14:02:30 -0700476 if [[ "$flags" == "-e" && "$GLOBAL_VENV" == "False" ]]; then
Dean Troyer490430d2015-01-30 14:38:35 -0600477 safe_chown -R $STACK_USER $1/*.egg-info
478 fi
479}
480
Doug Hellmannddc38392015-05-07 21:06:24 +0000481# Report whether python 3 should be used
Jens Harbottd7a82f42020-06-23 10:21:09 +0200482# TODO(frickler): drop this once all legacy uses are removed
Doug Hellmannddc38392015-05-07 21:06:24 +0000483function python3_enabled {
Radosław Piliszek3cbb33e2020-06-30 17:52:10 +0200484 return 0
Doug Hellmannddc38392015-05-07 21:06:24 +0000485}
486
Federico Ressi21a10d32020-01-31 07:43:30 +0100487# Provide requested python version and sets PYTHON variable
488function install_python {
Jens Harbottd7a82f42020-06-23 10:21:09 +0200489 install_python3
490 export PYTHON=$(which python${PYTHON3_VERSION} 2>/dev/null)
Federico Ressi21a10d32020-01-31 07:43:30 +0100491}
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
Terry Wilson78cf6f62019-10-15 19:45:09 +0000497 elif is_fedora; then
Federico Ressi2dcbc282020-02-05 11:29:51 +0100498 if [ "$os_VENDOR" = "Fedora" ]; then
499 install_package python${PYTHON3_VERSION//.}
500 else
501 install_package python${PYTHON3_VERSION//.} python${PYTHON3_VERSION//.}-devel
502 fi
Doug Hellmannddc38392015-05-07 21:06:24 +0000503 fi
504}
Dean Troyer490430d2015-01-30 14:38:35 -0600505
Sean Daguef80e2cf2017-01-18 15:42:32 -0500506function install_devstack_tools {
507 # intentionally old to ensure devstack-gate has control
508 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
509 install_python3
510 sudo pip3 install -U devstack-tools==${dstools_version}
511}
512
Dean Troyer490430d2015-01-30 14:38:35 -0600513# Restore xtrace
514$INC_PY_TRACE
515
516# Local variables:
517# mode: shell-script
518# End: