blob: c94e5a49528f4738a602c1c8673a5dd89c304220 [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"
Joel Capitaoc6c5e122024-11-13 10:33:28 +0100202 local sudo_pip="sudo -H LC_ALL=en_US.UTF-8"
Jens Harbottd7a82f42020-06-23 10:21:09 +0200203 echo "Using python $PYTHON3_VERSION to install $package_dir"
Dean Troyer490430d2015-01-30 14:38:35 -0600204 fi
205
Robert Collins635a5ba2015-06-10 08:48:06 +1200206 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700207 # Always apply constraints
208 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200209
Dean Troyer490430d2015-01-30 14:38:35 -0600210 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700211
Dean Troyer490430d2015-01-30 14:38:35 -0600212 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800213 http_proxy="${http_proxy:-}" \
214 https_proxy="${https_proxy:-}" \
215 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800216 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Monty Taylor09b5b052020-03-27 11:22:39 -0500217 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600218 $@
Federico Ressie208d062015-11-21 11:15:39 +0000219 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600220
Sean Daguecb658fa2015-10-08 17:12:03 -0400221 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000222 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600223}
224
Sean Daguef28e7ef2017-05-07 22:02:10 -0400225function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900226 # Skip uninstall if offline
227 [[ "${OFFLINE}" = "True" ]] && return
228
Sean Daguef28e7ef2017-05-07 22:02:10 -0400229 local name=$1
230 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
231 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
232 local sudo_pip="env"
233 else
Jens Harbottd7a82f42020-06-23 10:21:09 +0200234 local cmd_pip="python$PYTHON3_VERSION -m pip"
235 local sudo_pip="sudo -H LC_ALL=en_US.UTF-8"
Sean Daguef28e7ef2017-05-07 22:02:10 -0400236 fi
237 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400238 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400239}
240
Joe Gordond5ac7852015-02-06 19:29:23 -0800241# get version of a package from global requirements file
242# get_from_global_requirements <package>
243function get_from_global_requirements {
244 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100245 local required_pkg
246 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800247 if [[ $required_pkg == "" ]]; then
248 die $LINENO "Can't find package $package in requirements"
249 fi
250 echo $required_pkg
251}
252
Radosław Piliszekbe263062020-03-30 09:56:53 +0200253# get only version constraints of a package from global requirements file
254# get_version_constraints_from_global_requirements <package>
255function get_version_constraints_from_global_requirements {
256 local package=$1
257 local required_pkg_version_constraint
258 # drop the package name from output (\K)
259 required_pkg_version_constraint=$(grep -i -h -o -P "^${package}\K.*" $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
260 if [[ $required_pkg_version_constraint == "" ]]; then
261 die $LINENO "Can't find package $package in requirements"
262 fi
263 echo $required_pkg_version_constraint
264}
265
Dean Troyer490430d2015-01-30 14:38:35 -0600266# should we use this library from their git repo, or should we let it
267# get pulled in via pip dependencies.
268function use_library_from_git {
269 local name=$1
270 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200271 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600272 return $enabled
273}
274
Sean Daguec71973e2015-09-08 07:12:48 -0400275# determine if a package was installed from git
276function lib_installed_from_git {
277 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800278 local safe_name
279 safe_name=$(python -c "from pkg_resources import safe_name; \
280 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000281 # Note "pip freeze" doesn't always work here, because it tries to
282 # be smart about finding the remote of the git repo the package
283 # was installed from. This doesn't work with zuul which clones
284 # repos with no remote.
285 #
286 # The best option seems to be to use "pip list" which will tell
287 # you the path an editable install was installed from; for example
288 # in response to something like
Matt Riedemann9b6d2f22019-06-18 10:43:16 -0400289 # pip install -e 'git+https://opendev.org/openstack/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500290 # pip list --format columns shows
291 # bashate 0.5.2.dev19 /tmp/env/src/bashate
292 # Thus we check the third column to see if we're installed from
293 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800294 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400295}
296
Dean Troyer490430d2015-01-30 14:38:35 -0600297# setup a library by name. If we are trying to use the library from
298# git, we'll do a git based install, otherwise we'll punt and the
299# library should be installed by a requirements pull from another
300# project.
301function setup_lib {
302 local name=$1
303 local dir=${GITDIR[$name]}
304 setup_install $dir
305}
306
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900307# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600308# the library from git, we'll do a git based install, otherwise we'll
309# punt and the library should be installed by a requirements pull from
310# another project.
311#
312# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100313#
Radosław Piliszekbe263062020-03-30 09:56:53 +0200314# setup_dev_lib [-bindep] <name> [<extras>]
Dean Troyer490430d2015-01-30 14:38:35 -0600315function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100316 local bindep
317 if [[ $1 == -bindep* ]]; then
318 bindep="${1}"
319 shift
320 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600321 local name=$1
322 local dir=${GITDIR[$name]}
Radosław Piliszekbe263062020-03-30 09:56:53 +0200323 local extras=$2
324 setup_develop $bindep $dir $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600325}
326
327# this should be used if you want to install globally, all libraries should
328# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500329#
330# setup_install project_dir [extras]
331# project_dir: directory of project repo (e.g., /opt/stack/keystone)
332# extras: comma-separated list of optional dependencies to install
333# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900334# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Ian Wienand58243f62018-12-13 14:05:53 +1100335# bindep: Set "-bindep" as first argument to install bindep.txt packages
Brant Knudson0842b812015-08-03 13:31:25 -0500336# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600337function setup_install {
Ian Wienand58243f62018-12-13 14:05:53 +1100338 local bindep
339 if [[ $1 == -bindep* ]]; then
340 bindep="${1}"
341 shift
342 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600343 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500344 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100345 _setup_package_with_constraints_edit $bindep $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600346}
347
348# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500349#
350# setup_develop project_dir [extras]
351# project_dir: directory of project repo (e.g., /opt/stack/keystone)
352# extras: comma-separated list of optional dependencies to install
353# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900354# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500355# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600356function setup_develop {
Ian Wienand58243f62018-12-13 14:05:53 +1100357 local bindep
358 if [[ $1 == -bindep* ]]; then
359 bindep="${1}"
360 shift
361 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600362 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500363 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100364 _setup_package_with_constraints_edit $bindep $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600365}
366
Dean Troyer490430d2015-01-30 14:38:35 -0600367# ``pip install -e`` the package, which processes the dependencies
368# using pip before running `setup.py develop`
369#
Clark Boylan05aa3842015-08-03 11:14:13 -0700370# Updates the constraints from REQUIREMENTS_DIR to reflect the
371# future installed state of this package. This ensures when we
372# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600373#
Clark Boylan05aa3842015-08-03 11:14:13 -0700374# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500375# _setup_package_with_constraints_edit project_dir flags [extras]
376# project_dir: directory of project repo (e.g., /opt/stack/keystone)
377# flags: pip CLI options/flags
378# extras: comma-separated list of optional dependencies to install
379# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900380# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500381# The command is like "pip install <flags> <project_dir>[<extras>]"
382function _setup_package_with_constraints_edit {
Ian Wienand58243f62018-12-13 14:05:53 +1100383 local bindep
384 if [[ $1 == -bindep* ]]; then
385 bindep="${1}"
386 shift
387 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600388 local project_dir=$1
389 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500390 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600391
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900392 # Normalize the directory name to avoid
393 # "installation from path or url cannot be constrained to a version"
394 # error.
395 # REVISIT(yamamoto): Remove this when fixed in pip.
396 # https://github.com/pypa/pip/pull/3582
397 project_dir=$(cd $project_dir && pwd)
398
Robert Collins635a5ba2015-06-10 08:48:06 +1200399 if [ -n "$REQUIREMENTS_DIR" ]; then
Ian Wienand6b9a5642021-07-28 11:19:57 +1000400 # Remove this package from constraints before we install it.
401 # That way, later installs won't "downgrade" the install from
402 # source we are about to do.
Ian Wienandada886d2015-10-07 14:06:26 +1100403 local name
404 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Slawek Kaplonski4ddd4562024-02-09 14:11:44 +0100405 if [ -z $name ]; then
406 name=$(awk '/^name =/ {gsub(/"/, "", $3); print $3}' $project_dir/pyproject.toml)
407 fi
Robert Collins7c838612015-07-03 13:28:09 +1200408 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
Ian Wienand6b9a5642021-07-28 11:19:57 +1000409 $REQUIREMENTS_DIR/upper-constraints.txt -- $name
Robert Collins635a5ba2015-06-10 08:48:06 +1200410 fi
411
Ian Wienand58243f62018-12-13 14:05:53 +1100412 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600413
James E. Blaire1edde32018-03-02 15:05:14 +0000414 # If this project is in LIBS_FROM_GIT, verify it was actually installed
415 # correctly. This helps catch errors caused by constraints mismatches.
416 if use_library_from_git "$project_dir"; then
417 if ! lib_installed_from_git "$project_dir"; then
418 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
419 fi
420 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600421}
422
423# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100424# using pip before running `setup.py develop`. The command is like
425# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500426#
Dean Troyer490430d2015-01-30 14:38:35 -0600427# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100428#
429# Usage:
430# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
431#
432# -bindep : Use bindep to install dependencies; select extra profiles
433# as comma separated arguments after "="
434# project_dir : directory of project repo (e.g., /opt/stack/keystone)
435# flags : pip CLI options/flags
436# extras : comma-separated list of optional dependencies to install
437# (e.g., ldap,memcache).
438# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600439function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100440 local bindep=0
441 local bindep_flag=""
442 local bindep_profiles=""
443 if [[ $1 == -bindep* ]]; then
444 bindep=1
445 IFS="=" read bindep_flag bindep_profiles <<< ${1}
446 shift
447 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600448 local project_dir=$1
449 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500450 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600451
Brant Knudson0842b812015-08-03 13:31:25 -0500452 # if the flags variable exists, and it doesn't look like a flag,
453 # assume it's actually the extras list.
454 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
455 extras=$flags
456 flags=""
457 fi
458
459 if [[ ! -z "$extras" ]]; then
460 extras="[$extras]"
461 fi
462
Ian Wienand58243f62018-12-13 14:05:53 +1100463 # install any bindep packages
464 if [[ $bindep == 1 ]]; then
465 install_bindep $project_dir/bindep.txt $bindep_profiles
466 fi
467
Brant Knudson0842b812015-08-03 13:31:25 -0500468 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600469 # ensure that further actions can do things like setup.py sdist
Clark Boylana40f9cb2018-04-04 14:02:30 -0700470 if [[ "$flags" == "-e" && "$GLOBAL_VENV" == "False" ]]; then
yatinkarel0ff62722024-09-02 17:29:55 +0530471 # egg-info is not created when project have pyproject.toml
472 if [ -d $1/*.egg-info ]; then
473 safe_chown -R $STACK_USER $1/*.egg-info
474 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600475 fi
476}
477
Doug Hellmannddc38392015-05-07 21:06:24 +0000478# Report whether python 3 should be used
Jens Harbottd7a82f42020-06-23 10:21:09 +0200479# TODO(frickler): drop this once all legacy uses are removed
Doug Hellmannddc38392015-05-07 21:06:24 +0000480function python3_enabled {
Radosław Piliszek3cbb33e2020-06-30 17:52:10 +0200481 return 0
Doug Hellmannddc38392015-05-07 21:06:24 +0000482}
483
Federico Ressi21a10d32020-01-31 07:43:30 +0100484# Provide requested python version and sets PYTHON variable
485function install_python {
Jens Harbottd7a82f42020-06-23 10:21:09 +0200486 install_python3
487 export PYTHON=$(which python${PYTHON3_VERSION} 2>/dev/null)
Federico Ressi21a10d32020-01-31 07:43:30 +0100488}
489
Doug Hellmannddc38392015-05-07 21:06:24 +0000490# Install python3 packages
491function install_python3 {
492 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500493 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Terry Wilson78cf6f62019-10-15 19:45:09 +0000494 elif is_fedora; then
Federico Ressi2dcbc282020-02-05 11:29:51 +0100495 if [ "$os_VENDOR" = "Fedora" ]; then
496 install_package python${PYTHON3_VERSION//.}
497 else
498 install_package python${PYTHON3_VERSION//.} python${PYTHON3_VERSION//.}-devel
499 fi
Doug Hellmannddc38392015-05-07 21:06:24 +0000500 fi
501}
Dean Troyer490430d2015-01-30 14:38:35 -0600502
Sean Daguef80e2cf2017-01-18 15:42:32 -0500503function install_devstack_tools {
504 # intentionally old to ensure devstack-gate has control
505 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
506 install_python3
507 sudo pip3 install -U devstack-tools==${dstools_version}
508}
509
Dean Troyer490430d2015-01-30 14:38:35 -0600510# Restore xtrace
511$INC_PY_TRACE
512
513# Local variables:
514# mode: shell-script
515# End: