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