blob: cd90ac82c6ed0aad6aaa2f111926df0f1be839d7 [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.
Stephen Finucane320c2bf2024-12-03 17:04:39 +000043 $PYTHON -m venv --system-site-packages "${DEVSTACK_VENV}"
44 pip_install -U pip setuptools[core]
Clark Boylana40f9cb2018-04-04 14:02:30 -070045 fi
46 if [[ ":$PATH:" != *":$DEVSTACK_VENV/bin:"* ]] ; then
47 export PATH="$DEVSTACK_VENV/bin:$PATH"
48 export PYTHON="$DEVSTACK_VENV/bin/python3"
49 fi
50}
51
Dean Troyer490430d2015-01-30 14:38:35 -060052# Get the path to the pip command.
53# get_pip_command
54function get_pip_command {
Doug Hellmannddc38392015-05-07 21:06:24 +000055 local version="$1"
Tom Barron4db9d562019-01-09 08:43:52 -050056 if [ -z "$version" ]; then
57 die $LINENO "pip python version is not set."
58 fi
59
Doug Hellmannddc38392015-05-07 21:06:24 +000060 # NOTE(dhellmann): I don't know if we actually get a pip3.4-python
61 # under any circumstances.
62 which pip${version} || which pip${version}-python
Dean Troyer490430d2015-01-30 14:38:35 -060063
64 if [ $? -ne 0 ]; then
Doug Hellmannddc38392015-05-07 21:06:24 +000065 die $LINENO "Unable to find pip${version}; cannot continue"
Dean Troyer490430d2015-01-30 14:38:35 -060066 fi
67}
68
Atsushi SAKAI5509ed52015-11-30 20:20:21 +090069# Get the path to the directory where python executables are installed.
Dean Troyer490430d2015-01-30 14:38:35 -060070# get_python_exec_prefix
71function get_python_exec_prefix {
Ian Wienand433a9b12015-10-07 13:29:31 +110072 local xtrace
73 xtrace=$(set +o | grep xtrace)
Dean Troyer2b564762015-02-11 17:01:02 -060074 set +o xtrace
75 if [[ -z "$os_PACKAGE" ]]; then
76 GetOSVersion
77 fi
78 $xtrace
79
Clark Boylana40f9cb2018-04-04 14:02:30 -070080 if [[ "$GLOBAL_VENV" == "True" ]] ; then
81 echo "$DEVSTACK_VENV/bin"
82 else
83 echo "/usr/local/bin"
84 fi
Dean Troyer490430d2015-01-30 14:38:35 -060085}
86
Sean Dague60996b12015-04-08 09:06:49 -040087# Wrapper for ``pip install`` that only installs versions of libraries
88# from the global-requirements specification.
89#
90# Uses globals ``REQUIREMENTS_DIR``
91#
92# pip_install_gr packagename
93function pip_install_gr {
94 local name=$1
Ian Wienandada886d2015-10-07 14:06:26 +110095 local clean_name
96 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -040097 pip_install $clean_name
98}
99
Mehdi Abaakouk52b10742016-12-01 16:11:17 +0100100# Wrapper for ``pip install`` that only installs versions of libraries
101# from the global-requirements specification with extras.
102#
103# Uses globals ``REQUIREMENTS_DIR``
104#
105# pip_install_gr_extras packagename extra1,extra2,...
106function pip_install_gr_extras {
107 local name=$1
108 local extras=$2
Radosław Piliszekbe263062020-03-30 09:56:53 +0200109 local version_constraints
110 version_constraints=$(get_version_constraints_from_global_requirements $name)
111 pip_install $name[$extras]$version_constraints
Mehdi Abaakouk52b10742016-12-01 16:11:17 +0100112}
113
Doug Hellmann36377f62018-12-04 11:33:03 -0500114# enable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000115#
Doug Hellmann94129c72017-01-09 21:24:24 +0000116# enable_python3_package dir [dir ...]
117function enable_python3_package {
118 local xtrace
119 xtrace=$(set +o | grep xtrace)
120 set +o xtrace
121
Doug Hellmann36377f62018-12-04 11:33:03 -0500122 echo "It is no longer necessary to call enable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000123
124 $xtrace
125}
126
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100127# disable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000128#
Doug Hellmann94129c72017-01-09 21:24:24 +0000129# disable_python3_package dir [dir ...]
130function disable_python3_package {
131 local xtrace
132 xtrace=$(set +o | grep xtrace)
133 set +o xtrace
134
Stephen Finucane6b6bdc72019-10-09 16:13:20 +0100135 echo "It is no longer possible to call disable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000136
137 $xtrace
138}
139
Dean Troyer490430d2015-01-30 14:38:35 -0600140# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500141# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Ian Wienandbcb2c302020-01-13 16:31:20 +1100142# ``PIP_UPGRADE``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400143# Usage:
144# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600145function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000146 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100147 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600148 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000149 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600150 local offline=${OFFLINE:-False}
151 if [[ "$offline" == "True" || -z "$@" ]]; then
152 $xtrace
153 return
154 fi
155
Sean Daguecb658fa2015-10-08 17:12:03 -0400156 time_start "pip_install"
157
Chris Dentebdd9ac2015-03-04 12:35:14 +0000158 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
159 if [[ "$PIP_UPGRADE" = "True" ]] ; then
160 upgrade="--upgrade"
161 fi
162
Dean Troyer490430d2015-01-30 14:38:35 -0600163 if [[ -z "$os_PACKAGE" ]]; then
164 GetOSVersion
165 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400166
167 # Try to extract the path of the package we are installing into
168 # package_dir. We need this to check for test-requirements.txt,
169 # at least.
170 #
171 # ${!#} expands to the last positional argument to this function.
172 # With "extras" syntax included, our arguments might be something
173 # like:
174 # -e /path/to/fooproject[extra]
175 # Thus this magic line grabs just the path without extras
176 #
177 # Note that this makes no sense if this is a pypi (rather than
178 # local path) install; ergo you must check this path exists before
179 # use. Also, if we had multiple or mixed installs, we would also
180 # likely break. But for historical reasons, it's basically only
181 # the other wrapper functions in here calling this to install
182 # local packages, and they do so with single call per install. So
183 # this works (for now...)
184 local package_dir=${!#%\[*\]}
185
Ian Wienandbcb2c302020-01-13 16:31:20 +1100186 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
187 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
Dean Troyer490430d2015-01-30 14:38:35 -0600188 local sudo_pip="env"
Clark Boylana40f9cb2018-04-04 14:02:30 -0700189 elif [[ "${GLOBAL_VENV}" == "True" && -d ${DEVSTACK_VENV} ]] ; then
190 # We have to check that the DEVSTACK_VENV exists because early
191 # devstack boostrapping needs to operate in a system context
192 # too bootstrap pip. Once pip is bootstrapped we create the
193 # global venv and can start to use it.
194 local cmd_pip=$DEVSTACK_VENV/bin/pip
195 local sudo_pip="env"
196 echo "Using python $PYTHON3_VERSION to install $package_dir"
Dean Troyer490430d2015-01-30 14:38:35 -0600197 else
Jens Harbottd7a82f42020-06-23 10:21:09 +0200198 local cmd_pip="python$PYTHON3_VERSION -m pip"
Joel Capitaoc6c5e122024-11-13 10:33:28 +0100199 local sudo_pip="sudo -H LC_ALL=en_US.UTF-8"
Jens Harbottd7a82f42020-06-23 10:21:09 +0200200 echo "Using python $PYTHON3_VERSION to install $package_dir"
Dean Troyer490430d2015-01-30 14:38:35 -0600201 fi
202
Robert Collins635a5ba2015-06-10 08:48:06 +1200203 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700204 # Always apply constraints
205 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200206
Dean Troyer490430d2015-01-30 14:38:35 -0600207 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700208
Dean Troyer490430d2015-01-30 14:38:35 -0600209 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800210 http_proxy="${http_proxy:-}" \
211 https_proxy="${https_proxy:-}" \
212 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800213 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Monty Taylor09b5b052020-03-27 11:22:39 -0500214 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600215 $@
Federico Ressie208d062015-11-21 11:15:39 +0000216 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600217
Sean Daguecb658fa2015-10-08 17:12:03 -0400218 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000219 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600220}
221
Sean Daguef28e7ef2017-05-07 22:02:10 -0400222function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900223 # Skip uninstall if offline
224 [[ "${OFFLINE}" = "True" ]] && return
225
Sean Daguef28e7ef2017-05-07 22:02:10 -0400226 local name=$1
227 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
228 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
229 local sudo_pip="env"
230 else
Jens Harbottd7a82f42020-06-23 10:21:09 +0200231 local cmd_pip="python$PYTHON3_VERSION -m pip"
232 local sudo_pip="sudo -H LC_ALL=en_US.UTF-8"
Sean Daguef28e7ef2017-05-07 22:02:10 -0400233 fi
234 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400235 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400236}
237
Joe Gordond5ac7852015-02-06 19:29:23 -0800238# get version of a package from global requirements file
239# get_from_global_requirements <package>
240function get_from_global_requirements {
241 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100242 local required_pkg
243 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800244 if [[ $required_pkg == "" ]]; then
245 die $LINENO "Can't find package $package in requirements"
246 fi
247 echo $required_pkg
248}
249
Radosław Piliszekbe263062020-03-30 09:56:53 +0200250# get only version constraints of a package from global requirements file
251# get_version_constraints_from_global_requirements <package>
252function get_version_constraints_from_global_requirements {
253 local package=$1
254 local required_pkg_version_constraint
255 # drop the package name from output (\K)
256 required_pkg_version_constraint=$(grep -i -h -o -P "^${package}\K.*" $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
257 if [[ $required_pkg_version_constraint == "" ]]; then
258 die $LINENO "Can't find package $package in requirements"
259 fi
260 echo $required_pkg_version_constraint
261}
262
Dean Troyer490430d2015-01-30 14:38:35 -0600263# should we use this library from their git repo, or should we let it
264# get pulled in via pip dependencies.
265function use_library_from_git {
266 local name=$1
267 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200268 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600269 return $enabled
270}
271
Sean Daguec71973e2015-09-08 07:12:48 -0400272# determine if a package was installed from git
273function lib_installed_from_git {
274 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800275 local safe_name
276 safe_name=$(python -c "from pkg_resources import safe_name; \
277 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000278 # Note "pip freeze" doesn't always work here, because it tries to
279 # be smart about finding the remote of the git repo the package
280 # was installed from. This doesn't work with zuul which clones
281 # repos with no remote.
282 #
283 # The best option seems to be to use "pip list" which will tell
284 # you the path an editable install was installed from; for example
285 # in response to something like
Matt Riedemann9b6d2f22019-06-18 10:43:16 -0400286 # pip install -e 'git+https://opendev.org/openstack/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500287 # pip list --format columns shows
288 # bashate 0.5.2.dev19 /tmp/env/src/bashate
289 # Thus we check the third column to see if we're installed from
290 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800291 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400292}
293
Dean Troyer490430d2015-01-30 14:38:35 -0600294# setup a library by name. If we are trying to use the library from
295# git, we'll do a git based install, otherwise we'll punt and the
296# library should be installed by a requirements pull from another
297# project.
298function setup_lib {
299 local name=$1
300 local dir=${GITDIR[$name]}
301 setup_install $dir
302}
303
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900304# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600305# the library from git, we'll do a git based install, otherwise we'll
306# punt and the library should be installed by a requirements pull from
307# another project.
308#
309# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100310#
Radosław Piliszekbe263062020-03-30 09:56:53 +0200311# setup_dev_lib [-bindep] <name> [<extras>]
Dean Troyer490430d2015-01-30 14:38:35 -0600312function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100313 local bindep
314 if [[ $1 == -bindep* ]]; then
315 bindep="${1}"
316 shift
317 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600318 local name=$1
319 local dir=${GITDIR[$name]}
Radosław Piliszekbe263062020-03-30 09:56:53 +0200320 local extras=$2
321 setup_develop $bindep $dir $extras
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
Ian Wienand6b9a5642021-07-28 11:19:57 +1000397 # Remove this package from constraints before we install it.
398 # That way, later installs won't "downgrade" the install from
399 # source we are about to do.
Ian Wienandada886d2015-10-07 14:06:26 +1100400 local name
401 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Slawek Kaplonski4ddd4562024-02-09 14:11:44 +0100402 if [ -z $name ]; then
403 name=$(awk '/^name =/ {gsub(/"/, "", $3); print $3}' $project_dir/pyproject.toml)
404 fi
Robert Collins7c838612015-07-03 13:28:09 +1200405 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
Ian Wienand6b9a5642021-07-28 11:19:57 +1000406 $REQUIREMENTS_DIR/upper-constraints.txt -- $name
Robert Collins635a5ba2015-06-10 08:48:06 +1200407 fi
408
Ian Wienand58243f62018-12-13 14:05:53 +1100409 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600410
James E. Blaire1edde32018-03-02 15:05:14 +0000411 # If this project is in LIBS_FROM_GIT, verify it was actually installed
412 # correctly. This helps catch errors caused by constraints mismatches.
413 if use_library_from_git "$project_dir"; then
414 if ! lib_installed_from_git "$project_dir"; then
415 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
416 fi
417 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600418}
419
420# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100421# using pip before running `setup.py develop`. The command is like
422# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500423#
Dean Troyer490430d2015-01-30 14:38:35 -0600424# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100425#
426# Usage:
427# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
428#
429# -bindep : Use bindep to install dependencies; select extra profiles
430# as comma separated arguments after "="
431# project_dir : directory of project repo (e.g., /opt/stack/keystone)
432# flags : pip CLI options/flags
433# extras : comma-separated list of optional dependencies to install
434# (e.g., ldap,memcache).
435# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600436function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100437 local bindep=0
438 local bindep_flag=""
439 local bindep_profiles=""
440 if [[ $1 == -bindep* ]]; then
441 bindep=1
442 IFS="=" read bindep_flag bindep_profiles <<< ${1}
443 shift
444 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600445 local project_dir=$1
446 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500447 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600448
Brant Knudson0842b812015-08-03 13:31:25 -0500449 # if the flags variable exists, and it doesn't look like a flag,
450 # assume it's actually the extras list.
451 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
452 extras=$flags
453 flags=""
454 fi
455
456 if [[ ! -z "$extras" ]]; then
457 extras="[$extras]"
458 fi
459
Ian Wienand58243f62018-12-13 14:05:53 +1100460 # install any bindep packages
461 if [[ $bindep == 1 ]]; then
462 install_bindep $project_dir/bindep.txt $bindep_profiles
463 fi
464
Brant Knudson0842b812015-08-03 13:31:25 -0500465 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600466 # ensure that further actions can do things like setup.py sdist
Clark Boylana40f9cb2018-04-04 14:02:30 -0700467 if [[ "$flags" == "-e" && "$GLOBAL_VENV" == "False" ]]; then
yatinkarel0ff62722024-09-02 17:29:55 +0530468 # egg-info is not created when project have pyproject.toml
469 if [ -d $1/*.egg-info ]; then
470 safe_chown -R $STACK_USER $1/*.egg-info
471 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600472 fi
473}
474
Doug Hellmannddc38392015-05-07 21:06:24 +0000475# Report whether python 3 should be used
Jens Harbottd7a82f42020-06-23 10:21:09 +0200476# TODO(frickler): drop this once all legacy uses are removed
Doug Hellmannddc38392015-05-07 21:06:24 +0000477function python3_enabled {
Radosław Piliszek3cbb33e2020-06-30 17:52:10 +0200478 return 0
Doug Hellmannddc38392015-05-07 21:06:24 +0000479}
480
Federico Ressi21a10d32020-01-31 07:43:30 +0100481# Provide requested python version and sets PYTHON variable
482function install_python {
Jens Harbottd7a82f42020-06-23 10:21:09 +0200483 install_python3
484 export PYTHON=$(which python${PYTHON3_VERSION} 2>/dev/null)
Federico Ressi21a10d32020-01-31 07:43:30 +0100485}
486
Doug Hellmannddc38392015-05-07 21:06:24 +0000487# Install python3 packages
488function install_python3 {
489 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500490 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Terry Wilson78cf6f62019-10-15 19:45:09 +0000491 elif is_fedora; then
yatinkarelf41a16c2025-04-09 18:14:00 +0530492 install_package python${PYTHON3_VERSION}-devel python${PYTHON3_VERSION}-pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000493 fi
494}
Dean Troyer490430d2015-01-30 14:38:35 -0600495
Sean Daguef80e2cf2017-01-18 15:42:32 -0500496function install_devstack_tools {
497 # intentionally old to ensure devstack-gate has control
498 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
499 install_python3
500 sudo pip3 install -U devstack-tools==${dstools_version}
501}
502
Dean Troyer490430d2015-01-30 14:38:35 -0600503# Restore xtrace
504$INC_PY_TRACE
505
506# Local variables:
507# mode: shell-script
508# End: