Skip to content

Commit ddc3839

Browse files
dhellmanndims
authored andcommitted
Enable optional Python 3 support
Add USE_PYTHON3 and PYTHON3_VERSION variables to allow services to use python 3 if they indicate support in their python package metadata. Tested in Heat here -> I837c2fba682ab430d50e9f43913f2fed20325a7a. Project config change to add a dedicated job to Heat is here -> I0837e62d6ccc66397a5e409f0961edd4be31f467 Change-Id: I079e18b58b214bf8362945c253d6d894ca8b1a6b
1 parent b889294 commit ddc3839

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

inc/python

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ declare -A PROJECT_VENV
2828
# Get the path to the pip command.
2929
# get_pip_command
3030
function get_pip_command {
31-
which pip || which pip-python
31+
local version="$1"
32+
# NOTE(dhellmann): I don't know if we actually get a pip3.4-python
33+
# under any circumstances.
34+
which pip${version} || which pip${version}-python
3235

3336
if [ $? -ne 0 ]; then
34-
die $LINENO "Unable to find pip; cannot continue"
37+
die $LINENO "Unable to find pip${version}; cannot continue"
3538
fi
3639
}
3740

@@ -66,6 +69,13 @@ function pip_install_gr {
6669
pip_install $clean_name
6770
}
6871

72+
# Determine the python versions supported by a package
73+
function get_python_versions_for_package {
74+
local name=$1
75+
cd $name && python setup.py --classifiers \
76+
| grep 'Language' | cut -f5 -d: | grep '\.' | tr '\n' ' '
77+
}
78+
6979
# Wrapper for ``pip install`` to set cache and proxy environment variables
7080
# Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
7181
# ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
@@ -104,15 +114,31 @@ function pip_install {
104114
local sudo_pip="env"
105115
else
106116
local cmd_pip
107-
cmd_pip=$(get_pip_command)
117+
cmd_pip=$(get_pip_command $PYTHON2_VERSION)
108118
local sudo_pip="sudo -H"
119+
if python3_enabled; then
120+
# Look at the package classifiers to find the python
121+
# versions supported, and if we find the version of
122+
# python3 we've been told to use, use that instead of the
123+
# default pip
124+
local package_dir=${!#}
125+
local python_versions
126+
if [[ -d "$package_dir" ]]; then
127+
python_versions=$(get_python_versions_for_package $package_dir)
128+
if [[ $python_versions =~ $PYTHON3_VERSION ]]; then
129+
cmd_pip=$(get_pip_command $PYTHON3_VERSION)
130+
fi
131+
fi
132+
fi
109133
fi
110134
fi
111135

112136
cmd_pip="$cmd_pip install"
113137
# Always apply constraints
114138
cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
115139

140+
# FIXME(dhellmann): Need to force multiple versions of pip for
141+
# packages like setuptools?
116142
local pip_version
117143
pip_version=$(python -c "import pip; \
118144
print(pip.__version__.strip('.')[0])")
@@ -276,6 +302,21 @@ function setup_package {
276302
fi
277303
}
278304

305+
# Report whether python 3 should be used
306+
function python3_enabled {
307+
if [[ $USE_PYTHON3 == "True" ]]; then
308+
return 0
309+
else
310+
return 1
311+
fi
312+
}
313+
314+
# Install python3 packages
315+
function install_python3 {
316+
if is_ubuntu; then
317+
apt_get install python3.4 python3.4-dev
318+
fi
319+
}
279320

280321
# Restore xtrace
281322
$INC_PY_TRACE

lib/stack

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
function stack_install_service {
2020
local service=$1
2121
if type install_${service} >/dev/null 2>&1; then
22+
# FIXME(dhellmann): Needs to be python3-aware at some point.
2223
if [[ ${USE_VENV} = True && -n ${PROJECT_VENV[$service]:-} ]]; then
2324
rm -rf ${PROJECT_VENV[$service]}
2425
source $TOP_DIR/tools/build_venv.sh ${PROJECT_VENV[$service]} ${ADDITIONAL_VENV_PACKAGES//,/ }

stackrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ if [[ -r $RC_DIR/.localrc.password ]]; then
118118
source $RC_DIR/.localrc.password
119119
fi
120120

121+
# Control whether Python 3 should be used.
122+
export USE_PYTHON3=${USE_PYTHON3:-False}
123+
124+
# When Python 3 is supported by an application, adding the specific
125+
# version of Python 3 to this variable will install the app using that
126+
# version of the interpreter instead of 2.7.
127+
export PYTHON3_VERSION=${PYTHON3_VERSION:-3.4}
128+
129+
# Just to be more explicit on the Python 2 version to use.
130+
export PYTHON2_VERSION=${PYTHON2_VERSION:-2.7}
131+
121132
# allow local overrides of env variables, including repo config
122133
if [[ -f $RC_DIR/localrc ]]; then
123134
# Old-style user-supplied config

tools/install_pip.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# Assumptions:
1010
# - update pip to $INSTALL_PIP_VERSION
11+
# - if USE_PYTHON3=True, PYTHON3_VERSION refers to a version already installed
1112

1213
set -o errexit
1314
set -o xtrace
@@ -31,6 +32,8 @@ GetDistro
3132
echo "Distro: $DISTRO"
3233

3334
function get_versions {
35+
# FIXME(dhellmann): Deal with multiple python versions here? This
36+
# is just used for reporting, so maybe not?
3437
PIP=$(which pip 2>/dev/null || which pip-python 2>/dev/null || true)
3538
if [[ -n $PIP ]]; then
3639
PIP_VERSION=$($PIP --version | awk '{ print $2}')
@@ -75,6 +78,9 @@ function install_get_pip {
7578
touch $LOCAL_PIP.downloaded
7679
fi
7780
sudo -H -E python $LOCAL_PIP
81+
if python3_enabled; then
82+
sudo -H -E python${PYTHON3_VERSION} $LOCAL_PIP
83+
fi
7884
}
7985

8086

@@ -114,6 +120,7 @@ get_versions
114120
# python in f23 depends on the python-pip package
115121
if ! { is_fedora && [[ $DISTRO == "f23" ]]; }; then
116122
uninstall_package python-pip
123+
uninstall_package python3-pip
117124
fi
118125

119126
install_get_pip
@@ -122,6 +129,7 @@ if [[ -n $PYPI_ALTERNATIVE_URL ]]; then
122129
configure_pypi_alternative_url
123130
fi
124131

132+
set -x
125133
pip_install -U setuptools
126134

127135
get_versions

tools/install_prereqs.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ if [[ -n "$SYSLOG" && "$SYSLOG" != "False" ]]; then
8181
fi
8282
fi
8383

84+
if python3_enabled; then
85+
install_python3
86+
fi
8487

8588
# Mark end of run
8689
# ---------------

0 commit comments

Comments
 (0)