blob: 5c9dc5c3e5b3c6c5db0e39aa0af7b2fe28517a36 [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
20# PROJECT_VENV contains the name of the virtual enviromnet for each
21# 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 {
31 which pip || which pip-python
32
33 if [ $? -ne 0 ]; then
34 die $LINENO "Unable to find pip; cannot continue"
35 fi
36}
37
38# Get the path to the direcotry where python executables are installed.
39# get_python_exec_prefix
40function get_python_exec_prefix {
Dean Troyer2b564762015-02-11 17:01:02 -060041 local xtrace=$(set +o | grep xtrace)
42 set +o xtrace
43 if [[ -z "$os_PACKAGE" ]]; then
44 GetOSVersion
45 fi
46 $xtrace
47
Dean Troyer490430d2015-01-30 14:38:35 -060048 if is_fedora || is_suse; then
49 echo "/usr/bin"
50 else
51 echo "/usr/local/bin"
52 fi
53}
54
Sean Dague60996b12015-04-08 09:06:49 -040055# Wrapper for ``pip install`` that only installs versions of libraries
56# from the global-requirements specification.
57#
58# Uses globals ``REQUIREMENTS_DIR``
59#
60# pip_install_gr packagename
61function pip_install_gr {
62 local name=$1
63 local clean_name=$(get_from_global_requirements $name)
64 pip_install $clean_name
65}
66
Dean Troyer490430d2015-01-30 14:38:35 -060067# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -050068# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +120069# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Dean Troyer490430d2015-01-30 14:38:35 -060070# pip_install package [package ...]
71function pip_install {
72 local xtrace=$(set +o | grep xtrace)
73 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +000074 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -060075 local offline=${OFFLINE:-False}
76 if [[ "$offline" == "True" || -z "$@" ]]; then
77 $xtrace
78 return
79 fi
80
Chris Dentebdd9ac2015-03-04 12:35:14 +000081 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
82 if [[ "$PIP_UPGRADE" = "True" ]] ; then
83 upgrade="--upgrade"
84 fi
85
Dean Troyer490430d2015-01-30 14:38:35 -060086 if [[ -z "$os_PACKAGE" ]]; then
87 GetOSVersion
88 fi
89 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
90 # TRACK_DEPENDS=True installation creates a circular dependency when
91 # we attempt to install virtualenv into a virualenv, so we must global
92 # that installation.
93 source $DEST/.venv/bin/activate
94 local cmd_pip=$DEST/.venv/bin/pip
95 local sudo_pip="env"
96 else
Dean Troyer2b564762015-02-11 17:01:02 -060097 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
98 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
99 local sudo_pip="env"
100 else
101 local cmd_pip=$(get_pip_command)
102 local sudo_pip="sudo -H"
103 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600104 fi
105
Robert Collins635a5ba2015-06-10 08:48:06 +1200106 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700107 # Always apply constraints
108 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200109
Dean Troyer490430d2015-01-30 14:38:35 -0600110 local pip_version=$(python -c "import pip; \
111 print(pip.__version__.strip('.')[0])")
112 if (( pip_version<6 )); then
113 die $LINENO "Currently installed pip version ${pip_version} does not" \
114 "meet minimum requirements (>=6)."
115 fi
116
117 $xtrace
118 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800119 http_proxy="${http_proxy:-}" \
120 https_proxy="${https_proxy:-}" \
121 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800122 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200123 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600124 $@
125
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400126 # Also install test requirements
127 local test_req="$@/test-requirements.txt"
128 if [[ -e "$test_req" ]]; then
129 echo "Installing test-requirements for $test_req"
130 $sudo_pip \
131 http_proxy=${http_proxy:-} \
132 https_proxy=${https_proxy:-} \
133 no_proxy=${no_proxy:-} \
134 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200135 $cmd_pip $upgrade \
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400136 -r $test_req
Dean Troyer490430d2015-01-30 14:38:35 -0600137 fi
138}
139
Joe Gordond5ac7852015-02-06 19:29:23 -0800140# get version of a package from global requirements file
141# get_from_global_requirements <package>
142function get_from_global_requirements {
143 local package=$1
Amrith Kumar98608762015-04-08 15:37:58 -0400144 local required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800145 if [[ $required_pkg == "" ]]; then
146 die $LINENO "Can't find package $package in requirements"
147 fi
148 echo $required_pkg
149}
150
Dean Troyer490430d2015-01-30 14:38:35 -0600151# should we use this library from their git repo, or should we let it
152# get pulled in via pip dependencies.
153function use_library_from_git {
154 local name=$1
155 local enabled=1
156 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
157 return $enabled
158}
159
160# setup a library by name. If we are trying to use the library from
161# git, we'll do a git based install, otherwise we'll punt and the
162# library should be installed by a requirements pull from another
163# project.
164function setup_lib {
165 local name=$1
166 local dir=${GITDIR[$name]}
167 setup_install $dir
168}
169
170# setup a library by name in editiable mode. If we are trying to use
171# the library from git, we'll do a git based install, otherwise we'll
172# punt and the library should be installed by a requirements pull from
173# another project.
174#
175# use this for non namespaced libraries
176function setup_dev_lib {
177 local name=$1
178 local dir=${GITDIR[$name]}
179 setup_develop $dir
180}
181
182# this should be used if you want to install globally, all libraries should
183# use this, especially *oslo* ones
184function setup_install {
185 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700186 setup_package_with_constraints_edit $project_dir
Dean Troyer490430d2015-01-30 14:38:35 -0600187}
188
189# this should be used for projects which run services, like all services
190function setup_develop {
191 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700192 setup_package_with_constraints_edit $project_dir -e
Dean Troyer490430d2015-01-30 14:38:35 -0600193}
194
195# determine if a project as specified by directory is in
196# projects.txt. This will not be an exact match because we throw away
197# the namespacing when we clone, but it should be good enough in all
198# practical ways.
199function is_in_projects_txt {
200 local project_dir=$1
201 local project_name=$(basename $project_dir)
Ihar Hrachyshka2ba4a722015-06-26 10:45:44 +0200202 grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
Dean Troyer490430d2015-01-30 14:38:35 -0600203}
204
205# ``pip install -e`` the package, which processes the dependencies
206# using pip before running `setup.py develop`
207#
Clark Boylan05aa3842015-08-03 11:14:13 -0700208# Updates the constraints from REQUIREMENTS_DIR to reflect the
209# future installed state of this package. This ensures when we
210# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600211#
Clark Boylan05aa3842015-08-03 11:14:13 -0700212# Uses globals ``REQUIREMENTS_DIR``
Dean Troyer490430d2015-01-30 14:38:35 -0600213# setup_develop directory
Clark Boylan05aa3842015-08-03 11:14:13 -0700214function setup_package_with_constraints_edit {
Dean Troyer490430d2015-01-30 14:38:35 -0600215 local project_dir=$1
216 local flags=$2
217
Robert Collins635a5ba2015-06-10 08:48:06 +1200218 if [ -n "$REQUIREMENTS_DIR" ]; then
219 # Constrain this package to this project directory from here on out.
220 local name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200221 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
222 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
223 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200224 fi
225
Dean Troyer490430d2015-01-30 14:38:35 -0600226 setup_package $project_dir $flags
227
Dean Troyer490430d2015-01-30 14:38:35 -0600228}
229
230# ``pip install -e`` the package, which processes the dependencies
231# using pip before running `setup.py develop`
232# Uses globals ``STACK_USER``
233# setup_develop_no_requirements_update directory
234function setup_package {
235 local project_dir=$1
236 local flags=$2
237
238 pip_install $flags $project_dir
239 # ensure that further actions can do things like setup.py sdist
240 if [[ "$flags" == "-e" ]]; then
241 safe_chown -R $STACK_USER $1/*.egg-info
242 fi
243}
244
245
246# Restore xtrace
247$INC_PY_TRACE
248
249# Local variables:
250# mode: shell-script
251# End: