blob: 419d5c570184e7c9338dafaf57398b2bb3c12ae2 [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
Sean Mooneyc7597062019-02-28 11:20:36 +000052 if python3_enabled && [[ "$os_VENDOR" == "CentOS" ]] || \
53 [[ "$os_VENDOR" == "Fedora" && $os_RELEASE -gt 26 ]]; then
Victor Stinnerb9891ee2018-01-08 15:20:36 +010054 # Default Python 3 install prefix changed to /usr/local in Fedora 27:
55 # https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe
56 echo "/usr/local/bin"
57 elif is_fedora || is_suse; then
Dean Troyer490430d2015-01-30 14:38:35 -060058 echo "/usr/bin"
59 else
60 echo "/usr/local/bin"
61 fi
62}
63
Sean Dague60996b12015-04-08 09:06:49 -040064# Wrapper for ``pip install`` that only installs versions of libraries
65# from the global-requirements specification.
66#
67# Uses globals ``REQUIREMENTS_DIR``
68#
69# pip_install_gr packagename
70function pip_install_gr {
71 local name=$1
Ian Wienandada886d2015-10-07 14:06:26 +110072 local clean_name
73 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -040074 pip_install $clean_name
75}
76
Mehdi Abaakouk52b10742016-12-01 16:11:17 +010077# Wrapper for ``pip install`` that only installs versions of libraries
78# from the global-requirements specification with extras.
79#
80# Uses globals ``REQUIREMENTS_DIR``
81#
82# pip_install_gr_extras packagename extra1,extra2,...
83function pip_install_gr_extras {
84 local name=$1
85 local extras=$2
86 local clean_name
87 clean_name=$(get_from_global_requirements $name)
88 pip_install $clean_name[$extras]
89}
90
Doug Hellmannddc38392015-05-07 21:06:24 +000091# Determine the python versions supported by a package
92function get_python_versions_for_package {
93 local name=$1
94 cd $name && python setup.py --classifiers \
95 | grep 'Language' | cut -f5 -d: | grep '\.' | tr '\n' ' '
96}
97
Davanum Srinivasafa8a002016-12-19 09:51:01 -050098# Check for python3 classifier in local directory
99function check_python3_support_for_package_local {
100 local name=$1
101 cd $name
102 set +e
103 classifier=$(python setup.py --classifiers \
104 | grep 'Programming Language :: Python :: 3$')
105 set -e
106 echo $classifier
107}
108
109# Check for python3 classifier on pypi
110function check_python3_support_for_package_remote {
111 local name=$1
112 set +e
113 classifier=$(curl -s -L "https://pypi.python.org/pypi/$name/json" \
114 | grep '"Programming Language :: Python :: 3"')
115 set -e
116 echo $classifier
117}
118
Doug Hellmann36377f62018-12-04 11:33:03 -0500119# python3_enabled_for() assumes the service(s) specified as arguments are
120# enabled for python 3 unless explicitly disabled. See python3_disabled_for().
Doug Hellmann94129c72017-01-09 21:24:24 +0000121#
122# Multiple services specified as arguments are ``OR``'ed together; the test
123# is a short-circuit boolean, i.e it returns on the first match.
124#
Doug Hellmann94129c72017-01-09 21:24:24 +0000125# 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
Doug Hellmann36377f62018-12-04 11:33:03 -0500135 if ! python3_disabled_for "${dir}"; then
136 enabled=0
137 fi
Doug Hellmann94129c72017-01-09 21:24:24 +0000138 done
139
140 $xtrace
141 return $enabled
142}
143
144# python3_disabled_for() checks if the service(s) specified as arguments are
145# disabled by the user in ``DISABLED_PYTHON3_PACKAGES``.
146#
147# Multiple services specified as arguments are ``OR``'ed together; the test
148# is a short-circuit boolean, i.e it returns on the first match.
149#
150# Uses global ``DISABLED_PYTHON3_PACKAGES``
151# python3_disabled_for dir [dir ...]
152function python3_disabled_for {
153 local xtrace
154 xtrace=$(set +o | grep xtrace)
155 set +o xtrace
156
157 local enabled=1
158 local dirs=$@
159 local dir
160 for dir in ${dirs}; do
161 [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
162 done
163
164 $xtrace
165 return $enabled
166}
167
Doug Hellmann36377f62018-12-04 11:33:03 -0500168# enable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000169#
170# For example:
171# enable_python3_package nova
172#
Doug Hellmann94129c72017-01-09 21:24:24 +0000173# enable_python3_package dir [dir ...]
174function enable_python3_package {
175 local xtrace
176 xtrace=$(set +o | grep xtrace)
177 set +o xtrace
178
Doug Hellmann36377f62018-12-04 11:33:03 -0500179 echo "It is no longer necessary to call enable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000180
181 $xtrace
182}
183
Doug Hellmann36377f62018-12-04 11:33:03 -0500184# disable_python3_package() adds the services passed as argument to
185# the ``DISABLED_PYTHON3_PACKAGES`` list.
Doug Hellmann94129c72017-01-09 21:24:24 +0000186#
187# For example:
188# disable_python3_package swift
189#
Doug Hellmann36377f62018-12-04 11:33:03 -0500190# Uses global ``DISABLED_PYTHON3_PACKAGES``
Doug Hellmann94129c72017-01-09 21:24:24 +0000191# disable_python3_package dir [dir ...]
192function disable_python3_package {
193 local xtrace
194 xtrace=$(set +o | grep xtrace)
195 set +o xtrace
196
197 local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}"
Doug Hellmann94129c72017-01-09 21:24:24 +0000198 local dir
199 for dir in $@; do
200 disabled_svcs+=",$dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000201 done
202 DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs")
Doug Hellmann94129c72017-01-09 21:24:24 +0000203
204 $xtrace
205}
206
Dean Troyer490430d2015-01-30 14:38:35 -0600207# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500208# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +1200209# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400210# Usage:
211# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600212function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000213 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100214 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600215 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000216 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600217 local offline=${OFFLINE:-False}
218 if [[ "$offline" == "True" || -z "$@" ]]; then
219 $xtrace
220 return
221 fi
222
Sean Daguecb658fa2015-10-08 17:12:03 -0400223 time_start "pip_install"
224
Chris Dentebdd9ac2015-03-04 12:35:14 +0000225 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
226 if [[ "$PIP_UPGRADE" = "True" ]] ; then
227 upgrade="--upgrade"
228 fi
229
Dean Troyer490430d2015-01-30 14:38:35 -0600230 if [[ -z "$os_PACKAGE" ]]; then
231 GetOSVersion
232 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400233
234 # Try to extract the path of the package we are installing into
235 # package_dir. We need this to check for test-requirements.txt,
236 # at least.
237 #
238 # ${!#} expands to the last positional argument to this function.
239 # With "extras" syntax included, our arguments might be something
240 # like:
241 # -e /path/to/fooproject[extra]
242 # Thus this magic line grabs just the path without extras
243 #
244 # Note that this makes no sense if this is a pypi (rather than
245 # local path) install; ergo you must check this path exists before
246 # use. Also, if we had multiple or mixed installs, we would also
247 # likely break. But for historical reasons, it's basically only
248 # the other wrapper functions in here calling this to install
249 # local packages, and they do so with single call per install. So
250 # this works (for now...)
251 local package_dir=${!#%\[*\]}
252
Dean Troyer490430d2015-01-30 14:38:35 -0600253 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
254 # TRACK_DEPENDS=True installation creates a circular dependency when
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900255 # we attempt to install virtualenv into a virtualenv, so we must global
Dean Troyer490430d2015-01-30 14:38:35 -0600256 # that installation.
257 source $DEST/.venv/bin/activate
258 local cmd_pip=$DEST/.venv/bin/pip
259 local sudo_pip="env"
260 else
Dean Troyer2b564762015-02-11 17:01:02 -0600261 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
262 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
263 local sudo_pip="env"
264 else
Ian Wienandada886d2015-10-07 14:06:26 +1100265 local cmd_pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000266 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600267 local sudo_pip="sudo -H"
Doug Hellmannddc38392015-05-07 21:06:24 +0000268 if python3_enabled; then
269 # Look at the package classifiers to find the python
270 # versions supported, and if we find the version of
271 # python3 we've been told to use, use that instead of the
272 # default pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000273 local python_versions
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500274
275 # Special case some services that have experimental
276 # support for python3 in progress, but don't claim support
277 # in their classifier
278 echo "Check python version for : $package_dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000279 if python3_disabled_for ${package_dir##*/}; then
280 echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES"
281 elif python3_enabled_for ${package_dir##*/}; then
Doug Hellmann36377f62018-12-04 11:33:03 -0500282 echo "Using $PYTHON3_VERSION version to install $package_dir based on default behavior"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500283 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
284 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
285 elif [[ -d "$package_dir" ]]; then
Doug Hellmannddc38392015-05-07 21:06:24 +0000286 python_versions=$(get_python_versions_for_package $package_dir)
287 if [[ $python_versions =~ $PYTHON3_VERSION ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000288 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on classifiers"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500289 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
290 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
291 else
292 # The package may not have yet advertised python3.5
293 # support so check for just python3 classifier and log
294 # a warning.
295 python3_classifier=$(check_python3_support_for_package_local $package_dir)
296 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000297 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on local package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500298 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
299 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
300 fi
301 fi
302 else
303 # Check pypi as we don't have the package on disk
304 package=$(echo $package_dir | grep -o '^[.a-zA-Z0-9_-]*')
305 python3_classifier=$(check_python3_support_for_package_remote $package)
306 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000307 echo "Automatically using $PYTHON3_VERSION version to install $package based on remote package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500308 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
Doug Hellmannddc38392015-05-07 21:06:24 +0000309 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
310 fi
311 fi
312 fi
Dean Troyer2b564762015-02-11 17:01:02 -0600313 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600314 fi
315
Robert Collins635a5ba2015-06-10 08:48:06 +1200316 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700317 # Always apply constraints
318 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200319
Doug Hellmannddc38392015-05-07 21:06:24 +0000320 # FIXME(dhellmann): Need to force multiple versions of pip for
321 # packages like setuptools?
Ian Wienandada886d2015-10-07 14:06:26 +1100322 local pip_version
323 pip_version=$(python -c "import pip; \
Clark Boylan06577952017-10-20 12:14:29 -0700324 print(pip.__version__.split('.')[0])")
Dean Troyer490430d2015-01-30 14:38:35 -0600325 if (( pip_version<6 )); then
326 die $LINENO "Currently installed pip version ${pip_version} does not" \
327 "meet minimum requirements (>=6)."
328 fi
329
330 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700331
332 # Also install test requirements
333 local install_test_reqs=""
Zane Bitter9e7ead92017-10-05 16:51:09 -0400334 local test_req="${package_dir}/test-requirements.txt"
Clark Boylanf266a2d2017-06-12 14:57:59 -0700335 if [[ -e "$test_req" ]]; then
336 install_test_reqs="-r $test_req"
337 fi
338
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200339 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
340 # the same behaviour of setuptools before version 25.0.0.
341 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600342 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800343 http_proxy="${http_proxy:-}" \
344 https_proxy="${https_proxy:-}" \
345 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800346 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200347 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Clark Boylanf266a2d2017-06-12 14:57:59 -0700348 $cmd_pip $upgrade $install_test_reqs \
Dean Troyer490430d2015-01-30 14:38:35 -0600349 $@
Federico Ressie208d062015-11-21 11:15:39 +0000350 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600351
Sean Daguecb658fa2015-10-08 17:12:03 -0400352 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000353 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600354}
355
Sean Daguef28e7ef2017-05-07 22:02:10 -0400356function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900357 # Skip uninstall if offline
358 [[ "${OFFLINE}" = "True" ]] && return
359
Sean Daguef28e7ef2017-05-07 22:02:10 -0400360 local name=$1
361 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
362 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
363 local sudo_pip="env"
364 else
365 local cmd_pip
366 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
367 local sudo_pip="sudo -H"
368 fi
369 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400370 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400371}
372
Joe Gordond5ac7852015-02-06 19:29:23 -0800373# get version of a package from global requirements file
374# get_from_global_requirements <package>
375function get_from_global_requirements {
376 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100377 local required_pkg
378 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800379 if [[ $required_pkg == "" ]]; then
380 die $LINENO "Can't find package $package in requirements"
381 fi
382 echo $required_pkg
383}
384
Dean Troyer490430d2015-01-30 14:38:35 -0600385# should we use this library from their git repo, or should we let it
386# get pulled in via pip dependencies.
387function use_library_from_git {
388 local name=$1
389 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200390 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600391 return $enabled
392}
393
Sean Daguec71973e2015-09-08 07:12:48 -0400394# determine if a package was installed from git
395function lib_installed_from_git {
396 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800397 local safe_name
398 safe_name=$(python -c "from pkg_resources import safe_name; \
399 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000400 # Note "pip freeze" doesn't always work here, because it tries to
401 # be smart about finding the remote of the git repo the package
402 # was installed from. This doesn't work with zuul which clones
403 # repos with no remote.
404 #
405 # The best option seems to be to use "pip list" which will tell
406 # you the path an editable install was installed from; for example
407 # in response to something like
408 # pip install -e 'git+http://git.openstack.org/openstack-dev/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500409 # pip list --format columns shows
410 # bashate 0.5.2.dev19 /tmp/env/src/bashate
411 # Thus we check the third column to see if we're installed from
412 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800413 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400414}
415
Dean Troyer490430d2015-01-30 14:38:35 -0600416# setup a library by name. If we are trying to use the library from
417# git, we'll do a git based install, otherwise we'll punt and the
418# library should be installed by a requirements pull from another
419# project.
420function setup_lib {
421 local name=$1
422 local dir=${GITDIR[$name]}
423 setup_install $dir
424}
425
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900426# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600427# the library from git, we'll do a git based install, otherwise we'll
428# punt and the library should be installed by a requirements pull from
429# another project.
430#
431# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100432#
433# setup_dev_lib [-bindep] <name>
Dean Troyer490430d2015-01-30 14:38:35 -0600434function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100435 local bindep
436 if [[ $1 == -bindep* ]]; then
437 bindep="${1}"
438 shift
439 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600440 local name=$1
441 local dir=${GITDIR[$name]}
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000442 if python3_enabled; then
443 # Turn off Python 3 mode and install the package again,
444 # forcing a Python 2 installation. This ensures that all libs
445 # being used for development are installed under both versions
446 # of Python.
447 echo "Installing $name again without Python 3 enabled"
448 USE_PYTHON3=False
Ian Wienand58243f62018-12-13 14:05:53 +1100449 setup_develop $bindep $dir
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000450 USE_PYTHON3=True
451 fi
Ian Wienand58243f62018-12-13 14:05:53 +1100452 setup_develop $bindep $dir
Dean Troyer490430d2015-01-30 14:38:35 -0600453}
454
455# this should be used if you want to install globally, all libraries should
456# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500457#
458# setup_install project_dir [extras]
459# project_dir: directory of project repo (e.g., /opt/stack/keystone)
460# extras: comma-separated list of optional dependencies to install
461# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900462# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Ian Wienand58243f62018-12-13 14:05:53 +1100463# bindep: Set "-bindep" as first argument to install bindep.txt packages
Brant Knudson0842b812015-08-03 13:31:25 -0500464# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600465function setup_install {
Ian Wienand58243f62018-12-13 14:05:53 +1100466 local bindep
467 if [[ $1 == -bindep* ]]; then
468 bindep="${1}"
469 shift
470 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600471 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500472 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100473 _setup_package_with_constraints_edit $bindep $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600474}
475
476# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500477#
478# setup_develop project_dir [extras]
479# project_dir: directory of project repo (e.g., /opt/stack/keystone)
480# extras: comma-separated list of optional dependencies to install
481# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900482# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500483# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600484function setup_develop {
Ian Wienand58243f62018-12-13 14:05:53 +1100485 local bindep
486 if [[ $1 == -bindep* ]]; then
487 bindep="${1}"
488 shift
489 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600490 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500491 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100492 _setup_package_with_constraints_edit $bindep $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600493}
494
Dean Troyer490430d2015-01-30 14:38:35 -0600495# ``pip install -e`` the package, which processes the dependencies
496# using pip before running `setup.py develop`
497#
Clark Boylan05aa3842015-08-03 11:14:13 -0700498# Updates the constraints from REQUIREMENTS_DIR to reflect the
499# future installed state of this package. This ensures when we
500# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600501#
Clark Boylan05aa3842015-08-03 11:14:13 -0700502# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500503# _setup_package_with_constraints_edit project_dir flags [extras]
504# project_dir: directory of project repo (e.g., /opt/stack/keystone)
505# flags: pip CLI options/flags
506# extras: comma-separated list of optional dependencies to install
507# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900508# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500509# The command is like "pip install <flags> <project_dir>[<extras>]"
510function _setup_package_with_constraints_edit {
Ian Wienand58243f62018-12-13 14:05:53 +1100511 local bindep
512 if [[ $1 == -bindep* ]]; then
513 bindep="${1}"
514 shift
515 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600516 local project_dir=$1
517 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500518 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600519
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900520 # Normalize the directory name to avoid
521 # "installation from path or url cannot be constrained to a version"
522 # error.
523 # REVISIT(yamamoto): Remove this when fixed in pip.
524 # https://github.com/pypa/pip/pull/3582
525 project_dir=$(cd $project_dir && pwd)
526
Robert Collins635a5ba2015-06-10 08:48:06 +1200527 if [ -n "$REQUIREMENTS_DIR" ]; then
528 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100529 local name
530 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200531 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
532 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
533 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200534 fi
535
Ian Wienand58243f62018-12-13 14:05:53 +1100536 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600537
James E. Blaire1edde32018-03-02 15:05:14 +0000538 # If this project is in LIBS_FROM_GIT, verify it was actually installed
539 # correctly. This helps catch errors caused by constraints mismatches.
540 if use_library_from_git "$project_dir"; then
541 if ! lib_installed_from_git "$project_dir"; then
542 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
543 fi
544 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600545}
546
547# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100548# using pip before running `setup.py develop`. The command is like
549# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500550#
Dean Troyer490430d2015-01-30 14:38:35 -0600551# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100552#
553# Usage:
554# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
555#
556# -bindep : Use bindep to install dependencies; select extra profiles
557# as comma separated arguments after "="
558# project_dir : directory of project repo (e.g., /opt/stack/keystone)
559# flags : pip CLI options/flags
560# extras : comma-separated list of optional dependencies to install
561# (e.g., ldap,memcache).
562# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600563function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100564 local bindep=0
565 local bindep_flag=""
566 local bindep_profiles=""
567 if [[ $1 == -bindep* ]]; then
568 bindep=1
569 IFS="=" read bindep_flag bindep_profiles <<< ${1}
570 shift
571 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600572 local project_dir=$1
573 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500574 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600575
Brant Knudson0842b812015-08-03 13:31:25 -0500576 # if the flags variable exists, and it doesn't look like a flag,
577 # assume it's actually the extras list.
578 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
579 extras=$flags
580 flags=""
581 fi
582
583 if [[ ! -z "$extras" ]]; then
584 extras="[$extras]"
585 fi
586
Ian Wienand58243f62018-12-13 14:05:53 +1100587 # install any bindep packages
588 if [[ $bindep == 1 ]]; then
589 install_bindep $project_dir/bindep.txt $bindep_profiles
590 fi
591
Brant Knudson0842b812015-08-03 13:31:25 -0500592 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600593 # ensure that further actions can do things like setup.py sdist
594 if [[ "$flags" == "-e" ]]; then
595 safe_chown -R $STACK_USER $1/*.egg-info
596 fi
597}
598
Doug Hellmannddc38392015-05-07 21:06:24 +0000599# Report whether python 3 should be used
600function python3_enabled {
601 if [[ $USE_PYTHON3 == "True" ]]; then
602 return 0
603 else
604 return 1
605 fi
606}
607
608# Install python3 packages
609function install_python3 {
610 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500611 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700612 elif is_suse; then
613 install_package python3-devel python3-dbm
Doug Hellmannddc38392015-05-07 21:06:24 +0000614 fi
615}
Dean Troyer490430d2015-01-30 14:38:35 -0600616
Sean Daguef80e2cf2017-01-18 15:42:32 -0500617function install_devstack_tools {
618 # intentionally old to ensure devstack-gate has control
619 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
620 install_python3
621 sudo pip3 install -U devstack-tools==${dstools_version}
622}
623
Dean Troyer490430d2015-01-30 14:38:35 -0600624# Restore xtrace
625$INC_PY_TRACE
626
627# Local variables:
628# mode: shell-script
629# End: