blob: 0e575ae9e9a3454e8a39dc8a76bc7838aecff581 [file] [log] [blame]
Dean Troyer490430d2015-01-30 14:38:35 -06001#!/bin/bash
2#
3# **inc/python** - Python-related functions
4#
5# Support for pip/setuptools interfaces and virtual environments
6#
7# External functions used:
8# - GetOSVersion
9# - is_fedora
10# - is_suse
11# - safe_chown
12
13# Save trace setting
14INC_PY_TRACE=$(set +o | grep xtrace)
15set +o xtrace
16
17
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060018# Global Config Variables
19
Atsushi SAKAI5509ed52015-11-30 20:20:21 +090020# PROJECT_VENV contains the name of the virtual environment for each
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060021# project. A null value installs to the system Python directories.
Sean Dagueafef8bf2017-03-06 14:07:23 -050022declare -A -g PROJECT_VENV
Dean Troyer8c2ce6e2015-02-18 14:47:54 -060023
24
Dean Troyer490430d2015-01-30 14:38:35 -060025# Python Functions
26# ================
27
28# Get the path to the pip command.
29# get_pip_command
30function get_pip_command {
Doug Hellmannddc38392015-05-07 21:06:24 +000031 local version="$1"
Tom Barron4db9d562019-01-09 08:43:52 -050032 if [ -z "$version" ]; then
33 die $LINENO "pip python version is not set."
34 fi
35
Doug Hellmannddc38392015-05-07 21:06:24 +000036 # NOTE(dhellmann): I don't know if we actually get a pip3.4-python
37 # under any circumstances.
38 which pip${version} || which pip${version}-python
Dean Troyer490430d2015-01-30 14:38:35 -060039
40 if [ $? -ne 0 ]; then
Doug Hellmannddc38392015-05-07 21:06:24 +000041 die $LINENO "Unable to find pip${version}; cannot continue"
Dean Troyer490430d2015-01-30 14:38:35 -060042 fi
43}
44
Atsushi SAKAI5509ed52015-11-30 20:20:21 +090045# Get the path to the directory where python executables are installed.
Dean Troyer490430d2015-01-30 14:38:35 -060046# get_python_exec_prefix
47function get_python_exec_prefix {
Ian Wienand433a9b12015-10-07 13:29:31 +110048 local xtrace
49 xtrace=$(set +o | grep xtrace)
Dean Troyer2b564762015-02-11 17:01:02 -060050 set +o xtrace
51 if [[ -z "$os_PACKAGE" ]]; then
52 GetOSVersion
53 fi
54 $xtrace
55
Lenny Verkhovskya30dd1c2019-03-14 13:19:36 +020056 local PYTHON_PATH=/usr/local/bin
57 ( is_fedora && ! python3_enabled ) || is_suse && PYTHON_PATH=/usr/bin
58 echo $PYTHON_PATH
Dean Troyer490430d2015-01-30 14:38:35 -060059}
60
Sean Dague60996b12015-04-08 09:06:49 -040061# Wrapper for ``pip install`` that only installs versions of libraries
62# from the global-requirements specification.
63#
64# Uses globals ``REQUIREMENTS_DIR``
65#
66# pip_install_gr packagename
67function pip_install_gr {
68 local name=$1
Ian Wienandada886d2015-10-07 14:06:26 +110069 local clean_name
70 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -040071 pip_install $clean_name
72}
73
Mehdi Abaakouk52b10742016-12-01 16:11:17 +010074# Wrapper for ``pip install`` that only installs versions of libraries
75# from the global-requirements specification with extras.
76#
77# Uses globals ``REQUIREMENTS_DIR``
78#
79# pip_install_gr_extras packagename extra1,extra2,...
80function pip_install_gr_extras {
81 local name=$1
82 local extras=$2
83 local clean_name
84 clean_name=$(get_from_global_requirements $name)
85 pip_install $clean_name[$extras]
86}
87
Doug Hellmann36377f62018-12-04 11:33:03 -050088# python3_enabled_for() assumes the service(s) specified as arguments are
89# enabled for python 3 unless explicitly disabled. See python3_disabled_for().
Doug Hellmann94129c72017-01-09 21:24:24 +000090#
91# Multiple services specified as arguments are ``OR``'ed together; the test
92# is a short-circuit boolean, i.e it returns on the first match.
93#
Doug Hellmann94129c72017-01-09 21:24:24 +000094# python3_enabled_for dir [dir ...]
95function python3_enabled_for {
96 local xtrace
97 xtrace=$(set +o | grep xtrace)
98 set +o xtrace
99
100 local enabled=1
101 local dirs=$@
102 local dir
103 for dir in ${dirs}; do
Doug Hellmann36377f62018-12-04 11:33:03 -0500104 if ! python3_disabled_for "${dir}"; then
105 enabled=0
106 fi
Doug Hellmann94129c72017-01-09 21:24:24 +0000107 done
108
109 $xtrace
110 return $enabled
111}
112
113# python3_disabled_for() checks if the service(s) specified as arguments are
114# disabled by the user in ``DISABLED_PYTHON3_PACKAGES``.
115#
116# Multiple services specified as arguments are ``OR``'ed together; the test
117# is a short-circuit boolean, i.e it returns on the first match.
118#
119# Uses global ``DISABLED_PYTHON3_PACKAGES``
120# python3_disabled_for dir [dir ...]
121function python3_disabled_for {
122 local xtrace
123 xtrace=$(set +o | grep xtrace)
124 set +o xtrace
125
126 local enabled=1
127 local dirs=$@
128 local dir
129 for dir in ${dirs}; do
130 [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
131 done
132
133 $xtrace
134 return $enabled
135}
136
Doug Hellmann36377f62018-12-04 11:33:03 -0500137# enable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000138#
139# For example:
140# enable_python3_package nova
141#
Doug Hellmann94129c72017-01-09 21:24:24 +0000142# enable_python3_package dir [dir ...]
143function enable_python3_package {
144 local xtrace
145 xtrace=$(set +o | grep xtrace)
146 set +o xtrace
147
Doug Hellmann36377f62018-12-04 11:33:03 -0500148 echo "It is no longer necessary to call enable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000149
150 $xtrace
151}
152
Doug Hellmann36377f62018-12-04 11:33:03 -0500153# disable_python3_package() adds the services passed as argument to
154# the ``DISABLED_PYTHON3_PACKAGES`` list.
Doug Hellmann94129c72017-01-09 21:24:24 +0000155#
156# For example:
157# disable_python3_package swift
158#
Doug Hellmann36377f62018-12-04 11:33:03 -0500159# Uses global ``DISABLED_PYTHON3_PACKAGES``
Doug Hellmann94129c72017-01-09 21:24:24 +0000160# disable_python3_package dir [dir ...]
161function disable_python3_package {
162 local xtrace
163 xtrace=$(set +o | grep xtrace)
164 set +o xtrace
165
166 local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}"
Doug Hellmann94129c72017-01-09 21:24:24 +0000167 local dir
168 for dir in $@; do
169 disabled_svcs+=",$dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000170 done
171 DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs")
Doug Hellmann94129c72017-01-09 21:24:24 +0000172
173 $xtrace
174}
175
Dean Troyer490430d2015-01-30 14:38:35 -0600176# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500177# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +1200178# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400179# Usage:
180# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600181function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000182 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100183 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600184 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000185 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600186 local offline=${OFFLINE:-False}
187 if [[ "$offline" == "True" || -z "$@" ]]; then
188 $xtrace
189 return
190 fi
191
Sean Daguecb658fa2015-10-08 17:12:03 -0400192 time_start "pip_install"
193
Chris Dentebdd9ac2015-03-04 12:35:14 +0000194 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
195 if [[ "$PIP_UPGRADE" = "True" ]] ; then
196 upgrade="--upgrade"
197 fi
198
Dean Troyer490430d2015-01-30 14:38:35 -0600199 if [[ -z "$os_PACKAGE" ]]; then
200 GetOSVersion
201 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400202
203 # Try to extract the path of the package we are installing into
204 # package_dir. We need this to check for test-requirements.txt,
205 # at least.
206 #
207 # ${!#} expands to the last positional argument to this function.
208 # With "extras" syntax included, our arguments might be something
209 # like:
210 # -e /path/to/fooproject[extra]
211 # Thus this magic line grabs just the path without extras
212 #
213 # Note that this makes no sense if this is a pypi (rather than
214 # local path) install; ergo you must check this path exists before
215 # use. Also, if we had multiple or mixed installs, we would also
216 # likely break. But for historical reasons, it's basically only
217 # the other wrapper functions in here calling this to install
218 # local packages, and they do so with single call per install. So
219 # this works (for now...)
220 local package_dir=${!#%\[*\]}
221
Dean Troyer490430d2015-01-30 14:38:35 -0600222 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
223 # TRACK_DEPENDS=True installation creates a circular dependency when
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900224 # we attempt to install virtualenv into a virtualenv, so we must global
Dean Troyer490430d2015-01-30 14:38:35 -0600225 # that installation.
226 source $DEST/.venv/bin/activate
227 local cmd_pip=$DEST/.venv/bin/pip
228 local sudo_pip="env"
229 else
Dean Troyer2b564762015-02-11 17:01:02 -0600230 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
Ian Wienandada886d2015-10-07 14:06:26 +1100234 local cmd_pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000235 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600236 local sudo_pip="sudo -H"
Doug Hellmannddc38392015-05-07 21:06:24 +0000237 if python3_enabled; then
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500238 # Special case some services that have experimental
239 # support for python3 in progress, but don't claim support
240 # in their classifier
241 echo "Check python version for : $package_dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000242 if python3_disabled_for ${package_dir##*/}; then
243 echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES"
Matt Riedemanne03bcb22019-04-01 12:19:45 -0400244 else
245 # For everything that is not explicitly blacklisted with
246 # DISABLED_PYTHON3_PACKAGES, assume it supports python3
247 # and we will let pip sort out the install, regardless of
248 # the package being local or remote.
Doug Hellmann36377f62018-12-04 11:33:03 -0500249 echo "Using $PYTHON3_VERSION version to install $package_dir based on default behavior"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500250 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
251 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
Doug Hellmannddc38392015-05-07 21:06:24 +0000252 fi
253 fi
Dean Troyer2b564762015-02-11 17:01:02 -0600254 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600255 fi
256
Robert Collins635a5ba2015-06-10 08:48:06 +1200257 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700258 # Always apply constraints
259 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200260
Doug Hellmannddc38392015-05-07 21:06:24 +0000261 # FIXME(dhellmann): Need to force multiple versions of pip for
262 # packages like setuptools?
Ian Wienandada886d2015-10-07 14:06:26 +1100263 local pip_version
264 pip_version=$(python -c "import pip; \
Clark Boylan06577952017-10-20 12:14:29 -0700265 print(pip.__version__.split('.')[0])")
Dean Troyer490430d2015-01-30 14:38:35 -0600266 if (( pip_version<6 )); then
267 die $LINENO "Currently installed pip version ${pip_version} does not" \
268 "meet minimum requirements (>=6)."
269 fi
270
271 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700272
273 # Also install test requirements
274 local install_test_reqs=""
Zane Bitter9e7ead92017-10-05 16:51:09 -0400275 local test_req="${package_dir}/test-requirements.txt"
Clark Boylanf266a2d2017-06-12 14:57:59 -0700276 if [[ -e "$test_req" ]]; then
277 install_test_reqs="-r $test_req"
278 fi
279
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200280 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
281 # the same behaviour of setuptools before version 25.0.0.
282 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600283 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800284 http_proxy="${http_proxy:-}" \
285 https_proxy="${https_proxy:-}" \
286 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800287 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200288 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Clark Boylanf266a2d2017-06-12 14:57:59 -0700289 $cmd_pip $upgrade $install_test_reqs \
Dean Troyer490430d2015-01-30 14:38:35 -0600290 $@
Federico Ressie208d062015-11-21 11:15:39 +0000291 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600292
Sean Daguecb658fa2015-10-08 17:12:03 -0400293 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000294 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600295}
296
Sean Daguef28e7ef2017-05-07 22:02:10 -0400297function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900298 # Skip uninstall if offline
299 [[ "${OFFLINE}" = "True" ]] && return
300
Sean Daguef28e7ef2017-05-07 22:02:10 -0400301 local name=$1
302 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
303 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
304 local sudo_pip="env"
305 else
306 local cmd_pip
307 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
308 local sudo_pip="sudo -H"
309 fi
310 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400311 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400312}
313
Joe Gordond5ac7852015-02-06 19:29:23 -0800314# get version of a package from global requirements file
315# get_from_global_requirements <package>
316function get_from_global_requirements {
317 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100318 local required_pkg
319 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800320 if [[ $required_pkg == "" ]]; then
321 die $LINENO "Can't find package $package in requirements"
322 fi
323 echo $required_pkg
324}
325
Dean Troyer490430d2015-01-30 14:38:35 -0600326# should we use this library from their git repo, or should we let it
327# get pulled in via pip dependencies.
328function use_library_from_git {
329 local name=$1
330 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200331 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600332 return $enabled
333}
334
Sean Daguec71973e2015-09-08 07:12:48 -0400335# determine if a package was installed from git
336function lib_installed_from_git {
337 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800338 local safe_name
339 safe_name=$(python -c "from pkg_resources import safe_name; \
340 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000341 # Note "pip freeze" doesn't always work here, because it tries to
342 # be smart about finding the remote of the git repo the package
343 # was installed from. This doesn't work with zuul which clones
344 # repos with no remote.
345 #
346 # The best option seems to be to use "pip list" which will tell
347 # you the path an editable install was installed from; for example
348 # in response to something like
349 # pip install -e 'git+http://git.openstack.org/openstack-dev/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500350 # pip list --format columns shows
351 # bashate 0.5.2.dev19 /tmp/env/src/bashate
352 # Thus we check the third column to see if we're installed from
353 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800354 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400355}
356
Dean Troyer490430d2015-01-30 14:38:35 -0600357# setup a library by name. If we are trying to use the library from
358# git, we'll do a git based install, otherwise we'll punt and the
359# library should be installed by a requirements pull from another
360# project.
361function setup_lib {
362 local name=$1
363 local dir=${GITDIR[$name]}
364 setup_install $dir
365}
366
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900367# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600368# the library from git, we'll do a git based install, otherwise we'll
369# punt and the library should be installed by a requirements pull from
370# another project.
371#
372# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100373#
374# setup_dev_lib [-bindep] <name>
Dean Troyer490430d2015-01-30 14:38:35 -0600375function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100376 local bindep
377 if [[ $1 == -bindep* ]]; then
378 bindep="${1}"
379 shift
380 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600381 local name=$1
382 local dir=${GITDIR[$name]}
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000383 if python3_enabled; then
384 # Turn off Python 3 mode and install the package again,
385 # forcing a Python 2 installation. This ensures that all libs
386 # being used for development are installed under both versions
387 # of Python.
388 echo "Installing $name again without Python 3 enabled"
389 USE_PYTHON3=False
Ian Wienand58243f62018-12-13 14:05:53 +1100390 setup_develop $bindep $dir
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000391 USE_PYTHON3=True
392 fi
Ian Wienand58243f62018-12-13 14:05:53 +1100393 setup_develop $bindep $dir
Dean Troyer490430d2015-01-30 14:38:35 -0600394}
395
396# this should be used if you want to install globally, all libraries should
397# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500398#
399# setup_install project_dir [extras]
400# project_dir: directory of project repo (e.g., /opt/stack/keystone)
401# extras: comma-separated list of optional dependencies to install
402# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900403# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Ian Wienand58243f62018-12-13 14:05:53 +1100404# bindep: Set "-bindep" as first argument to install bindep.txt packages
Brant Knudson0842b812015-08-03 13:31:25 -0500405# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600406function setup_install {
Ian Wienand58243f62018-12-13 14:05:53 +1100407 local bindep
408 if [[ $1 == -bindep* ]]; then
409 bindep="${1}"
410 shift
411 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600412 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500413 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100414 _setup_package_with_constraints_edit $bindep $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600415}
416
417# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500418#
419# setup_develop project_dir [extras]
420# project_dir: directory of project repo (e.g., /opt/stack/keystone)
421# extras: comma-separated list of optional dependencies to install
422# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900423# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500424# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600425function setup_develop {
Ian Wienand58243f62018-12-13 14:05:53 +1100426 local bindep
427 if [[ $1 == -bindep* ]]; then
428 bindep="${1}"
429 shift
430 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600431 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500432 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100433 _setup_package_with_constraints_edit $bindep $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600434}
435
Dean Troyer490430d2015-01-30 14:38:35 -0600436# ``pip install -e`` the package, which processes the dependencies
437# using pip before running `setup.py develop`
438#
Clark Boylan05aa3842015-08-03 11:14:13 -0700439# Updates the constraints from REQUIREMENTS_DIR to reflect the
440# future installed state of this package. This ensures when we
441# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600442#
Clark Boylan05aa3842015-08-03 11:14:13 -0700443# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500444# _setup_package_with_constraints_edit project_dir flags [extras]
445# project_dir: directory of project repo (e.g., /opt/stack/keystone)
446# flags: pip CLI options/flags
447# extras: comma-separated list of optional dependencies to install
448# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900449# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500450# The command is like "pip install <flags> <project_dir>[<extras>]"
451function _setup_package_with_constraints_edit {
Ian Wienand58243f62018-12-13 14:05:53 +1100452 local bindep
453 if [[ $1 == -bindep* ]]; then
454 bindep="${1}"
455 shift
456 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600457 local project_dir=$1
458 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500459 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600460
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900461 # Normalize the directory name to avoid
462 # "installation from path or url cannot be constrained to a version"
463 # error.
464 # REVISIT(yamamoto): Remove this when fixed in pip.
465 # https://github.com/pypa/pip/pull/3582
466 project_dir=$(cd $project_dir && pwd)
467
Robert Collins635a5ba2015-06-10 08:48:06 +1200468 if [ -n "$REQUIREMENTS_DIR" ]; then
469 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100470 local name
471 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200472 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
473 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
474 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200475 fi
476
Ian Wienand58243f62018-12-13 14:05:53 +1100477 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600478
James E. Blaire1edde32018-03-02 15:05:14 +0000479 # If this project is in LIBS_FROM_GIT, verify it was actually installed
480 # correctly. This helps catch errors caused by constraints mismatches.
481 if use_library_from_git "$project_dir"; then
482 if ! lib_installed_from_git "$project_dir"; then
483 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
484 fi
485 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600486}
487
488# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100489# using pip before running `setup.py develop`. The command is like
490# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500491#
Dean Troyer490430d2015-01-30 14:38:35 -0600492# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100493#
494# Usage:
495# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
496#
497# -bindep : Use bindep to install dependencies; select extra profiles
498# as comma separated arguments after "="
499# project_dir : directory of project repo (e.g., /opt/stack/keystone)
500# flags : pip CLI options/flags
501# extras : comma-separated list of optional dependencies to install
502# (e.g., ldap,memcache).
503# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600504function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100505 local bindep=0
506 local bindep_flag=""
507 local bindep_profiles=""
508 if [[ $1 == -bindep* ]]; then
509 bindep=1
510 IFS="=" read bindep_flag bindep_profiles <<< ${1}
511 shift
512 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600513 local project_dir=$1
514 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500515 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600516
Brant Knudson0842b812015-08-03 13:31:25 -0500517 # if the flags variable exists, and it doesn't look like a flag,
518 # assume it's actually the extras list.
519 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
520 extras=$flags
521 flags=""
522 fi
523
524 if [[ ! -z "$extras" ]]; then
525 extras="[$extras]"
526 fi
527
Ian Wienand58243f62018-12-13 14:05:53 +1100528 # install any bindep packages
529 if [[ $bindep == 1 ]]; then
530 install_bindep $project_dir/bindep.txt $bindep_profiles
531 fi
532
Brant Knudson0842b812015-08-03 13:31:25 -0500533 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600534 # ensure that further actions can do things like setup.py sdist
535 if [[ "$flags" == "-e" ]]; then
536 safe_chown -R $STACK_USER $1/*.egg-info
537 fi
538}
539
Doug Hellmannddc38392015-05-07 21:06:24 +0000540# Report whether python 3 should be used
541function python3_enabled {
542 if [[ $USE_PYTHON3 == "True" ]]; then
543 return 0
544 else
545 return 1
546 fi
547}
548
549# Install python3 packages
550function install_python3 {
551 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500552 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700553 elif is_suse; then
554 install_package python3-devel python3-dbm
Doug Hellmannddc38392015-05-07 21:06:24 +0000555 fi
556}
Dean Troyer490430d2015-01-30 14:38:35 -0600557
Sean Daguef80e2cf2017-01-18 15:42:32 -0500558function install_devstack_tools {
559 # intentionally old to ensure devstack-gate has control
560 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
561 install_python3
562 sudo pip3 install -U devstack-tools==${dstools_version}
563}
564
Dean Troyer490430d2015-01-30 14:38:35 -0600565# Restore xtrace
566$INC_PY_TRACE
567
568# Local variables:
569# mode: shell-script
570# End: