blob: 0348cb389f279d03ebc32bd8a24f887dc64a7cba [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
98# should we use this library from their git repo, or should we let it
99# get pulled in via pip dependencies.
100function use_library_from_git {
101 local name=$1
102 local enabled=1
103 [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
104 return $enabled
105}
106
107# setup a library by name. If we are trying to use the library from
108# git, we'll do a git based install, otherwise we'll punt and the
109# library should be installed by a requirements pull from another
110# project.
111function setup_lib {
112 local name=$1
113 local dir=${GITDIR[$name]}
114 setup_install $dir
115}
116
117# setup a library by name in editiable mode. If we are trying to use
118# the library from git, we'll do a git based install, otherwise we'll
119# punt and the library should be installed by a requirements pull from
120# another project.
121#
122# use this for non namespaced libraries
123function setup_dev_lib {
124 local name=$1
125 local dir=${GITDIR[$name]}
126 setup_develop $dir
127}
128
129# this should be used if you want to install globally, all libraries should
130# use this, especially *oslo* ones
131function setup_install {
132 local project_dir=$1
133 setup_package_with_req_sync $project_dir
134}
135
136# this should be used for projects which run services, like all services
137function setup_develop {
138 local project_dir=$1
139 setup_package_with_req_sync $project_dir -e
140}
141
142# determine if a project as specified by directory is in
143# projects.txt. This will not be an exact match because we throw away
144# the namespacing when we clone, but it should be good enough in all
145# practical ways.
146function is_in_projects_txt {
147 local project_dir=$1
148 local project_name=$(basename $project_dir)
149 return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
150}
151
152# ``pip install -e`` the package, which processes the dependencies
153# using pip before running `setup.py develop`
154#
155# Updates the dependencies in project_dir from the
156# openstack/requirements global list before installing anything.
157#
158# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
159# setup_develop directory
160function setup_package_with_req_sync {
161 local project_dir=$1
162 local flags=$2
163
164 # Don't update repo if local changes exist
165 # Don't use buggy "git diff --quiet"
166 # ``errexit`` requires us to trap the exit code when the repo is changed
167 local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
168
169 if [[ $update_requirements != "changed" ]]; then
170 if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
171 if is_in_projects_txt $project_dir; then
172 (cd $REQUIREMENTS_DIR; \
173 python update.py $project_dir)
174 else
175 # soft update projects not found in requirements project.txt
176 (cd $REQUIREMENTS_DIR; \
177 python update.py -s $project_dir)
178 fi
179 else
180 (cd $REQUIREMENTS_DIR; \
181 python update.py $project_dir)
182 fi
183 fi
184
185 setup_package $project_dir $flags
186
187 # We've just gone and possibly modified the user's source tree in an
188 # automated way, which is considered bad form if it's a development
189 # tree because we've screwed up their next git checkin. So undo it.
190 #
191 # However... there are some circumstances, like running in the gate
192 # where we really really want the overridden version to stick. So provide
193 # a variable that tells us whether or not we should UNDO the requirements
194 # changes (this will be set to False in the OpenStack ci gate)
195 if [ $UNDO_REQUIREMENTS = "True" ]; then
196 if [[ $update_requirements != "changed" ]]; then
197 (cd $project_dir && git reset --hard)
198 fi
199 fi
200}
201
202# ``pip install -e`` the package, which processes the dependencies
203# using pip before running `setup.py develop`
204# Uses globals ``STACK_USER``
205# setup_develop_no_requirements_update directory
206function setup_package {
207 local project_dir=$1
208 local flags=$2
209
210 pip_install $flags $project_dir
211 # ensure that further actions can do things like setup.py sdist
212 if [[ "$flags" == "-e" ]]; then
213 safe_chown -R $STACK_USER $1/*.egg-info
214 fi
215}
216
217
218# Restore xtrace
219$INC_PY_TRACE
220
221# Local variables:
222# mode: shell-script
223# End: