| 1 | # Copyright 1999-2009 Gentoo Foundation |
1 | # Copyright 1999-2010 Gentoo Foundation |
| 2 | # Distributed under the terms of the GNU General Public License v2 |
2 | # Distributed under the terms of the GNU General Public License v2 |
| 3 | # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.75 2009/10/02 17:32:23 arfrever Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.84 2010/01/11 16:07:23 arfrever Exp $ |
| 4 | |
4 | |
| 5 | # @ECLASS: python.eclass |
5 | # @ECLASS: python.eclass |
| 6 | # @MAINTAINER: |
6 | # @MAINTAINER: |
| 7 | # python@gentoo.org |
7 | # python@gentoo.org |
| 8 | # @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules. |
8 | # @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules. |
| 9 | # @DESCRIPTION: |
9 | # @DESCRIPTION: |
| 10 | # Some useful functions for dealing with Python. |
10 | # Some useful functions for dealing with Python. |
| 11 | |
11 | |
| 12 | inherit multilib |
12 | inherit multilib |
| 13 | |
13 | |
|
|
14 | if ! has "${EAPI:-0}" 0 1 2; then |
|
|
15 | die "API of python.eclass in EAPI=\"${EAPI}\" not established" |
|
|
16 | fi |
|
|
17 | |
| 14 | if [[ -n "${NEED_PYTHON}" ]] ; then |
18 | if [[ -n "${NEED_PYTHON}" ]]; then |
| 15 | PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" |
19 | PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" |
| 16 | DEPEND="${PYTHON_ATOM}" |
20 | DEPEND="${PYTHON_ATOM}" |
| 17 | RDEPEND="${DEPEND}" |
21 | RDEPEND="${DEPEND}" |
| 18 | else |
22 | else |
| 19 | PYTHON_ATOM="dev-lang/python" |
23 | PYTHON_ATOM="dev-lang/python" |
| 20 | fi |
24 | fi |
| 21 | |
25 | |
| 22 | DEPEND+=" >=app-admin/eselect-python-20090804 |
26 | DEPEND+=" >=app-admin/eselect-python-20090804" |
| 23 | >=app-shells/bash-3.2" |
|
|
| 24 | |
27 | |
| 25 | __python_eclass_test() { |
28 | __python_eclass_test() { |
| 26 | __python_version_extract 2.3 |
29 | __python_version_extract 2.3 |
| 27 | echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" |
30 | echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" |
| 28 | echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" |
31 | echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" |
| … | |
… | |
| 56 | |
59 | |
| 57 | python_version() { |
60 | python_version() { |
| 58 | [[ -n "${PYVER}" ]] && return 0 |
61 | [[ -n "${PYVER}" ]] && return 0 |
| 59 | local tmpstr |
62 | local tmpstr |
| 60 | python=${python:-/usr/bin/python} |
63 | python=${python:-/usr/bin/python} |
| 61 | tmpstr="$(${python} -V 2>&1 )" |
64 | tmpstr="$(EPYTHON= ${python} -V 2>&1 )" |
| 62 | export PYVER_ALL="${tmpstr#Python }" |
65 | export PYVER_ALL="${tmpstr#Python }" |
| 63 | __python_version_extract $PYVER_ALL |
66 | __python_version_extract $PYVER_ALL |
| 64 | } |
67 | } |
| 65 | |
68 | |
| 66 | # @FUNCTION: PYTHON |
69 | # @FUNCTION: PYTHON |
| 67 | # @USAGE: [-a|--absolute-path] [--] <Python_ABI="${PYTHON_ABI}"> |
70 | # @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}"> |
| 68 | # @DESCRIPTION: |
71 | # @DESCRIPTION: |
| 69 | # Get Python interpreter filename for specified Python ABI. If Python_ABI argument |
72 | # Get Python interpreter filename for specified Python ABI. If Python_ABI argument |
| 70 | # is ommitted, then PYTHON_ABI environment variable must be set and is used. |
73 | # is ommitted, then PYTHON_ABI environment variable must be set and is used. |
|
|
74 | # If -2 option is specified, then active version of Python 2 is used. |
|
|
75 | # If -3 option is specified, then active version of Python 3 is used. |
|
|
76 | # If --active option is specified, then active version of Python is used. |
|
|
77 | # Active version of Python can be set by python_set_active_version(). |
|
|
78 | # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used. |
|
|
79 | # -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously. |
|
|
80 | # If --ABI option is specified, then only specified Python ABI is printed instead of |
|
|
81 | # Python interpreter filename. |
|
|
82 | # --ABI and --absolute-path options cannot be specified simultaneously. |
| 71 | PYTHON() { |
83 | PYTHON() { |
| 72 | local absolute_path="0" slot= |
84 | local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" python2="0" python3="0" slot= |
| 73 | |
85 | |
| 74 | while (($#)); do |
86 | while (($#)); do |
| 75 | case "$1" in |
87 | case "$1" in |
|
|
88 | -2) |
|
|
89 | python2="1" |
|
|
90 | ;; |
|
|
91 | -3) |
|
|
92 | python3="1" |
|
|
93 | ;; |
|
|
94 | --ABI) |
|
|
95 | ABI_output="1" |
|
|
96 | ;; |
|
|
97 | -A|--active) |
|
|
98 | active="1" |
|
|
99 | ;; |
| 76 | -a|--absolute-path) |
100 | -a|--absolute-path) |
| 77 | absolute_path="1" |
101 | absolute_path_output="1" |
|
|
102 | ;; |
|
|
103 | -f|--final-ABI) |
|
|
104 | final_ABI="1" |
| 78 | ;; |
105 | ;; |
| 79 | --) |
106 | --) |
| 80 | break |
107 | break |
| 81 | ;; |
108 | ;; |
| 82 | -*) |
109 | -*) |
| 83 | die "${FUNCNAME}(): Unrecognized option $1" |
110 | die "${FUNCNAME}(): Unrecognized option '$1'" |
| 84 | ;; |
111 | ;; |
| 85 | *) |
112 | *) |
| 86 | break |
113 | break |
| 87 | ;; |
114 | ;; |
| 88 | esac |
115 | esac |
| 89 | shift |
116 | shift |
| 90 | done |
117 | done |
| 91 | |
118 | |
|
|
119 | if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then |
|
|
120 | die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously" |
|
|
121 | fi |
|
|
122 | |
|
|
123 | if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then |
|
|
124 | die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously" |
|
|
125 | fi |
|
|
126 | |
| 92 | if [[ "$#" -eq "0" ]]; then |
127 | if [[ "$#" -eq 0 ]]; then |
|
|
128 | if [[ "${active}" == "1" ]]; then |
|
|
129 | if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then |
|
|
130 | die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python" |
|
|
131 | fi |
|
|
132 | slot="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
|
|
133 | elif [[ "${final_ABI}" == "1" ]]; then |
|
|
134 | validate_PYTHON_ABIS |
|
|
135 | slot="${PYTHON_ABIS##* }" |
|
|
136 | elif [[ "${python2}" == "1" ]]; then |
|
|
137 | slot="$(eselect python show --python2)" |
|
|
138 | if [[ -z "${slot}" ]]; then |
|
|
139 | die "${FUNCNAME}(): Active Python 2 interpreter not set" |
|
|
140 | elif [[ "${slot}" != "python2."* ]]; then |
|
|
141 | die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`" |
|
|
142 | fi |
|
|
143 | slot="${slot#python}" |
|
|
144 | elif [[ "${python3}" == "1" ]]; then |
|
|
145 | slot="$(eselect python show --python3)" |
|
|
146 | if [[ -z "${slot}" ]]; then |
|
|
147 | die "${FUNCNAME}(): Active Python 3 interpreter not set" |
|
|
148 | elif [[ "${slot}" != "python3."* ]]; then |
|
|
149 | die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`" |
|
|
150 | fi |
|
|
151 | slot="${slot#python}" |
| 93 | if [[ -n "${PYTHON_ABI}" ]]; then |
152 | elif [[ -n "${PYTHON_ABI}" ]]; then |
| 94 | slot="${PYTHON_ABI}" |
153 | slot="${PYTHON_ABI}" |
| 95 | else |
154 | else |
| 96 | die "${FUNCNAME}(): Invalid usage" |
155 | die "${FUNCNAME}(): Invalid usage" |
| 97 | fi |
156 | fi |
| 98 | elif [[ "$#" -eq "1" ]]; then |
157 | elif [[ "$#" -eq 1 ]]; then |
|
|
158 | if [[ "${active}" == "1" ]]; then |
|
|
159 | die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously" |
|
|
160 | fi |
|
|
161 | if [[ "${final_ABI}" == "1" ]]; then |
|
|
162 | die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously" |
|
|
163 | fi |
|
|
164 | if [[ "${python2}" == "1" ]]; then |
|
|
165 | die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously" |
|
|
166 | fi |
|
|
167 | if [[ "${python3}" == "1" ]]; then |
|
|
168 | die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously" |
|
|
169 | fi |
| 99 | slot="$1" |
170 | slot="$1" |
| 100 | else |
171 | else |
| 101 | die "${FUNCNAME}(): Invalid usage" |
172 | die "${FUNCNAME}(): Invalid usage" |
| 102 | fi |
173 | fi |
| 103 | |
174 | |
|
|
175 | if [[ "${ABI_output}" == "1" ]]; then |
|
|
176 | echo -n "${slot}" |
|
|
177 | return |
| 104 | if [[ "${absolute_path}" == "1" ]]; then |
178 | elif [[ "${absolute_path_output}" == "1" ]]; then |
| 105 | echo -n "/usr/bin/python${slot}" |
179 | echo -n "/usr/bin/python${slot}" |
| 106 | else |
180 | else |
| 107 | echo -n "python${slot}" |
181 | echo -n "python${slot}" |
| 108 | fi |
182 | fi |
|
|
183 | |
|
|
184 | if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then |
|
|
185 | echo -n "-${ABI}" |
|
|
186 | fi |
| 109 | } |
187 | } |
|
|
188 | |
|
|
189 | _python_implementation() { |
|
|
190 | if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then |
|
|
191 | return 0 |
|
|
192 | else |
|
|
193 | return 1 |
|
|
194 | fi |
|
|
195 | } |
|
|
196 | |
|
|
197 | # @FUNCTION: python_set_active_version |
|
|
198 | # @USAGE: <Python_ABI|2|3> |
|
|
199 | # @DESCRIPTION: |
|
|
200 | # Set active version of Python. |
|
|
201 | python_set_active_version() { |
|
|
202 | if [[ "$#" -ne "1" ]]; then |
|
|
203 | die "${FUNCNAME}() requires 1 argument" |
|
|
204 | fi |
|
|
205 | |
|
|
206 | if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then |
|
|
207 | if ! _python_implementation && ! has_version "dev-lang/python:$1"; then |
|
|
208 | die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed" |
|
|
209 | fi |
|
|
210 | export EPYTHON="$(PYTHON "$1")" |
|
|
211 | elif [[ "$1" == "2" ]]; then |
|
|
212 | if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then |
|
|
213 | die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed" |
|
|
214 | fi |
|
|
215 | export EPYTHON="$(PYTHON -2)" |
|
|
216 | elif [[ "$1" == "3" ]]; then |
|
|
217 | if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then |
|
|
218 | die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed" |
|
|
219 | fi |
|
|
220 | export EPYTHON="$(PYTHON -3)" |
|
|
221 | else |
|
|
222 | die "${FUNCNAME}(): Unrecognized argument '$1'" |
|
|
223 | fi |
|
|
224 | |
|
|
225 | # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses, |
|
|
226 | # so it does not need to be exported to subprocesses. |
|
|
227 | PYTHON_ABI="${EPYTHON#python}" |
|
|
228 | PYTHON_ABI="${PYTHON_ABI%%-*}" |
|
|
229 | } |
|
|
230 | |
|
|
231 | unset PYTHON_ABIS |
|
|
232 | unset PYTHON_ABIS_SANITY_CHECKS |
| 110 | |
233 | |
| 111 | # @FUNCTION: validate_PYTHON_ABIS |
234 | # @FUNCTION: validate_PYTHON_ABIS |
| 112 | # @DESCRIPTION: |
235 | # @DESCRIPTION: |
| 113 | # Make sure PYTHON_ABIS variable has valid value. |
236 | # Ensure that PYTHON_ABIS variable has valid value. |
| 114 | validate_PYTHON_ABIS() { |
237 | validate_PYTHON_ABIS() { |
| 115 | # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable. |
238 | # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable. |
| 116 | if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then |
239 | if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then |
| 117 | die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable" |
240 | die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable" |
| 118 | fi |
241 | fi |
| 119 | |
242 | |
| 120 | # Ensure that /usr/bin/python and /usr/bin/python-config are valid. |
243 | # Ensure that /usr/bin/python and /usr/bin/python-config are valid. |
| 121 | if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then |
244 | if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then |
|
|
245 | eerror "'/usr/bin/python' is not valid symlink." |
|
|
246 | eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem." |
| 122 | die "'/usr/bin/python' isn't valid symlink" |
247 | die "'/usr/bin/python' is not valid symlink" |
| 123 | fi |
248 | fi |
| 124 | if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then |
249 | if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then |
|
|
250 | eerror "'/usr/bin/python-config' is not valid script" |
|
|
251 | eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem." |
| 125 | die "'/usr/bin/python-config' isn't valid script" |
252 | die "'/usr/bin/python-config' is not valid script" |
| 126 | fi |
253 | fi |
| 127 | |
254 | |
| 128 | # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. |
255 | # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5. |
| 129 | if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then |
256 | if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then |
| 130 | local ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS= |
257 | local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS= |
| 131 | PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" |
258 | PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" |
| 132 | python2_supported_versions="2.4 2.5 2.6 2.7" |
259 | python2_supported_versions="2.4 2.5 2.6 2.7" |
| 133 | python3_supported_versions="3.0 3.1 3.2" |
260 | python3_supported_versions="3.0 3.1 3.2" |
| 134 | |
261 | |
| 135 | if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then |
262 | if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then |
| … | |
… | |
| 137 | |
264 | |
| 138 | if [[ -z "${USE_PYTHON}" ]]; then |
265 | if [[ -z "${USE_PYTHON}" ]]; then |
| 139 | die "USE_PYTHON variable is empty" |
266 | die "USE_PYTHON variable is empty" |
| 140 | fi |
267 | fi |
| 141 | |
268 | |
| 142 | for ABI in ${USE_PYTHON}; do |
269 | for PYTHON_ABI in ${USE_PYTHON}; do |
| 143 | if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then |
270 | if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then |
| 144 | die "USE_PYTHON variable contains invalid value '${ABI}'" |
271 | die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'" |
| 145 | fi |
272 | fi |
| 146 | |
273 | |
| 147 | if has "${ABI}" ${python2_supported_versions}; then |
274 | if has "${PYTHON_ABI}" ${python2_supported_versions}; then |
| 148 | python2_enabled="1" |
275 | python2_enabled="1" |
| 149 | fi |
276 | fi |
| 150 | if has "${ABI}" ${python3_supported_versions}; then |
277 | if has "${PYTHON_ABI}" ${python3_supported_versions}; then |
| 151 | python3_enabled="1" |
278 | python3_enabled="1" |
| 152 | fi |
279 | fi |
| 153 | |
280 | |
| 154 | support_ABI="1" |
281 | support_ABI="1" |
| 155 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
282 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
| 156 | if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then |
283 | if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then |
| 157 | support_ABI="0" |
284 | support_ABI="0" |
| 158 | break |
285 | break |
| 159 | fi |
286 | fi |
| 160 | done |
287 | done |
| 161 | [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" |
288 | [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}" |
| 162 | done |
289 | done |
| 163 | export PYTHON_ABIS="${supported_PYTHON_ABIS# }" |
|
|
| 164 | |
290 | |
| 165 | if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then |
291 | if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then |
| 166 | die "USE_PYTHON variable doesn't enable any version of Python supported by ${CATEGORY}/${PF}" |
292 | die "USE_PYTHON variable does not enable any version of Python supported by ${CATEGORY}/${PF}" |
| 167 | fi |
293 | fi |
| 168 | |
294 | |
| 169 | if [[ "${python2_enabled}" == "0" ]]; then |
295 | if [[ "${python2_enabled}" == "0" ]]; then |
| 170 | ewarn "USE_PYTHON variable doesn't enable any version of Python 2. This configuration is unsupported." |
296 | ewarn "USE_PYTHON variable does not enable any version of Python 2. This configuration is unsupported." |
| 171 | fi |
297 | fi |
| 172 | if [[ "${python3_enabled}" == "0" ]]; then |
298 | if [[ "${python3_enabled}" == "0" ]]; then |
| 173 | ewarn "USE_PYTHON variable doesn't enable any version of Python 3. This configuration is unsupported." |
299 | ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported." |
| 174 | fi |
300 | fi |
| 175 | else |
301 | else |
| 176 | local python2_version= python3_version= support_python_major_version |
302 | local python_version python2_version= python3_version= support_python_major_version |
|
|
303 | |
|
|
304 | python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
| 177 | |
305 | |
| 178 | if has_version "=dev-lang/python-2*"; then |
306 | if has_version "=dev-lang/python-2*"; then |
| 179 | if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then |
307 | if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then |
| 180 | die "'/usr/bin/python2' isn't valid symlink" |
308 | die "'/usr/bin/python2' is not valid symlink" |
| 181 | fi |
309 | fi |
| 182 | |
310 | |
| 183 | python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
311 | python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
| 184 | |
312 | |
| 185 | for ABI in ${python2_supported_versions}; do |
313 | for PYTHON_ABI in ${python2_supported_versions}; do |
| 186 | support_python_major_version="1" |
314 | support_python_major_version="1" |
| 187 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
315 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
| 188 | if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then |
316 | if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then |
| 189 | support_python_major_version="0" |
317 | support_python_major_version="0" |
| 190 | fi |
318 | fi |
| 191 | done |
319 | done |
| 192 | [[ "${support_python_major_version}" == "1" ]] && break |
320 | [[ "${support_python_major_version}" == "1" ]] && break |
| 193 | done |
321 | done |
| 194 | if [[ "${support_python_major_version}" == "1" ]]; then |
322 | if [[ "${support_python_major_version}" == "1" ]]; then |
| 195 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
323 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
| 196 | if python -c "from fnmatch import fnmatch; exit(not fnmatch('${python2_version}', '${restricted_ABI}'))"; then |
324 | if [[ "${python2_version}" == ${restricted_ABI} ]]; then |
| 197 | die "Active version of Python 2 isn't supported by ${CATEGORY}/${PF}" |
325 | die "Active version of Python 2 is not supported by ${CATEGORY}/${PF}" |
| 198 | fi |
326 | fi |
| 199 | done |
327 | done |
| 200 | else |
328 | else |
| 201 | python2_version="" |
329 | python2_version="" |
| 202 | fi |
330 | fi |
| 203 | fi |
331 | fi |
| 204 | |
332 | |
| 205 | if has_version "=dev-lang/python-3*"; then |
333 | if has_version "=dev-lang/python-3*"; then |
| 206 | if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then |
334 | if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then |
| 207 | die "'/usr/bin/python3' isn't valid symlink" |
335 | die "'/usr/bin/python3' is not valid symlink" |
| 208 | fi |
336 | fi |
| 209 | |
337 | |
| 210 | python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
338 | python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
| 211 | |
339 | |
| 212 | for ABI in ${python3_supported_versions}; do |
340 | for PYTHON_ABI in ${python3_supported_versions}; do |
| 213 | support_python_major_version="1" |
341 | support_python_major_version="1" |
| 214 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
342 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
| 215 | if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then |
343 | if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then |
| 216 | support_python_major_version="0" |
344 | support_python_major_version="0" |
| 217 | fi |
345 | fi |
| 218 | done |
346 | done |
| 219 | [[ "${support_python_major_version}" == "1" ]] && break |
347 | [[ "${support_python_major_version}" == "1" ]] && break |
| 220 | done |
348 | done |
| 221 | if [[ "${support_python_major_version}" == "1" ]]; then |
349 | if [[ "${support_python_major_version}" == "1" ]]; then |
| 222 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
350 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
| 223 | if python -c "from fnmatch import fnmatch; exit(not fnmatch('${python3_version}', '${restricted_ABI}'))"; then |
351 | if [[ "${python3_version}" == ${restricted_ABI} ]]; then |
| 224 | die "Active version of Python 3 isn't supported by ${CATEGORY}/${PF}" |
352 | die "Active version of Python 3 is not supported by ${CATEGORY}/${PF}" |
| 225 | fi |
353 | fi |
| 226 | done |
354 | done |
| 227 | else |
355 | else |
| 228 | python3_version="" |
356 | python3_version="" |
| 229 | fi |
357 | fi |
| 230 | fi |
358 | fi |
| 231 | |
359 | |
|
|
360 | if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then |
|
|
361 | eerror "Python wrapper is configured incorrectly or /usr/bin/python2 symlink" |
|
|
362 | eerror "is set incorrectly. Use \`eselect python\` to fix configuration." |
|
|
363 | die "Incorrect configuration of Python" |
|
|
364 | fi |
|
|
365 | if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then |
|
|
366 | eerror "Python wrapper is configured incorrectly or /usr/bin/python3 symlink" |
|
|
367 | eerror "is set incorrectly. Use \`eselect python\` to fix configuration." |
|
|
368 | die "Incorrect configuration of Python" |
|
|
369 | fi |
|
|
370 | |
| 232 | PYTHON_ABIS="${python2_version} ${python3_version}" |
371 | PYTHON_ABIS="${python2_version} ${python3_version}" |
| 233 | PYTHON_ABIS="${PYTHON_ABIS# }" |
372 | PYTHON_ABIS="${PYTHON_ABIS# }" |
| 234 | export PYTHON_ABIS="${PYTHON_ABIS% }" |
373 | export PYTHON_ABIS="${PYTHON_ABIS% }" |
| 235 | fi |
374 | fi |
| 236 | fi |
375 | fi |
| 237 | |
376 | |
|
|
377 | if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then |
| 238 | local PYTHON_ABI |
378 | local PYTHON_ABI |
| 239 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
379 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
| 240 | # Ensure that appropriate Python version is installed. |
380 | # Ensure that appropriate version of Python is installed. |
| 241 | if ! has_version "dev-lang/python:${PYTHON_ABI}"; then |
381 | if ! has_version "dev-lang/python:${PYTHON_ABI}"; then |
| 242 | die "dev-lang/python:${PYTHON_ABI} isn't installed" |
382 | die "dev-lang/python:${PYTHON_ABI} is not installed" |
| 243 | fi |
383 | fi |
| 244 | |
384 | |
| 245 | # Ensure that EPYTHON variable is respected. |
385 | # Ensure that EPYTHON variable is respected. |
| 246 | if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then |
386 | if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then |
|
|
387 | eerror "python: '$(type -p python)'" |
|
|
388 | eerror "ABI: '${ABI}'" |
|
|
389 | eerror "DEFAULT_ABI: '${DEFAULT_ABI}'" |
|
|
390 | eerror "EPYTHON: '$(PYTHON)'" |
|
|
391 | eerror "PYTHON_ABI: '${PYTHON_ABI}'" |
|
|
392 | eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')'" |
| 247 | die "'python' doesn't respect EPYTHON variable" |
393 | die "'python' does not respect EPYTHON variable" |
| 248 | fi |
394 | fi |
| 249 | done |
395 | done |
|
|
396 | PYTHON_ABIS_SANITY_CHECKS="1" |
|
|
397 | fi |
| 250 | } |
398 | } |
| 251 | |
399 | |
| 252 | # @FUNCTION: python_copy_sources |
400 | # @FUNCTION: python_copy_sources |
| 253 | # @USAGE: [--no-link] [--] [directory] |
401 | # @USAGE: [--no-link] [--] [directory] |
| 254 | # @DESCRIPTION: |
402 | # @DESCRIPTION: |
| … | |
… | |
| 272 | ;; |
420 | ;; |
| 273 | esac |
421 | esac |
| 274 | shift |
422 | shift |
| 275 | done |
423 | done |
| 276 | |
424 | |
| 277 | if [[ "$#" -eq "0" ]]; then |
425 | if [[ "$#" -eq 0 ]]; then |
| 278 | if [[ "${WORKDIR}" == "${S}" ]]; then |
426 | if [[ "${WORKDIR}" == "${S}" ]]; then |
| 279 | die "${FUNCNAME}() cannot be used" |
427 | die "${FUNCNAME}() cannot be used" |
| 280 | fi |
428 | fi |
| 281 | dirs="${S}" |
429 | dirs="${S}" |
| 282 | else |
430 | else |
| … | |
… | |
| 303 | local dir="$1" |
451 | local dir="$1" |
| 304 | |
452 | |
| 305 | [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set" |
453 | [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set" |
| 306 | [[ -z "${dir}" ]] && dir="build" |
454 | [[ -z "${dir}" ]] && dir="build" |
| 307 | |
455 | |
| 308 | # Don't delete preexistent directories. |
456 | # Do not delete preexistent directories. |
| 309 | rm -f "${dir}" || die "Deletion of '${dir}' failed" |
457 | rm -f "${dir}" || die "Deletion of '${dir}' failed" |
| 310 | ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed" |
458 | ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed" |
| 311 | } |
459 | } |
| 312 | |
460 | |
| 313 | # @FUNCTION: python_execute_function |
461 | # @FUNCTION: python_execute_function |
| 314 | # @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments] |
462 | # @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments] |
| 315 | # @DESCRIPTION: |
463 | # @DESCRIPTION: |
| 316 | # Execute specified function for each value of PYTHON_ABIS, optionally passing additional |
464 | # Execute specified function for each value of PYTHON_ABIS, optionally passing additional |
| 317 | # arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. |
465 | # arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. |
| 318 | python_execute_function() { |
466 | python_execute_function() { |
| 319 | local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" |
467 | local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir= |
| 320 | |
468 | |
| 321 | while (($#)); do |
469 | while (($#)); do |
| 322 | case "$1" in |
470 | case "$1" in |
| 323 | --action-message) |
471 | --action-message) |
| 324 | action_message_template="$2" |
472 | action_message_template="$2" |
| … | |
… | |
| 338 | quiet="1" |
486 | quiet="1" |
| 339 | ;; |
487 | ;; |
| 340 | -s|--separate-build-dirs) |
488 | -s|--separate-build-dirs) |
| 341 | separate_build_dirs="1" |
489 | separate_build_dirs="1" |
| 342 | ;; |
490 | ;; |
|
|
491 | --source-dir) |
|
|
492 | source_dir="$2" |
|
|
493 | shift |
|
|
494 | ;; |
| 343 | --) |
495 | --) |
| 344 | break |
496 | break |
| 345 | ;; |
497 | ;; |
| 346 | -*) |
498 | -*) |
| 347 | die "${FUNCNAME}(): Unrecognized option '$1'" |
499 | die "${FUNCNAME}(): Unrecognized option '$1'" |
| … | |
… | |
| 351 | ;; |
503 | ;; |
| 352 | esac |
504 | esac |
| 353 | shift |
505 | shift |
| 354 | done |
506 | done |
| 355 | |
507 | |
|
|
508 | if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then |
|
|
509 | die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option" |
|
|
510 | fi |
|
|
511 | |
| 356 | if [[ "${default_function}" == "0" ]]; then |
512 | if [[ "${default_function}" == "0" ]]; then |
| 357 | if [[ "$#" -eq "0" ]]; then |
513 | if [[ "$#" -eq 0 ]]; then |
| 358 | die "${FUNCNAME}(): Missing function name" |
514 | die "${FUNCNAME}(): Missing function name" |
| 359 | fi |
515 | fi |
| 360 | function="$1" |
516 | function="$1" |
| 361 | shift |
517 | shift |
| 362 | |
518 | |
| 363 | if [[ -z "$(type -t "${function}")" ]]; then |
519 | if [[ -z "$(type -t "${function}")" ]]; then |
| 364 | die "${FUNCNAME}(): '${function}' function isn't defined" |
520 | die "${FUNCNAME}(): '${function}' function is not defined" |
| 365 | fi |
521 | fi |
| 366 | else |
522 | else |
| 367 | if [[ "$#" -ne "0" ]]; then |
523 | if [[ "$#" -ne "0" ]]; then |
| 368 | die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously" |
524 | die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously" |
| 369 | fi |
525 | fi |
| 370 | if has "${EAPI:-0}" 0 1; then |
526 | if has "${EAPI:-0}" 0 1; then |
| 371 | die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI" |
527 | die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI" |
| 372 | fi |
528 | fi |
| 373 | |
529 | |
| 374 | if [[ "${EBUILD_PHASE}" == "configure" ]]; then |
530 | if [[ "${EBUILD_PHASE}" == "configure" ]]; then |
| 375 | if has "${EAPI}" 2; then |
531 | if has "${EAPI}" 2 3; then |
| 376 | python_default_function() { |
532 | python_default_function() { |
| 377 | econf |
533 | econf |
| 378 | } |
534 | } |
| 379 | else |
535 | else |
| 380 | python_default_function() { |
536 | python_default_function() { |
| … | |
… | |
| 396 | elif [[ "${EBUILD_PHASE}" == "install" ]]; then |
552 | elif [[ "${EBUILD_PHASE}" == "install" ]]; then |
| 397 | python_default_function() { |
553 | python_default_function() { |
| 398 | emake DESTDIR="${D}" install |
554 | emake DESTDIR="${D}" install |
| 399 | } |
555 | } |
| 400 | else |
556 | else |
| 401 | die "${FUNCNAME}(): --default-function option cannot be used in this ebuild phase" |
557 | die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase" |
| 402 | fi |
558 | fi |
| 403 | function="python_default_function" |
559 | function="python_default_function" |
| 404 | fi |
560 | fi |
| 405 | |
561 | |
| 406 | if [[ "${quiet}" == "0" ]]; then |
562 | if [[ "${quiet}" == "0" ]]; then |
| … | |
… | |
| 440 | fi |
596 | fi |
| 441 | echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" |
597 | echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" |
| 442 | fi |
598 | fi |
| 443 | |
599 | |
| 444 | if [[ "${separate_build_dirs}" == "1" ]]; then |
600 | if [[ "${separate_build_dirs}" == "1" ]]; then |
|
|
601 | if [[ -n "${source_dir}" ]]; then |
|
|
602 | export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}" |
|
|
603 | else |
| 445 | export BUILDDIR="${S}-${PYTHON_ABI}" |
604 | export BUILDDIR="${S}-${PYTHON_ABI}" |
|
|
605 | fi |
| 446 | pushd "${BUILDDIR}" > /dev/null || die "pushd failed" |
606 | pushd "${BUILDDIR}" > /dev/null || die "pushd failed" |
| 447 | else |
607 | else |
| 448 | export BUILDDIR="${S}" |
608 | export BUILDDIR="${S}" |
| 449 | fi |
609 | fi |
| 450 | |
610 | |
| 451 | previous_directory="$(pwd)" |
611 | previous_directory="$(pwd)" |
| 452 | previous_directory_stack="$(dirs -p)" |
612 | previous_directory_stack="$(dirs -p)" |
| 453 | previous_directory_stack_length="$(dirs -p | wc -l)" |
613 | previous_directory_stack_length="$(dirs -p | wc -l)" |
| 454 | |
614 | |
| 455 | if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then |
615 | if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then |
| 456 | EPYTHON="$(PYTHON)" nonfatal "${function}" "$@" |
616 | EPYTHON="$(PYTHON)" nonfatal "${function}" "$@" |
| 457 | else |
617 | else |
| 458 | EPYTHON="$(PYTHON)" "${function}" "$@" |
618 | EPYTHON="$(PYTHON)" "${function}" "$@" |
| 459 | fi |
619 | fi |
| 460 | |
620 | |
| … | |
… | |
| 469 | if [[ "${quiet}" == "0" ]]; then |
629 | if [[ "${quiet}" == "0" ]]; then |
| 470 | ewarn "${RED}${failure_message}${NORMAL}" |
630 | ewarn "${RED}${failure_message}${NORMAL}" |
| 471 | fi |
631 | fi |
| 472 | elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then |
632 | elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then |
| 473 | if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then |
633 | if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then |
| 474 | local ABI enabled_PYTHON_ABIS= |
634 | local enabled_PYTHON_ABIS= other_PYTHON_ABI |
| 475 | for ABI in ${PYTHON_ABIS}; do |
635 | for other_PYTHON_ABI in ${PYTHON_ABIS}; do |
| 476 | [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}" |
636 | [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}" |
| 477 | done |
637 | done |
| 478 | export PYTHON_ABIS="${enabled_PYTHON_ABIS# }" |
638 | export PYTHON_ABIS="${enabled_PYTHON_ABIS}" |
| 479 | fi |
639 | fi |
| 480 | if [[ "${quiet}" == "0" ]]; then |
640 | if [[ "${quiet}" == "0" ]]; then |
| 481 | ewarn "${RED}${failure_message}${NORMAL}" |
641 | ewarn "${RED}${failure_message}${NORMAL}" |
| 482 | fi |
642 | fi |
| 483 | if [[ -z "${PYTHON_ABIS}" ]]; then |
643 | if [[ -z "${PYTHON_ABIS}" ]]; then |
| … | |
… | |
| 486 | else |
646 | else |
| 487 | die "${failure_message}" |
647 | die "${failure_message}" |
| 488 | fi |
648 | fi |
| 489 | fi |
649 | fi |
| 490 | |
650 | |
| 491 | # Ensure that directory stack hasn't been decreased. |
651 | # Ensure that directory stack has not been decreased. |
| 492 | if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then |
652 | if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then |
| 493 | die "Directory stack decreased illegally" |
653 | die "Directory stack decreased illegally" |
| 494 | fi |
654 | fi |
| 495 | |
655 | |
| 496 | # Avoid side effects of earlier returning from the specified function. |
656 | # Avoid side effects of earlier returning from the specified function. |
| 497 | while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do |
657 | while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do |
| 498 | popd > /dev/null || die "popd failed" |
658 | popd > /dev/null || die "popd failed" |
| 499 | done |
659 | done |
| 500 | |
660 | |
| 501 | # Ensure that the bottom part of directory stack hasn't been changed. Restore |
661 | # Ensure that the bottom part of directory stack has not been changed. Restore |
| 502 | # previous directory (from before running of the specified function) before |
662 | # previous directory (from before running of the specified function) before |
| 503 | # comparison of directory stacks to avoid mismatch of directory stacks after |
663 | # comparison of directory stacks to avoid mismatch of directory stacks after |
| 504 | # potential using of 'cd' to change current directory. Restoration of previous |
664 | # potential using of 'cd' to change current directory. Restoration of previous |
| 505 | # directory allows to safely use 'cd' to change current directory in the |
665 | # directory allows to safely use 'cd' to change current directory in the |
| 506 | # specified function without changing it back to original directory. |
666 | # specified function without changing it back to original directory. |
| … | |
… | |
| 518 | if [[ "${default_function}" == "1" ]]; then |
678 | if [[ "${default_function}" == "1" ]]; then |
| 519 | unset -f python_default_function |
679 | unset -f python_default_function |
| 520 | fi |
680 | fi |
| 521 | } |
681 | } |
| 522 | |
682 | |
|
|
683 | # @FUNCTION: python_convert_shebangs |
|
|
684 | # @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories] |
|
|
685 | # @DESCRIPTION: |
|
|
686 | # Convert shebangs in specified files. Directories can be specified only with --recursive option. |
|
|
687 | python_convert_shebangs() { |
|
|
688 | local argument file files=() only_executables="0" python_version quiet="0" recursive="0" |
|
|
689 | |
|
|
690 | while (($#)); do |
|
|
691 | case "$1" in |
|
|
692 | -r|--recursive) |
|
|
693 | recursive="1" |
|
|
694 | ;; |
|
|
695 | -q|--quiet) |
|
|
696 | quiet="1" |
|
|
697 | ;; |
|
|
698 | -x|--only-executables) |
|
|
699 | only_executables="1" |
|
|
700 | ;; |
|
|
701 | --) |
|
|
702 | break |
|
|
703 | ;; |
|
|
704 | -*) |
|
|
705 | die "${FUNCNAME}(): Unrecognized option '$1'" |
|
|
706 | ;; |
|
|
707 | *) |
|
|
708 | break |
|
|
709 | ;; |
|
|
710 | esac |
|
|
711 | shift |
|
|
712 | done |
|
|
713 | |
|
|
714 | if [[ "$#" -eq 0 ]]; then |
|
|
715 | die "${FUNCNAME}(): Missing Python version and files or directories" |
|
|
716 | elif [[ "$#" -eq 1 ]]; then |
|
|
717 | die "${FUNCNAME}(): Missing files or directories" |
|
|
718 | fi |
|
|
719 | |
|
|
720 | python_version="$1" |
|
|
721 | shift |
|
|
722 | |
|
|
723 | for argument in "$@"; do |
|
|
724 | if [[ ! -e "${argument}" ]]; then |
|
|
725 | die "${FUNCNAME}(): '${argument}' does not exist" |
|
|
726 | elif [[ -f "${argument}" ]]; then |
|
|
727 | files+=("${argument}") |
|
|
728 | elif [[ -d "${argument}" ]]; then |
|
|
729 | if [[ "${recursive}" == "1" ]]; then |
|
|
730 | if [[ "${only_executables}" == "1" ]]; then |
|
|
731 | files+=($(find "${argument}" -perm /111 -type f)) |
|
|
732 | else |
|
|
733 | files+=($(find "${argument}" -type f)) |
|
|
734 | fi |
|
|
735 | else |
|
|
736 | die "${FUNCNAME}(): '${argument}' is not a regular file" |
|
|
737 | fi |
|
|
738 | else |
|
|
739 | die "${FUNCNAME}(): '${argument}' is not a regular file or a directory" |
|
|
740 | fi |
|
|
741 | done |
|
|
742 | |
|
|
743 | for file in "${files[@]}"; do |
|
|
744 | file="${file#./}" |
|
|
745 | [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue |
|
|
746 | |
|
|
747 | if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then |
|
|
748 | if [[ "${quiet}" == "0" ]]; then |
|
|
749 | einfo "Converting shebang in '${file}'" |
|
|
750 | fi |
|
|
751 | sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed" |
|
|
752 | |
|
|
753 | # Delete potential whitespace after "#!". |
|
|
754 | sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed" |
|
|
755 | fi |
|
|
756 | done |
|
|
757 | } |
|
|
758 | |
|
|
759 | # @FUNCTION: python_generate_wrapper_scripts |
|
|
760 | # @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files] |
|
|
761 | # @DESCRIPTION: |
|
|
762 | # Generate wrapper scripts. Existing files are overwritten only with --force option. |
|
|
763 | # If --respect-EPYTHON option is specified, then generated wrapper scripts will |
|
|
764 | # respect EPYTHON variable at run time. |
|
|
765 | python_generate_wrapper_scripts() { |
|
|
766 | local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python2_supported_versions python3_enabled="0" python3_supported_versions respect_EPYTHON="0" |
|
|
767 | python2_supported_versions="2.4 2.5 2.6 2.7" |
|
|
768 | python3_supported_versions="3.0 3.1 3.2" |
|
|
769 | |
|
|
770 | while (($#)); do |
|
|
771 | case "$1" in |
|
|
772 | -E|--respect-EPYTHON) |
|
|
773 | respect_EPYTHON="1" |
|
|
774 | ;; |
|
|
775 | -f|--force) |
|
|
776 | force="1" |
|
|
777 | ;; |
|
|
778 | -q|--quiet) |
|
|
779 | quiet="1" |
|
|
780 | ;; |
|
|
781 | --) |
|
|
782 | break |
|
|
783 | ;; |
|
|
784 | -*) |
|
|
785 | die "${FUNCNAME}(): Unrecognized option '$1'" |
|
|
786 | ;; |
|
|
787 | *) |
|
|
788 | break |
|
|
789 | ;; |
|
|
790 | esac |
|
|
791 | shift |
|
|
792 | done |
|
|
793 | |
|
|
794 | if [[ "$#" -eq 0 ]]; then |
|
|
795 | die "${FUNCNAME}(): Missing arguments" |
|
|
796 | fi |
|
|
797 | |
|
|
798 | validate_PYTHON_ABIS |
|
|
799 | for PYTHON_ABI in ${python2_supported_versions}; do |
|
|
800 | if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then |
|
|
801 | python2_enabled="1" |
|
|
802 | fi |
|
|
803 | done |
|
|
804 | for PYTHON_ABI in ${python3_supported_versions}; do |
|
|
805 | if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then |
|
|
806 | python3_enabled="1" |
|
|
807 | fi |
|
|
808 | done |
|
|
809 | |
|
|
810 | if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then |
|
|
811 | eselect_python_option= |
|
|
812 | elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then |
|
|
813 | eselect_python_option="--python2" |
|
|
814 | elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then |
|
|
815 | eselect_python_option="--python3" |
|
|
816 | else |
|
|
817 | die "${FUNCNAME}(): Unsupported environment" |
|
|
818 | fi |
|
|
819 | |
|
|
820 | for file in "$@"; do |
|
|
821 | if [[ -f "${file}" && "${force}" == "0" ]]; then |
|
|
822 | die "${FUNCNAME}(): '$1' already exists" |
|
|
823 | fi |
|
|
824 | |
|
|
825 | if [[ "${quiet}" == "0" ]]; then |
|
|
826 | einfo "Generating '${file#${D%/}}' wrapper script" |
|
|
827 | fi |
|
|
828 | |
|
|
829 | cat << EOF > "${file}" |
|
|
830 | #!/usr/bin/env python |
|
|
831 | # Gentoo '${file##*/}' wrapper script |
|
|
832 | |
|
|
833 | import os |
|
|
834 | import re |
|
|
835 | import subprocess |
|
|
836 | import sys |
|
|
837 | |
|
|
838 | EPYTHON_re = re.compile(r"^python(\d+\.\d+)$") |
|
|
839 | |
|
|
840 | EOF |
|
|
841 | if [[ "$?" != "0" ]]; then |
|
|
842 | die "${FUNCNAME}(): Generation of '$1' failed" |
|
|
843 | fi |
|
|
844 | if [[ "${respect_EPYTHON}" == "1" ]]; then |
|
|
845 | cat << EOF >> "${file}" |
|
|
846 | EPYTHON = os.environ.get("EPYTHON") |
|
|
847 | if EPYTHON: |
|
|
848 | EPYTHON_matched = EPYTHON_re.match(EPYTHON) |
|
|
849 | if EPYTHON_matched: |
|
|
850 | PYTHON_ABI = EPYTHON_matched.group(1) |
|
|
851 | else: |
|
|
852 | sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON) |
|
|
853 | sys.exit(1) |
|
|
854 | else: |
|
|
855 | try: |
|
|
856 | eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE) |
|
|
857 | if eselect_process.wait() != 0: |
|
|
858 | raise ValueError |
|
|
859 | except (OSError, ValueError): |
|
|
860 | sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n") |
|
|
861 | sys.exit(1) |
|
|
862 | |
|
|
863 | eselect_output = eselect_process.stdout.read() |
|
|
864 | if not isinstance(eselect_output, str): |
|
|
865 | # Python 3 |
|
|
866 | eselect_output = eselect_output.decode() |
|
|
867 | |
|
|
868 | EPYTHON_matched = EPYTHON_re.match(eselect_output) |
|
|
869 | if EPYTHON_matched: |
|
|
870 | PYTHON_ABI = EPYTHON_matched.group(1) |
|
|
871 | else: |
|
|
872 | sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output) |
|
|
873 | sys.exit(1) |
|
|
874 | EOF |
|
|
875 | if [[ "$?" != "0" ]]; then |
|
|
876 | die "${FUNCNAME}(): Generation of '$1' failed" |
|
|
877 | fi |
|
|
878 | else |
|
|
879 | cat << EOF >> "${file}" |
|
|
880 | try: |
|
|
881 | eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE) |
|
|
882 | if eselect_process.wait() != 0: |
|
|
883 | raise ValueError |
|
|
884 | except (OSError, ValueError): |
|
|
885 | sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n") |
|
|
886 | sys.exit(1) |
|
|
887 | |
|
|
888 | eselect_output = eselect_process.stdout.read() |
|
|
889 | if not isinstance(eselect_output, str): |
|
|
890 | # Python 3 |
|
|
891 | eselect_output = eselect_output.decode() |
|
|
892 | |
|
|
893 | EPYTHON_matched = EPYTHON_re.match(eselect_output) |
|
|
894 | if EPYTHON_matched: |
|
|
895 | PYTHON_ABI = EPYTHON_matched.group(1) |
|
|
896 | else: |
|
|
897 | sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output) |
|
|
898 | sys.exit(1) |
|
|
899 | EOF |
|
|
900 | if [[ "$?" != "0" ]]; then |
|
|
901 | die "${FUNCNAME}(): Generation of '$1' failed" |
|
|
902 | fi |
|
|
903 | fi |
|
|
904 | cat << EOF >> "${file}" |
|
|
905 | |
|
|
906 | os.environ["PYTHON_PROCESS_NAME"] = sys.argv[0] |
|
|
907 | target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI) |
|
|
908 | if not os.path.exists(target_executable): |
|
|
909 | sys.stderr.write("'%s' does not exist\n" % target_executable) |
|
|
910 | sys.exit(1) |
|
|
911 | |
|
|
912 | os.execv(target_executable, sys.argv) |
|
|
913 | EOF |
|
|
914 | if [[ "$?" != "0" ]]; then |
|
|
915 | die "${FUNCNAME}(): Generation of '$1' failed" |
|
|
916 | fi |
|
|
917 | fperms +x "${file#${D%/}}" || die "fperms '${file}' failed" |
|
|
918 | done |
|
|
919 | } |
| 523 | |
920 | |
| 524 | # @ECLASS-VARIABLE: PYTHON_USE_WITH |
921 | # @ECLASS-VARIABLE: PYTHON_USE_WITH |
| 525 | # @DESCRIPTION: |
922 | # @DESCRIPTION: |
| 526 | # Set this to a space separated list of use flags |
923 | # Set this to a space separated list of use flags |
| 527 | # the python slot in use must be built with. |
924 | # the python slot in use must be built with. |
| … | |
… | |
| 553 | python_pkg_setup_check_USE_flags() { |
950 | python_pkg_setup_check_USE_flags() { |
| 554 | local pyatom use |
951 | local pyatom use |
| 555 | if [[ -n "${PYTHON_ABI}" ]]; then |
952 | if [[ -n "${PYTHON_ABI}" ]]; then |
| 556 | pyatom="dev-lang/python:${PYTHON_ABI}" |
953 | pyatom="dev-lang/python:${PYTHON_ABI}" |
| 557 | else |
954 | else |
| 558 | python_version |
|
|
| 559 | pyatom="dev-lang/python:${PYVER}" |
955 | pyatom="dev-lang/python:$(PYTHON -A --ABI)" |
| 560 | fi |
956 | fi |
| 561 | |
957 | |
| 562 | for use in ${PYTHON_USE_WITH}; do |
958 | for use in ${PYTHON_USE_WITH}; do |
| 563 | if ! has_version "${pyatom}[${use}]"; then |
959 | if ! has_version "${pyatom}[${use}]"; then |
| 564 | python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}" |
960 | python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}" |
| … | |
… | |
| 620 | fi |
1016 | fi |
| 621 | |
1017 | |
| 622 | # @FUNCTION: python_disable_pyc |
1018 | # @FUNCTION: python_disable_pyc |
| 623 | # @DESCRIPTION: |
1019 | # @DESCRIPTION: |
| 624 | # Tell Python not to automatically recompile modules to .pyc/.pyo |
1020 | # Tell Python not to automatically recompile modules to .pyc/.pyo |
| 625 | # even if the timestamps/version stamps don't match. This is done |
1021 | # even if the timestamps/version stamps do not match. This is done |
| 626 | # to protect sandbox. |
1022 | # to protect sandbox. |
| 627 | python_disable_pyc() { |
1023 | python_disable_pyc() { |
| 628 | export PYTHONDONTWRITEBYTECODE="1" |
1024 | export PYTHONDONTWRITEBYTECODE="1" |
| 629 | } |
1025 | } |
| 630 | |
1026 | |
| … | |
… | |
| 634 | # timestamps/version stamps have changed. |
1030 | # timestamps/version stamps have changed. |
| 635 | python_enable_pyc() { |
1031 | python_enable_pyc() { |
| 636 | unset PYTHONDONTWRITEBYTECODE |
1032 | unset PYTHONDONTWRITEBYTECODE |
| 637 | } |
1033 | } |
| 638 | |
1034 | |
| 639 | python_disable_pyc |
|
|
| 640 | |
|
|
| 641 | # @FUNCTION: python_need_rebuild |
1035 | # @FUNCTION: python_need_rebuild |
| 642 | # @DESCRIPTION: Run without arguments, specifies that the package should be |
1036 | # @DESCRIPTION: Run without arguments, specifies that the package should be |
| 643 | # rebuilt after a python upgrade. |
1037 | # rebuilt after a python upgrade. |
|
|
1038 | # Do not use this function in ebuilds of packages supporting installation |
|
|
1039 | # for multiple versions of Python. |
| 644 | python_need_rebuild() { |
1040 | python_need_rebuild() { |
| 645 | python_version |
1041 | export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)" |
| 646 | export PYTHON_NEED_REBUILD=${PYVER} |
|
|
| 647 | } |
1042 | } |
| 648 | |
1043 | |
| 649 | # @FUNCTION: python_get_includedir |
1044 | # @FUNCTION: python_get_includedir |
| 650 | # @DESCRIPTION: |
1045 | # @DESCRIPTION: |
| 651 | # Run without arguments, returns the Python include directory. |
1046 | # Run without arguments, returns the Python include directory. |
| 652 | python_get_includedir() { |
1047 | python_get_includedir() { |
| 653 | if [[ -n "${PYTHON_ABI}" ]]; then |
1048 | if [[ -n "${PYTHON_ABI}" ]]; then |
| 654 | echo "/usr/include/python${PYTHON_ABI}" |
1049 | echo "/usr/include/python${PYTHON_ABI}" |
| 655 | else |
1050 | else |
| 656 | python_version |
|
|
| 657 | echo "/usr/include/python${PYVER}" |
1051 | echo "/usr/include/python$(PYTHON -A --ABI)" |
| 658 | fi |
1052 | fi |
| 659 | } |
1053 | } |
| 660 | |
1054 | |
| 661 | # @FUNCTION: python_get_libdir |
1055 | # @FUNCTION: python_get_libdir |
| 662 | # @DESCRIPTION: |
1056 | # @DESCRIPTION: |
| 663 | # Run without arguments, returns the Python library directory. |
1057 | # Run without arguments, returns the Python library directory. |
| 664 | python_get_libdir() { |
1058 | python_get_libdir() { |
| 665 | if [[ -n "${PYTHON_ABI}" ]]; then |
1059 | if [[ -n "${PYTHON_ABI}" ]]; then |
| 666 | echo "/usr/$(get_libdir)/python${PYTHON_ABI}" |
1060 | echo "/usr/$(get_libdir)/python${PYTHON_ABI}" |
| 667 | else |
1061 | else |
| 668 | python_version |
|
|
| 669 | echo "/usr/$(get_libdir)/python${PYVER}" |
1062 | echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)" |
| 670 | fi |
1063 | fi |
| 671 | } |
1064 | } |
| 672 | |
1065 | |
| 673 | # @FUNCTION: python_get_sitedir |
1066 | # @FUNCTION: python_get_sitedir |
| 674 | # @DESCRIPTION: |
1067 | # @DESCRIPTION: |
| … | |
… | |
| 726 | |
1119 | |
| 727 | # Check if phase is pkg_postinst() |
1120 | # Check if phase is pkg_postinst() |
| 728 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
1121 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
| 729 | die "${FUNCNAME} should only be run in pkg_postinst()" |
1122 | die "${FUNCNAME} should only be run in pkg_postinst()" |
| 730 | |
1123 | |
| 731 | # allow compiling for older python versions |
|
|
| 732 | if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then |
|
|
| 733 | PYVER=${PYTHON_OVERRIDE_PYVER} |
|
|
| 734 | else |
|
|
| 735 | python_version |
|
|
| 736 | fi |
|
|
| 737 | |
|
|
| 738 | # strip trailing slash |
1124 | # strip trailing slash |
| 739 | myroot="${ROOT%/}" |
1125 | myroot="${ROOT%/}" |
| 740 | |
1126 | |
| 741 | # respect ROOT |
1127 | # respect ROOT |
| 742 | for f in "$@"; do |
1128 | for f in "$@"; do |
| 743 | [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") |
1129 | [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") |
| 744 | done |
1130 | done |
| 745 | |
1131 | |
| 746 | if ((${#myfiles[@]})); then |
1132 | if ((${#myfiles[@]})); then |
| 747 | python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" |
1133 | "$(PYTHON -A)" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" |
| 748 | python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" &> /dev/null |
1134 | "$(PYTHON -A)" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null |
| 749 | else |
1135 | else |
| 750 | ewarn "No files to compile!" |
1136 | ewarn "No files to compile!" |
| 751 | fi |
1137 | fi |
| 752 | } |
1138 | } |
| 753 | |
1139 | |
| … | |
… | |
| 787 | ;; |
1173 | ;; |
| 788 | -*) |
1174 | -*) |
| 789 | ewarn "${FUNCNAME}: Ignoring compile option $1" |
1175 | ewarn "${FUNCNAME}: Ignoring compile option $1" |
| 790 | ;; |
1176 | ;; |
| 791 | *) |
1177 | *) |
| 792 | if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then |
1178 | if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then |
| 793 | die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" |
1179 | die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories" |
| 794 | elif [[ "$1" =~ ^/ ]]; then |
1180 | elif [[ "$1" =~ ^/ ]]; then |
| 795 | if [[ -d "${root}/$1" ]]; then |
1181 | if [[ -d "${root}/$1" ]]; then |
| 796 | other_dirs+=("${root}/$1") |
1182 | other_dirs+=("${root}/$1") |
| 797 | elif [[ -f "${root}/$1" ]]; then |
1183 | elif [[ -f "${root}/$1" ]]; then |
| 798 | other_files+=("${root}/$1") |
1184 | other_files+=("${root}/$1") |
| 799 | elif [[ -e "${root}/$1" ]]; then |
1185 | elif [[ -e "${root}/$1" ]]; then |
| 800 | ewarn "'${root}/$1' is not a file or a directory!" |
1186 | ewarn "'${root}/$1' is not a file or a directory!" |
| 801 | else |
1187 | else |
| 802 | ewarn "'${root}/$1' doesn't exist!" |
1188 | ewarn "'${root}/$1' does not exist!" |
| 803 | fi |
1189 | fi |
| 804 | else |
1190 | else |
| 805 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
1191 | for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do |
| 806 | if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then |
1192 | if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then |
| 807 | site_packages_dirs+=("$1") |
1193 | site_packages_dirs+=("$1") |
| 808 | break |
1194 | break |
| 809 | elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then |
1195 | elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then |
| 810 | site_packages_files+=("$1") |
1196 | site_packages_files+=("$1") |
| 811 | break |
1197 | break |
| 812 | elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then |
1198 | elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then |
| 813 | ewarn "'$1' is not a file or a directory!" |
1199 | ewarn "'$1' is not a file or a directory!" |
| 814 | else |
1200 | else |
| 815 | ewarn "'$1' doesn't exist!" |
1201 | ewarn "'$1' does not exist!" |
| 816 | fi |
1202 | fi |
| 817 | done |
1203 | done |
| 818 | fi |
1204 | fi |
| 819 | ;; |
1205 | ;; |
| 820 | esac |
1206 | esac |
| … | |
… | |
| 822 | done |
1208 | done |
| 823 | |
1209 | |
| 824 | # Set additional options. |
1210 | # Set additional options. |
| 825 | options+=("-q") |
1211 | options+=("-q") |
| 826 | |
1212 | |
| 827 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
1213 | for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do |
| 828 | if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then |
1214 | if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then |
| 829 | return_code="0" |
1215 | return_code="0" |
| 830 | ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" |
1216 | ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" |
| 831 | if ((${#site_packages_dirs[@]})); then |
1217 | if ((${#site_packages_dirs[@]})); then |
| 832 | for dir in "${site_packages_dirs[@]}"; do |
1218 | for dir in "${site_packages_dirs[@]}"; do |
| … | |
… | |
| 845 | eend "${return_code}" |
1231 | eend "${return_code}" |
| 846 | fi |
1232 | fi |
| 847 | unset site_packages_absolute_dirs site_packages_absolute_files |
1233 | unset site_packages_absolute_dirs site_packages_absolute_files |
| 848 | done |
1234 | done |
| 849 | |
1235 | |
| 850 | # Don't use PYTHON_ABI in next calls to python_get_libdir(). |
1236 | # Do not use PYTHON_ABI in next calls to python_get_libdir(). |
| 851 | unset PYTHON_ABI |
1237 | unset PYTHON_ABI |
| 852 | |
1238 | |
| 853 | if ((${#other_dirs[@]})) || ((${#other_files[@]})); then |
1239 | if ((${#other_dirs[@]})) || ((${#other_files[@]})); then |
| 854 | return_code="0" |
1240 | return_code="0" |
| 855 | ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." |
1241 | ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)" |
| 856 | if ((${#other_dirs[@]})); then |
1242 | if ((${#other_dirs[@]})); then |
| 857 | python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" |
1243 | "$(PYTHON -A)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" |
| 858 | python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" |
1244 | "$(PYTHON -A)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" |
| 859 | fi |
1245 | fi |
| 860 | if ((${#other_files[@]})); then |
1246 | if ((${#other_files[@]})); then |
| 861 | python${PYVER} "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" |
1247 | "$(PYTHON -A)" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" |
| 862 | python${PYVER} -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1" |
1248 | "$(PYTHON -A)" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1" |
| 863 | fi |
1249 | fi |
| 864 | eend "${return_code}" |
1250 | eend "${return_code}" |
| 865 | fi |
1251 | fi |
| 866 | else |
1252 | else |
| 867 | local myroot mydirs=() myfiles=() myopts=() return_code="0" |
1253 | local myroot mydirs=() myfiles=() myopts=() return_code="0" |
| … | |
… | |
| 889 | # Files are passed to python_mod_compile which is ROOT-aware |
1275 | # Files are passed to python_mod_compile which is ROOT-aware |
| 890 | myfiles+=("$1") |
1276 | myfiles+=("$1") |
| 891 | elif [[ -e "${myroot}/$1" ]]; then |
1277 | elif [[ -e "${myroot}/$1" ]]; then |
| 892 | ewarn "${myroot}/$1 is not a file or directory!" |
1278 | ewarn "${myroot}/$1 is not a file or directory!" |
| 893 | else |
1279 | else |
| 894 | ewarn "${myroot}/$1 doesn't exist!" |
1280 | ewarn "${myroot}/$1 does not exist!" |
| 895 | fi |
1281 | fi |
| 896 | ;; |
1282 | ;; |
| 897 | esac |
1283 | esac |
| 898 | shift |
1284 | shift |
| 899 | done |
1285 | done |
| 900 | |
1286 | |
| 901 | # allow compiling for older python versions |
|
|
| 902 | if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then |
|
|
| 903 | PYVER=${PYTHON_OVERRIDE_PYVER} |
|
|
| 904 | else |
|
|
| 905 | python_version |
|
|
| 906 | fi |
|
|
| 907 | |
|
|
| 908 | # set additional opts |
1287 | # set additional opts |
| 909 | myopts+=(-q) |
1288 | myopts+=(-q) |
| 910 | |
1289 | |
| 911 | ebegin "Byte compiling python modules for python-${PYVER} .." |
1290 | ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)" |
| 912 | if ((${#mydirs[@]})); then |
1291 | if ((${#mydirs[@]})); then |
| 913 | python${PYVER} \ |
1292 | "$(PYTHON -A)" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1" |
| 914 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
1293 | "$(PYTHON -A)" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1" |
| 915 | "${myopts[@]}" "${mydirs[@]}" || return_code="1" |
|
|
| 916 | python${PYVER} -O \ |
|
|
| 917 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
|
|
| 918 | "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1" |
|
|
| 919 | fi |
1294 | fi |
| 920 | |
1295 | |
| 921 | if ((${#myfiles[@]})); then |
1296 | if ((${#myfiles[@]})); then |
| 922 | python_mod_compile "${myfiles[@]}" |
1297 | python_mod_compile "${myfiles[@]}" |
| 923 | fi |
1298 | fi |
| … | |
… | |
| 925 | eend "${return_code}" |
1300 | eend "${return_code}" |
| 926 | fi |
1301 | fi |
| 927 | } |
1302 | } |
| 928 | |
1303 | |
| 929 | # @FUNCTION: python_mod_cleanup |
1304 | # @FUNCTION: python_mod_cleanup |
| 930 | # @USAGE: [directory] |
1305 | # @USAGE: [directory|file] |
| 931 | # @DESCRIPTION: |
1306 | # @DESCRIPTION: |
| 932 | # Run with optional arguments, where arguments are directories of |
1307 | # Run with optional arguments, where arguments are Python modules. If none given, |
| 933 | # python modules. If none given, it will look in /usr/lib/python[0-9].[0-9]. |
1308 | # it will look in /usr/lib/python[0-9].[0-9]. |
| 934 | # |
1309 | # |
| 935 | # It will recursively scan all compiled Python modules in the directories and |
1310 | # It will recursively scan all compiled Python modules in the directories and |
| 936 | # determine if they are orphaned (i.e. their corresponding .py files are missing.) |
1311 | # determine if they are orphaned (i.e. their corresponding .py files are missing.) |
| 937 | # If they are, then it will remove their corresponding .pyc and .pyo files. |
1312 | # If they are, then it will remove their corresponding .pyc and .pyo files. |
| 938 | # |
1313 | # |
| 939 | # This function should only be run in pkg_postrm(). |
1314 | # This function should only be run in pkg_postrm(). |
| 940 | python_mod_cleanup() { |
1315 | python_mod_cleanup() { |
| 941 | local PYTHON_ABI SEARCH_PATH=() root src_py |
1316 | local path py_file PYTHON_ABI SEARCH_PATH=() root |
| 942 | |
1317 | |
| 943 | # Check if phase is pkg_postrm(). |
1318 | # Check if phase is pkg_postrm(). |
| 944 | [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" |
1319 | [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" |
| 945 | |
1320 | |
| 946 | # Strip trailing slash from ROOT. |
1321 | # Strip trailing slash from ROOT. |
| 947 | root="${ROOT%/}" |
1322 | root="${ROOT%/}" |
| 948 | |
1323 | |
| 949 | if (($#)); then |
1324 | if (($#)); then |
| 950 | if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then |
1325 | if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then |
| 951 | while (($#)); do |
1326 | while (($#)); do |
| 952 | if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then |
1327 | if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then |
| 953 | die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" |
1328 | die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories" |
| 954 | elif [[ "$1" =~ ^/ ]]; then |
1329 | elif [[ "$1" =~ ^/ ]]; then |
| 955 | SEARCH_PATH+=("${root}/${1#/}") |
1330 | SEARCH_PATH+=("${root}/${1#/}") |
| 956 | else |
1331 | else |
| 957 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
1332 | for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do |
| 958 | SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") |
1333 | SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") |
| 959 | done |
1334 | done |
| 960 | fi |
1335 | fi |
| 961 | shift |
1336 | shift |
| 962 | done |
1337 | done |
| 963 | else |
1338 | else |
| 964 | SEARCH_PATH=("${@#/}") |
1339 | SEARCH_PATH=("${@#/}") |
| 965 | SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") |
1340 | SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") |
| 966 | fi |
1341 | fi |
| 967 | else |
1342 | else |
| 968 | SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) |
1343 | local dir sitedir |
|
|
1344 | for dir in "${root}"/usr/lib*; do |
|
|
1345 | if [[ -d "${dir}" && ! -L "${dir}" ]]; then |
|
|
1346 | for sitedir in "${dir}"/python*/site-packages; do |
|
|
1347 | if [[ -d "${sitedir}" ]]; then |
|
|
1348 | SEARCH_PATH+=("${sitedir}") |
|
|
1349 | fi |
|
|
1350 | done |
|
|
1351 | fi |
|
|
1352 | done |
|
|
1353 | fi |
|
|
1354 | |
|
|
1355 | local BLUE CYAN NORMAL |
|
|
1356 | if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then |
|
|
1357 | BLUE=$'\e[1;34m' |
|
|
1358 | CYAN=$'\e[1;36m' |
|
|
1359 | NORMAL=$'\e[0m' |
|
|
1360 | else |
|
|
1361 | BLUE= |
|
|
1362 | CYAN= |
|
|
1363 | NORMAL= |
| 969 | fi |
1364 | fi |
| 970 | |
1365 | |
| 971 | for path in "${SEARCH_PATH[@]}"; do |
1366 | for path in "${SEARCH_PATH[@]}"; do |
| 972 | [[ ! -d "${path}" ]] && continue |
1367 | if [[ -d "${path}" ]]; then |
| 973 | einfo "Cleaning orphaned Python bytecode from ${path} .." |
|
|
| 974 | find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do |
1368 | find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do |
| 975 | src_py="${REPLY%[co]}" |
1369 | py_file="${REPLY%[co]}" |
| 976 | [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue |
1370 | [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue |
| 977 | einfo "Purging ${src_py}[co]" |
1371 | einfo "${BLUE}<<< ${py_file}[co]${NORMAL}" |
| 978 | rm -f "${src_py}"[co] |
1372 | rm -f "${py_file}"[co] |
| 979 | done |
1373 | done |
| 980 | |
1374 | |
| 981 | # Attempt to remove directories that may be empty. |
1375 | # Attempt to delete directories, which may be empty. |
| 982 | find "${path}" -type d | sort -r | while read -r dir; do |
1376 | find "${path}" -type d | sort -r | while read -r dir; do |
| 983 | rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}" |
1377 | rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}" |
| 984 | done |
1378 | done |
|
|
1379 | elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then |
|
|
1380 | einfo "${BLUE}<<< ${path}[co]${NORMAL}" |
|
|
1381 | rm -f "${path}"[co] |
|
|
1382 | fi |
| 985 | done |
1383 | done |
| 986 | } |
1384 | } |