blob: 04cde34fe1a52fc8f784befbdbf4504f55837bd8 [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.
22declare -A PROJECT_VENV
23
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
Dean Troyer490430d2015-01-30 14:38:35 -0600114# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -0500115# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +1200116# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Dean Troyer490430d2015-01-30 14:38:35 -0600117# pip_install package [package ...]
118function pip_install {
Federico Ressie208d062015-11-21 11:15:39 +0000119 local xtrace result
Ian Wienand433a9b12015-10-07 13:29:31 +1100120 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -0600121 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +0000122 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -0600123 local offline=${OFFLINE:-False}
124 if [[ "$offline" == "True" || -z "$@" ]]; then
125 $xtrace
126 return
127 fi
128
Sean Daguecb658fa2015-10-08 17:12:03 -0400129 time_start "pip_install"
130
Chris Dentebdd9ac2015-03-04 12:35:14 +0000131 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
132 if [[ "$PIP_UPGRADE" = "True" ]] ; then
133 upgrade="--upgrade"
134 fi
135
Dean Troyer490430d2015-01-30 14:38:35 -0600136 if [[ -z "$os_PACKAGE" ]]; then
137 GetOSVersion
138 fi
139 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
140 # TRACK_DEPENDS=True installation creates a circular dependency when
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900141 # we attempt to install virtualenv into a virtualenv, so we must global
Dean Troyer490430d2015-01-30 14:38:35 -0600142 # that installation.
143 source $DEST/.venv/bin/activate
144 local cmd_pip=$DEST/.venv/bin/pip
145 local sudo_pip="env"
146 else
Dean Troyer2b564762015-02-11 17:01:02 -0600147 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
148 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
149 local sudo_pip="env"
150 else
Ian Wienandada886d2015-10-07 14:06:26 +1100151 local cmd_pip
Doug Hellmannddc38392015-05-07 21:06:24 +0000152 cmd_pip=$(get_pip_command $PYTHON2_VERSION)
Dean Troyer2b564762015-02-11 17:01:02 -0600153 local sudo_pip="sudo -H"
Doug Hellmannddc38392015-05-07 21:06:24 +0000154 if python3_enabled; then
155 # Look at the package classifiers to find the python
156 # versions supported, and if we find the version of
157 # python3 we've been told to use, use that instead of the
158 # default pip
159 local package_dir=${!#}
160 local python_versions
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500161
162 # Special case some services that have experimental
163 # support for python3 in progress, but don't claim support
164 # in their classifier
165 echo "Check python version for : $package_dir"
Davanum Srinivas0c0d8482017-01-03 08:52:25 -0500166 if [[ ${package_dir##*/} == "nova" || ${package_dir##*/} == "glance" || \
167 ${package_dir##*/} == "cinder" || ${package_dir##*/} == "swift" || \
168 ${package_dir##*/} == "uwsgi" ]]; then
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500169 echo "Using $PYTHON3_VERSION version to install $package_dir"
170 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
171 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
172 elif [[ -d "$package_dir" ]]; then
Doug Hellmannddc38392015-05-07 21:06:24 +0000173 python_versions=$(get_python_versions_for_package $package_dir)
174 if [[ $python_versions =~ $PYTHON3_VERSION ]]; then
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500175 echo "Using $PYTHON3_VERSION version to install $package_dir"
176 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
177 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
178 else
179 # The package may not have yet advertised python3.5
180 # support so check for just python3 classifier and log
181 # a warning.
182 python3_classifier=$(check_python3_support_for_package_local $package_dir)
183 if [[ ! -z "$python3_classifier" ]]; then
184 echo "Using $PYTHON3_VERSION version to install $package_dir"
185 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
186 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
187 fi
188 fi
189 else
190 # Check pypi as we don't have the package on disk
191 package=$(echo $package_dir | grep -o '^[.a-zA-Z0-9_-]*')
192 python3_classifier=$(check_python3_support_for_package_remote $package)
193 if [[ ! -z "$python3_classifier" ]]; then
194 echo "Using $PYTHON3_VERSION version to install $package"
195 sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
Doug Hellmannddc38392015-05-07 21:06:24 +0000196 cmd_pip=$(get_pip_command $PYTHON3_VERSION)
197 fi
198 fi
199 fi
Dean Troyer2b564762015-02-11 17:01:02 -0600200 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600201 fi
202
Robert Collins635a5ba2015-06-10 08:48:06 +1200203 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700204 # Always apply constraints
205 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200206
Doug Hellmannddc38392015-05-07 21:06:24 +0000207 # FIXME(dhellmann): Need to force multiple versions of pip for
208 # packages like setuptools?
Ian Wienandada886d2015-10-07 14:06:26 +1100209 local pip_version
210 pip_version=$(python -c "import pip; \
Dean Troyer490430d2015-01-30 14:38:35 -0600211 print(pip.__version__.strip('.')[0])")
212 if (( pip_version<6 )); then
213 die $LINENO "Currently installed pip version ${pip_version} does not" \
214 "meet minimum requirements (>=6)."
215 fi
216
217 $xtrace
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200218 # adding SETUPTOOLS_SYS_PATH_TECHNIQUE is a workaround to keep
219 # the same behaviour of setuptools before version 25.0.0.
220 # related issue: https://github.com/pypa/pip/issues/3874
Dean Troyer490430d2015-01-30 14:38:35 -0600221 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800222 http_proxy="${http_proxy:-}" \
223 https_proxy="${https_proxy:-}" \
224 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800225 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Spyros Trigazis88ccd472016-07-24 22:13:57 +0200226 SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite \
Robert Collins635a5ba2015-06-10 08:48:06 +1200227 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600228 $@
Federico Ressie208d062015-11-21 11:15:39 +0000229 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600230
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400231 # Also install test requirements
Sirushti Murugesan713fd2f2015-09-30 15:12:50 +0530232 local test_req="${!#}/test-requirements.txt"
Federico Ressie208d062015-11-21 11:15:39 +0000233 if [[ $result == 0 ]] && [[ -e "$test_req" ]]; then
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400234 echo "Installing test-requirements for $test_req"
235 $sudo_pip \
236 http_proxy=${http_proxy:-} \
237 https_proxy=${https_proxy:-} \
238 no_proxy=${no_proxy:-} \
239 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200240 $cmd_pip $upgrade \
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400241 -r $test_req
Federico Ressie208d062015-11-21 11:15:39 +0000242 result=$?
Dean Troyer490430d2015-01-30 14:38:35 -0600243 fi
Sean Daguecb658fa2015-10-08 17:12:03 -0400244
245 time_stop "pip_install"
Federico Ressie208d062015-11-21 11:15:39 +0000246 return $result
Dean Troyer490430d2015-01-30 14:38:35 -0600247}
248
Joe Gordond5ac7852015-02-06 19:29:23 -0800249# get version of a package from global requirements file
250# get_from_global_requirements <package>
251function get_from_global_requirements {
252 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100253 local required_pkg
254 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800255 if [[ $required_pkg == "" ]]; then
256 die $LINENO "Can't find package $package in requirements"
257 fi
258 echo $required_pkg
259}
260
Dean Troyer490430d2015-01-30 14:38:35 -0600261# should we use this library from their git repo, or should we let it
262# get pulled in via pip dependencies.
263function use_library_from_git {
264 local name=$1
265 local enabled=1
Marc Koderer46f8cb72016-05-13 09:08:16 +0200266 [[ ${LIBS_FROM_GIT} = 'ALL' ]] || [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
Dean Troyer490430d2015-01-30 14:38:35 -0600267 return $enabled
268}
269
Sean Daguec71973e2015-09-08 07:12:48 -0400270# determine if a package was installed from git
271function lib_installed_from_git {
272 local name=$1
273 pip freeze 2>/dev/null | grep -- "$name" | grep -q -- '-e git'
274}
275
276# check that everything that's in LIBS_FROM_GIT was actually installed
277# correctly, this helps double check issues with library fat fingering.
278function check_libs_from_git {
279 local lib=""
280 local not_installed=""
281 for lib in $(echo ${LIBS_FROM_GIT} | tr "," " "); do
282 if ! lib_installed_from_git "$lib"; then
283 not_installed+=" $lib"
284 fi
285 done
286 # if anything is not installed, say what it is.
287 if [[ -n "$not_installed" ]]; then
288 die $LINENO "The following LIBS_FROM_GIT were not installed correct: $not_installed"
289 fi
290}
291
Dean Troyer490430d2015-01-30 14:38:35 -0600292# setup a library by name. If we are trying to use the library from
293# git, we'll do a git based install, otherwise we'll punt and the
294# library should be installed by a requirements pull from another
295# project.
296function setup_lib {
297 local name=$1
298 local dir=${GITDIR[$name]}
299 setup_install $dir
300}
301
Atsushi SAKAI5509ed52015-11-30 20:20:21 +0900302# setup a library by name in editable mode. If we are trying to use
Dean Troyer490430d2015-01-30 14:38:35 -0600303# the library from git, we'll do a git based install, otherwise we'll
304# punt and the library should be installed by a requirements pull from
305# another project.
306#
307# use this for non namespaced libraries
308function setup_dev_lib {
309 local name=$1
310 local dir=${GITDIR[$name]}
311 setup_develop $dir
312}
313
314# this should be used if you want to install globally, all libraries should
315# use this, especially *oslo* ones
Brant Knudson0842b812015-08-03 13:31:25 -0500316#
317# setup_install project_dir [extras]
318# project_dir: directory of project repo (e.g., /opt/stack/keystone)
319# extras: comma-separated list of optional dependencies to install
320# (e.g., ldap,memcache).
321# See http://docs.openstack.org/developer/pbr/#extra-requirements
322# The command is like "pip install <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600323function setup_install {
324 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500325 local extras=$2
326 _setup_package_with_constraints_edit $project_dir "" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600327}
328
329# this should be used for projects which run services, like all services
Brant Knudson0842b812015-08-03 13:31:25 -0500330#
331# setup_develop project_dir [extras]
332# project_dir: directory of project repo (e.g., /opt/stack/keystone)
333# extras: comma-separated list of optional dependencies to install
334# (e.g., ldap,memcache).
335# See http://docs.openstack.org/developer/pbr/#extra-requirements
336# The command is like "pip install -e <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600337function setup_develop {
338 local project_dir=$1
Brant Knudson0842b812015-08-03 13:31:25 -0500339 local extras=$2
340 _setup_package_with_constraints_edit $project_dir -e $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600341}
342
343# determine if a project as specified by directory is in
344# projects.txt. This will not be an exact match because we throw away
345# the namespacing when we clone, but it should be good enough in all
346# practical ways.
347function is_in_projects_txt {
348 local project_dir=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100349 local project_name
350 project_name=$(basename $project_dir)
Ihar Hrachyshka2ba4a722015-06-26 10:45:44 +0200351 grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
Dean Troyer490430d2015-01-30 14:38:35 -0600352}
353
354# ``pip install -e`` the package, which processes the dependencies
355# using pip before running `setup.py develop`
356#
Clark Boylan05aa3842015-08-03 11:14:13 -0700357# Updates the constraints from REQUIREMENTS_DIR to reflect the
358# future installed state of this package. This ensures when we
359# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600360#
Clark Boylan05aa3842015-08-03 11:14:13 -0700361# Uses globals ``REQUIREMENTS_DIR``
Brant Knudson0842b812015-08-03 13:31:25 -0500362# _setup_package_with_constraints_edit project_dir flags [extras]
363# project_dir: directory of project repo (e.g., /opt/stack/keystone)
364# flags: pip CLI options/flags
365# extras: comma-separated list of optional dependencies to install
366# (e.g., ldap,memcache).
367# See http://docs.openstack.org/developer/pbr/#extra-requirements
368# The command is like "pip install <flags> <project_dir>[<extras>]"
369function _setup_package_with_constraints_edit {
Dean Troyer490430d2015-01-30 14:38:35 -0600370 local project_dir=$1
371 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500372 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600373
YAMAMOTO Takashic8c1c612016-03-22 14:29:47 +0900374 # Normalize the directory name to avoid
375 # "installation from path or url cannot be constrained to a version"
376 # error.
377 # REVISIT(yamamoto): Remove this when fixed in pip.
378 # https://github.com/pypa/pip/pull/3582
379 project_dir=$(cd $project_dir && pwd)
380
Robert Collins635a5ba2015-06-10 08:48:06 +1200381 if [ -n "$REQUIREMENTS_DIR" ]; then
382 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100383 local name
384 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200385 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
386 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
387 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200388 fi
389
Brant Knudson0842b812015-08-03 13:31:25 -0500390 setup_package $project_dir "$flags" $extras
Dean Troyer490430d2015-01-30 14:38:35 -0600391
Dean Troyer490430d2015-01-30 14:38:35 -0600392}
393
394# ``pip install -e`` the package, which processes the dependencies
395# using pip before running `setup.py develop`
Brant Knudson0842b812015-08-03 13:31:25 -0500396#
Dean Troyer490430d2015-01-30 14:38:35 -0600397# Uses globals ``STACK_USER``
Brant Knudson0842b812015-08-03 13:31:25 -0500398# setup_package project_dir [flags] [extras]
399# project_dir: directory of project repo (e.g., /opt/stack/keystone)
400# flags: pip CLI options/flags
401# extras: comma-separated list of optional dependencies to install
402# (e.g., ldap,memcache).
403# See http://docs.openstack.org/developer/pbr/#extra-requirements
404# The command is like "pip install <flags> <project_dir>[<extras>]"
Dean Troyer490430d2015-01-30 14:38:35 -0600405function setup_package {
406 local project_dir=$1
407 local flags=$2
Brant Knudson0842b812015-08-03 13:31:25 -0500408 local extras=$3
Dean Troyer490430d2015-01-30 14:38:35 -0600409
Brant Knudson0842b812015-08-03 13:31:25 -0500410 # if the flags variable exists, and it doesn't look like a flag,
411 # assume it's actually the extras list.
412 if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
413 extras=$flags
414 flags=""
415 fi
416
417 if [[ ! -z "$extras" ]]; then
418 extras="[$extras]"
419 fi
420
421 pip_install $flags "$project_dir$extras"
Dean Troyer490430d2015-01-30 14:38:35 -0600422 # ensure that further actions can do things like setup.py sdist
423 if [[ "$flags" == "-e" ]]; then
424 safe_chown -R $STACK_USER $1/*.egg-info
425 fi
426}
427
Doug Hellmannddc38392015-05-07 21:06:24 +0000428# Report whether python 3 should be used
429function python3_enabled {
430 if [[ $USE_PYTHON3 == "True" ]]; then
431 return 0
432 else
433 return 1
434 fi
435}
436
437# Install python3 packages
438function install_python3 {
439 if is_ubuntu; then
Lubosz "diltram" Kosnik0a099762016-08-03 10:21:41 -0500440 apt_get install python${PYTHON3_VERSION} python${PYTHON3_VERSION}-dev
Doug Hellmannddc38392015-05-07 21:06:24 +0000441 fi
442}
Dean Troyer490430d2015-01-30 14:38:35 -0600443
444# Restore xtrace
445$INC_PY_TRACE
446
447# Local variables:
448# mode: shell-script
449# End: