blob: e074ea498fc916c9fa20b40b9864c351cc0bc9d2 [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
Victor Stinnerb9891ee2018-01-08 15:20:36 +010052 if python3_enabled && [ "$os_VENDOR" = "Fedora" -a $os_RELEASE -gt 26 ]; then
53 # 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
Monty Taylore46f22d2017-12-03 10:21:26 -0600414 # TODO(mordred) This is a special case for python-openstacksdk, where the
415 # repo name and the pip name do not match. We should either add systemic
416 # support for providing aliases, or we should rename the git repo.
417 if [[ $name == 'python-openstacksdk' ]] ; then
418 name=openstacksdk
419 fi
DamonLi007f5882017-11-23 10:05:46 +0800420 safe_name=$(python -c "from pkg_resources import safe_name; \
421 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000422 # Note "pip freeze" doesn't always work here, because it tries to
423 # be smart about finding the remote of the git repo the package
424 # was installed from. This doesn't work with zuul which clones
425 # repos with no remote.
426 #
427 # The best option seems to be to use "pip list" which will tell
428 # you the path an editable install was installed from; for example
429 # in response to something like
430 # pip install -e 'git+http://git.openstack.org/openstack-dev/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500431 # pip list --format columns shows
432 # bashate 0.5.2.dev19 /tmp/env/src/bashate
433 # Thus we check the third column to see if we're installed from
434 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800435 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400436}
437
438# check that everything that's in LIBS_FROM_GIT was actually installed
439# correctly, this helps double check issues with library fat fingering.
440function check_libs_from_git {
441 local lib=""
442 local not_installed=""
443 for lib in $(echo ${LIBS_FROM_GIT} | tr "," " "); do
444 if ! lib_installed_from_git "$lib"; then
445 not_installed+=" $lib"
446 fi
447 done
448 # if anything is not installed, say what it is.
449 if [[ -n "$not_installed" ]]; then
450 die $LINENO "The following LIBS_FROM_GIT were not installed correct: $not_installed"
451 fi
452}
453
Dean Troyer490430d2015-01-30 14:38:35 -0600454# setup a library by name. If we are trying to use the library from
455# git, we'll do a git based install, otherwise we'll punt and the
456# library should be installed by a requirements pull from another
457# project.
458function setup_lib {
459 local name=$1
460 local dir=${GITDIR[$name]}
461 setup_install $dir
462}
463
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900464# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600465# the library from git, we'll do a git based install, otherwise we'll
466# punt and the library should be installed by a requirements pull from
467# another project.
468#
469# use this for non namespaced libraries
470function setup_dev_lib {
471 local name=$1
472 local dir=${GITDIR[$name]}
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000473 if python3_enabled; then
474 # Turn off Python 3 mode and install the package again,
475 # forcing a Python 2 installation. This ensures that all libs
476 # being used for development are installed under both versions
477 # of Python.
478 echo "Installing $name again without Python 3 enabled"
479 USE_PYTHON3=False
480 setup_develop $dir
481 USE_PYTHON3=True
482 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600483 setup_develop $dir
484}
485
486# this should be used if you want to install globally, all libraries should
487# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500488#
489# setup_install project_dir [extras]
490# project_dir: directory of project repo (e.g., /opt/stack/keystone)
491# extras: comma-separated list of optional dependencies to install
492# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900493# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500494# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600495function setup_install {
496 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500497 local extras=$2
498 _setup_package_with_constraints_edit $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600499}
500
501# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500502#
503# setup_develop project_dir [extras]
504# project_dir: directory of project repo (e.g., /opt/stack/keystone)
505# extras: comma-separated list of optional dependencies to install
506# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900507# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500508# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600509function setup_develop {
510 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500511 local extras=$2
512 _setup_package_with_constraints_edit $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600513}
514
515# determine if a project as specified by directory is in
516# projects.txt. This will not be an exact match because we throw away
517# the namespacing when we clone, but it should be good enough in all
518# practical ways.
519function is_in_projects_txt {
520 local project_dir=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100521 local project_name
522 project_name=$(basename $project_dir)
Ihar Hrachyshka2ba4a722015-06-26 10:45:44 +0200523 grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
Dean Troyer490430d2015-01-30 14:38:35 -0600524}
525
526# ``pip install -e`` the package, which processes the dependencies
527# using pip before running `setup.py develop`
528#
Clark Boylan05aa3842015-08-03 11:14:13 -0700529# Updates the constraints from REQUIREMENTS_DIR to reflect the
530# future installed state of this package. This ensures when we
531# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600532#
Clark Boylan05aa3842015-08-03 11:14:13 -0700533# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500534# _setup_package_with_constraints_edit project_dir flags [extras]
535# project_dir: directory of project repo (e.g., /opt/stack/keystone)
536# flags: pip CLI options/flags
537# extras: comma-separated list of optional dependencies to install
538# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900539# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500540# The command is like "pip install <flags> <project_dir>[<extras>]"
541function _setup_package_with_constraints_edit {
Dean Troyer490430d2015-01-30 14:38:35 -0600542 local project_dir=$1
543 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500544 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600545
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900546 # Normalize the directory name to avoid
547 # "installation from path or url cannot be constrained to a version"
548 # error.
549 # REVISIT(yamamoto): Remove this when fixed in pip.
550 # https://github.com/pypa/pip/pull/3582
551 project_dir=$(cd $project_dir && pwd)
552
Robert Collins635a5ba2015-06-10 08:48:06 +1200553 if [ -n "$REQUIREMENTS_DIR" ]; then
554 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100555 local name
556 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200557 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
558 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
559 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200560 fi
561
Brant Knudson0842b812015-08-03 13:31:25 -0500562 setup_package $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600563
Dean Troyer490430d2015-01-30 14:38:35 -0600564}
565
566# ``pip install -e`` the package, which processes the dependencies
567# using pip before running `setup.py develop`
Brant Knudson0842b812015-08-03 13:31:25 -0500568#
Dean Troyer490430d2015-01-30 14:38:35 -0600569# Uses globals ``STACK_USER``
Brant Knudson0842b812015-08-03 13:31:25 -0500570# setup_package project_dir [flags] [extras]
571# project_dir: directory of project repo (e.g., /opt/stack/keystone)
572# flags: pip CLI options/flags
573# extras: comma-separated list of optional dependencies to install
574# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900575# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500576# The command is like "pip install <flags> <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600577function setup_package {
578 local project_dir=$1
579 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500580 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600581
Brant Knudson0842b812015-08-03 13:31:25 -0500582 # if the flags variable exists, and it doesn't look like a flag,
583 # assume it's actually the extras list.
584 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
585 extras=$flags
586 flags=""
587 fi
588
589 if [[ ! -z "$extras" ]]; then
590 extras="[$extras]"
591 fi
592
593 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600594 # ensure that further actions can do things like setup.py sdist
595 if [[ "$flags" == "-e" ]]; then
596 safe_chown -R $STACK_USER $1/*.egg-info
597 fi
598}
599
Doug Hellmannddc38392015-05-07 21:06:24 +0000600# Report whether python 3 should be used
601function python3_enabled {
602 if [[ $USE_PYTHON3 == "True" ]]; then
603 return 0
604 else
605 return 1
606 fi
607}
608
609# Install python3 packages
610function install_python3 {
611 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500612 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700613 elif is_suse; then
614 install_package python3-devel python3-dbm
Doug Hellmannddc38392015-05-07 21:06:24 +0000615 fi
616}
Dean Troyer490430d2015-01-30 14:38:35 -0600617
Sean Daguef80e2cf2017-01-18 15:42:32 -0500618function install_devstack_tools {
619 # intentionally old to ensure devstack-gate has control
620 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
621 install_python3
622 sudo pip3 install -U devstack-tools==${dstools_version}
623}
624
Dean Troyer490430d2015-01-30 14:38:35 -0600625# Restore xtrace
626$INC_PY_TRACE
627
628# Local variables:
629# mode: shell-script
630# End: