blob: fe7bba69928069ecb9bfd85ec8b798492dce7a23 [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 {
Ian Wienand433a9b12015-10-07 13:29:31 +110041 local xtrace
42 xtrace=$(set +o | grep xtrace)
Dean Troyer2b564762015-02-11 17:01:02 -060043 set +o xtrace
44 if [[ -z "$os_PACKAGE" ]]; then
45 GetOSVersion
46 fi
47 $xtrace
48
Dean Troyer490430d2015-01-30 14:38:35 -060049 if is_fedora || is_suse; then
50 echo "/usr/bin"
51 else
52 echo "/usr/local/bin"
53 fi
54}
55
Sean Dague60996b12015-04-08 09:06:49 -040056# Wrapper for ``pip install`` that only installs versions of libraries
57# from the global-requirements specification.
58#
59# Uses globals ``REQUIREMENTS_DIR``
60#
61# pip_install_gr packagename
62function pip_install_gr {
63 local name=$1
64 local clean_name=$(get_from_global_requirements $name)
65 pip_install $clean_name
66}
67
Dean Troyer490430d2015-01-30 14:38:35 -060068# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -050069# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +120070# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Dean Troyer490430d2015-01-30 14:38:35 -060071# pip_install package [package ...]
72function pip_install {
Ian Wienand433a9b12015-10-07 13:29:31 +110073 local xtrace
74 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -060075 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +000076 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -060077 local offline=${OFFLINE:-False}
78 if [[ "$offline" == "True" || -z "$@" ]]; then
79 $xtrace
80 return
81 fi
82
Chris Dentebdd9ac2015-03-04 12:35:14 +000083 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
84 if [[ "$PIP_UPGRADE" = "True" ]] ; then
85 upgrade="--upgrade"
86 fi
87
Dean Troyer490430d2015-01-30 14:38:35 -060088 if [[ -z "$os_PACKAGE" ]]; then
89 GetOSVersion
90 fi
91 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
92 # TRACK_DEPENDS=True installation creates a circular dependency when
93 # we attempt to install virtualenv into a virualenv, so we must global
94 # that installation.
95 source $DEST/.venv/bin/activate
96 local cmd_pip=$DEST/.venv/bin/pip
97 local sudo_pip="env"
98 else
Dean Troyer2b564762015-02-11 17:01:02 -060099 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
100 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
101 local sudo_pip="env"
102 else
103 local cmd_pip=$(get_pip_command)
104 local sudo_pip="sudo -H"
105 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600106 fi
107
Robert Collins635a5ba2015-06-10 08:48:06 +1200108 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700109 # Always apply constraints
110 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200111
Dean Troyer490430d2015-01-30 14:38:35 -0600112 local pip_version=$(python -c "import pip; \
113 print(pip.__version__.strip('.')[0])")
114 if (( pip_version<6 )); then
115 die $LINENO "Currently installed pip version ${pip_version} does not" \
116 "meet minimum requirements (>=6)."
117 fi
118
119 $xtrace
120 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800121 http_proxy="${http_proxy:-}" \
122 https_proxy="${https_proxy:-}" \
123 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800124 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200125 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600126 $@
127
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400128 # Also install test requirements
Sirushti Murugesan713fd2f2015-09-30 15:12:50 +0530129 local test_req="${!#}/test-requirements.txt"
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400130 if [[ -e "$test_req" ]]; then
131 echo "Installing test-requirements for $test_req"
132 $sudo_pip \
133 http_proxy=${http_proxy:-} \
134 https_proxy=${https_proxy:-} \
135 no_proxy=${no_proxy:-} \
136 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200137 $cmd_pip $upgrade \
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400138 -r $test_req
Dean Troyer490430d2015-01-30 14:38:35 -0600139 fi
140}
141
Joe Gordond5ac7852015-02-06 19:29:23 -0800142# get version of a package from global requirements file
143# get_from_global_requirements <package>
144function get_from_global_requirements {
145 local package=$1
Amrith Kumar98608762015-04-08 15:37:58 -0400146 local required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800147 if [[ $required_pkg == "" ]]; then
148 die $LINENO "Can't find package $package in requirements"
149 fi
150 echo $required_pkg
151}
152
Dean Troyer490430d2015-01-30 14:38:35 -0600153# should we use this library from their git repo, or should we let it
154# get pulled in via pip dependencies.
155function use_library_from_git {
156 local name=$1
157 local enabled=1
158 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
159 return $enabled
160}
161
Sean Daguec71973e2015-09-08 07:12:48 -0400162# determine if a package was installed from git
163function lib_installed_from_git {
164 local name=$1
165 pip freeze 2>/dev/null | grep -- "$name" | grep -q -- '-e git'
166}
167
168# check that everything that's in LIBS_FROM_GIT was actually installed
169# correctly, this helps double check issues with library fat fingering.
170function check_libs_from_git {
171 local lib=""
172 local not_installed=""
173 for lib in $(echo ${LIBS_FROM_GIT} | tr "," " "); do
174 if ! lib_installed_from_git "$lib"; then
175 not_installed+=" $lib"
176 fi
177 done
178 # if anything is not installed, say what it is.
179 if [[ -n "$not_installed" ]]; then
180 die $LINENO "The following LIBS_FROM_GIT were not installed correct: $not_installed"
181 fi
182}
183
Dean Troyer490430d2015-01-30 14:38:35 -0600184# setup a library by name. If we are trying to use the library from
185# git, we'll do a git based install, otherwise we'll punt and the
186# library should be installed by a requirements pull from another
187# project.
188function setup_lib {
189 local name=$1
190 local dir=${GITDIR[$name]}
191 setup_install $dir
192}
193
194# setup a library by name in editiable mode. If we are trying to use
195# the library from git, we'll do a git based install, otherwise we'll
196# punt and the library should be installed by a requirements pull from
197# another project.
198#
199# use this for non namespaced libraries
200function setup_dev_lib {
201 local name=$1
202 local dir=${GITDIR[$name]}
203 setup_develop $dir
204}
205
206# this should be used if you want to install globally, all libraries should
207# use this, especially *oslo* ones
208function setup_install {
209 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700210 setup_package_with_constraints_edit $project_dir
Dean Troyer490430d2015-01-30 14:38:35 -0600211}
212
213# this should be used for projects which run services, like all services
214function setup_develop {
215 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700216 setup_package_with_constraints_edit $project_dir -e
Dean Troyer490430d2015-01-30 14:38:35 -0600217}
218
219# determine if a project as specified by directory is in
220# projects.txt. This will not be an exact match because we throw away
221# the namespacing when we clone, but it should be good enough in all
222# practical ways.
223function is_in_projects_txt {
224 local project_dir=$1
225 local project_name=$(basename $project_dir)
Ihar Hrachyshka2ba4a722015-06-26 10:45:44 +0200226 grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
Dean Troyer490430d2015-01-30 14:38:35 -0600227}
228
229# ``pip install -e`` the package, which processes the dependencies
230# using pip before running `setup.py develop`
231#
Clark Boylan05aa3842015-08-03 11:14:13 -0700232# Updates the constraints from REQUIREMENTS_DIR to reflect the
233# future installed state of this package. This ensures when we
234# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600235#
Clark Boylan05aa3842015-08-03 11:14:13 -0700236# Uses globals ``REQUIREMENTS_DIR``
Dean Troyer490430d2015-01-30 14:38:35 -0600237# setup_develop directory
Clark Boylan05aa3842015-08-03 11:14:13 -0700238function setup_package_with_constraints_edit {
Dean Troyer490430d2015-01-30 14:38:35 -0600239 local project_dir=$1
240 local flags=$2
241
Robert Collins635a5ba2015-06-10 08:48:06 +1200242 if [ -n "$REQUIREMENTS_DIR" ]; then
243 # Constrain this package to this project directory from here on out.
244 local name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200245 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
246 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
247 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200248 fi
249
Dean Troyer490430d2015-01-30 14:38:35 -0600250 setup_package $project_dir $flags
251
Dean Troyer490430d2015-01-30 14:38:35 -0600252}
253
254# ``pip install -e`` the package, which processes the dependencies
255# using pip before running `setup.py develop`
256# Uses globals ``STACK_USER``
257# setup_develop_no_requirements_update directory
258function setup_package {
259 local project_dir=$1
260 local flags=$2
261
262 pip_install $flags $project_dir
263 # ensure that further actions can do things like setup.py sdist
264 if [[ "$flags" == "-e" ]]; then
265 safe_chown -R $STACK_USER $1/*.egg-info
266 fi
267}
268
269
270# Restore xtrace
271$INC_PY_TRACE
272
273# Local variables:
274# mode: shell-script
275# End: