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