blob: 19e1228d23164ceb2723580634b744ebf8ea835b [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"
32 # NOTE(dhellmann): I don't know if we actually get a pip3.4-python
33 # under any circumstances.
34 which pip${version} || which pip${version}-python
Dean Troyer490430d2015-01-30 14:38:35 -060035
36 if [ $? -ne 0 ]; then
Doug Hellmannddc38392015-05-07 21:06:24 +000037 die $LINENO "Unable to find pip${version}; cannot continue"
Dean Troyer490430d2015-01-30 14:38:35 -060038 fi
39}
40
Atsushi SAKAI5509ed52015-11-30 20:20:21 +090041# Get the path to the directory where python executables are installed.
Dean Troyer490430d2015-01-30 14:38:35 -060042# get_python_exec_prefix
43function get_python_exec_prefix {
Ian Wienand433a9b12015-10-07 13:29:31 +110044 local xtrace
45 xtrace=$(set +o | grep xtrace)
Dean Troyer2b564762015-02-11 17:01:02 -060046 set +o xtrace
47 if [[ -z "$os_PACKAGE" ]]; then
48 GetOSVersion
49 fi
50 $xtrace
51
Lenny Verkhovskya30dd1c2019-03-14 13:19:36 +020052 local PYTHON_PATH=/usr/local/bin
53 ( is_fedora && ! python3_enabled ) || is_suse && PYTHON_PATH=/usr/bin
54 echo $PYTHON_PATH
Dean Troyer490430d2015-01-30 14:38:35 -060055}
56
Sean Dague60996b12015-04-08 09:06:49 -040057# Wrapper for ``pip install`` that only installs versions of libraries
58# from the global-requirements specification.
59#
60# Uses globals ``REQUIREMENTS_DIR``
61#
62# pip_install_gr packagename
63function pip_install_gr {
64 local name=$1
Ian Wienandada886d2015-10-07 14:06:26 +110065 local clean_name
66 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -040067 pip_install $clean_name
68}
69
Mehdi Abaakouk52b10742016-12-01 16:11:17 +010070# Wrapper for ``pip install`` that only installs versions of libraries
71# from the global-requirements specification with extras.
72#
73# Uses globals ``REQUIREMENTS_DIR``
74#
75# pip_install_gr_extras packagename extra1,extra2,...
76function pip_install_gr_extras {
77 local name=$1
78 local extras=$2
79 local clean_name
80 clean_name=$(get_from_global_requirements $name)
81 pip_install $clean_name[$extras]
82}
83
Doug Hellmann36377f62018-12-04 11:33:03 -050084# python3_enabled_for() assumes the service(s) specified as arguments are
85# enabled for python 3 unless explicitly disabled. See python3_disabled_for().
Doug Hellmann94129c72017-01-09 21:24:24 +000086#
87# Multiple services specified as arguments are ``OR``'ed together; the test
88# is a short-circuit boolean, i.e it returns on the first match.
89#
Doug Hellmann94129c72017-01-09 21:24:24 +000090# python3_enabled_for dir [dir ...]
91function python3_enabled_for {
92 local xtrace
93 xtrace=$(set +o | grep xtrace)
94 set +o xtrace
95
96 local enabled=1
97 local dirs=$@
98 local dir
99 for dir in ${dirs}; do
Doug Hellmann36377f62018-12-04 11:33:03 -0500100 if ! python3_disabled_for "${dir}"; then
101 enabled=0
102 fi
Doug Hellmann94129c72017-01-09 21:24:24 +0000103 done
104
105 $xtrace
106 return $enabled
107}
108
109# python3_disabled_for() checks if the service(s) specified as arguments are
110# disabled by the user in ``DISABLED_PYTHON3_PACKAGES``.
111#
112# Multiple services specified as arguments are ``OR``'ed together; the test
113# is a short-circuit boolean, i.e it returns on the first match.
114#
115# Uses global ``DISABLED_PYTHON3_PACKAGES``
116# python3_disabled_for dir [dir ...]
117function python3_disabled_for {
118 local xtrace
119 xtrace=$(set +o | grep xtrace)
120 set +o xtrace
121
122 local enabled=1
123 local dirs=$@
124 local dir
125 for dir in ${dirs}; do
126 [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
127 done
128
129 $xtrace
130 return $enabled
131}
132
Doug Hellmann36377f62018-12-04 11:33:03 -0500133# enable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000134#
135# For example:
136# enable_python3_package nova
137#
Doug Hellmann94129c72017-01-09 21:24:24 +0000138# enable_python3_package dir [dir ...]
139function enable_python3_package {
140 local xtrace
141 xtrace=$(set +o | grep xtrace)
142 set +o xtrace
143
Doug Hellmann36377f62018-12-04 11:33:03 -0500144 echo "It is no longer necessary to call enable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000145
146 $xtrace
147}
148
Doug Hellmann36377f62018-12-04 11:33:03 -0500149# disable_python3_package() adds the services passed as argument to
150# the ``DISABLED_PYTHON3_PACKAGES`` list.
Doug Hellmann94129c72017-01-09 21:24:24 +0000151#
152# For example:
153# disable_python3_package swift
154#
Doug Hellmann36377f62018-12-04 11:33:03 -0500155# Uses global ``DISABLED_PYTHON3_PACKAGES``
Doug Hellmann94129c72017-01-09 21:24:24 +0000156# disable_python3_package dir [dir ...]
157function disable_python3_package {
158 local xtrace
159 xtrace=$(set +o | grep xtrace)
160 set +o xtrace
161
162 local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}"
Doug Hellmann94129c72017-01-09 21:24:24 +0000163 local dir
164 for dir in $@; do
165 disabled_svcs+=",$dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000166 done
167 DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs")
Doug Hellmann94129c72017-01-09 21:24:24 +0000168
169 $xtrace
170}
171
Dean Troyer490430d2015-01-30 14:38:35 -0600172# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500173# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +1200174# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400175# Usage:
176# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600177function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000178 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100179 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600180 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000181 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600182 local offline=${OFFLINE:-False}
183 if [[ "$offline" == "True" || -z "$@" ]]; then
184 $xtrace
185 return
186 fi
187
Sean Daguecb658fa2015-10-08 17:12:03 -0400188 time_start "pip_install"
189
Chris Dentebdd9ac2015-03-04 12:35:14 +0000190 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
191 if [[ "$PIP_UPGRADE" = "True" ]] ; then
192 upgrade="--upgrade"
193 fi
194
Dean Troyer490430d2015-01-30 14:38:35 -0600195 if [[ -z "$os_PACKAGE" ]]; then
196 GetOSVersion
197 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400198
199 # Try to extract the path of the package we are installing into
200 # package_dir. We need this to check for test-requirements.txt,
201 # at least.
202 #
203 # ${!#} expands to the last positional argument to this function.
204 # With "extras" syntax included, our arguments might be something
205 # like:
206 # -e /path/to/fooproject[extra]
207 # Thus this magic line grabs just the path without extras
208 #
209 # Note that this makes no sense if this is a pypi (rather than
210 # local path) install; ergo you must check this path exists before
211 # use. Also, if we had multiple or mixed installs, we would also
212 # likely break. But for historical reasons, it's basically only
213 # the other wrapper functions in here calling this to install
214 # local packages, and they do so with single call per install. So
215 # this works (for now...)
216 local package_dir=${!#%\[*\]}
217
Dean Troyer490430d2015-01-30 14:38:35 -0600218 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
219 # TRACK_DEPENDS=True installation creates a circular dependency when
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900220 # we attempt to install virtualenv into a virtualenv, so we must global
Dean Troyer490430d2015-01-30 14:38:35 -0600221 # that installation.
222 source $DEST/.venv/bin/activate
223 local cmd_pip=$DEST/.venv/bin/pip
224 local sudo_pip="env"
225 else
Dean Troyer2b564762015-02-11 17:01:02 -0600226 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
227 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
228 local sudo_pip="env"
229 else
Ian Wienandada886d2015-10-07 14:06:26 +1100230 local cmd_pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000231 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600232 local sudo_pip="sudo -H"
Doug Hellmannddc38392015-05-07 21:06:24 +0000233 if python3_enabled; then
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500234 # Special case some services that have experimental
235 # support for python3 in progress, but don't claim support
236 # in their classifier
237 echo "Check python version for : $package_dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000238 if python3_disabled_for ${package_dir##*/}; then
239 echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES"
Matt Riedemanne03bcb22019-04-01 12:19:45 -0400240 else
241 # For everything that is not explicitly blacklisted with
242 # DISABLED_PYTHON3_PACKAGES, assume it supports python3
243 # and we will let pip sort out the install, regardless of
244 # the package being local or remote.
Doug Hellmann36377f62018-12-04 11:33:03 -0500245 echo "Using $PYTHON3_VERSION version to install $package_dir based on default behavior"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500246 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
247 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
Doug Hellmannddc38392015-05-07 21:06:24 +0000248 fi
249 fi
Dean Troyer2b564762015-02-11 17:01:02 -0600250 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600251 fi
252
Robert Collins635a5ba2015-06-10 08:48:06 +1200253 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700254 # Always apply constraints
255 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200256
Doug Hellmannddc38392015-05-07 21:06:24 +0000257 # FIXME(dhellmann): Need to force multiple versions of pip for
258 # packages like setuptools?
Ian Wienandada886d2015-10-07 14:06:26 +1100259 local pip_version
260 pip_version=$(python -c "import pip; \
Clark Boylan06577952017-10-20 12:14:29 -0700261 print(pip.__version__.split('.')[0])")
Dean Troyer490430d2015-01-30 14:38:35 -0600262 if (( pip_version<6 )); then
263 die $LINENO "Currently installed pip version ${pip_version} does not" \
264 "meet minimum requirements (>=6)."
265 fi
266
267 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700268
269 # Also install test requirements
270 local install_test_reqs=""
Zane Bitter9e7ead92017-10-05 16:51:09 -0400271 local test_req="${package_dir}/test-requirements.txt"
Clark Boylanf266a2d2017-06-12 14:57:59 -0700272 if [[ -e "$test_req" ]]; then
273 install_test_reqs="-r $test_req"
274 fi
275
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200276 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
277 # the same behaviour of setuptools before version 25.0.0.
278 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600279 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800280 http_proxy="${http_proxy:-}" \
281 https_proxy="${https_proxy:-}" \
282 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800283 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200284 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Clark Boylanf266a2d2017-06-12 14:57:59 -0700285 $cmd_pip $upgrade $install_test_reqs \
Dean Troyer490430d2015-01-30 14:38:35 -0600286 $@
Federico Ressie208d062015-11-21 11:15:39 +0000287 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600288
Sean Daguecb658fa2015-10-08 17:12:03 -0400289 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000290 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600291}
292
Sean Daguef28e7ef2017-05-07 22:02:10 -0400293function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900294 # Skip uninstall if offline
295 [[ "${OFFLINE}" = "True" ]] && return
296
Sean Daguef28e7ef2017-05-07 22:02:10 -0400297 local name=$1
298 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
299 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
300 local sudo_pip="env"
301 else
302 local cmd_pip
303 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
304 local sudo_pip="sudo -H"
305 fi
306 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400307 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400308}
309
Joe Gordond5ac7852015-02-06 19:29:23 -0800310# get version of a package from global requirements file
311# get_from_global_requirements <package>
312function get_from_global_requirements {
313 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100314 local required_pkg
315 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800316 if [[ $required_pkg == "" ]]; then
317 die $LINENO "Can't find package $package in requirements"
318 fi
319 echo $required_pkg
320}
321
Dean Troyer490430d2015-01-30 14:38:35 -0600322# should we use this library from their git repo, or should we let it
323# get pulled in via pip dependencies.
324function use_library_from_git {
325 local name=$1
326 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200327 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600328 return $enabled
329}
330
Sean Daguec71973e2015-09-08 07:12:48 -0400331# determine if a package was installed from git
332function lib_installed_from_git {
333 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800334 local safe_name
335 safe_name=$(python -c "from pkg_resources import safe_name; \
336 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000337 # Note "pip freeze" doesn't always work here, because it tries to
338 # be smart about finding the remote of the git repo the package
339 # was installed from. This doesn't work with zuul which clones
340 # repos with no remote.
341 #
342 # The best option seems to be to use "pip list" which will tell
343 # you the path an editable install was installed from; for example
344 # in response to something like
345 # pip install -e 'git+http://git.openstack.org/openstack-dev/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500346 # pip list --format columns shows
347 # bashate 0.5.2.dev19 /tmp/env/src/bashate
348 # Thus we check the third column to see if we're installed from
349 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800350 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400351}
352
Dean Troyer490430d2015-01-30 14:38:35 -0600353# setup a library by name. If we are trying to use the library from
354# git, we'll do a git based install, otherwise we'll punt and the
355# library should be installed by a requirements pull from another
356# project.
357function setup_lib {
358 local name=$1
359 local dir=${GITDIR[$name]}
360 setup_install $dir
361}
362
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900363# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600364# the library from git, we'll do a git based install, otherwise we'll
365# punt and the library should be installed by a requirements pull from
366# another project.
367#
368# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100369#
370# setup_dev_lib [-bindep] <name>
Dean Troyer490430d2015-01-30 14:38:35 -0600371function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100372 local bindep
373 if [[ $1 == -bindep* ]]; then
374 bindep="${1}"
375 shift
376 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600377 local name=$1
378 local dir=${GITDIR[$name]}
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000379 if python3_enabled; then
380 # Turn off Python 3 mode and install the package again,
381 # forcing a Python 2 installation. This ensures that all libs
382 # being used for development are installed under both versions
383 # of Python.
384 echo "Installing $name again without Python 3 enabled"
385 USE_PYTHON3=False
Ian Wienand58243f62018-12-13 14:05:53 +1100386 setup_develop $bindep $dir
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000387 USE_PYTHON3=True
388 fi
Ian Wienand58243f62018-12-13 14:05:53 +1100389 setup_develop $bindep $dir
Dean Troyer490430d2015-01-30 14:38:35 -0600390}
391
392# this should be used if you want to install globally, all libraries should
393# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500394#
395# setup_install project_dir [extras]
396# project_dir: directory of project repo (e.g., /opt/stack/keystone)
397# extras: comma-separated list of optional dependencies to install
398# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900399# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Ian Wienand58243f62018-12-13 14:05:53 +1100400# bindep: Set "-bindep" as first argument to install bindep.txt packages
Brant Knudson0842b812015-08-03 13:31:25 -0500401# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600402function setup_install {
Ian Wienand58243f62018-12-13 14:05:53 +1100403 local bindep
404 if [[ $1 == -bindep* ]]; then
405 bindep="${1}"
406 shift
407 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600408 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500409 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100410 _setup_package_with_constraints_edit $bindep $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600411}
412
413# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500414#
415# setup_develop project_dir [extras]
416# project_dir: directory of project repo (e.g., /opt/stack/keystone)
417# extras: comma-separated list of optional dependencies to install
418# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900419# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500420# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600421function setup_develop {
Ian Wienand58243f62018-12-13 14:05:53 +1100422 local bindep
423 if [[ $1 == -bindep* ]]; then
424 bindep="${1}"
425 shift
426 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600427 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500428 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100429 _setup_package_with_constraints_edit $bindep $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600430}
431
Dean Troyer490430d2015-01-30 14:38:35 -0600432# ``pip install -e`` the package, which processes the dependencies
433# using pip before running `setup.py develop`
434#
Clark Boylan05aa3842015-08-03 11:14:13 -0700435# Updates the constraints from REQUIREMENTS_DIR to reflect the
436# future installed state of this package. This ensures when we
437# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600438#
Clark Boylan05aa3842015-08-03 11:14:13 -0700439# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500440# _setup_package_with_constraints_edit project_dir flags [extras]
441# project_dir: directory of project repo (e.g., /opt/stack/keystone)
442# flags: pip CLI options/flags
443# extras: comma-separated list of optional dependencies to install
444# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900445# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500446# The command is like "pip install <flags> <project_dir>[<extras>]"
447function _setup_package_with_constraints_edit {
Ian Wienand58243f62018-12-13 14:05:53 +1100448 local bindep
449 if [[ $1 == -bindep* ]]; then
450 bindep="${1}"
451 shift
452 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600453 local project_dir=$1
454 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500455 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600456
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900457 # Normalize the directory name to avoid
458 # "installation from path or url cannot be constrained to a version"
459 # error.
460 # REVISIT(yamamoto): Remove this when fixed in pip.
461 # https://github.com/pypa/pip/pull/3582
462 project_dir=$(cd $project_dir && pwd)
463
Robert Collins635a5ba2015-06-10 08:48:06 +1200464 if [ -n "$REQUIREMENTS_DIR" ]; then
465 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100466 local name
467 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200468 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
469 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
470 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200471 fi
472
Ian Wienand58243f62018-12-13 14:05:53 +1100473 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600474
James E. Blaire1edde32018-03-02 15:05:14 +0000475 # If this project is in LIBS_FROM_GIT, verify it was actually installed
476 # correctly. This helps catch errors caused by constraints mismatches.
477 if use_library_from_git "$project_dir"; then
478 if ! lib_installed_from_git "$project_dir"; then
479 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
480 fi
481 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600482}
483
484# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100485# using pip before running `setup.py develop`. The command is like
486# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500487#
Dean Troyer490430d2015-01-30 14:38:35 -0600488# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100489#
490# Usage:
491# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
492#
493# -bindep : Use bindep to install dependencies; select extra profiles
494# as comma separated arguments after "="
495# project_dir : directory of project repo (e.g., /opt/stack/keystone)
496# flags : pip CLI options/flags
497# extras : comma-separated list of optional dependencies to install
498# (e.g., ldap,memcache).
499# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600500function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100501 local bindep=0
502 local bindep_flag=""
503 local bindep_profiles=""
504 if [[ $1 == -bindep* ]]; then
505 bindep=1
506 IFS="=" read bindep_flag bindep_profiles <<< ${1}
507 shift
508 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600509 local project_dir=$1
510 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500511 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600512
Brant Knudson0842b812015-08-03 13:31:25 -0500513 # if the flags variable exists, and it doesn't look like a flag,
514 # assume it's actually the extras list.
515 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
516 extras=$flags
517 flags=""
518 fi
519
520 if [[ ! -z "$extras" ]]; then
521 extras="[$extras]"
522 fi
523
Ian Wienand58243f62018-12-13 14:05:53 +1100524 # install any bindep packages
525 if [[ $bindep == 1 ]]; then
526 install_bindep $project_dir/bindep.txt $bindep_profiles
527 fi
528
Brant Knudson0842b812015-08-03 13:31:25 -0500529 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600530 # ensure that further actions can do things like setup.py sdist
531 if [[ "$flags" == "-e" ]]; then
532 safe_chown -R $STACK_USER $1/*.egg-info
533 fi
534}
535
Doug Hellmannddc38392015-05-07 21:06:24 +0000536# Report whether python 3 should be used
537function python3_enabled {
538 if [[ $USE_PYTHON3 == "True" ]]; then
539 return 0
540 else
541 return 1
542 fi
543}
544
545# Install python3 packages
546function install_python3 {
547 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500548 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700549 elif is_suse; then
550 install_package python3-devel python3-dbm
Doug Hellmannddc38392015-05-07 21:06:24 +0000551 fi
552}
Dean Troyer490430d2015-01-30 14:38:35 -0600553
Sean Daguef80e2cf2017-01-18 15:42:32 -0500554function install_devstack_tools {
555 # intentionally old to ensure devstack-gate has control
556 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
557 install_python3
558 sudo pip3 install -U devstack-tools==${dstools_version}
559}
560
Dean Troyer490430d2015-01-30 14:38:35 -0600561# Restore xtrace
562$INC_PY_TRACE
563
564# Local variables:
565# mode: shell-script
566# End: