blob: 91ceb44499c401ac906182a6a821d1eefef6e8a8 [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
Ian Wienandada886d2015-10-07 14:06:26 +110064 local clean_name
65 clean_name=$(get_from_global_requirements $name)
Sean Dague60996b12015-04-08 09:06:49 -040066 pip_install $clean_name
67}
68
Dean Troyer490430d2015-01-30 14:38:35 -060069# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer41d6f852015-03-25 22:42:46 -050070# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
Robert Collins635a5ba2015-06-10 08:48:06 +120071# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
Dean Troyer490430d2015-01-30 14:38:35 -060072# pip_install package [package ...]
73function pip_install {
Ian Wienand433a9b12015-10-07 13:29:31 +110074 local xtrace
75 xtrace=$(set +o | grep xtrace)
Dean Troyer490430d2015-01-30 14:38:35 -060076 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +000077 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -060078 local offline=${OFFLINE:-False}
79 if [[ "$offline" == "True" || -z "$@" ]]; then
80 $xtrace
81 return
82 fi
83
Sean Daguecb658fa2015-10-08 17:12:03 -040084 time_start "pip_install"
85
Chris Dentebdd9ac2015-03-04 12:35:14 +000086 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
87 if [[ "$PIP_UPGRADE" = "True" ]] ; then
88 upgrade="--upgrade"
89 fi
90
Dean Troyer490430d2015-01-30 14:38:35 -060091 if [[ -z "$os_PACKAGE" ]]; then
92 GetOSVersion
93 fi
94 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
95 # TRACK_DEPENDS=True installation creates a circular dependency when
96 # we attempt to install virtualenv into a virualenv, so we must global
97 # that installation.
98 source $DEST/.venv/bin/activate
99 local cmd_pip=$DEST/.venv/bin/pip
100 local sudo_pip="env"
101 else
Dean Troyer2b564762015-02-11 17:01:02 -0600102 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
103 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
104 local sudo_pip="env"
105 else
Ian Wienandada886d2015-10-07 14:06:26 +1100106 local cmd_pip
107 cmd_pip=$(get_pip_command)
Dean Troyer2b564762015-02-11 17:01:02 -0600108 local sudo_pip="sudo -H"
109 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600110 fi
111
Robert Collins635a5ba2015-06-10 08:48:06 +1200112 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700113 # Always apply constraints
114 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200115
Ian Wienandada886d2015-10-07 14:06:26 +1100116 local pip_version
117 pip_version=$(python -c "import pip; \
Dean Troyer490430d2015-01-30 14:38:35 -0600118 print(pip.__version__.strip('.')[0])")
119 if (( pip_version<6 )); then
120 die $LINENO "Currently installed pip version ${pip_version} does not" \
121 "meet minimum requirements (>=6)."
122 fi
123
124 $xtrace
125 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800126 http_proxy="${http_proxy:-}" \
127 https_proxy="${https_proxy:-}" \
128 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800129 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200130 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600131 $@
132
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400133 # Also install test requirements
Sirushti Murugesan713fd2f2015-09-30 15:12:50 +0530134 local test_req="${!#}/test-requirements.txt"
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400135 if [[ -e "$test_req" ]]; then
136 echo "Installing test-requirements for $test_req"
137 $sudo_pip \
138 http_proxy=${http_proxy:-} \
139 https_proxy=${https_proxy:-} \
140 no_proxy=${no_proxy:-} \
141 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200142 $cmd_pip $upgrade \
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400143 -r $test_req
Dean Troyer490430d2015-01-30 14:38:35 -0600144 fi
Sean Daguecb658fa2015-10-08 17:12:03 -0400145
146 time_stop "pip_install"
Dean Troyer490430d2015-01-30 14:38:35 -0600147}
148
Joe Gordond5ac7852015-02-06 19:29:23 -0800149# get version of a package from global requirements file
150# get_from_global_requirements <package>
151function get_from_global_requirements {
152 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100153 local required_pkg
154 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800155 if [[ $required_pkg == "" ]]; then
156 die $LINENO "Can't find package $package in requirements"
157 fi
158 echo $required_pkg
159}
160
Dean Troyer490430d2015-01-30 14:38:35 -0600161# should we use this library from their git repo, or should we let it
162# get pulled in via pip dependencies.
163function use_library_from_git {
164 local name=$1
165 local enabled=1
166 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
167 return $enabled
168}
169
Sean Daguec71973e2015-09-08 07:12:48 -0400170# determine if a package was installed from git
171function lib_installed_from_git {
172 local name=$1
173 pip freeze 2>/dev/null | grep -- "$name" | grep -q -- '-e git'
174}
175
176# check that everything that's in LIBS_FROM_GIT was actually installed
177# correctly, this helps double check issues with library fat fingering.
178function check_libs_from_git {
179 local lib=""
180 local not_installed=""
181 for lib in $(echo ${LIBS_FROM_GIT} | tr "," " "); do
182 if ! lib_installed_from_git "$lib"; then
183 not_installed+=" $lib"
184 fi
185 done
186 # if anything is not installed, say what it is.
187 if [[ -n "$not_installed" ]]; then
188 die $LINENO "The following LIBS_FROM_GIT were not installed correct: $not_installed"
189 fi
190}
191
Dean Troyer490430d2015-01-30 14:38:35 -0600192# setup a library by name. If we are trying to use the library from
193# git, we'll do a git based install, otherwise we'll punt and the
194# library should be installed by a requirements pull from another
195# project.
196function setup_lib {
197 local name=$1
198 local dir=${GITDIR[$name]}
199 setup_install $dir
200}
201
202# setup a library by name in editiable mode. If we are trying to use
203# the library from git, we'll do a git based install, otherwise we'll
204# punt and the library should be installed by a requirements pull from
205# another project.
206#
207# use this for non namespaced libraries
208function setup_dev_lib {
209 local name=$1
210 local dir=${GITDIR[$name]}
211 setup_develop $dir
212}
213
214# this should be used if you want to install globally, all libraries should
215# use this, especially *oslo* ones
216function setup_install {
217 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700218 setup_package_with_constraints_edit $project_dir
Dean Troyer490430d2015-01-30 14:38:35 -0600219}
220
221# this should be used for projects which run services, like all services
222function setup_develop {
223 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700224 setup_package_with_constraints_edit $project_dir -e
Dean Troyer490430d2015-01-30 14:38:35 -0600225}
226
227# determine if a project as specified by directory is in
228# projects.txt. This will not be an exact match because we throw away
229# the namespacing when we clone, but it should be good enough in all
230# practical ways.
231function is_in_projects_txt {
232 local project_dir=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100233 local project_name
234 project_name=$(basename $project_dir)
Ihar Hrachyshka2ba4a722015-06-26 10:45:44 +0200235 grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
Dean Troyer490430d2015-01-30 14:38:35 -0600236}
237
238# ``pip install -e`` the package, which processes the dependencies
239# using pip before running `setup.py develop`
240#
Clark Boylan05aa3842015-08-03 11:14:13 -0700241# Updates the constraints from REQUIREMENTS_DIR to reflect the
242# future installed state of this package. This ensures when we
243# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600244#
Clark Boylan05aa3842015-08-03 11:14:13 -0700245# Uses globals ``REQUIREMENTS_DIR``
Dean Troyer490430d2015-01-30 14:38:35 -0600246# setup_develop directory
Clark Boylan05aa3842015-08-03 11:14:13 -0700247function setup_package_with_constraints_edit {
Dean Troyer490430d2015-01-30 14:38:35 -0600248 local project_dir=$1
249 local flags=$2
250
Robert Collins635a5ba2015-06-10 08:48:06 +1200251 if [ -n "$REQUIREMENTS_DIR" ]; then
252 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100253 local name
254 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200255 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
256 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
257 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200258 fi
259
Dean Troyer490430d2015-01-30 14:38:35 -0600260 setup_package $project_dir $flags
261
Dean Troyer490430d2015-01-30 14:38:35 -0600262}
263
264# ``pip install -e`` the package, which processes the dependencies
265# using pip before running `setup.py develop`
266# Uses globals ``STACK_USER``
267# setup_develop_no_requirements_update directory
268function setup_package {
269 local project_dir=$1
270 local flags=$2
271
272 pip_install $flags $project_dir
273 # ensure that further actions can do things like setup.py sdist
274 if [[ "$flags" == "-e" ]]; then
275 safe_chown -R $STACK_USER $1/*.egg-info
276 fi
277}
278
279
280# Restore xtrace
281$INC_PY_TRACE
282
283# Local variables:
284# mode: shell-script
285# End: