blob: 4bc1856fbd2686a830eb793a3742c11285894dd7 [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
Dean Troyer490430d2015-01-30 14:38:35 -060052 if is_fedora || is_suse; then
53 echo "/usr/bin"
54 else
55 echo "/usr/local/bin"
56 fi
57}
58
Sean Dague60996b12015-04-08 09:06:49 -040059# Wrapper for ``pip install`` that only installs versions of libraries
60# from the global-requirements specification.
61#
62# Uses globals ``REQUIREMENTS_DIR``
63#
64# pip_install_gr packagename
65function pip_install_gr {
66 local name=$1
Ian Wienandada886d2015-10-07 14:06:26 +110067 local clean_name
68 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -040069 pip_install $clean_name
70}
71
Mehdi Abaakouk52b10742016-12-01 16:11:17 +010072# Wrapper for ``pip install`` that only installs versions of libraries
73# from the global-requirements specification with extras.
74#
75# Uses globals ``REQUIREMENTS_DIR``
76#
77# pip_install_gr_extras packagename extra1,extra2,...
78function pip_install_gr_extras {
79 local name=$1
80 local extras=$2
81 local clean_name
82 clean_name=$(get_from_global_requirements $name)
83 pip_install $clean_name[$extras]
84}
85
Doug Hellmannddc38392015-05-07 21:06:24 +000086# Determine the python versions supported by a package
87function get_python_versions_for_package {
88 local name=$1
89 cd $name && python setup.py --classifiers \
90 | grep 'Language' | cut -f5 -d: | grep '\.' | tr '\n' ' '
91}
92
Davanum Srinivasafa8a002016-12-19 09:51:01 -050093# Check for python3 classifier in local directory
94function check_python3_support_for_package_local {
95 local name=$1
96 cd $name
97 set +e
98 classifier=$(python setup.py --classifiers \
99 | grep 'Programming Language :: Python :: 3$')
100 set -e
101 echo $classifier
102}
103
104# Check for python3 classifier on pypi
105function check_python3_support_for_package_remote {
106 local name=$1
107 set +e
108 classifier=$(curl -s -L "https://pypi.python.org/pypi/$name/json" \
109 | grep '"Programming Language :: Python :: 3"')
110 set -e
111 echo $classifier
112}
113
Doug Hellmann94129c72017-01-09 21:24:24 +0000114# python3_enabled_for() checks if the service(s) specified as arguments are
115# enabled by the user in ``ENABLED_PYTHON3_PACKAGES``.
116#
117# Multiple services specified as arguments are ``OR``'ed together; the test
118# is a short-circuit boolean, i.e it returns on the first match.
119#
120# Uses global ``ENABLED_PYTHON3_PACKAGES``
121# python3_enabled_for dir [dir ...]
122function python3_enabled_for {
123 local xtrace
124 xtrace=$(set +o | grep xtrace)
125 set +o xtrace
126
127 local enabled=1
128 local dirs=$@
129 local dir
130 for dir in ${dirs}; do
131 [[ ,${ENABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
132 done
133
134 $xtrace
135 return $enabled
136}
137
138# python3_disabled_for() checks if the service(s) specified as arguments are
139# disabled by the user in ``DISABLED_PYTHON3_PACKAGES``.
140#
141# Multiple services specified as arguments are ``OR``'ed together; the test
142# is a short-circuit boolean, i.e it returns on the first match.
143#
144# Uses global ``DISABLED_PYTHON3_PACKAGES``
145# python3_disabled_for dir [dir ...]
146function python3_disabled_for {
147 local xtrace
148 xtrace=$(set +o | grep xtrace)
149 set +o xtrace
150
151 local enabled=1
152 local dirs=$@
153 local dir
154 for dir in ${dirs}; do
155 [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
156 done
157
158 $xtrace
159 return $enabled
160}
161
162# enable_python3_package() adds the repositories passed as argument to the
163# ``ENABLED_PYTHON3_PACKAGES`` list, if they are not already present.
164#
165# For example:
166# enable_python3_package nova
167#
168# Uses global ``ENABLED_PYTHON3_PACKAGES``
169# enable_python3_package dir [dir ...]
170function enable_python3_package {
171 local xtrace
172 xtrace=$(set +o | grep xtrace)
173 set +o xtrace
174
175 local tmpsvcs="${ENABLED_PYTHON3_PACKAGES}"
176 local python3
177 for dir in $@; do
178 if [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]]; then
179 warn $LINENO "Attempt to enable_python3_package ${dir} when it has been disabled"
180 continue
181 fi
182 if ! python3_enabled_for $dir; then
183 tmpsvcs+=",$dir"
184 fi
185 done
186 ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$tmpsvcs")
187
188 $xtrace
189}
190
191# disable_python3_package() prepares the services passed as argument to be
192# removed from the ``ENABLED_PYTHON3_PACKAGES`` list, if they are present.
193#
194# For example:
195# disable_python3_package swift
196#
197# Uses globals ``ENABLED_PYTHON3_PACKAGES`` and ``DISABLED_PYTHON3_PACKAGES``
198# disable_python3_package dir [dir ...]
199function disable_python3_package {
200 local xtrace
201 xtrace=$(set +o | grep xtrace)
202 set +o xtrace
203
204 local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}"
205 local enabled_svcs=",${ENABLED_PYTHON3_PACKAGES},"
206 local dir
207 for dir in $@; do
208 disabled_svcs+=",$dir"
209 if python3_enabled_for $dir; then
210 enabled_svcs=${enabled_svcs//,$dir,/,}
211 fi
212 done
213 DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs")
214 ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$enabled_svcs")
215
216 $xtrace
217}
218
Dean Troyer490430d2015-01-30 14:38:35 -0600219# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500220# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +1200221# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Dean Troyer490430d2015-01-30 14:38:35 -0600222# pip_install package [package ...]
223function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000224 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100225 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600226 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000227 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600228 local offline=${OFFLINE:-False}
229 if [[ "$offline" == "True" || -z "$@" ]]; then
230 $xtrace
231 return
232 fi
233
Sean Daguecb658fa2015-10-08 17:12:03 -0400234 time_start "pip_install"
235
Chris Dentebdd9ac2015-03-04 12:35:14 +0000236 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
237 if [[ "$PIP_UPGRADE" = "True" ]] ; then
238 upgrade="--upgrade"
239 fi
240
Dean Troyer490430d2015-01-30 14:38:35 -0600241 if [[ -z "$os_PACKAGE" ]]; then
242 GetOSVersion
243 fi
244 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
245 # TRACK_DEPENDS=True installation creates a circular dependency when
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900246 # we attempt to install virtualenv into a virtualenv, so we must global
Dean Troyer490430d2015-01-30 14:38:35 -0600247 # that installation.
248 source $DEST/.venv/bin/activate
249 local cmd_pip=$DEST/.venv/bin/pip
250 local sudo_pip="env"
251 else
Dean Troyer2b564762015-02-11 17:01:02 -0600252 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
253 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
254 local sudo_pip="env"
255 else
Ian Wienandada886d2015-10-07 14:06:26 +1100256 local cmd_pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000257 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600258 local sudo_pip="sudo -H"
Doug Hellmannddc38392015-05-07 21:06:24 +0000259 if python3_enabled; then
260 # Look at the package classifiers to find the python
261 # versions supported, and if we find the version of
262 # python3 we've been told to use, use that instead of the
263 # default pip
264 local package_dir=${!#}
265 local python_versions
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500266
267 # Special case some services that have experimental
268 # support for python3 in progress, but don't claim support
269 # in their classifier
270 echo "Check python version for : $package_dir"
Doug Hellmann94129c72017-01-09 21:24:24 +0000271 if python3_disabled_for ${package_dir##*/}; then
272 echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES"
273 elif python3_enabled_for ${package_dir##*/}; then
274 echo "Explicitly using $PYTHON3_VERSION version to install $package_dir based on ENABLED_PYTHON3_PACKAGES"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500275 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
276 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
277 elif [[ -d "$package_dir" ]]; then
Doug Hellmannddc38392015-05-07 21:06:24 +0000278 python_versions=$(get_python_versions_for_package $package_dir)
279 if [[ $python_versions =~ $PYTHON3_VERSION ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000280 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on classifiers"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500281 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
282 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
283 else
284 # The package may not have yet advertised python3.5
285 # support so check for just python3 classifier and log
286 # a warning.
287 python3_classifier=$(check_python3_support_for_package_local $package_dir)
288 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000289 echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on local package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500290 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
291 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
292 fi
293 fi
294 else
295 # Check pypi as we don't have the package on disk
296 package=$(echo $package_dir | grep -o '^[.a-zA-Z0-9_-]*')
297 python3_classifier=$(check_python3_support_for_package_remote $package)
298 if [[ ! -z "$python3_classifier" ]]; then
Doug Hellmann94129c72017-01-09 21:24:24 +0000299 echo "Automatically using $PYTHON3_VERSION version to install $package based on remote package settings"
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500300 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
Doug Hellmannddc38392015-05-07 21:06:24 +0000301 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
302 fi
303 fi
304 fi
Dean Troyer2b564762015-02-11 17:01:02 -0600305 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600306 fi
307
Robert Collins635a5ba2015-06-10 08:48:06 +1200308 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700309 # Always apply constraints
310 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200311
Doug Hellmannddc38392015-05-07 21:06:24 +0000312 # FIXME(dhellmann): Need to force multiple versions of pip for
313 # packages like setuptools?
Ian Wienandada886d2015-10-07 14:06:26 +1100314 local pip_version
315 pip_version=$(python -c "import pip; \
Dean Troyer490430d2015-01-30 14:38:35 -0600316 print(pip.__version__.strip('.')[0])")
317 if (( pip_version<6 )); then
318 die $LINENO "Currently installed pip version ${pip_version} does not" \
319 "meet minimum requirements (>=6)."
320 fi
321
322 $xtrace
Clark Boylanf266a2d2017-06-12 14:57:59 -0700323
324 # Also install test requirements
325 local install_test_reqs=""
326 local test_req="${!#}/test-requirements.txt"
327 if [[ -e "$test_req" ]]; then
328 install_test_reqs="-r $test_req"
329 fi
330
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200331 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
332 # the same behaviour of setuptools before version 25.0.0.
333 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600334 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800335 http_proxy="${http_proxy:-}" \
336 https_proxy="${https_proxy:-}" \
337 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800338 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200339 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Clark Boylanf266a2d2017-06-12 14:57:59 -0700340 $cmd_pip $upgrade $install_test_reqs \
Dean Troyer490430d2015-01-30 14:38:35 -0600341 $@
Federico Ressie208d062015-11-21 11:15:39 +0000342 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600343
Sean Daguecb658fa2015-10-08 17:12:03 -0400344 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000345 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600346}
347
Sean Daguef28e7ef2017-05-07 22:02:10 -0400348function pip_uninstall {
Sampath Priyankara87d23962017-08-03 16:12:40 +0900349 # Skip uninstall if offline
350 [[ "${OFFLINE}" = "True" ]] && return
351
Sean Daguef28e7ef2017-05-07 22:02:10 -0400352 local name=$1
353 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
354 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
355 local sudo_pip="env"
356 else
357 local cmd_pip
358 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
359 local sudo_pip="sudo -H"
360 fi
361 # don't error if we can't uninstall, it might not be there
Brian Haley954fd1b2017-05-16 12:24:45 -0400362 $sudo_pip $cmd_pip uninstall -y $name || /bin/true
Sean Daguef28e7ef2017-05-07 22:02:10 -0400363}
364
Joe Gordond5ac7852015-02-06 19:29:23 -0800365# get version of a package from global requirements file
366# get_from_global_requirements <package>
367function get_from_global_requirements {
368 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100369 local required_pkg
370 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800371 if [[ $required_pkg == "" ]]; then
372 die $LINENO "Can't find package $package in requirements"
373 fi
374 echo $required_pkg
375}
376
Dean Troyer490430d2015-01-30 14:38:35 -0600377# should we use this library from their git repo, or should we let it
378# get pulled in via pip dependencies.
379function use_library_from_git {
380 local name=$1
381 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200382 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600383 return $enabled
384}
385
Sean Daguec71973e2015-09-08 07:12:48 -0400386# determine if a package was installed from git
387function lib_installed_from_git {
388 local name=$1
Ian Wienandae9c6ab2017-09-29 10:16:47 +1000389 # Note "pip freeze" doesn't always work here, because it tries to
390 # be smart about finding the remote of the git repo the package
391 # was installed from. This doesn't work with zuul which clones
392 # repos with no remote.
393 #
394 # The best option seems to be to use "pip list" which will tell
395 # you the path an editable install was installed from; for example
396 # in response to something like
397 # pip install -e 'git+http://git.openstack.org/openstack-dev/bashate#egg=bashate'
398 # pip list shows
399 # bashate (0.5.2.dev19, /tmp/env/src/bashate)
400 # Thus we look for "path after a comma" to indicate we were
401 # installed from some local place
402 pip list 2>/dev/null | grep -- "$name" | grep -q -- ', .*)$'
Sean Daguec71973e2015-09-08 07:12:48 -0400403}
404
405# check that everything that's in LIBS_FROM_GIT was actually installed
406# correctly, this helps double check issues with library fat fingering.
407function check_libs_from_git {
408 local lib=""
409 local not_installed=""
410 for lib in $(echo ${LIBS_FROM_GIT} | tr "," " "); do
411 if ! lib_installed_from_git "$lib"; then
412 not_installed+=" $lib"
413 fi
414 done
415 # if anything is not installed, say what it is.
416 if [[ -n "$not_installed" ]]; then
417 die $LINENO "The following LIBS_FROM_GIT were not installed correct: $not_installed"
418 fi
419}
420
Dean Troyer490430d2015-01-30 14:38:35 -0600421# setup a library by name. If we are trying to use the library from
422# git, we'll do a git based install, otherwise we'll punt and the
423# library should be installed by a requirements pull from another
424# project.
425function setup_lib {
426 local name=$1
427 local dir=${GITDIR[$name]}
428 setup_install $dir
429}
430
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900431# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600432# the library from git, we'll do a git based install, otherwise we'll
433# punt and the library should be installed by a requirements pull from
434# another project.
435#
436# use this for non namespaced libraries
437function setup_dev_lib {
438 local name=$1
439 local dir=${GITDIR[$name]}
Doug Hellmanna2eb8942017-01-09 22:11:49 +0000440 if python3_enabled; then
441 # Turn off Python 3 mode and install the package again,
442 # forcing a Python 2 installation. This ensures that all libs
443 # being used for development are installed under both versions
444 # of Python.
445 echo "Installing $name again without Python 3 enabled"
446 USE_PYTHON3=False
447 setup_develop $dir
448 USE_PYTHON3=True
449 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600450 setup_develop $dir
451}
452
453# this should be used if you want to install globally, all libraries should
454# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500455#
456# setup_install project_dir [extras]
457# project_dir: directory of project repo (e.g., /opt/stack/keystone)
458# extras: comma-separated list of optional dependencies to install
459# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900460# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500461# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600462function setup_install {
463 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500464 local extras=$2
465 _setup_package_with_constraints_edit $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600466}
467
468# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500469#
470# setup_develop project_dir [extras]
471# project_dir: directory of project repo (e.g., /opt/stack/keystone)
472# extras: comma-separated list of optional dependencies to install
473# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900474# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500475# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600476function setup_develop {
477 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500478 local extras=$2
479 _setup_package_with_constraints_edit $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600480}
481
482# determine if a project as specified by directory is in
483# projects.txt. This will not be an exact match because we throw away
484# the namespacing when we clone, but it should be good enough in all
485# practical ways.
486function is_in_projects_txt {
487 local project_dir=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100488 local project_name
489 project_name=$(basename $project_dir)
Ihar Hrachyshka2ba4a722015-06-26 10:45:44 +0200490 grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
Dean Troyer490430d2015-01-30 14:38:35 -0600491}
492
493# ``pip install -e`` the package, which processes the dependencies
494# using pip before running `setup.py develop`
495#
Clark Boylan05aa3842015-08-03 11:14:13 -0700496# Updates the constraints from REQUIREMENTS_DIR to reflect the
497# future installed state of this package. This ensures when we
498# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600499#
Clark Boylan05aa3842015-08-03 11:14:13 -0700500# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500501# _setup_package_with_constraints_edit project_dir flags [extras]
502# project_dir: directory of project repo (e.g., /opt/stack/keystone)
503# flags: pip CLI options/flags
504# extras: comma-separated list of optional dependencies to install
505# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900506# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500507# The command is like "pip install <flags> <project_dir>[<extras>]"
508function _setup_package_with_constraints_edit {
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
Brant Knudson0842b812015-08-03 13:31:25 -0500529 setup_package $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600530
Dean Troyer490430d2015-01-30 14:38:35 -0600531}
532
533# ``pip install -e`` the package, which processes the dependencies
534# using pip before running `setup.py develop`
Brant Knudson0842b812015-08-03 13:31:25 -0500535#
Dean Troyer490430d2015-01-30 14:38:35 -0600536# Uses globals ``STACK_USER``
Brant Knudson0842b812015-08-03 13:31:25 -0500537# setup_package project_dir [flags] [extras]
538# project_dir: directory of project repo (e.g., /opt/stack/keystone)
539# flags: pip CLI options/flags
540# extras: comma-separated list of optional dependencies to install
541# (e.g., ldap,memcache).
Takashi NATSUMEfa007772017-07-22 08:59:43 +0900542# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
Brant Knudson0842b812015-08-03 13:31:25 -0500543# The command is like "pip install <flags> <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600544function setup_package {
545 local project_dir=$1
546 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500547 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600548
Brant Knudson0842b812015-08-03 13:31:25 -0500549 # if the flags variable exists, and it doesn't look like a flag,
550 # assume it's actually the extras list.
551 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
552 extras=$flags
553 flags=""
554 fi
555
556 if [[ ! -z "$extras" ]]; then
557 extras="[$extras]"
558 fi
559
560 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600561 # ensure that further actions can do things like setup.py sdist
562 if [[ "$flags" == "-e" ]]; then
563 safe_chown -R $STACK_USER $1/*.egg-info
564 fi
565}
566
Doug Hellmannddc38392015-05-07 21:06:24 +0000567# Report whether python 3 should be used
568function python3_enabled {
569 if [[ $USE_PYTHON3 == "True" ]]; then
570 return 0
571 else
572 return 1
573 fi
574}
575
576# Install python3 packages
577function install_python3 {
578 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500579 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Armando Migliacciobacfb942017-03-20 22:27:20 -0700580 elif is_suse; then
581 install_package python3-devel python3-dbm
Doug Hellmannddc38392015-05-07 21:06:24 +0000582 fi
583}
Dean Troyer490430d2015-01-30 14:38:35 -0600584
Sean Daguef80e2cf2017-01-18 15:42:32 -0500585function install_devstack_tools {
586 # intentionally old to ensure devstack-gate has control
587 local dstools_version=${DSTOOLS_VERSION:-0.1.2}
588 install_python3
589 sudo pip3 install -U devstack-tools==${dstools_version}
590}
591
Dean Troyer490430d2015-01-30 14:38:35 -0600592# Restore xtrace
593$INC_PY_TRACE
594
595# Local variables:
596# mode: shell-script
597# End: