blob: fd0d616b62b78ef57b2392ddc3116e9135f299cd [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
Sirushti Murugesan713fd2f2015-09-30 15:12:50 +0530127 local test_req="${!#}/test-requirements.txt"
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400128 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
Sean Daguec71973e2015-09-08 07:12:48 -0400160# determine if a package was installed from git
161function lib_installed_from_git {
162 local name=$1
163 pip freeze 2>/dev/null | grep -- "$name" | grep -q -- '-e git'
164}
165
166# check that everything that's in LIBS_FROM_GIT was actually installed
167# correctly, this helps double check issues with library fat fingering.
168function check_libs_from_git {
169 local lib=""
170 local not_installed=""
171 for lib in $(echo ${LIBS_FROM_GIT} | tr "," " "); do
172 if ! lib_installed_from_git "$lib"; then
173 not_installed+=" $lib"
174 fi
175 done
176 # if anything is not installed, say what it is.
177 if [[ -n "$not_installed" ]]; then
178 die $LINENO "The following LIBS_FROM_GIT were not installed correct: $not_installed"
179 fi
180}
181
Dean Troyer490430d2015-01-30 14:38:35 -0600182# setup a library by name. If we are trying to use the library from
183# git, we'll do a git based install, otherwise we'll punt and the
184# library should be installed by a requirements pull from another
185# project.
186function setup_lib {
187 local name=$1
188 local dir=${GITDIR[$name]}
189 setup_install $dir
190}
191
192# setup a library by name in editiable mode. If we are trying to use
193# the library from git, we'll do a git based install, otherwise we'll
194# punt and the library should be installed by a requirements pull from
195# another project.
196#
197# use this for non namespaced libraries
198function setup_dev_lib {
199 local name=$1
200 local dir=${GITDIR[$name]}
201 setup_develop $dir
202}
203
204# this should be used if you want to install globally, all libraries should
205# use this, especially *oslo* ones
206function setup_install {
207 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700208 setup_package_with_constraints_edit $project_dir
Dean Troyer490430d2015-01-30 14:38:35 -0600209}
210
211# this should be used for projects which run services, like all services
212function setup_develop {
213 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700214 setup_package_with_constraints_edit $project_dir -e
Dean Troyer490430d2015-01-30 14:38:35 -0600215}
216
217# determine if a project as specified by directory is in
218# projects.txt. This will not be an exact match because we throw away
219# the namespacing when we clone, but it should be good enough in all
220# practical ways.
221function is_in_projects_txt {
222 local project_dir=$1
223 local project_name=$(basename $project_dir)
Ihar Hrachyshka2ba4a722015-06-26 10:45:44 +0200224 grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
Dean Troyer490430d2015-01-30 14:38:35 -0600225}
226
227# ``pip install -e`` the package, which processes the dependencies
228# using pip before running `setup.py develop`
229#
Clark Boylan05aa3842015-08-03 11:14:13 -0700230# Updates the constraints from REQUIREMENTS_DIR to reflect the
231# future installed state of this package. This ensures when we
232# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600233#
Clark Boylan05aa3842015-08-03 11:14:13 -0700234# Uses globals ``REQUIREMENTS_DIR``
Dean Troyer490430d2015-01-30 14:38:35 -0600235# setup_develop directory
Clark Boylan05aa3842015-08-03 11:14:13 -0700236function setup_package_with_constraints_edit {
Dean Troyer490430d2015-01-30 14:38:35 -0600237 local project_dir=$1
238 local flags=$2
239
Robert Collins635a5ba2015-06-10 08:48:06 +1200240 if [ -n "$REQUIREMENTS_DIR" ]; then
241 # Constrain this package to this project directory from here on out.
242 local name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200243 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
244 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
245 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200246 fi
247
Dean Troyer490430d2015-01-30 14:38:35 -0600248 setup_package $project_dir $flags
249
Dean Troyer490430d2015-01-30 14:38:35 -0600250}
251
252# ``pip install -e`` the package, which processes the dependencies
253# using pip before running `setup.py develop`
254# Uses globals ``STACK_USER``
255# setup_develop_no_requirements_update directory
256function setup_package {
257 local project_dir=$1
258 local flags=$2
259
260 pip_install $flags $project_dir
261 # ensure that further actions can do things like setup.py sdist
262 if [[ "$flags" == "-e" ]]; then
263 safe_chown -R $STACK_USER $1/*.egg-info
264 fi
265}
266
267
268# Restore xtrace
269$INC_PY_TRACE
270
271# Local variables:
272# mode: shell-script
273# End: