blob: ba2048dd84b7edaf40a876105e1a270fc27046cb [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 Hellmannddc38392015-05-07 21:06:24 +000084# Determine the python versions supported by a package
85function get_python_versions_for_package {
86 local name=$1
87 cd $name && python setup.py --classifiers \
88 | grep 'Language' | cut -f5 -d: | grep '\.' | tr '\n' ' '
89}
90
Davanum Srinivasafa8a002016-12-19 09:51:01 -050091# Check for python3 classifier in local directory
92function check_python3_support_for_package_local {
93 local name=$1
94 cd $name
95 set +e
96 classifier=$(python setup.py --classifiers \
97 | grep 'Programming Language :: Python :: 3$')
98 set -e
99 echo $classifier
100}
101
102# Check for python3 classifier on pypi
103function check_python3_support_for_package_remote {
104 local name=$1
105 set +e
106 classifier=$(curl -s -L "https://pypi.python.org/pypi/$name/json" \
107 | grep '"Programming Language :: Python :: 3"')
108 set -e
109 echo $classifier
110}
111
Doug Hellmann36377f62018-12-04 11:33:03 -0500112# python3_enabled_for() assumes the service(s) specified as arguments are
113# enabled for python 3 unless explicitly disabled. See python3_disabled_for().
Doug Hellmann94129c72017-01-09 21:24:24 +0000114#
115# Multiple services specified as arguments are ``OR``'ed together; the test
116# is a short-circuit boolean, i.e it returns on the first match.
117#
Doug Hellmann94129c72017-01-09 21:24:24 +0000118# python3_enabled_for dir [dir ...]
119function python3_enabled_for {
120 local xtrace
121 xtrace=$(set +o | grep xtrace)
122 set +o xtrace
123
124 local enabled=1
125 local dirs=$@
126 local dir
127 for dir in ${dirs}; do
Doug Hellmann36377f62018-12-04 11:33:03 -0500128 if ! python3_disabled_for "${dir}"; then
129 enabled=0
130 fi
Doug Hellmann94129c72017-01-09 21:24:24 +0000131 done
132
133 $xtrace
134 return $enabled
135}
136
137# python3_disabled_for() checks if the service(s) specified as arguments are
138# disabled by the user in ``DISABLED_PYTHON3_PACKAGES``.
139#
140# Multiple services specified as arguments are ``OR``'ed together; the test
141# is a short-circuit boolean, i.e it returns on the first match.
142#
143# Uses global ``DISABLED_PYTHON3_PACKAGES``
144# python3_disabled_for dir [dir ...]
145function python3_disabled_for {
146 local xtrace
147 xtrace=$(set +o | grep xtrace)
148 set +o xtrace
149
150 local enabled=1
151 local dirs=$@
152 local dir
153 for dir in ${dirs}; do
154 [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
155 done
156
157 $xtrace
158 return $enabled
159}
160
Doug Hellmann36377f62018-12-04 11:33:03 -0500161# enable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000162#
163# For example:
164# enable_python3_package nova
165#
Doug Hellmann94129c72017-01-09 21:24:24 +0000166# enable_python3_package dir [dir ...]
167function enable_python3_package {
168 local xtrace
169 xtrace=$(set +o | grep xtrace)
170 set +o xtrace
171
Doug Hellmann36377f62018-12-04 11:33:03 -0500172 echo "It is no longer necessary to call enable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000173
174 $xtrace
175}
176
Doug Hellmann36377f62018-12-04 11:33:03 -0500177# disable_python3_package() adds the services passed as argument to
178# the ``DISABLED_PYTHON3_PACKAGES`` list.
Doug Hellmann94129c72017-01-09 21:24:24 +0000179#
180# For example:
181# disable_python3_package swift
182#
Doug Hellmann36377f62018-12-04 11:33:03 -0500183# Uses global ``DISABLED_PYTHON3_PACKAGES``
Doug Hellmann94129c72017-01-09 21:24:24 +0000184# disable_python3_package dir [dir ...]
185function disable_python3_package {
186 local xtrace
187 xtrace=$(set +o | grep xtrace)
188 set +o xtrace
189
190 local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}"
Doug Hellmann94129c72017-01-09 21:24:24 +0000191 local dir
192 for dir in $@; do
193 disabled_svcs+=",$dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000194 done
195 DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs")
Doug Hellmann94129c72017-01-09 21:24:24 +0000196
197 $xtrace
198}
199
Dean Troyer490430d2015-01-30 14:38:35 -0600200# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500201# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +1200202# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400203# Usage:
204# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600205function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000206 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100207 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600208 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000209 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600210 local offline=${OFFLINE:-False}
211 if [[ "$offline" == "True" || -z "$@" ]]; then
212 $xtrace
213 return
214 fi
215
Sean Daguecb658fa2015-10-08 17:12:03 -0400216 time_start "pip_install"
217
Chris Dentebdd9ac2015-03-04 12:35:14 +0000218 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
219 if [[ "$PIP_UPGRADE" = "True" ]] ; then
220 upgrade="--upgrade"
221 fi
222
Dean Troyer490430d2015-01-30 14:38:35 -0600223 if [[ -z "$os_PACKAGE" ]]; then
224 GetOSVersion
225 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400226
227 # Try to extract the path of the package we are installing into
228 # package_dir. We need this to check for test-requirements.txt,
229 # at least.
230 #
231 # ${!#} expands to the last positional argument to this function.
232 # With "extras" syntax included, our arguments might be something
233 # like:
234 # -e /path/to/fooproject[extra]
235 # Thus this magic line grabs just the path without extras
236 #
237 # Note that this makes no sense if this is a pypi (rather than
238 # local path) install; ergo you must check this path exists before
239 # use. Also, if we had multiple or mixed installs, we would also
240 # likely break. But for historical reasons, it's basically only
241 # the other wrapper functions in here calling this to install
242 # local packages, and they do so with single call per install. So
243 # this works (for now...)
244 local package_dir=${!#%\[*\]}
245
Dean Troyer490430d2015-01-30 14:38:35 -0600246 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
247 # TRACK_DEPENDS=True installation creates a circular dependency when
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900248 # we attempt to install virtualenv into a virtualenv, so we must global
Dean Troyer490430d2015-01-30 14:38:35 -0600249 # that installation.
250 source $DEST/.venv/bin/activate
251 local cmd_pip=$DEST/.venv/bin/pip
252 local sudo_pip="env"
253 else
Dean Troyer2b564762015-02-11 17:01:02 -0600254 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
255 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
256 local sudo_pip="env"
257 else
Ian Wienandada886d2015-10-07 14:06:26 +1100258 local cmd_pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000259 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600260 local sudo_pip="sudo -H"
Doug Hellmannddc38392015-05-07 21:06:24 +0000261 if python3_enabled; then
262 # Look at the package classifiers to find the python
263 # versions supported, and if we find the version of
264 # python3 we've been told to use, use that instead of the
265 # default pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000266 local python_versions
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500267
268 # Special case some services that have experimental
269 # support for python3 in progress, but don't claim support
270 # in their classifier
271 echo "Check python version for : $package_dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000272 if python3_disabled_for ${package_dir##*/}; then
273 echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES"
274 elif python3_enabled_for ${package_dir##*/}; then
Doug Hellmann36377f62018-12-04 11:33:03 -0500275 echo "Using $PYTHON3_VERSION version to install $package_dir based on default behavior"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500276 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
277 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
278 elif [[ -d "$package_dir" ]]; then
Doug Hellmannddc38392015-05-07 21:06:24 +0000279 python_versions=$(get_python_versions_for_package $package_dir)
280 if [[ $python_versions =~ $PYTHON3_VERSION ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000281 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on classifiers"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500282 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
283 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
284 else
285 # The package may not have yet advertised python3.5
286 # support so check for just python3 classifier and log
287 # a warning.
288 python3_classifier=$(check_python3_support_for_package_local $package_dir)
289 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000290 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on local package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500291 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
292 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
293 fi
294 fi
295 else
296 # Check pypi as we don't have the package on disk
297 package=$(echo $package_dir | grep -o '^[.a-zA-Z0-9_-]*')
298 python3_classifier=$(check_python3_support_for_package_remote $package)
299 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000300 echo "Automatically using $PYTHON3_VERSION version to install $package based on remote package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500301 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
Doug Hellmannddc38392015-05-07 21:06:24 +0000302 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
303 fi
304 fi
305 fi
Dean Troyer2b564762015-02-11 17:01:02 -0600306 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600307 fi
308
Robert Collins635a5ba2015-06-10 08:48:06 +1200309 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700310 # Always apply constraints
311 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200312
Doug Hellmannddc38392015-05-07 21:06:24 +0000313 # FIXME(dhellmann): Need to force multiple versions of pip for
314 # packages like setuptools?
Ian Wienandada886d2015-10-07 14:06:26 +1100315 local pip_version
316 pip_version=$(python -c "import pip; \
Clark Boylan06577952017-10-20 12:14:29 -0700317 print(pip.__version__.split('.')[0])")
Dean Troyer490430d2015-01-30 14:38:35 -0600318 if (( pip_version<6 )); then
319 die $LINENO "Currently installed pip version ${pip_version} does not" \
320 "meet minimum requirements (>=6)."
321 fi
322
323 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700324
325 # Also install test requirements
326 local install_test_reqs=""
Zane Bitter9e7ead92017-10-05 16:51:09 -0400327 local test_req="${package_dir}/test-requirements.txt"
Clark Boylanf266a2d2017-06-12 14:57:59 -0700328 if [[ -e "$test_req" ]]; then
329 install_test_reqs="-r $test_req"
330 fi
331
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200332 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
333 # the same behaviour of setuptools before version 25.0.0.
334 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600335 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800336 http_proxy="${http_proxy:-}" \
337 https_proxy="${https_proxy:-}" \
338 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800339 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200340 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Clark Boylanf266a2d2017-06-12 14:57:59 -0700341 $cmd_pip $upgrade $install_test_reqs \
Dean Troyer490430d2015-01-30 14:38:35 -0600342 $@
Federico Ressie208d062015-11-21 11:15:39 +0000343 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600344
Sean Daguecb658fa2015-10-08 17:12:03 -0400345 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000346 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600347}
348
Sean Daguef28e7ef2017-05-07 22:02:10 -0400349function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900350 # Skip uninstall if offline
351 [[ "${OFFLINE}" = "True" ]] && return
352
Sean Daguef28e7ef2017-05-07 22:02:10 -0400353 local name=$1
354 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
355 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
356 local sudo_pip="env"
357 else
358 local cmd_pip
359 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
360 local sudo_pip="sudo -H"
361 fi
362 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400363 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400364}
365
Joe Gordond5ac7852015-02-06 19:29:23 -0800366# get version of a package from global requirements file
367# get_from_global_requirements <package>
368function get_from_global_requirements {
369 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100370 local required_pkg
371 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800372 if [[ $required_pkg == "" ]]; then
373 die $LINENO "Can't find package $package in requirements"
374 fi
375 echo $required_pkg
376}
377
Dean Troyer490430d2015-01-30 14:38:35 -0600378# should we use this library from their git repo, or should we let it
379# get pulled in via pip dependencies.
380function use_library_from_git {
381 local name=$1
382 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200383 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600384 return $enabled
385}
386
Sean Daguec71973e2015-09-08 07:12:48 -0400387# determine if a package was installed from git
388function lib_installed_from_git {
389 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800390 local safe_name
391 safe_name=$(python -c "from pkg_resources import safe_name; \
392 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000393 # Note "pip freeze" doesn't always work here, because it tries to
394 # be smart about finding the remote of the git repo the package
395 # was installed from. This doesn't work with zuul which clones
396 # repos with no remote.
397 #
398 # The best option seems to be to use "pip list" which will tell
399 # you the path an editable install was installed from; for example
400 # in response to something like
401 # pip install -e 'git+http://git.openstack.org/openstack-dev/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500402 # pip list --format columns shows
403 # bashate 0.5.2.dev19 /tmp/env/src/bashate
404 # Thus we check the third column to see if we're installed from
405 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800406 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400407}
408
Dean Troyer490430d2015-01-30 14:38:35 -0600409# setup a library by name. If we are trying to use the library from
410# git, we'll do a git based install, otherwise we'll punt and the
411# library should be installed by a requirements pull from another
412# project.
413function setup_lib {
414 local name=$1
415 local dir=${GITDIR[$name]}
416 setup_install $dir
417}
418
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900419# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600420# the library from git, we'll do a git based install, otherwise we'll
421# punt and the library should be installed by a requirements pull from
422# another project.
423#
424# use this for non namespaced libraries
Ian Wienand58243f62018-12-13 14:05:53 +1100425#
426# setup_dev_lib [-bindep] <name>
Dean Troyer490430d2015-01-30 14:38:35 -0600427function setup_dev_lib {
Ian Wienand58243f62018-12-13 14:05:53 +1100428 local bindep
429 if [[ $1 == -bindep* ]]; then
430 bindep="${1}"
431 shift
432 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600433 local name=$1
434 local dir=${GITDIR[$name]}
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000435 if python3_enabled; then
436 # Turn off Python 3 mode and install the package again,
437 # forcing a Python 2 installation. This ensures that all libs
438 # being used for development are installed under both versions
439 # of Python.
440 echo "Installing $name again without Python 3 enabled"
441 USE_PYTHON3=False
Ian Wienand58243f62018-12-13 14:05:53 +1100442 setup_develop $bindep $dir
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000443 USE_PYTHON3=True
444 fi
Ian Wienand58243f62018-12-13 14:05:53 +1100445 setup_develop $bindep $dir
Dean Troyer490430d2015-01-30 14:38:35 -0600446}
447
448# this should be used if you want to install globally, all libraries should
449# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500450#
451# setup_install project_dir [extras]
452# project_dir: directory of project repo (e.g., /opt/stack/keystone)
453# extras: comma-separated list of optional dependencies to install
454# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900455# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Ian Wienand58243f62018-12-13 14:05:53 +1100456# bindep: Set "-bindep" as first argument to install bindep.txt packages
Brant Knudson0842b812015-08-03 13:31:25 -0500457# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600458function setup_install {
Ian Wienand58243f62018-12-13 14:05:53 +1100459 local bindep
460 if [[ $1 == -bindep* ]]; then
461 bindep="${1}"
462 shift
463 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600464 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500465 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100466 _setup_package_with_constraints_edit $bindep $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600467}
468
469# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500470#
471# setup_develop project_dir [extras]
472# project_dir: directory of project repo (e.g., /opt/stack/keystone)
473# extras: comma-separated list of optional dependencies to install
474# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900475# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500476# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600477function setup_develop {
Ian Wienand58243f62018-12-13 14:05:53 +1100478 local bindep
479 if [[ $1 == -bindep* ]]; then
480 bindep="${1}"
481 shift
482 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600483 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500484 local extras=$2
Ian Wienand58243f62018-12-13 14:05:53 +1100485 _setup_package_with_constraints_edit $bindep $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600486}
487
Dean Troyer490430d2015-01-30 14:38:35 -0600488# ``pip install -e`` the package, which processes the dependencies
489# using pip before running `setup.py develop`
490#
Clark Boylan05aa3842015-08-03 11:14:13 -0700491# Updates the constraints from REQUIREMENTS_DIR to reflect the
492# future installed state of this package. This ensures when we
493# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600494#
Clark Boylan05aa3842015-08-03 11:14:13 -0700495# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500496# _setup_package_with_constraints_edit project_dir flags [extras]
497# project_dir: directory of project repo (e.g., /opt/stack/keystone)
498# flags: pip CLI options/flags
499# extras: comma-separated list of optional dependencies to install
500# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900501# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500502# The command is like "pip install <flags> <project_dir>[<extras>]"
503function _setup_package_with_constraints_edit {
Ian Wienand58243f62018-12-13 14:05:53 +1100504 local bindep
505 if [[ $1 == -bindep* ]]; then
506 bindep="${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
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900513 # Normalize the directory name to avoid
514 # "installation from path or url cannot be constrained to a version"
515 # error.
516 # REVISIT(yamamoto): Remove this when fixed in pip.
517 # https://github.com/pypa/pip/pull/3582
518 project_dir=$(cd $project_dir && pwd)
519
Robert Collins635a5ba2015-06-10 08:48:06 +1200520 if [ -n "$REQUIREMENTS_DIR" ]; then
521 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100522 local name
523 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200524 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
525 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
526 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200527 fi
528
Ian Wienand58243f62018-12-13 14:05:53 +1100529 setup_package $bindep $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600530
James E. Blaire1edde32018-03-02 15:05:14 +0000531 # If this project is in LIBS_FROM_GIT, verify it was actually installed
532 # correctly. This helps catch errors caused by constraints mismatches.
533 if use_library_from_git "$project_dir"; then
534 if ! lib_installed_from_git "$project_dir"; then
535 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
536 fi
537 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600538}
539
540# ``pip install -e`` the package, which processes the dependencies
Ian Wienand58243f62018-12-13 14:05:53 +1100541# using pip before running `setup.py develop`. The command is like
542# "pip install <flags> <project_dir>[<extras>]"
Brant Knudson0842b812015-08-03 13:31:25 -0500543#
Dean Troyer490430d2015-01-30 14:38:35 -0600544# Uses globals ``STACK_USER``
Ian Wienand58243f62018-12-13 14:05:53 +1100545#
546# Usage:
547# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
548#
549# -bindep : Use bindep to install dependencies; select extra profiles
550# as comma separated arguments after "="
551# project_dir : directory of project repo (e.g., /opt/stack/keystone)
552# flags : pip CLI options/flags
553# extras : comma-separated list of optional dependencies to install
554# (e.g., ldap,memcache).
555# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Dean Troyer490430d2015-01-30 14:38:35 -0600556function setup_package {
Ian Wienand58243f62018-12-13 14:05:53 +1100557 local bindep=0
558 local bindep_flag=""
559 local bindep_profiles=""
560 if [[ $1 == -bindep* ]]; then
561 bindep=1
562 IFS="=" read bindep_flag bindep_profiles <<< ${1}
563 shift
564 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600565 local project_dir=$1
566 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500567 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600568
Brant Knudson0842b812015-08-03 13:31:25 -0500569 # if the flags variable exists, and it doesn't look like a flag,
570 # assume it's actually the extras list.
571 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
572 extras=$flags
573 flags=""
574 fi
575
576 if [[ ! -z "$extras" ]]; then
577 extras="[$extras]"
578 fi
579
Ian Wienand58243f62018-12-13 14:05:53 +1100580 # install any bindep packages
581 if [[ $bindep == 1 ]]; then
582 install_bindep $project_dir/bindep.txt $bindep_profiles
583 fi
584
Brant Knudson0842b812015-08-03 13:31:25 -0500585 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600586 # ensure that further actions can do things like setup.py sdist
587 if [[ "$flags" == "-e" ]]; then
588 safe_chown -R $STACK_USER $1/*.egg-info
589 fi
590}
591
Doug Hellmannddc38392015-05-07 21:06:24 +0000592# Report whether python 3 should be used
593function python3_enabled {
594 if [[ $USE_PYTHON3 == "True" ]]; then
595 return 0
596 else
597 return 1
598 fi
599}
600
601# Install python3 packages
602function install_python3 {
603 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500604 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700605 elif is_suse; then
606 install_package python3-devel python3-dbm
Doug Hellmannddc38392015-05-07 21:06:24 +0000607 fi
608}
Dean Troyer490430d2015-01-30 14:38:35 -0600609
Sean Daguef80e2cf2017-01-18 15:42:32 -0500610function install_devstack_tools {
611 # intentionally old to ensure devstack-gate has control
612 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
613 install_python3
614 sudo pip3 install -U devstack-tools==${dstools_version}
615}
616
Dean Troyer490430d2015-01-30 14:38:35 -0600617# Restore xtrace
618$INC_PY_TRACE
619
620# Local variables:
621# mode: shell-script
622# End: