blob: d4237674ede2aab8198d8bb5871abd74761a1d0f [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
imacdonne991f7d2018-10-04 19:41:59 +000052 if python3_enabled && [[ "$os_VENDOR" == "Fedora" && $os_RELEASE -gt 26 ]]; then
Victor Stinnerb9891ee2018-01-08 15:20:36 +010053 # Default Python 3 install prefix changed to /usr/local in Fedora 27:
54 # https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
55 echo "/usr/local/bin"
56 elif is_fedora || is_suse; then
Dean Troyer490430d2015-01-30 14:38:35 -060057 echo "/usr/bin"
58 else
59 echo "/usr/local/bin"
60 fi
61}
62
Sean Dague60996b12015-04-08 09:06:49 -040063# Wrapper for ``pip install`` that only installs versions of libraries
64# from the global-requirements specification.
65#
66# Uses globals ``REQUIREMENTS_DIR``
67#
68# pip_install_gr packagename
69function pip_install_gr {
70 local name=$1
Ian Wienandada886d2015-10-07 14:06:26 +110071 local clean_name
72 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -040073 pip_install $clean_name
74}
75
Mehdi Abaakouk52b10742016-12-01 16:11:17 +010076# Wrapper for ``pip install`` that only installs versions of libraries
77# from the global-requirements specification with extras.
78#
79# Uses globals ``REQUIREMENTS_DIR``
80#
81# pip_install_gr_extras packagename extra1,extra2,...
82function pip_install_gr_extras {
83 local name=$1
84 local extras=$2
85 local clean_name
86 clean_name=$(get_from_global_requirements $name)
87 pip_install $clean_name[$extras]
88}
89
Doug Hellmannddc38392015-05-07 21:06:24 +000090# Determine the python versions supported by a package
91function get_python_versions_for_package {
92 local name=$1
93 cd $name && python setup.py --classifiers \
94 | grep 'Language' | cut -f5 -d: | grep '\.' | tr '\n' ' '
95}
96
Davanum Srinivasafa8a002016-12-19 09:51:01 -050097# Check for python3 classifier in local directory
98function check_python3_support_for_package_local {
99 local name=$1
100 cd $name
101 set +e
102 classifier=$(python setup.py --classifiers \
103 | grep 'Programming Language :: Python :: 3$')
104 set -e
105 echo $classifier
106}
107
108# Check for python3 classifier on pypi
109function check_python3_support_for_package_remote {
110 local name=$1
111 set +e
112 classifier=$(curl -s -L "https://pypi.python.org/pypi/$name/json" \
113 | grep '"Programming Language :: Python :: 3"')
114 set -e
115 echo $classifier
116}
117
Doug Hellmann94129c72017-01-09 21:24:24 +0000118# python3_enabled_for() checks if the service(s) specified as arguments are
119# enabled by the user in ``ENABLED_PYTHON3_PACKAGES``.
120#
121# Multiple services specified as arguments are ``OR``'ed together; the test
122# is a short-circuit boolean, i.e it returns on the first match.
123#
124# Uses global ``ENABLED_PYTHON3_PACKAGES``
125# python3_enabled_for dir [dir ...]
126function python3_enabled_for {
127 local xtrace
128 xtrace=$(set +o | grep xtrace)
129 set +o xtrace
130
131 local enabled=1
132 local dirs=$@
133 local dir
134 for dir in ${dirs}; do
135 [[ ,${ENABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
136 done
137
138 $xtrace
139 return $enabled
140}
141
142# python3_disabled_for() checks if the service(s) specified as arguments are
143# disabled by the user in ``DISABLED_PYTHON3_PACKAGES``.
144#
145# Multiple services specified as arguments are ``OR``'ed together; the test
146# is a short-circuit boolean, i.e it returns on the first match.
147#
148# Uses global ``DISABLED_PYTHON3_PACKAGES``
149# python3_disabled_for dir [dir ...]
150function python3_disabled_for {
151 local xtrace
152 xtrace=$(set +o | grep xtrace)
153 set +o xtrace
154
155 local enabled=1
156 local dirs=$@
157 local dir
158 for dir in ${dirs}; do
159 [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
160 done
161
162 $xtrace
163 return $enabled
164}
165
166# enable_python3_package() adds the repositories passed as argument to the
167# ``ENABLED_PYTHON3_PACKAGES`` list, if they are not already present.
168#
169# For example:
170# enable_python3_package nova
171#
172# Uses global ``ENABLED_PYTHON3_PACKAGES``
173# enable_python3_package dir [dir ...]
174function enable_python3_package {
175 local xtrace
176 xtrace=$(set +o | grep xtrace)
177 set +o xtrace
178
179 local tmpsvcs="${ENABLED_PYTHON3_PACKAGES}"
180 local python3
181 for dir in $@; do
182 if [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]]; then
183 warn $LINENO "Attempt to enable_python3_package ${dir} when it has been disabled"
184 continue
185 fi
186 if ! python3_enabled_for $dir; then
187 tmpsvcs+=",$dir"
188 fi
189 done
190 ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$tmpsvcs")
191
192 $xtrace
193}
194
195# disable_python3_package() prepares the services passed as argument to be
196# removed from the ``ENABLED_PYTHON3_PACKAGES`` list, if they are present.
197#
198# For example:
199# disable_python3_package swift
200#
201# Uses globals ``ENABLED_PYTHON3_PACKAGES`` and ``DISABLED_PYTHON3_PACKAGES``
202# disable_python3_package dir [dir ...]
203function disable_python3_package {
204 local xtrace
205 xtrace=$(set +o | grep xtrace)
206 set +o xtrace
207
208 local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}"
209 local enabled_svcs=",${ENABLED_PYTHON3_PACKAGES},"
210 local dir
211 for dir in $@; do
212 disabled_svcs+=",$dir"
213 if python3_enabled_for $dir; then
214 enabled_svcs=${enabled_svcs//,$dir,/,}
215 fi
216 done
217 DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs")
218 ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$enabled_svcs")
219
220 $xtrace
221}
222
Dean Troyer490430d2015-01-30 14:38:35 -0600223# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500224# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +1200225# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400226# Usage:
227# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600228function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000229 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100230 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600231 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000232 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600233 local offline=${OFFLINE:-False}
234 if [[ "$offline" == "True" || -z "$@" ]]; then
235 $xtrace
236 return
237 fi
238
Sean Daguecb658fa2015-10-08 17:12:03 -0400239 time_start "pip_install"
240
Chris Dentebdd9ac2015-03-04 12:35:14 +0000241 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
242 if [[ "$PIP_UPGRADE" = "True" ]] ; then
243 upgrade="--upgrade"
244 fi
245
Dean Troyer490430d2015-01-30 14:38:35 -0600246 if [[ -z "$os_PACKAGE" ]]; then
247 GetOSVersion
248 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400249
250 # Try to extract the path of the package we are installing into
251 # package_dir. We need this to check for test-requirements.txt,
252 # at least.
253 #
254 # ${!#} expands to the last positional argument to this function.
255 # With "extras" syntax included, our arguments might be something
256 # like:
257 # -e /path/to/fooproject[extra]
258 # Thus this magic line grabs just the path without extras
259 #
260 # Note that this makes no sense if this is a pypi (rather than
261 # local path) install; ergo you must check this path exists before
262 # use. Also, if we had multiple or mixed installs, we would also
263 # likely break. But for historical reasons, it's basically only
264 # the other wrapper functions in here calling this to install
265 # local packages, and they do so with single call per install. So
266 # this works (for now...)
267 local package_dir=${!#%\[*\]}
268
Dean Troyer490430d2015-01-30 14:38:35 -0600269 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
270 # TRACK_DEPENDS=True installation creates a circular dependency when
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900271 # we attempt to install virtualenv into a virtualenv, so we must global
Dean Troyer490430d2015-01-30 14:38:35 -0600272 # that installation.
273 source $DEST/.venv/bin/activate
274 local cmd_pip=$DEST/.venv/bin/pip
275 local sudo_pip="env"
276 else
Dean Troyer2b564762015-02-11 17:01:02 -0600277 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
278 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
279 local sudo_pip="env"
280 else
Ian Wienandada886d2015-10-07 14:06:26 +1100281 local cmd_pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000282 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600283 local sudo_pip="sudo -H"
Doug Hellmannddc38392015-05-07 21:06:24 +0000284 if python3_enabled; then
285 # Look at the package classifiers to find the python
286 # versions supported, and if we find the version of
287 # python3 we've been told to use, use that instead of the
288 # default pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000289 local python_versions
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500290
291 # Special case some services that have experimental
292 # support for python3 in progress, but don't claim support
293 # in their classifier
294 echo "Check python version for : $package_dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000295 if python3_disabled_for ${package_dir##*/}; then
296 echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES"
297 elif python3_enabled_for ${package_dir##*/}; then
298 echo "Explicitly using $PYTHON3_VERSION version to install $package_dir based on ENABLED_PYTHON3_PACKAGES"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500299 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
300 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
301 elif [[ -d "$package_dir" ]]; then
Doug Hellmannddc38392015-05-07 21:06:24 +0000302 python_versions=$(get_python_versions_for_package $package_dir)
303 if [[ $python_versions =~ $PYTHON3_VERSION ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000304 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on classifiers"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500305 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
306 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
307 else
308 # The package may not have yet advertised python3.5
309 # support so check for just python3 classifier and log
310 # a warning.
311 python3_classifier=$(check_python3_support_for_package_local $package_dir)
312 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000313 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on local package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500314 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
315 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
316 fi
317 fi
318 else
319 # Check pypi as we don't have the package on disk
320 package=$(echo $package_dir | grep -o '^[.a-zA-Z0-9_-]*')
321 python3_classifier=$(check_python3_support_for_package_remote $package)
322 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000323 echo "Automatically using $PYTHON3_VERSION version to install $package based on remote package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500324 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
Doug Hellmannddc38392015-05-07 21:06:24 +0000325 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
326 fi
327 fi
328 fi
Dean Troyer2b564762015-02-11 17:01:02 -0600329 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600330 fi
331
Robert Collins635a5ba2015-06-10 08:48:06 +1200332 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700333 # Always apply constraints
334 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200335
Doug Hellmannddc38392015-05-07 21:06:24 +0000336 # FIXME(dhellmann): Need to force multiple versions of pip for
337 # packages like setuptools?
Ian Wienandada886d2015-10-07 14:06:26 +1100338 local pip_version
339 pip_version=$(python -c "import pip; \
Clark Boylan06577952017-10-20 12:14:29 -0700340 print(pip.__version__.split('.')[0])")
Dean Troyer490430d2015-01-30 14:38:35 -0600341 if (( pip_version<6 )); then
342 die $LINENO "Currently installed pip version ${pip_version} does not" \
343 "meet minimum requirements (>=6)."
344 fi
345
346 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700347
348 # Also install test requirements
349 local install_test_reqs=""
Zane Bitter9e7ead92017-10-05 16:51:09 -0400350 local test_req="${package_dir}/test-requirements.txt"
Clark Boylanf266a2d2017-06-12 14:57:59 -0700351 if [[ -e "$test_req" ]]; then
352 install_test_reqs="-r $test_req"
353 fi
354
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200355 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
356 # the same behaviour of setuptools before version 25.0.0.
357 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600358 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800359 http_proxy="${http_proxy:-}" \
360 https_proxy="${https_proxy:-}" \
361 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800362 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200363 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Clark Boylanf266a2d2017-06-12 14:57:59 -0700364 $cmd_pip $upgrade $install_test_reqs \
Dean Troyer490430d2015-01-30 14:38:35 -0600365 $@
Federico Ressie208d062015-11-21 11:15:39 +0000366 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600367
Sean Daguecb658fa2015-10-08 17:12:03 -0400368 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000369 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600370}
371
Sean Daguef28e7ef2017-05-07 22:02:10 -0400372function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900373 # Skip uninstall if offline
374 [[ "${OFFLINE}" = "True" ]] && return
375
Sean Daguef28e7ef2017-05-07 22:02:10 -0400376 local name=$1
377 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
378 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
379 local sudo_pip="env"
380 else
381 local cmd_pip
382 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
383 local sudo_pip="sudo -H"
384 fi
385 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400386 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400387}
388
Joe Gordond5ac7852015-02-06 19:29:23 -0800389# get version of a package from global requirements file
390# get_from_global_requirements <package>
391function get_from_global_requirements {
392 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100393 local required_pkg
394 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800395 if [[ $required_pkg == "" ]]; then
396 die $LINENO "Can't find package $package in requirements"
397 fi
398 echo $required_pkg
399}
400
Dean Troyer490430d2015-01-30 14:38:35 -0600401# should we use this library from their git repo, or should we let it
402# get pulled in via pip dependencies.
403function use_library_from_git {
404 local name=$1
405 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200406 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600407 return $enabled
408}
409
Sean Daguec71973e2015-09-08 07:12:48 -0400410# determine if a package was installed from git
411function lib_installed_from_git {
412 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800413 local safe_name
414 safe_name=$(python -c "from pkg_resources import safe_name; \
415 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000416 # Note "pip freeze" doesn't always work here, because it tries to
417 # be smart about finding the remote of the git repo the package
418 # was installed from. This doesn't work with zuul which clones
419 # repos with no remote.
420 #
421 # The best option seems to be to use "pip list" which will tell
422 # you the path an editable install was installed from; for example
423 # in response to something like
424 # pip install -e 'git+http://git.openstack.org/openstack-dev/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500425 # pip list --format columns shows
426 # bashate 0.5.2.dev19 /tmp/env/src/bashate
427 # Thus we check the third column to see if we're installed from
428 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800429 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400430}
431
Dean Troyer490430d2015-01-30 14:38:35 -0600432# setup a library by name. If we are trying to use the library from
433# git, we'll do a git based install, otherwise we'll punt and the
434# library should be installed by a requirements pull from another
435# project.
436function setup_lib {
437 local name=$1
438 local dir=${GITDIR[$name]}
439 setup_install $dir
440}
441
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900442# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600443# the library from git, we'll do a git based install, otherwise we'll
444# punt and the library should be installed by a requirements pull from
445# another project.
446#
447# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100448#
449# setup_dev_lib [-bindep] <name>
Dean Troyer490430d2015-01-30 14:38:35 -0600450function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100451 local bindep
452 if [[ $1 == -bindep* ]]; then
453 bindep="${1}"
454 shift
455 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600456 local name=$1
457 local dir=${GITDIR[$name]}
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000458 if python3_enabled; then
459 # Turn off Python 3 mode and install the package again,
460 # forcing a Python 2 installation. This ensures that all libs
461 # being used for development are installed under both versions
462 # of Python.
463 echo "Installing $name again without Python 3 enabled"
464 USE_PYTHON3=False
Ian Wienand58243f62018-12-13 14:05:53 +1100465 setup_develop $bindep $dir
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000466 USE_PYTHON3=True
467 fi
Ian Wienand58243f62018-12-13 14:05:53 +1100468 setup_develop $bindep $dir
Dean Troyer490430d2015-01-30 14:38:35 -0600469}
470
471# this should be used if you want to install globally, all libraries should
472# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500473#
474# setup_install project_dir [extras]
475# project_dir: directory of project repo (e.g., /opt/stack/keystone)
476# extras: comma-separated list of optional dependencies to install
477# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900478# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Ian Wienand58243f62018-12-13 14:05:53 +1100479# bindep: Set "-bindep" as first argument to install bindep.txt packages
Brant Knudson0842b812015-08-03 13:31:25 -0500480# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600481function setup_install {
Ian Wienand58243f62018-12-13 14:05:53 +1100482 local bindep
483 if [[ $1 == -bindep* ]]; then
484 bindep="${1}"
485 shift
486 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600487 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500488 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100489 _setup_package_with_constraints_edit $bindep $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600490}
491
492# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500493#
494# setup_develop project_dir [extras]
495# project_dir: directory of project repo (e.g., /opt/stack/keystone)
496# extras: comma-separated list of optional dependencies to install
497# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900498# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500499# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600500function setup_develop {
Ian Wienand58243f62018-12-13 14:05:53 +1100501 local bindep
502 if [[ $1 == -bindep* ]]; then
503 bindep="${1}"
504 shift
505 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600506 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500507 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100508 _setup_package_with_constraints_edit $bindep $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600509}
510
Dean Troyer490430d2015-01-30 14:38:35 -0600511# ``pip install -e`` the package, which processes the dependencies
512# using pip before running `setup.py develop`
513#
Clark Boylan05aa3842015-08-03 11:14:13 -0700514# Updates the constraints from REQUIREMENTS_DIR to reflect the
515# future installed state of this package. This ensures when we
516# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600517#
Clark Boylan05aa3842015-08-03 11:14:13 -0700518# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500519# _setup_package_with_constraints_edit project_dir flags [extras]
520# project_dir: directory of project repo (e.g., /opt/stack/keystone)
521# flags: pip CLI options/flags
522# extras: comma-separated list of optional dependencies to install
523# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900524# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500525# The command is like "pip install <flags> <project_dir>[<extras>]"
526function _setup_package_with_constraints_edit {
Ian Wienand58243f62018-12-13 14:05:53 +1100527 local bindep
528 if [[ $1 == -bindep* ]]; then
529 bindep="${1}"
530 shift
531 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600532 local project_dir=$1
533 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500534 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600535
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900536 # Normalize the directory name to avoid
537 # "installation from path or url cannot be constrained to a version"
538 # error.
539 # REVISIT(yamamoto): Remove this when fixed in pip.
540 # https://github.com/pypa/pip/pull/3582
541 project_dir=$(cd $project_dir && pwd)
542
Robert Collins635a5ba2015-06-10 08:48:06 +1200543 if [ -n "$REQUIREMENTS_DIR" ]; then
544 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100545 local name
546 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200547 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
548 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
549 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200550 fi
551
Ian Wienand58243f62018-12-13 14:05:53 +1100552 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600553
James E. Blaire1edde32018-03-02 15:05:14 +0000554 # If this project is in LIBS_FROM_GIT, verify it was actually installed
555 # correctly. This helps catch errors caused by constraints mismatches.
556 if use_library_from_git "$project_dir"; then
557 if ! lib_installed_from_git "$project_dir"; then
558 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
559 fi
560 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600561}
562
563# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100564# using pip before running `setup.py develop`. The command is like
565# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500566#
Dean Troyer490430d2015-01-30 14:38:35 -0600567# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100568#
569# Usage:
570# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
571#
572# -bindep : Use bindep to install dependencies; select extra profiles
573# as comma separated arguments after "="
574# project_dir : directory of project repo (e.g., /opt/stack/keystone)
575# flags : pip CLI options/flags
576# extras : comma-separated list of optional dependencies to install
577# (e.g., ldap,memcache).
578# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600579function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100580 local bindep=0
581 local bindep_flag=""
582 local bindep_profiles=""
583 if [[ $1 == -bindep* ]]; then
584 bindep=1
585 IFS="=" read bindep_flag bindep_profiles <<< ${1}
586 shift
587 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600588 local project_dir=$1
589 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500590 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600591
Brant Knudson0842b812015-08-03 13:31:25 -0500592 # if the flags variable exists, and it doesn't look like a flag,
593 # assume it's actually the extras list.
594 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
595 extras=$flags
596 flags=""
597 fi
598
599 if [[ ! -z "$extras" ]]; then
600 extras="[$extras]"
601 fi
602
Ian Wienand58243f62018-12-13 14:05:53 +1100603 # install any bindep packages
604 if [[ $bindep == 1 ]]; then
605 install_bindep $project_dir/bindep.txt $bindep_profiles
606 fi
607
Brant Knudson0842b812015-08-03 13:31:25 -0500608 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600609 # ensure that further actions can do things like setup.py sdist
610 if [[ "$flags" == "-e" ]]; then
611 safe_chown -R $STACK_USER $1/*.egg-info
612 fi
613}
614
Doug Hellmannddc38392015-05-07 21:06:24 +0000615# Report whether python 3 should be used
616function python3_enabled {
617 if [[ $USE_PYTHON3 == "True" ]]; then
618 return 0
619 else
620 return 1
621 fi
622}
623
624# Install python3 packages
625function install_python3 {
626 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500627 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700628 elif is_suse; then
629 install_package python3-devel python3-dbm
Doug Hellmannddc38392015-05-07 21:06:24 +0000630 fi
631}
Dean Troyer490430d2015-01-30 14:38:35 -0600632
Sean Daguef80e2cf2017-01-18 15:42:32 -0500633function install_devstack_tools {
634 # intentionally old to ensure devstack-gate has control
635 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
636 install_python3
637 sudo pip3 install -U devstack-tools==${dstools_version}
638}
639
Dean Troyer490430d2015-01-30 14:38:35 -0600640# Restore xtrace
641$INC_PY_TRACE
642
643# Local variables:
644# mode: shell-script
645# End: