Skip to content

Commit 94129c7

Browse files
committed
allow config to manage python3 use explicitly
Add variables ENABLED_PYTHON3_PACKAGES and DISABLED_PYTHON3_PACKAGES to work like ENABLED_SERVICES and DISABLED_SERVICES and to manage which packages are installed using Python 3. Move the list of whitelisted packages in pip_install to the default for ENABLED_PYTHON3_PACKAGES, except swift which is not enabled by default for now. Add enable_python3_package and disable_python3_package functions to make editing the variables from local.conf easier. Add python3_enabled_for and python3_disabled_for functions to check the settings against packages being installed by pip. Update pip_install to check if python3 is disabled for a service, then see if it is explicitly enabled, and only then fall back to looking at the classifiers in the packaging metadata. Update pip_install messages to give more detail about why the choice between python 2 and 3 is being made for a given package. Change-Id: I69857d4e11f4767928614a3b637c894bcd03491f Signed-off-by: Doug Hellmann <[email protected]>
1 parent 858a105 commit 94129c7

File tree

3 files changed

+153
-8
lines changed

3 files changed

+153
-8
lines changed

inc/python

Lines changed: 112 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,111 @@ function check_python3_support_for_package_remote {
9797
echo $classifier
9898
}
9999

100+
# python3_enabled_for() checks if the service(s) specified as arguments are
101+
# enabled by the user in ``ENABLED_PYTHON3_PACKAGES``.
102+
#
103+
# Multiple services specified as arguments are ``OR``'ed together; the test
104+
# is a short-circuit boolean, i.e it returns on the first match.
105+
#
106+
# Uses global ``ENABLED_PYTHON3_PACKAGES``
107+
# python3_enabled_for dir [dir ...]
108+
function python3_enabled_for {
109+
local xtrace
110+
xtrace=$(set +o | grep xtrace)
111+
set +o xtrace
112+
113+
local enabled=1
114+
local dirs=$@
115+
local dir
116+
for dir in ${dirs}; do
117+
[[ ,${ENABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
118+
done
119+
120+
$xtrace
121+
return $enabled
122+
}
123+
124+
# python3_disabled_for() checks if the service(s) specified as arguments are
125+
# disabled by the user in ``DISABLED_PYTHON3_PACKAGES``.
126+
#
127+
# Multiple services specified as arguments are ``OR``'ed together; the test
128+
# is a short-circuit boolean, i.e it returns on the first match.
129+
#
130+
# Uses global ``DISABLED_PYTHON3_PACKAGES``
131+
# python3_disabled_for dir [dir ...]
132+
function python3_disabled_for {
133+
local xtrace
134+
xtrace=$(set +o | grep xtrace)
135+
set +o xtrace
136+
137+
local enabled=1
138+
local dirs=$@
139+
local dir
140+
for dir in ${dirs}; do
141+
[[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
142+
done
143+
144+
$xtrace
145+
return $enabled
146+
}
147+
148+
# enable_python3_package() adds the repositories passed as argument to the
149+
# ``ENABLED_PYTHON3_PACKAGES`` list, if they are not already present.
150+
#
151+
# For example:
152+
# enable_python3_package nova
153+
#
154+
# Uses global ``ENABLED_PYTHON3_PACKAGES``
155+
# enable_python3_package dir [dir ...]
156+
function enable_python3_package {
157+
local xtrace
158+
xtrace=$(set +o | grep xtrace)
159+
set +o xtrace
160+
161+
local tmpsvcs="${ENABLED_PYTHON3_PACKAGES}"
162+
local python3
163+
for dir in $@; do
164+
if [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]]; then
165+
warn $LINENO "Attempt to enable_python3_package ${dir} when it has been disabled"
166+
continue
167+
fi
168+
if ! python3_enabled_for $dir; then
169+
tmpsvcs+=",$dir"
170+
fi
171+
done
172+
ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$tmpsvcs")
173+
174+
$xtrace
175+
}
176+
177+
# disable_python3_package() prepares the services passed as argument to be
178+
# removed from the ``ENABLED_PYTHON3_PACKAGES`` list, if they are present.
179+
#
180+
# For example:
181+
# disable_python3_package swift
182+
#
183+
# Uses globals ``ENABLED_PYTHON3_PACKAGES`` and ``DISABLED_PYTHON3_PACKAGES``
184+
# disable_python3_package dir [dir ...]
185+
function disable_python3_package {
186+
local xtrace
187+
xtrace=$(set +o | grep xtrace)
188+
set +o xtrace
189+
190+
local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}"
191+
local enabled_svcs=",${ENABLED_PYTHON3_PACKAGES},"
192+
local dir
193+
for dir in $@; do
194+
disabled_svcs+=",$dir"
195+
if python3_enabled_for $dir; then
196+
enabled_svcs=${enabled_svcs//,$dir,/,}
197+
fi
198+
done
199+
DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs")
200+
ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$enabled_svcs")
201+
202+
$xtrace
203+
}
204+
100205
# Wrapper for ``pip install`` to set cache and proxy environment variables
101206
# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
102207
# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
@@ -149,16 +254,16 @@ function pip_install {
149254
# support for python3 in progress, but don't claim support
150255
# in their classifier
151256
echo "Check python version for : $package_dir"
152-
if [[ ${package_dir##*/} == "nova" || ${package_dir##*/} == "glance" || \
153-
${package_dir##*/} == "cinder" || ${package_dir##*/} == "swift" || \
154-
${package_dir##*/} == "uwsgi" ]]; then
155-
echo "Using $PYTHON3_VERSION version to install $package_dir"
257+
if python3_disabled_for ${package_dir##*/}; then
258+
echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES"
259+
elif python3_enabled_for ${package_dir##*/}; then
260+
echo "Explicitly using $PYTHON3_VERSION version to install $package_dir based on ENABLED_PYTHON3_PACKAGES"
156261
sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
157262
cmd_pip=$(get_pip_command $PYTHON3_VERSION)
158263
elif [[ -d "$package_dir" ]]; then
159264
python_versions=$(get_python_versions_for_package $package_dir)
160265
if [[ $python_versions =~ $PYTHON3_VERSION ]]; then
161-
echo "Using $PYTHON3_VERSION version to install $package_dir"
266+
echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on classifiers"
162267
sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
163268
cmd_pip=$(get_pip_command $PYTHON3_VERSION)
164269
else
@@ -167,7 +272,7 @@ function pip_install {
167272
# a warning.
168273
python3_classifier=$(check_python3_support_for_package_local $package_dir)
169274
if [[ ! -z "$python3_classifier" ]]; then
170-
echo "Using $PYTHON3_VERSION version to install $package_dir"
275+
echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on local package settings"
171276
sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
172277
cmd_pip=$(get_pip_command $PYTHON3_VERSION)
173278
fi
@@ -177,7 +282,7 @@ function pip_install {
177282
package=$(echo $package_dir | grep -o '^[.a-zA-Z0-9_-]*')
178283
python3_classifier=$(check_python3_support_for_package_remote $package)
179284
if [[ ! -z "$python3_classifier" ]]; then
180-
echo "Using $PYTHON3_VERSION version to install $package"
285+
echo "Automatically using $PYTHON3_VERSION version to install $package based on remote package settings"
181286
sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
182287
cmd_pip=$(get_pip_command $PYTHON3_VERSION)
183288
fi

stackrc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,19 @@ if [[ -r $RC_DIR/.localrc.password ]]; then
102102
source $RC_DIR/.localrc.password
103103
fi
104104

105-
# Control whether Python 3 should be used.
105+
# Control whether Python 3 should be used at all.
106106
export USE_PYTHON3=$(trueorfalse False USE_PYTHON3)
107107

108+
# Control whether Python 3 is enabled for specific services by the
109+
# base name of the directory from which they are installed. See
110+
# enable_python3_package to edit this variable and use_python3_for to
111+
# test membership.
112+
export ENABLED_PYTHON3_PACKAGES="nova,glance,cinder,uwsgi"
113+
114+
# Explicitly list services not to run under Python 3. See
115+
# disable_python3_package to edit this variable.
116+
export DISABLED_PYTHON3_PACKAGES=""
117+
108118
# When Python 3 is supported by an application, adding the specific
109119
# version of Python 3 to this variable will install the app using that
110120
# version of the interpreter instead of 2.7.

tests/test_python.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# Tests for DevStack INI functions
4+
5+
TOP=$(cd $(dirname "$0")/.. && pwd)
6+
7+
source $TOP/functions-common
8+
source $TOP/inc/python
9+
10+
source $TOP/tests/unittest.sh
11+
12+
echo "Testing Python 3 functions"
13+
14+
# Initialize variables manipulated by functions under test.
15+
export ENABLED_PYTHON3_PACKAGES=""
16+
export DISABLED_PYTHON3_PACKAGES=""
17+
18+
assert_false "should not be enabled yet" python3_enabled_for testpackage1
19+
20+
enable_python3_package testpackage1
21+
assert_equal "$ENABLED_PYTHON3_PACKAGES" "testpackage1" "unexpected result"
22+
assert_true "should be enabled" python3_enabled_for testpackage1
23+
24+
assert_false "should not be disabled yet" python3_disabled_for testpackage2
25+
26+
disable_python3_package testpackage2
27+
assert_equal "$DISABLED_PYTHON3_PACKAGES" "testpackage2" "unexpected result"
28+
assert_true "should be disabled" python3_disabled_for testpackage2
29+
30+
report_results

0 commit comments

Comments
 (0)