blob: c7ba51a81d065199eef41eb7ad4d99121b79ab6d [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
Chris Dentebdd9ac2015-03-04 12:35:14 +000084 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
85 if [[ "$PIP_UPGRADE" = "True" ]] ; then
86 upgrade="--upgrade"
87 fi
88
Dean Troyer490430d2015-01-30 14:38:35 -060089 if [[ -z "$os_PACKAGE" ]]; then
90 GetOSVersion
91 fi
92 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
93 # TRACK_DEPENDS=True installation creates a circular dependency when
94 # we attempt to install virtualenv into a virualenv, so we must global
95 # that installation.
96 source $DEST/.venv/bin/activate
97 local cmd_pip=$DEST/.venv/bin/pip
98 local sudo_pip="env"
99 else
Dean Troyer2b564762015-02-11 17:01:02 -0600100 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
101 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
102 local sudo_pip="env"
103 else
Ian Wienandada886d2015-10-07 14:06:26 +1100104 local cmd_pip
105 cmd_pip=$(get_pip_command)
Dean Troyer2b564762015-02-11 17:01:02 -0600106 local sudo_pip="sudo -H"
107 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600108 fi
109
Robert Collins635a5ba2015-06-10 08:48:06 +1200110 cmd_pip="$cmd_pip install"
Clark Boylan05aa3842015-08-03 11:14:13 -0700111 # Always apply constraints
112 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
Robert Collins635a5ba2015-06-10 08:48:06 +1200113
Ian Wienandada886d2015-10-07 14:06:26 +1100114 local pip_version
115 pip_version=$(python -c "import pip; \
Dean Troyer490430d2015-01-30 14:38:35 -0600116 print(pip.__version__.strip('.')[0])")
117 if (( pip_version<6 )); then
118 die $LINENO "Currently installed pip version ${pip_version} does not" \
119 "meet minimum requirements (>=6)."
120 fi
121
122 $xtrace
123 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800124 http_proxy="${http_proxy:-}" \
125 https_proxy="${https_proxy:-}" \
126 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800127 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200128 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600129 $@
130
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400131 # Also install test requirements
Sirushti Murugesan713fd2f2015-09-30 15:12:50 +0530132 local test_req="${!#}/test-requirements.txt"
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400133 if [[ -e "$test_req" ]]; then
134 echo "Installing test-requirements for $test_req"
135 $sudo_pip \
136 http_proxy=${http_proxy:-} \
137 https_proxy=${https_proxy:-} \
138 no_proxy=${no_proxy:-} \
139 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200140 $cmd_pip $upgrade \
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400141 -r $test_req
Dean Troyer490430d2015-01-30 14:38:35 -0600142 fi
143}
144
Joe Gordond5ac7852015-02-06 19:29:23 -0800145# get version of a package from global requirements file
146# get_from_global_requirements <package>
147function get_from_global_requirements {
148 local package=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100149 local required_pkg
150 required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800151 if [[ $required_pkg == "" ]]; then
152 die $LINENO "Can't find package $package in requirements"
153 fi
154 echo $required_pkg
155}
156
Dean Troyer490430d2015-01-30 14:38:35 -0600157# should we use this library from their git repo, or should we let it
158# get pulled in via pip dependencies.
159function use_library_from_git {
160 local name=$1
161 local enabled=1
162 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
163 return $enabled
164}
165
Sean Daguec71973e2015-09-08 07:12:48 -0400166# determine if a package was installed from git
167function lib_installed_from_git {
168 local name=$1
169 pip freeze 2>/dev/null | grep -- "$name" | grep -q -- '-e git'
170}
171
172# check that everything that's in LIBS_FROM_GIT was actually installed
173# correctly, this helps double check issues with library fat fingering.
174function check_libs_from_git {
175 local lib=""
176 local not_installed=""
177 for lib in $(echo ${LIBS_FROM_GIT} | tr "," " "); do
178 if ! lib_installed_from_git "$lib"; then
179 not_installed+=" $lib"
180 fi
181 done
182 # if anything is not installed, say what it is.
183 if [[ -n "$not_installed" ]]; then
184 die $LINENO "The following LIBS_FROM_GIT were not installed correct: $not_installed"
185 fi
186}
187
Dean Troyer490430d2015-01-30 14:38:35 -0600188# setup a library by name. If we are trying to use the library from
189# git, we'll do a git based install, otherwise we'll punt and the
190# library should be installed by a requirements pull from another
191# project.
192function setup_lib {
193 local name=$1
194 local dir=${GITDIR[$name]}
195 setup_install $dir
196}
197
198# setup a library by name in editiable mode. If we are trying to use
199# the library from git, we'll do a git based install, otherwise we'll
200# punt and the library should be installed by a requirements pull from
201# another project.
202#
203# use this for non namespaced libraries
204function setup_dev_lib {
205 local name=$1
206 local dir=${GITDIR[$name]}
207 setup_develop $dir
208}
209
210# this should be used if you want to install globally, all libraries should
211# use this, especially *oslo* ones
212function setup_install {
213 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700214 setup_package_with_constraints_edit $project_dir
Dean Troyer490430d2015-01-30 14:38:35 -0600215}
216
217# this should be used for projects which run services, like all services
218function setup_develop {
219 local project_dir=$1
Clark Boylan05aa3842015-08-03 11:14:13 -0700220 setup_package_with_constraints_edit $project_dir -e
Dean Troyer490430d2015-01-30 14:38:35 -0600221}
222
223# determine if a project as specified by directory is in
224# projects.txt. This will not be an exact match because we throw away
225# the namespacing when we clone, but it should be good enough in all
226# practical ways.
227function is_in_projects_txt {
228 local project_dir=$1
Ian Wienandada886d2015-10-07 14:06:26 +1100229 local project_name
230 project_name=$(basename $project_dir)
Ihar Hrachyshka2ba4a722015-06-26 10:45:44 +0200231 grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
Dean Troyer490430d2015-01-30 14:38:35 -0600232}
233
234# ``pip install -e`` the package, which processes the dependencies
235# using pip before running `setup.py develop`
236#
Clark Boylan05aa3842015-08-03 11:14:13 -0700237# Updates the constraints from REQUIREMENTS_DIR to reflect the
238# future installed state of this package. This ensures when we
239# install this package we get the from source version.
Dean Troyer490430d2015-01-30 14:38:35 -0600240#
Clark Boylan05aa3842015-08-03 11:14:13 -0700241# Uses globals ``REQUIREMENTS_DIR``
Dean Troyer490430d2015-01-30 14:38:35 -0600242# setup_develop directory
Clark Boylan05aa3842015-08-03 11:14:13 -0700243function setup_package_with_constraints_edit {
Dean Troyer490430d2015-01-30 14:38:35 -0600244 local project_dir=$1
245 local flags=$2
246
Robert Collins635a5ba2015-06-10 08:48:06 +1200247 if [ -n "$REQUIREMENTS_DIR" ]; then
248 # Constrain this package to this project directory from here on out.
Ian Wienandada886d2015-10-07 14:06:26 +1100249 local name
250 name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200251 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
252 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
253 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200254 fi
255
Dean Troyer490430d2015-01-30 14:38:35 -0600256 setup_package $project_dir $flags
257
Dean Troyer490430d2015-01-30 14:38:35 -0600258}
259
260# ``pip install -e`` the package, which processes the dependencies
261# using pip before running `setup.py develop`
262# Uses globals ``STACK_USER``
263# setup_develop_no_requirements_update directory
264function setup_package {
265 local project_dir=$1
266 local flags=$2
267
268 pip_install $flags $project_dir
269 # ensure that further actions can do things like setup.py sdist
270 if [[ "$flags" == "-e" ]]; then
271 safe_chown -R $STACK_USER $1/*.egg-info
272 fi
273}
274
275
276# Restore xtrace
277$INC_PY_TRACE
278
279# Local variables:
280# mode: shell-script
281# End: