blob: dfc4d63ca418522c5ac46302ade0ae6acf0de55a [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
55# Wrapper for ``pip install`` to set cache and proxy environment variables
Dean Troyer2b564762015-02-11 17:01:02 -060056# Uses globals ``INSTALL_TESTONLY_PACKAGES``, ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
57# ``TRACK_DEPENDS``, ``*_proxy``
Dean Troyer490430d2015-01-30 14:38:35 -060058# pip_install package [package ...]
59function pip_install {
60 local xtrace=$(set +o | grep xtrace)
61 set +o xtrace
62 local offline=${OFFLINE:-False}
63 if [[ "$offline" == "True" || -z "$@" ]]; then
64 $xtrace
65 return
66 fi
67
68 if [[ -z "$os_PACKAGE" ]]; then
69 GetOSVersion
70 fi
71 if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
72 # TRACK_DEPENDS=True installation creates a circular dependency when
73 # we attempt to install virtualenv into a virualenv, so we must global
74 # that installation.
75 source $DEST/.venv/bin/activate
76 local cmd_pip=$DEST/.venv/bin/pip
77 local sudo_pip="env"
78 else
Dean Troyer2b564762015-02-11 17:01:02 -060079 if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
80 local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
81 local sudo_pip="env"
82 else
83 local cmd_pip=$(get_pip_command)
84 local sudo_pip="sudo -H"
85 fi
Dean Troyer490430d2015-01-30 14:38:35 -060086 fi
87
88 local pip_version=$(python -c "import pip; \
89 print(pip.__version__.strip('.')[0])")
90 if (( pip_version<6 )); then
91 die $LINENO "Currently installed pip version ${pip_version} does not" \
92 "meet minimum requirements (>=6)."
93 fi
94
95 $xtrace
96 $sudo_pip \
97 http_proxy=${http_proxy:-} \
98 https_proxy=${https_proxy:-} \
99 no_proxy=${no_proxy:-} \
100 $cmd_pip install \
101 $@
102
103 INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
104 if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
105 local test_req="$@/test-requirements.txt"
106 if [[ -e "$test_req" ]]; then
107 $sudo_pip \
108 http_proxy=${http_proxy:-} \
109 https_proxy=${https_proxy:-} \
110 no_proxy=${no_proxy:-} \
111 $cmd_pip install \
112 -r $test_req
113 fi
114 fi
115}
116
Joe Gordond5ac7852015-02-06 19:29:23 -0800117# get version of a package from global requirements file
118# get_from_global_requirements <package>
119function get_from_global_requirements {
120 local package=$1
121 local required_pkg=$(grep -h ${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
122 if [[ $required_pkg == "" ]]; then
123 die $LINENO "Can't find package $package in requirements"
124 fi
125 echo $required_pkg
126}
127
Dean Troyer490430d2015-01-30 14:38:35 -0600128# should we use this library from their git repo, or should we let it
129# get pulled in via pip dependencies.
130function use_library_from_git {
131 local name=$1
132 local enabled=1
133 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
134 return $enabled
135}
136
137# setup a library by name. If we are trying to use the library from
138# git, we'll do a git based install, otherwise we'll punt and the
139# library should be installed by a requirements pull from another
140# project.
141function setup_lib {
142 local name=$1
143 local dir=${GITDIR[$name]}
144 setup_install $dir
145}
146
147# setup a library by name in editiable mode. If we are trying to use
148# the library from git, we'll do a git based install, otherwise we'll
149# punt and the library should be installed by a requirements pull from
150# another project.
151#
152# use this for non namespaced libraries
153function setup_dev_lib {
154 local name=$1
155 local dir=${GITDIR[$name]}
156 setup_develop $dir
157}
158
159# this should be used if you want to install globally, all libraries should
160# use this, especially *oslo* ones
161function setup_install {
162 local project_dir=$1
163 setup_package_with_req_sync $project_dir
164}
165
166# this should be used for projects which run services, like all services
167function setup_develop {
168 local project_dir=$1
169 setup_package_with_req_sync $project_dir -e
170}
171
172# determine if a project as specified by directory is in
173# projects.txt. This will not be an exact match because we throw away
174# the namespacing when we clone, but it should be good enough in all
175# practical ways.
176function is_in_projects_txt {
177 local project_dir=$1
178 local project_name=$(basename $project_dir)
179 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
180}
181
182# ``pip install -e`` the package, which processes the dependencies
183# using pip before running `setup.py develop`
184#
185# Updates the dependencies in project_dir from the
186# openstack/requirements global list before installing anything.
187#
188# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
189# setup_develop directory
190function setup_package_with_req_sync {
191 local project_dir=$1
192 local flags=$2
193
194 # Don't update repo if local changes exist
195 # Don't use buggy "git diff --quiet"
196 # ``errexit`` requires us to trap the exit code when the repo is changed
197 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
198
199 if [[ $update_requirements != "changed" ]]; then
200 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
201 if is_in_projects_txt $project_dir; then
202 (cd $REQUIREMENTS_DIR; \
203 python update.py $project_dir)
204 else
205 # soft update projects not found in requirements project.txt
206 (cd $REQUIREMENTS_DIR; \
207 python update.py -s $project_dir)
208 fi
209 else
210 (cd $REQUIREMENTS_DIR; \
211 python update.py $project_dir)
212 fi
213 fi
214
215 setup_package $project_dir $flags
216
217 # We've just gone and possibly modified the user's source tree in an
218 # automated way, which is considered bad form if it's a development
219 # tree because we've screwed up their next git checkin. So undo it.
220 #
221 # However... there are some circumstances, like running in the gate
222 # where we really really want the overridden version to stick. So provide
223 # a variable that tells us whether or not we should UNDO the requirements
224 # changes (this will be set to False in the OpenStack ci gate)
225 if [ $UNDO_REQUIREMENTS = "True" ]; then
226 if [[ $update_requirements != "changed" ]]; then
227 (cd $project_dir && git reset --hard)
228 fi
229 fi
230}
231
232# ``pip install -e`` the package, which processes the dependencies
233# using pip before running `setup.py develop`
234# Uses globals ``STACK_USER``
235# setup_develop_no_requirements_update directory
236function setup_package {
237 local project_dir=$1
238 local flags=$2
239
240 pip_install $flags $project_dir
241 # ensure that further actions can do things like setup.py sdist
242 if [[ "$flags" == "-e" ]]; then
243 safe_chown -R $STACK_USER $1/*.egg-info
244 fi
245}
246
247
248# Restore xtrace
249$INC_PY_TRACE
250
251# Local variables:
252# mode: shell-script
253# End: