blob: 9a7cea0a07eb020fbb46f9297551accf17a89a69 [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``,
Chris Dentebdd9ac2015-03-04 12:35:14 +000069# ``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
106 local pip_version=$(python -c "import pip; \
107 print(pip.__version__.strip('.')[0])")
108 if (( pip_version<6 )); then
109 die $LINENO "Currently installed pip version ${pip_version} does not" \
110 "meet minimum requirements (>=6)."
111 fi
112
113 $xtrace
114 $sudo_pip \
Eli Qiao6a83c422015-03-17 16:54:16 +0800115 http_proxy="${http_proxy:-}" \
116 https_proxy="${https_proxy:-}" \
117 no_proxy="${no_proxy:-}" \
Joe Gordoncd8824a2015-03-04 16:40:19 -0800118 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Chris Dentebdd9ac2015-03-04 12:35:14 +0000119 $cmd_pip install $upgrade \
Dean Troyer490430d2015-01-30 14:38:35 -0600120 $@
121
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400122 # Also install test requirements
123 local test_req="$@/test-requirements.txt"
124 if [[ -e "$test_req" ]]; then
125 echo "Installing test-requirements for $test_req"
126 $sudo_pip \
127 http_proxy=${http_proxy:-} \
128 https_proxy=${https_proxy:-} \
129 no_proxy=${no_proxy:-} \
130 PIP_FIND_LINKS=$PIP_FIND_LINKS \
Chris Dentebdd9ac2015-03-04 12:35:14 +0000131 $cmd_pip install $upgrade \
Sean Dagueeeb7bda2015-03-25 11:55:32 -0400132 -r $test_req
Dean Troyer490430d2015-01-30 14:38:35 -0600133 fi
134}
135
Joe Gordond5ac7852015-02-06 19:29:23 -0800136# get version of a package from global requirements file
137# get_from_global_requirements <package>
138function get_from_global_requirements {
139 local package=$1
Amrith Kumar98608762015-04-08 15:37:58 -0400140 local required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
Joe Gordond5ac7852015-02-06 19:29:23 -0800141 if [[ $required_pkg == "" ]]; then
142 die $LINENO "Can't find package $package in requirements"
143 fi
144 echo $required_pkg
145}
146
Dean Troyer490430d2015-01-30 14:38:35 -0600147# should we use this library from their git repo, or should we let it
148# get pulled in via pip dependencies.
149function use_library_from_git {
150 local name=$1
151 local enabled=1
152 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
153 return $enabled
154}
155
156# setup a library by name. If we are trying to use the library from
157# git, we'll do a git based install, otherwise we'll punt and the
158# library should be installed by a requirements pull from another
159# project.
160function setup_lib {
161 local name=$1
162 local dir=${GITDIR[$name]}
163 setup_install $dir
164}
165
166# setup a library by name in editiable mode. If we are trying to use
167# the library from git, we'll do a git based install, otherwise we'll
168# punt and the library should be installed by a requirements pull from
169# another project.
170#
171# use this for non namespaced libraries
172function setup_dev_lib {
173 local name=$1
174 local dir=${GITDIR[$name]}
175 setup_develop $dir
176}
177
178# this should be used if you want to install globally, all libraries should
179# use this, especially *oslo* ones
180function setup_install {
181 local project_dir=$1
182 setup_package_with_req_sync $project_dir
183}
184
185# this should be used for projects which run services, like all services
186function setup_develop {
187 local project_dir=$1
188 setup_package_with_req_sync $project_dir -e
189}
190
191# determine if a project as specified by directory is in
192# projects.txt. This will not be an exact match because we throw away
193# the namespacing when we clone, but it should be good enough in all
194# practical ways.
195function is_in_projects_txt {
196 local project_dir=$1
197 local project_name=$(basename $project_dir)
Ihar Hrachyshka2ba4a722015-06-26 10:45:44 +0200198 grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt
Dean Troyer490430d2015-01-30 14:38:35 -0600199}
200
201# ``pip install -e`` the package, which processes the dependencies
202# using pip before running `setup.py develop`
203#
204# Updates the dependencies in project_dir from the
205# openstack/requirements global list before installing anything.
206#
207# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
208# setup_develop directory
209function setup_package_with_req_sync {
210 local project_dir=$1
211 local flags=$2
212
213 # Don't update repo if local changes exist
214 # Don't use buggy "git diff --quiet"
215 # ``errexit`` requires us to trap the exit code when the repo is changed
216 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
217
218 if [[ $update_requirements != "changed" ]]; then
Sean Dague7af8a1b2015-06-24 05:51:54 -0400219 if is_in_projects_txt $project_dir; then
Dean Troyer490430d2015-01-30 14:38:35 -0600220 (cd $REQUIREMENTS_DIR; \
Robert Collins40f3e332015-06-19 08:04:00 +1200221 ./.venv/bin/python update.py $project_dir)
Sean Dague7af8a1b2015-06-24 05:51:54 -0400222 else
223 # soft update projects not found in requirements project.txt
224 echo "$project_dir not a constrained repository, soft enforcing requirements"
225 (cd $REQUIREMENTS_DIR; \
226 ./.venv/bin/python update.py -s $project_dir)
Dean Troyer490430d2015-01-30 14:38:35 -0600227 fi
228 fi
229
230 setup_package $project_dir $flags
231
232 # We've just gone and possibly modified the user's source tree in an
233 # automated way, which is considered bad form if it's a development
234 # tree because we've screwed up their next git checkin. So undo it.
235 #
236 # However... there are some circumstances, like running in the gate
237 # where we really really want the overridden version to stick. So provide
238 # a variable that tells us whether or not we should UNDO the requirements
239 # changes (this will be set to False in the OpenStack ci gate)
240 if [ $UNDO_REQUIREMENTS = "True" ]; then
241 if [[ $update_requirements != "changed" ]]; then
242 (cd $project_dir && git reset --hard)
243 fi
244 fi
245}
246
247# ``pip install -e`` the package, which processes the dependencies
248# using pip before running `setup.py develop`
249# Uses globals ``STACK_USER``
250# setup_develop_no_requirements_update directory
251function setup_package {
252 local project_dir=$1
253 local flags=$2
254
255 pip_install $flags $project_dir
256 # ensure that further actions can do things like setup.py sdist
257 if [[ "$flags" == "-e" ]]; then
258 safe_chown -R $STACK_USER $1/*.egg-info
259 fi
260}
261
262
263# Restore xtrace
264$INC_PY_TRACE
265
266# Local variables:
267# mode: shell-script
268# End: