blob: bba9cda44d4bff4904c62d77e034efd09db6b95b [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``,
70# ``USE_CONSTRAINTS``
Dean Troyer490430d2015-01-30 14:38:35 -060071# pip_install package [package ...]
72function pip_install {
73 local xtrace=$(set +o | grep xtrace)
74 set +o xtrace
Chris Dentebdd9ac2015-03-04 12:35:14 +000075 local upgrade=""
Dean Troyer490430d2015-01-30 14:38:35 -060076 local offline=${OFFLINE:-False}
77 if [[ "$offline" == "True" || -z "$@" ]]; then
78 $xtrace
79 return
80 fi
81
Chris Dentebdd9ac2015-03-04 12:35:14 +000082 PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE)
83 if [[ "$PIP_UPGRADE" = "True" ]] ; then
84 upgrade="--upgrade"
85 fi
86
Dean Troyer490430d2015-01-30 14:38:35 -060087 if [[ -z "$os_PACKAGE" ]]; then
88 GetOSVersion
89 fi
90 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
91 # TRACK_DEPENDS=True installation creates a circular dependency when
92 # we attempt to install virtualenv into a virualenv, so we must global
93 # that installation.
94 source $DEST/.venv/bin/activate
95 local cmd_pip=$DEST/.venv/bin/pip
96 local sudo_pip="env"
97 else
Dean Troyer2b564762015-02-11 17:01:02 -060098 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
99 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
100 local sudo_pip="env"
101 else
102 local cmd_pip=$(get_pip_command)
103 local sudo_pip="sudo -H"
104 fi
Dean Troyer490430d2015-01-30 14:38:35 -0600105 fi
106
Robert Collins635a5ba2015-06-10 08:48:06 +1200107 cmd_pip="$cmd_pip install"
108
109 # Handle a constraints file, if needed.
110 if [[ "$USE_CONSTRAINTS" == "True" ]]; then
111 cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
112 fi
113
Dean Troyer490430d2015-01-30 14:38:35 -0600114 local pip_version=$(python -c "import pip; \
115 print(pip.__version__.strip('.')[0])")
116 if (( pip_version<6 )); then
117 die $LINENO "Currently installed pip version ${pip_version} does not" \
118 "meet minimum requirements (>=6)."
119 fi
120
121 $xtrace
122 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800123 http_proxy="${http_proxy:-}" \
124 https_proxy="${https_proxy:-}" \
125 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800126 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200127 $cmd_pip $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600128 $@
129
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400130 # Also install test requirements
131 local test_req="$@/test-requirements.txt"
132 if [[ -e "$test_req" ]]; then
133 echo "Installing test-requirements for $test_req"
134 $sudo_pip \
135 http_proxy=${http_proxy:-} \
136 https_proxy=${https_proxy:-} \
137 no_proxy=${no_proxy:-} \
138 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Robert Collins635a5ba2015-06-10 08:48:06 +1200139 $cmd_pip $upgrade \
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400140 -r $test_req
Dean Troyer490430d2015-01-30 14:38:35 -0600141 fi
142}
143
Joe Gordond5ac7852015-02-06 19:29:23 -0800144# get version of a package from global requirements file
145# get_from_global_requirements <package>
146function get_from_global_requirements {
147 local package=$1
Amrith Kumar98608762015-04-08 15:37:58 -0400148 local required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800149 if [[ $required_pkg == "" ]]; then
150 die $LINENO "Can't find package $package in requirements"
151 fi
152 echo $required_pkg
153}
154
Dean Troyer490430d2015-01-30 14:38:35 -0600155# should we use this library from their git repo, or should we let it
156# get pulled in via pip dependencies.
157function use_library_from_git {
158 local name=$1
159 local enabled=1
160 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
161 return $enabled
162}
163
164# setup a library by name. If we are trying to use the library from
165# git, we'll do a git based install, otherwise we'll punt and the
166# library should be installed by a requirements pull from another
167# project.
168function setup_lib {
169 local name=$1
170 local dir=${GITDIR[$name]}
171 setup_install $dir
172}
173
174# setup a library by name in editiable mode. If we are trying to use
175# the library from git, we'll do a git based install, otherwise we'll
176# punt and the library should be installed by a requirements pull from
177# another project.
178#
179# use this for non namespaced libraries
180function setup_dev_lib {
181 local name=$1
182 local dir=${GITDIR[$name]}
183 setup_develop $dir
184}
185
186# this should be used if you want to install globally, all libraries should
187# use this, especially *oslo* ones
188function setup_install {
189 local project_dir=$1
190 setup_package_with_req_sync $project_dir
191}
192
193# this should be used for projects which run services, like all services
194function setup_develop {
195 local project_dir=$1
196 setup_package_with_req_sync $project_dir -e
197}
198
199# determine if a project as specified by directory is in
200# projects.txt. This will not be an exact match because we throw away
201# the namespacing when we clone, but it should be good enough in all
202# practical ways.
203function is_in_projects_txt {
204 local project_dir=$1
205 local project_name=$(basename $project_dir)
206 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
207}
208
209# ``pip install -e`` the package, which processes the dependencies
210# using pip before running `setup.py develop`
211#
212# Updates the dependencies in project_dir from the
213# openstack/requirements global list before installing anything.
214#
215# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
216# setup_develop directory
217function setup_package_with_req_sync {
218 local project_dir=$1
219 local flags=$2
220
221 # Don't update repo if local changes exist
222 # Don't use buggy "git diff --quiet"
223 # ``errexit`` requires us to trap the exit code when the repo is changed
224 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
225
Robert Collins635a5ba2015-06-10 08:48:06 +1200226 if [[ $update_requirements != "changed" && "$USE_CONSTRAINTS" == "False" ]]; then
Sean Dague7af8a1b2015-06-24 05:51:54 -0400227 if is_in_projects_txt $project_dir; then
Dean Troyer490430d2015-01-30 14:38:35 -0600228 (cd $REQUIREMENTS_DIR; \
Robert Collins40f3e332015-06-19 08:04:00 +1200229 ./.venv/bin/python update.py $project_dir)
Sean Dague7af8a1b2015-06-24 05:51:54 -0400230 else
231 # soft update projects not found in requirements project.txt
232 echo "$project_dir not a constrained repository, soft enforcing requirements"
233 (cd $REQUIREMENTS_DIR; \
234 ./.venv/bin/python update.py -s $project_dir)
Dean Troyer490430d2015-01-30 14:38:35 -0600235 fi
236 fi
237
Robert Collins635a5ba2015-06-10 08:48:06 +1200238 if [ -n "$REQUIREMENTS_DIR" ]; then
239 # Constrain this package to this project directory from here on out.
240 local name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg)
Robert Collins7c838612015-07-03 13:28:09 +1200241 $REQUIREMENTS_DIR/.venv/bin/edit-constraints \
242 $REQUIREMENTS_DIR/upper-constraints.txt -- $name \
243 "$flags file://$project_dir#egg=$name"
Robert Collins635a5ba2015-06-10 08:48:06 +1200244 fi
245
Dean Troyer490430d2015-01-30 14:38:35 -0600246 setup_package $project_dir $flags
247
248 # We've just gone and possibly modified the user's source tree in an
249 # automated way, which is considered bad form if it's a development
250 # tree because we've screwed up their next git checkin. So undo it.
251 #
252 # However... there are some circumstances, like running in the gate
253 # where we really really want the overridden version to stick. So provide
254 # a variable that tells us whether or not we should UNDO the requirements
255 # changes (this will be set to False in the OpenStack ci gate)
256 if [ $UNDO_REQUIREMENTS = "True" ]; then
257 if [[ $update_requirements != "changed" ]]; then
258 (cd $project_dir && git reset --hard)
259 fi
260 fi
261}
262
263# ``pip install -e`` the package, which processes the dependencies
264# using pip before running `setup.py develop`
265# Uses globals ``STACK_USER``
266# setup_develop_no_requirements_update directory
267function setup_package {
268 local project_dir=$1
269 local flags=$2
270
271 pip_install $flags $project_dir
272 # ensure that further actions can do things like setup.py sdist
273 if [[ "$flags" == "-e" ]]; then
274 safe_chown -R $STACK_USER $1/*.egg-info
275 fi
276}
277
278
279# Restore xtrace
280$INC_PY_TRACE
281
282# Local variables:
283# mode: shell-script
284# End: