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