Browse code

Merge "allow config to manage python3 use explicitly"

Jenkins authored on 2017/03/01 01:37:20
Showing 3 changed files
... ...
@@ -111,6 +111,111 @@ function check_python3_support_for_package_remote {
111 111
     echo $classifier
112 112
 }
113 113
 
114
+# python3_enabled_for() checks if the service(s) specified as arguments are
115
+# enabled by the user in ``ENABLED_PYTHON3_PACKAGES``.
116
+#
117
+# Multiple services specified as arguments are ``OR``'ed together; the test
118
+# is a short-circuit boolean, i.e it returns on the first match.
119
+#
120
+# Uses global ``ENABLED_PYTHON3_PACKAGES``
121
+# python3_enabled_for dir [dir ...]
122
+function python3_enabled_for {
123
+    local xtrace
124
+    xtrace=$(set +o | grep xtrace)
125
+    set +o xtrace
126
+
127
+    local enabled=1
128
+    local dirs=$@
129
+    local dir
130
+    for dir in ${dirs}; do
131
+        [[ ,${ENABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
132
+    done
133
+
134
+    $xtrace
135
+    return $enabled
136
+}
137
+
138
+# python3_disabled_for() checks if the service(s) specified as arguments are
139
+# disabled by the user in ``DISABLED_PYTHON3_PACKAGES``.
140
+#
141
+# Multiple services specified as arguments are ``OR``'ed together; the test
142
+# is a short-circuit boolean, i.e it returns on the first match.
143
+#
144
+# Uses global ``DISABLED_PYTHON3_PACKAGES``
145
+# python3_disabled_for dir [dir ...]
146
+function python3_disabled_for {
147
+    local xtrace
148
+    xtrace=$(set +o | grep xtrace)
149
+    set +o xtrace
150
+
151
+    local enabled=1
152
+    local dirs=$@
153
+    local dir
154
+    for dir in ${dirs}; do
155
+        [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]] && enabled=0
156
+    done
157
+
158
+    $xtrace
159
+    return $enabled
160
+}
161
+
162
+# enable_python3_package() adds the repositories passed as argument to the
163
+# ``ENABLED_PYTHON3_PACKAGES`` list, if they are not already present.
164
+#
165
+# For example:
166
+#   enable_python3_package nova
167
+#
168
+# Uses global ``ENABLED_PYTHON3_PACKAGES``
169
+# enable_python3_package dir [dir ...]
170
+function enable_python3_package {
171
+    local xtrace
172
+    xtrace=$(set +o | grep xtrace)
173
+    set +o xtrace
174
+
175
+    local tmpsvcs="${ENABLED_PYTHON3_PACKAGES}"
176
+    local python3
177
+    for dir in $@; do
178
+        if [[ ,${DISABLED_PYTHON3_PACKAGES}, =~ ,${dir}, ]]; then
179
+            warn $LINENO "Attempt to enable_python3_package ${dir} when it has been disabled"
180
+            continue
181
+        fi
182
+        if ! python3_enabled_for $dir; then
183
+            tmpsvcs+=",$dir"
184
+        fi
185
+    done
186
+    ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$tmpsvcs")
187
+
188
+    $xtrace
189
+}
190
+
191
+# disable_python3_package() prepares the services passed as argument to be
192
+# removed from the ``ENABLED_PYTHON3_PACKAGES`` list, if they are present.
193
+#
194
+# For example:
195
+#   disable_python3_package swift
196
+#
197
+# Uses globals ``ENABLED_PYTHON3_PACKAGES`` and ``DISABLED_PYTHON3_PACKAGES``
198
+# disable_python3_package dir [dir ...]
199
+function disable_python3_package {
200
+    local xtrace
201
+    xtrace=$(set +o | grep xtrace)
202
+    set +o xtrace
203
+
204
+    local disabled_svcs="${DISABLED_PYTHON3_PACKAGES}"
205
+    local enabled_svcs=",${ENABLED_PYTHON3_PACKAGES},"
206
+    local dir
207
+    for dir in $@; do
208
+        disabled_svcs+=",$dir"
209
+        if python3_enabled_for $dir; then
210
+            enabled_svcs=${enabled_svcs//,$dir,/,}
211
+        fi
212
+    done
213
+    DISABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$disabled_svcs")
214
+    ENABLED_PYTHON3_PACKAGES=$(_cleanup_service_list "$enabled_svcs")
215
+
216
+    $xtrace
217
+}
218
+
114 219
 # Wrapper for ``pip install`` to set cache and proxy environment variables
115 220
 # Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
116 221
 # ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``,
... ...
@@ -163,16 +268,16 @@ function pip_install {
163 163
                 # support for python3 in progress, but don't claim support
164 164
                 # in their classifier
165 165
                 echo "Check python version for : $package_dir"
166
-                if [[ ${package_dir##*/} == "nova" || ${package_dir##*/} == "glance" || \
167
-                        ${package_dir##*/} == "cinder" || ${package_dir##*/} == "swift" || \
168
-                        ${package_dir##*/} == "uwsgi" ]]; then
169
-                    echo "Using $PYTHON3_VERSION version to install $package_dir"
166
+                if python3_disabled_for ${package_dir##*/}; then
167
+                    echo "Explicitly using $PYTHON2_VERSION version to install $package_dir based on DISABLED_PYTHON3_PACKAGES"
168
+                elif python3_enabled_for ${package_dir##*/}; then
169
+                    echo "Explicitly using $PYTHON3_VERSION version to install $package_dir based on ENABLED_PYTHON3_PACKAGES"
170 170
                     sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
171 171
                     cmd_pip=$(get_pip_command $PYTHON3_VERSION)
172 172
                 elif [[ -d "$package_dir" ]]; then
173 173
                     python_versions=$(get_python_versions_for_package $package_dir)
174 174
                     if [[ $python_versions =~ $PYTHON3_VERSION ]]; then
175
-                        echo "Using $PYTHON3_VERSION version to install $package_dir"
175
+                        echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on classifiers"
176 176
                         sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
177 177
                         cmd_pip=$(get_pip_command $PYTHON3_VERSION)
178 178
                     else
... ...
@@ -181,7 +286,7 @@ function pip_install {
181 181
                         # a warning.
182 182
                         python3_classifier=$(check_python3_support_for_package_local $package_dir)
183 183
                         if [[ ! -z "$python3_classifier" ]]; then
184
-                            echo "Using $PYTHON3_VERSION version to install $package_dir"
184
+                            echo "Automatically using $PYTHON3_VERSION version to install $package_dir based on local package settings"
185 185
                             sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
186 186
                             cmd_pip=$(get_pip_command $PYTHON3_VERSION)
187 187
                         fi
... ...
@@ -191,7 +296,7 @@ function pip_install {
191 191
                     package=$(echo $package_dir | grep -o '^[.a-zA-Z0-9_-]*')
192 192
                     python3_classifier=$(check_python3_support_for_package_remote $package)
193 193
                     if [[ ! -z "$python3_classifier" ]]; then
194
-                        echo "Using $PYTHON3_VERSION version to install $package"
194
+                        echo "Automatically using $PYTHON3_VERSION version to install $package based on remote package settings"
195 195
                         sudo_pip="$sudo_pip LC_ALL=en_US.UTF-8"
196 196
                         cmd_pip=$(get_pip_command $PYTHON3_VERSION)
197 197
                     fi
... ...
@@ -102,9 +102,19 @@ if [[ -r $RC_DIR/.localrc.password ]]; then
102 102
     source $RC_DIR/.localrc.password
103 103
 fi
104 104
 
105
-# Control whether Python 3 should be used.
105
+# Control whether Python 3 should be used at all.
106 106
 export USE_PYTHON3=$(trueorfalse False USE_PYTHON3)
107 107
 
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
+
108 118
 # When Python 3 is supported by an application, adding the specific
109 119
 # version of Python 3 to this variable will install the app using that
110 120
 # version of the interpreter instead of 2.7.
111 121
new file mode 100755
... ...
@@ -0,0 +1,30 @@
0
+#!/usr/bin/env bash
1
+
2
+# Tests for DevStack INI functions
3
+
4
+TOP=$(cd $(dirname "$0")/.. && pwd)
5
+
6
+source $TOP/functions-common
7
+source $TOP/inc/python
8
+
9
+source $TOP/tests/unittest.sh
10
+
11
+echo "Testing Python 3 functions"
12
+
13
+# Initialize variables manipulated by functions under test.
14
+export ENABLED_PYTHON3_PACKAGES=""
15
+export DISABLED_PYTHON3_PACKAGES=""
16
+
17
+assert_false "should not be enabled yet" python3_enabled_for testpackage1
18
+
19
+enable_python3_package testpackage1
20
+assert_equal "$ENABLED_PYTHON3_PACKAGES" "testpackage1"  "unexpected result"
21
+assert_true "should be enabled" python3_enabled_for testpackage1
22
+
23
+assert_false "should not be disabled yet" python3_disabled_for testpackage2
24
+
25
+disable_python3_package testpackage2
26
+assert_equal "$DISABLED_PYTHON3_PACKAGES" "testpackage2"  "unexpected result"
27
+assert_true "should be disabled" python3_disabled_for testpackage2
28
+
29
+report_results