blob: f1df10193991302b984f0e9c19b844dee1527ffa [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 Hellmann36377f62018-12-04 11:33:03 -0500118# python3_enabled_for() assumes the service(s) specified as arguments are
119# enabled for python 3 unless explicitly disabled. See python3_disabled_for().
Doug Hellmann94129c72017-01-09 21:24:24 +0000120#
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#
Doug Hellmann94129c72017-01-09 21:24:24 +0000124# python3_enabled_for dir [dir ...]
125function python3_enabled_for {
126 local xtrace
127 xtrace=$(set +o | grep xtrace)
128 set +o xtrace
129
130 local enabled=1
131 local dirs=$@
132 local dir
133 for dir in ${dirs}; do
Doug Hellmann36377f62018-12-04 11:33:03 -0500134 if ! python3_disabled_for "${dir}"; then
135 enabled=0
136 fi
Doug Hellmann94129c72017-01-09 21:24:24 +0000137 done
138
139 $xtrace
140 return $enabled
141}
142
143# python3_disabled_for() checks if the service(s) specified as arguments are
144# disabled by the user in ``DISABLED_PYTHON3_PACKAGES``.
145#
146# Multiple services specified as arguments are ``OR``'ed together; the test
147# is a short-circuit boolean, i.e it returns on the first match.
148#
149# Uses global ``DISABLED_PYTHON3_PACKAGES``
150# python3_disabled_for dir [dir ...]
151function python3_disabled_for {
152 local xtrace
153 xtrace=$(set +o | grep xtrace)
154 set +o xtrace
155
156 local enabled=1
157 local dirs=$@
158 local dir
159 for dir in ${dirs}; do
160 [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
161 done
162
163 $xtrace
164 return $enabled
165}
166
Doug Hellmann36377f62018-12-04 11:33:03 -0500167# enable_python3_package() -- no-op for backwards compatibility
Doug Hellmann94129c72017-01-09 21:24:24 +0000168#
169# For example:
170# enable_python3_package nova
171#
Doug Hellmann94129c72017-01-09 21:24:24 +0000172# enable_python3_package dir [dir ...]
173function enable_python3_package {
174 local xtrace
175 xtrace=$(set +o | grep xtrace)
176 set +o xtrace
177
Doug Hellmann36377f62018-12-04 11:33:03 -0500178 echo "It is no longer necessary to call enable_python3_package()."
Doug Hellmann94129c72017-01-09 21:24:24 +0000179
180 $xtrace
181}
182
Doug Hellmann36377f62018-12-04 11:33:03 -0500183# disable_python3_package() adds the services passed as argument to
184# the ``DISABLED_PYTHON3_PACKAGES`` list.
Doug Hellmann94129c72017-01-09 21:24:24 +0000185#
186# For example:
187# disable_python3_package swift
188#
Doug Hellmann36377f62018-12-04 11:33:03 -0500189# Uses global ``DISABLED_PYTHON3_PACKAGES``
Doug Hellmann94129c72017-01-09 21:24:24 +0000190# disable_python3_package dir [dir ...]
191function disable_python3_package {
192 local xtrace
193 xtrace=$(set +o | grep xtrace)
194 set +o xtrace
195
196 local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}"
Doug Hellmann94129c72017-01-09 21:24:24 +0000197 local dir
198 for dir in $@; do
199 disabled_svcs+=",$dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000200 done
201 DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs")
Doug Hellmann94129c72017-01-09 21:24:24 +0000202
203 $xtrace
204}
205
Dean Troyer490430d2015-01-30 14:38:35 -0600206# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500207# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +1200208# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Zane Bitter9e7ead92017-10-05 16:51:09 -0400209# Usage:
210# pip_install pip_arguments
Dean Troyer490430d2015-01-30 14:38:35 -0600211function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000212 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100213 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600214 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000215 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600216 local offline=${OFFLINE:-False}
217 if [[ "$offline" == "True" || -z "$@" ]]; then
218 $xtrace
219 return
220 fi
221
Sean Daguecb658fa2015-10-08 17:12:03 -0400222 time_start "pip_install"
223
Chris Dentebdd9ac2015-03-04 12:35:14 +0000224 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
225 if [[ "$PIP_UPGRADE" = "True" ]] ; then
226 upgrade="--upgrade"
227 fi
228
Dean Troyer490430d2015-01-30 14:38:35 -0600229 if [[ -z "$os_PACKAGE" ]]; then
230 GetOSVersion
231 fi
Zane Bitter9e7ead92017-10-05 16:51:09 -0400232
233 # Try to extract the path of the package we are installing into
234 # package_dir. We need this to check for test-requirements.txt,
235 # at least.
236 #
237 # ${!#} expands to the last positional argument to this function.
238 # With "extras" syntax included, our arguments might be something
239 # like:
240 # -e /path/to/fooproject[extra]
241 # Thus this magic line grabs just the path without extras
242 #
243 # Note that this makes no sense if this is a pypi (rather than
244 # local path) install; ergo you must check this path exists before
245 # use. Also, if we had multiple or mixed installs, we would also
246 # likely break. But for historical reasons, it's basically only
247 # the other wrapper functions in here calling this to install
248 # local packages, and they do so with single call per install. So
249 # this works (for now...)
250 local package_dir=${!#%\[*\]}
251
Dean Troyer490430d2015-01-30 14:38:35 -0600252 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
253 # TRACK_DEPENDS=True installation creates a circular dependency when
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900254 # we attempt to install virtualenv into a virtualenv, so we must global
Dean Troyer490430d2015-01-30 14:38:35 -0600255 # that installation.
256 source $DEST/.venv/bin/activate
257 local cmd_pip=$DEST/.venv/bin/pip
258 local sudo_pip="env"
259 else
Dean Troyer2b564762015-02-11 17:01:02 -0600260 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
261 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
262 local sudo_pip="env"
263 else
Ian Wienandada886d2015-10-07 14:06:26 +1100264 local cmd_pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000265 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600266 local sudo_pip="sudo -H"
Doug Hellmannddc38392015-05-07 21:06:24 +0000267 if python3_enabled; then
268 # Look at the package classifiers to find the python
269 # versions supported, and if we find the version of
270 # python3 we've been told to use, use that instead of the
271 # default pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000272 local python_versions
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500273
274 # Special case some services that have experimental
275 # support for python3 in progress, but don't claim support
276 # in their classifier
277 echo "Check python version for : $package_dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000278 if python3_disabled_for ${package_dir##*/}; then
279 echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES"
280 elif python3_enabled_for ${package_dir##*/}; then
Doug Hellmann36377f62018-12-04 11:33:03 -0500281 echo "Using $PYTHON3_VERSION version to install $package_dir based on default behavior"
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 elif [[ -d "$package_dir" ]]; then
Doug Hellmannddc38392015-05-07 21:06:24 +0000285 python_versions=$(get_python_versions_for_package $package_dir)
286 if [[ $python_versions =~ $PYTHON3_VERSION ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000287 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on classifiers"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500288 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
289 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
290 else
291 # The package may not have yet advertised python3.5
292 # support so check for just python3 classifier and log
293 # a warning.
294 python3_classifier=$(check_python3_support_for_package_local $package_dir)
295 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000296 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on local package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500297 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
298 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
299 fi
300 fi
301 else
302 # Check pypi as we don't have the package on disk
303 package=$(echo $package_dir | grep -o '^[.a-zA-Z0-9_-]*')
304 python3_classifier=$(check_python3_support_for_package_remote $package)
305 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000306 echo "Automatically using $PYTHON3_VERSION version to install $package based on remote package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500307 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
Doug Hellmannddc38392015-05-07 21:06:24 +0000308 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
309 fi
310 fi
311 fi
Dean Troyer2b564762015-02-11 17:01:02 -0600312 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600313 fi
314
Robert Collins635a5ba2015-06-10 08:48:06 +1200315 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700316 # Always apply constraints
317 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200318
Doug Hellmannddc38392015-05-07 21:06:24 +0000319 # FIXME(dhellmann): Need to force multiple versions of pip for
320 # packages like setuptools?
Ian Wienandada886d2015-10-07 14:06:26 +1100321 local pip_version
322 pip_version=$(python -c "import pip; \
Clark Boylan06577952017-10-20 12:14:29 -0700323 print(pip.__version__.split('.')[0])")
Dean Troyer490430d2015-01-30 14:38:35 -0600324 if (( pip_version<6 )); then
325 die $LINENO "Currently installed pip version ${pip_version} does not" \
326 "meet minimum requirements (>=6)."
327 fi
328
329 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700330
331 # Also install test requirements
332 local install_test_reqs=""
Zane Bitter9e7ead92017-10-05 16:51:09 -0400333 local test_req="${package_dir}/test-requirements.txt"
Clark Boylanf266a2d2017-06-12 14:57:59 -0700334 if [[ -e "$test_req" ]]; then
335 install_test_reqs="-r $test_req"
336 fi
337
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200338 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
339 # the same behaviour of setuptools before version 25.0.0.
340 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600341 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800342 http_proxy="${http_proxy:-}" \
343 https_proxy="${https_proxy:-}" \
344 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800345 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200346 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Clark Boylanf266a2d2017-06-12 14:57:59 -0700347 $cmd_pip $upgrade $install_test_reqs \
Dean Troyer490430d2015-01-30 14:38:35 -0600348 $@
Federico Ressie208d062015-11-21 11:15:39 +0000349 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600350
Sean Daguecb658fa2015-10-08 17:12:03 -0400351 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000352 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600353}
354
Sean Daguef28e7ef2017-05-07 22:02:10 -0400355function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900356 # Skip uninstall if offline
357 [[ "${OFFLINE}" = "True" ]] && return
358
Sean Daguef28e7ef2017-05-07 22:02:10 -0400359 local name=$1
360 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
361 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
362 local sudo_pip="env"
363 else
364 local cmd_pip
365 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
366 local sudo_pip="sudo -H"
367 fi
368 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400369 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400370}
371
Joe Gordond5ac7852015-02-06 19:29:23 -0800372# get version of a package from global requirements file
373# get_from_global_requirements <package>
374function get_from_global_requirements {
375 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100376 local required_pkg
377 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800378 if [[ $required_pkg == "" ]]; then
379 die $LINENO "Can't find package $package in requirements"
380 fi
381 echo $required_pkg
382}
383
Dean Troyer490430d2015-01-30 14:38:35 -0600384# should we use this library from their git repo, or should we let it
385# get pulled in via pip dependencies.
386function use_library_from_git {
387 local name=$1
388 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200389 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600390 return $enabled
391}
392
Sean Daguec71973e2015-09-08 07:12:48 -0400393# determine if a package was installed from git
394function lib_installed_from_git {
395 local name=$1
DamonLi007f5882017-11-23 10:05:46 +0800396 local safe_name
397 safe_name=$(python -c "from pkg_resources import safe_name; \
398 print(safe_name('${name}'))")
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000399 # Note "pip freeze" doesn't always work here, because it tries to
400 # be smart about finding the remote of the git repo the package
401 # was installed from. This doesn't work with zuul which clones
402 # repos with no remote.
403 #
404 # The best option seems to be to use "pip list" which will tell
405 # you the path an editable install was installed from; for example
406 # in response to something like
407 # pip install -e 'git+http://git.openstack.org/openstack-dev/bashate#egg=bashate'
Monty Taylorf0cd9a82017-10-06 13:11:48 -0500408 # pip list --format columns shows
409 # bashate 0.5.2.dev19 /tmp/env/src/bashate
410 # Thus we check the third column to see if we're installed from
411 # some local place.
DamonLi007f5882017-11-23 10:05:46 +0800412 [[ -n $(pip list --format=columns 2>/dev/null | awk "/^$safe_name/ {print \$3}") ]]
Sean Daguec71973e2015-09-08 07:12:48 -0400413}
414
Dean Troyer490430d2015-01-30 14:38:35 -0600415# setup a library by name. If we are trying to use the library from
416# git, we'll do a git based install, otherwise we'll punt and the
417# library should be installed by a requirements pull from another
418# project.
419function setup_lib {
420 local name=$1
421 local dir=${GITDIR[$name]}
422 setup_install $dir
423}
424
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900425# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600426# the library from git, we'll do a git based install, otherwise we'll
427# punt and the library should be installed by a requirements pull from
428# another project.
429#
430# use this for non namespaced libraries
431function setup_dev_lib {
432 local name=$1
433 local dir=${GITDIR[$name]}
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000434 if python3_enabled; then
435 # Turn off Python 3 mode and install the package again,
436 # forcing a Python 2 installation. This ensures that all libs
437 # being used for development are installed under both versions
438 # of Python.
439 echo "Installing $name again without Python 3 enabled"
440 USE_PYTHON3=False
441 setup_develop $dir
442 USE_PYTHON3=True
443 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600444 setup_develop $dir
445}
446
447# this should be used if you want to install globally, all libraries should
448# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500449#
450# setup_install project_dir [extras]
451# project_dir: directory of project repo (e.g., /opt/stack/keystone)
452# extras: comma-separated list of optional dependencies to install
453# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900454# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500455# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600456function setup_install {
457 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500458 local extras=$2
459 _setup_package_with_constraints_edit $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600460}
461
462# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500463#
464# setup_develop project_dir [extras]
465# project_dir: directory of project repo (e.g., /opt/stack/keystone)
466# extras: comma-separated list of optional dependencies to install
467# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900468# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500469# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600470function setup_develop {
471 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500472 local extras=$2
473 _setup_package_with_constraints_edit $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600474}
475
Dean Troyer490430d2015-01-30 14:38:35 -0600476# ``pip install -e`` the package, which processes the dependencies
477# using pip before running `setup.py develop`
478#
Clark Boylan05aa3842015-08-03 11:14:13 -0700479# Updates the constraints from REQUIREMENTS_DIR to reflect the
480# future installed state of this package. This ensures when we
481# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600482#
Clark Boylan05aa3842015-08-03 11:14:13 -0700483# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500484# _setup_package_with_constraints_edit project_dir flags [extras]
485# project_dir: directory of project repo (e.g., /opt/stack/keystone)
486# flags: pip CLI options/flags
487# extras: comma-separated list of optional dependencies to install
488# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900489# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500490# The command is like "pip install <flags> <project_dir>[<extras>]"
491function _setup_package_with_constraints_edit {
Dean Troyer490430d2015-01-30 14:38:35 -0600492 local project_dir=$1
493 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500494 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600495
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900496 # Normalize the directory name to avoid
497 # "installation from path or url cannot be constrained to a version"
498 # error.
499 # REVISIT(yamamoto): Remove this when fixed in pip.
500 # https://github.com/pypa/pip/pull/3582
501 project_dir=$(cd $project_dir && pwd)
502
Robert Collins635a5ba2015-06-10 08:48:06 +1200503 if [ -n "$REQUIREMENTS_DIR" ]; then
504 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100505 local name
506 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200507 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
508 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
509 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200510 fi
511
Brant Knudson0842b812015-08-03 13:31:25 -0500512 setup_package $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600513
James E. Blaire1edde32018-03-02 15:05:14 +0000514 # If this project is in LIBS_FROM_GIT, verify it was actually installed
515 # correctly. This helps catch errors caused by constraints mismatches.
516 if use_library_from_git "$project_dir"; then
517 if ! lib_installed_from_git "$project_dir"; then
518 die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
519 fi
520 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600521}
522
523# ``pip install -e`` the package, which processes the dependencies
524# using pip before running `setup.py develop`
Brant Knudson0842b812015-08-03 13:31:25 -0500525#
Dean Troyer490430d2015-01-30 14:38:35 -0600526# Uses globals ``STACK_USER``
Brant Knudson0842b812015-08-03 13:31:25 -0500527# setup_package project_dir [flags] [extras]
528# project_dir: directory of project repo (e.g., /opt/stack/keystone)
529# flags: pip CLI options/flags
530# extras: comma-separated list of optional dependencies to install
531# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900532# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500533# The command is like "pip install <flags> <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600534function setup_package {
535 local project_dir=$1
536 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500537 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600538
Brant Knudson0842b812015-08-03 13:31:25 -0500539 # if the flags variable exists, and it doesn't look like a flag,
540 # assume it's actually the extras list.
541 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
542 extras=$flags
543 flags=""
544 fi
545
546 if [[ ! -z "$extras" ]]; then
547 extras="[$extras]"
548 fi
549
550 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600551 # ensure that further actions can do things like setup.py sdist
552 if [[ "$flags" == "-e" ]]; then
553 safe_chown -R $STACK_USER $1/*.egg-info
554 fi
555}
556
Doug Hellmannddc38392015-05-07 21:06:24 +0000557# Report whether python 3 should be used
558function python3_enabled {
559 if [[ $USE_PYTHON3 == "True" ]]; then
560 return 0
561 else
562 return 1
563 fi
564}
565
566# Install python3 packages
567function install_python3 {
568 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500569 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700570 elif is_suse; then
571 install_package python3-devel python3-dbm
Doug Hellmannddc38392015-05-07 21:06:24 +0000572 fi
573}
Dean Troyer490430d2015-01-30 14:38:35 -0600574
Sean Daguef80e2cf2017-01-18 15:42:32 -0500575function install_devstack_tools {
576 # intentionally old to ensure devstack-gate has control
577 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
578 install_python3
579 sudo pip3 install -U devstack-tools==${dstools_version}
580}
581
Dean Troyer490430d2015-01-30 14:38:35 -0600582# Restore xtrace
583$INC_PY_TRACE
584
585# Local variables:
586# mode: shell-script
587# End: