/[gentoo-x86]/eclass/python.eclass
Gentoo

Diff of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.41 Revision 1.87
1# Copyright 1999-2008 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.41 2008/05/30 09:58:28 hawking Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.87 2010/02/02 18:55:00 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#
9# original author: Alastair Tse <liquidx@gentoo.org>
10# @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.
11# @DESCRIPTION: 9# @DESCRIPTION:
12# Some useful functions for dealing with python. 10# Some useful functions for dealing with Python.
13inherit alternatives multilib
14 11
12inherit multilib
15 13
16if [[ -n "${NEED_PYTHON}" ]] ; then 14if ! has "${EAPI:-0}" 0 1 2 3; then
17 DEPEND=">=dev-lang/python-${NEED_PYTHON}" 15 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
18 RDEPEND="${DEPEND}"
19fi 16fi
20 17
21__python_eclass_test() { 18_PYTHON2_SUPPORTED_VERSIONS=(2.4 2.5 2.6 2.7)
22 __python_version_extract 2.3 19_PYTHON3_SUPPORTED_VERSIONS=(3.0 3.1 3.2)
23 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
24 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
25 __python_version_extract 2.3.4
26 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
27 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
28 __python_version_extract 2.3.5
29 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
30 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
31 __python_version_extract 2.4
32 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
33 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
34 __python_version_extract 2.5b3
35 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
36 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
37}
38 20
21# @ECLASS-VARIABLE: PYTHON_DEPEND
22# @DESCRIPTION:
23# Specification of dependency on dev-lang/python.
24# Syntax:
25# PYTHON_DEPEND: [[!]USE_flag? ]<version_components_group>[ version_components_group]
26# version_components_group: <major_version[:[minimal_version][:maximal_version]]>
27# major_version: <2|3|*>
28# minimal_version: <minimal_major_version.minimal_minor_version>
29# maximal_version: <maximal_major_version.maximal_minor_version>
30
31_parse_PYTHON_DEPEND() {
32 local accepted_version accepted_versions=() major_version maximal_version minimal_version python_all="0" python_atoms= python_maximal_version python_minimal_version python_versions=() python2="0" python2_maximal_version python2_minimal_version python3="0" python3_maximal_version python3_minimal_version USE_flag= version_components_group version_components_group_regex version_components_groups
33
34 version_components_group_regex="(2|3|\*)(:([[:digit:]]+\.[[:digit:]]+)?(:([[:digit:]]+\.[[:digit:]]+)?)?)?"
35 version_components_groups="${PYTHON_DEPEND}"
36
37 if [[ "${version_components_groups}" =~ ^((\!)?[[:alnum:]_-]+\?\ )?${version_components_group_regex}(\ ${version_components_group_regex})?$ ]]; then
38 if [[ "${version_components_groups}" =~ ^(\!)?[[:alnum:]_-]+\? ]]; then
39 USE_flag="${version_components_groups%\? *}"
40 version_components_groups="${version_components_groups#* }"
41 fi
42 if [[ "${version_components_groups}" =~ ("*".*" "|" *"|^2.*\ (2|\*)|^3.*\ (3|\*)) ]]; then
43 die "Invalid syntax of PYTHON_DEPEND: Incorrectly specified groups of versions"
44 fi
45
46 version_components_groups="${version_components_groups// /$'\n'}"
47 while read version_components_group; do
48 major_version="${version_components_group:0:1}"
49 minimal_version="${version_components_group:2}"
50 minimal_version="${minimal_version%:*}"
51 maximal_version="${version_components_group:$((3 + ${#minimal_version}))}"
52
53 if [[ "${major_version}" =~ ^(2|3)$ ]]; then
54 if [[ -n "${minimal_version}" && "${major_version}" != "${minimal_version:0:1}" ]]; then
55 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' not in specified group of versions"
56 fi
57 if [[ -n "${maximal_version}" && "${major_version}" != "${maximal_version:0:1}" ]]; then
58 die "Invalid syntax of PYTHON_DEPEND: Maximal version '${maximal_version}' not in specified group of versions"
59 fi
60 fi
61
62 if [[ "${major_version}" == "2" ]]; then
63 python2="1"
64 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}")
65 python2_minimal_version="${minimal_version}"
66 python2_maximal_version="${maximal_version}"
67 elif [[ "${major_version}" == "3" ]]; then
68 python3="1"
69 python_versions=("${_PYTHON3_SUPPORTED_VERSIONS[@]}")
70 python3_minimal_version="${minimal_version}"
71 python3_maximal_version="${maximal_version}"
72 else
73 python_all="1"
74 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}")
75 python_minimal_version="${minimal_version}"
76 python_maximal_version="${maximal_version}"
77 fi
78
79 if [[ -n "${minimal_version}" ]] && ! has "${minimal_version}" "${python_versions[@]}"; then
80 die "Invalid syntax of PYTHON_DEPEND: Unrecognized minimal version '${minimal_version}'"
81 fi
82 if [[ -n "${maximal_version}" ]] && ! has "${maximal_version}" "${python_versions[@]}"; then
83 die "Invalid syntax of PYTHON_DEPEND: Unrecognized maximal version '${maximal_version}'"
84 fi
85
86 if [[ -n "${minimal_version}" && -n "${maximal_version}" && "${minimal_version}" > "${maximal_version}" ]]; then
87 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' greater than maximal version '${maximal_version}'"
88 fi
89 done <<< "${version_components_groups}"
90
91 _create_accepted_versions_range() {
92 local accepted_version="0" i
93 for ((i = "${#python_versions[@]}"; i >= 0; i--)); do
94 if [[ "${python_versions[${i}]}" == "${python_maximal_version}" ]]; then
95 accepted_version="1"
96 fi
97 if [[ "${accepted_version}" == "1" ]]; then
98 accepted_versions+=("${python_versions[${i}]}")
99 fi
100 if [[ "${python_versions[${i}]}" == "${python_minimal_version}" ]]; then
101 accepted_version="0"
102 fi
103 done
104 }
105
106 if [[ "${python_all}" == "1" ]]; then
107 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}")
108 python_minimal_version="${python_minimal_version:-${python_versions[0]}}"
109 python_maximal_version="${python_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
110 _create_accepted_versions_range
111 else
112 if [[ "${python3}" == "1" ]]; then
113 python_versions=("${_PYTHON3_SUPPORTED_VERSIONS[@]}")
114 python_minimal_version="${python3_minimal_version:-${python_versions[0]}}"
115 python_maximal_version="${python3_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
116 _create_accepted_versions_range
117 fi
118 if [[ "${python2}" == "1" ]]; then
119 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}")
120 python_minimal_version="${python2_minimal_version:-${python_versions[0]}}"
121 python_maximal_version="${python2_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
122 _create_accepted_versions_range
123 fi
124 fi
125
126 for accepted_version in "${accepted_versions[@]}"; do
127 python_atoms+="${python_atoms:+ }=dev-lang/python-${accepted_version}*"
128 done
129
130 _PYTHON_ATOMS="${python_atoms// /$'\n'}"
131 DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${python_atoms} )${USE_flag:+ )}"
132 RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${python_atoms} )${USE_flag:+ )}"
133 else
134 die "Invalid syntax of PYTHON_DEPEND"
135 fi
136}
137
138DEPEND=">=app-admin/eselect-python-20090804"
139RDEPEND="${DEPEND}"
140
141if [[ -n "${PYTHON_DEPEND}" && -n "${NEED_PYTHON}" ]]; then
142 die "PYTHON_DEPEND and NEED_PYTHON cannot be set simultaneously"
143elif [[ -n "${PYTHON_DEPEND}" ]]; then
144 _parse_PYTHON_DEPEND
145elif [[ -n "${NEED_PYTHON}" ]]; then
146 if ! has "${EAPI:-0}" 0 1 2; then
147 eerror "Use PYTHON_DEPEND instead of NEED_PYTHON."
148 die "NEED_PYTHON cannot be used in this EAPI"
149 fi
150 _PYTHON_ATOMS=">=dev-lang/python-${NEED_PYTHON}"
151 DEPEND+="${DEPEND:+ }${_PYTHON_ATOMS}"
152 RDEPEND+="${RDEPEND:+ }${_PYTHON_ATOMS}"
153else
154 _PYTHON_ATOMS="dev-lang/python"
155fi
156
157# @ECLASS-VARIABLE: PYTHON_USE_WITH
158# @DESCRIPTION:
159# Set this to a space separated list of USE flags the Python slot in use must be built with.
160
161# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
162# @DESCRIPTION:
163# Set this to a space separated list of USE flags of which one must be turned on for the slot in use.
164
165# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
166# @DESCRIPTION:
167# Set this to a name of a USE flag if you need to make either PYTHON_USE_WITH or
168# PYTHON_USE_WITH_OR atoms conditional under a USE flag.
169
39# @FUNCTION: python_disable_pyc 170# @FUNCTION: python_pkg_setup
40# @DESCRIPTION: 171# @DESCRIPTION:
41# Tells python not to automatically recompile modules to .pyc/.pyo 172# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
42# even if the timestamps/version stamps don't match. This is done 173# are respected. Only exported if one of those variables is set.
43# to protect sandbox. 174if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
44# 175 python_pkg_setup() {
45# note: supported by >=dev-lang/python-2.2.3-r3 only. 176 python_pkg_setup_fail() {
46# 177 eerror "${1}"
47python_disable_pyc() { 178 die "${1}"
48 export PYTHON_DONTCOMPILE=1 179 }
49}
50 180
181 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
182
183 python_pkg_setup_check_USE_flags() {
184 local pyatom use
185 if [[ -n "${PYTHON_ABI}" ]]; then
186 pyatom="dev-lang/python:${PYTHON_ABI}"
187 else
188 pyatom="dev-lang/python:$(PYTHON -A --ABI)"
189 fi
190
191 for use in ${PYTHON_USE_WITH}; do
192 if ! has_version "${pyatom}[${use}]"; then
193 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
194 fi
195 done
196
197 for use in ${PYTHON_USE_WITH_OR}; do
198 if has_version "${pyatom}[${use}]"; then
199 return
200 fi
201 done
202
203 if [[ ${PYTHON_USE_WITH_OR} ]]; then
204 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
205 fi
206 }
207
208 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
209 python_execute_function -q python_pkg_setup_check_USE_flags
210 else
211 python_pkg_setup_check_USE_flags
212 fi
213 }
214
215 EXPORT_FUNCTIONS pkg_setup
216
217 _PYTHON_USE_WITH_ATOM=""
218 if [[ -n "${PYTHON_USE_WITH}" ]]; then
219 while read _PYTHON_ATOM; do
220 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
221 done <<< "${_PYTHON_ATOMS}"
222 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
223 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
224 for _USE_flag in ${PYTHON_USE_WITH_OR}; do
225 while read _PYTHON_ATOM; do
226 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${_USE_flag}]"
227 done <<< "${_PYTHON_ATOMS}"
228 done
229 unset _USE_flag
230 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
231 fi
232 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
233 _PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOM} )"
234 fi
235 DEPEND+=" ${_PYTHON_USE_WITH_ATOM}"
236 RDEPEND+=" ${_PYTHON_USE_WITH_ATOM}"
237 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOM
238fi
239
240unset _PYTHON_ATOMS
241
242# ================================================================================================
243# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ========
244# ================================================================================================
245
246# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
247# @DESCRIPTION:
248# Set this in EAPI <= 4 to indicate that given package supports installation for
249# multiple versions of Python.
250
251# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
252# @DESCRIPTION:
253# Set this to define default functions for the following ebuild phases:
254# src_prepare, src_configure, src_compile, src_test, src_install.
255if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
256 python_src_prepare() {
257 python_copy_sources
258 }
259
260 for python_default_function in src_configure src_compile src_test src_install; do
261 eval "python_${python_default_function}() { python_execute_function -d -s; }"
262 done
263 unset python_default_function
264
265 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
266fi
267
268unset PYTHON_ABIS
269unset PYTHON_ABIS_SANITY_CHECKS
270
271# @FUNCTION: validate_PYTHON_ABIS
272# @DESCRIPTION:
273# Ensure that PYTHON_ABIS variable has valid value.
274# This function usually should not be directly called in ebuilds.
275validate_PYTHON_ABIS() {
276 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable.
277 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
278 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
279 fi
280
281 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
282 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
283 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
284 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
285 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
286 fi
287 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
288 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
289 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
290 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
291 fi
292
293 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
294 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
295 local PYTHON_ABI restricted_ABI support_ABI supported_PYTHON_ABIS=
296 PYTHON_ABI_SUPPORTED_VALUES="${_PYTHON2_SUPPORTED_VERSIONS[@]} ${_PYTHON3_SUPPORTED_VERSIONS[@]}"
297
298 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
299 local python2_enabled="0" python3_enabled="0"
300
301 if [[ -z "${USE_PYTHON}" ]]; then
302 die "USE_PYTHON variable is empty"
303 fi
304
305 for PYTHON_ABI in ${USE_PYTHON}; do
306 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
307 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
308 fi
309
310 if has "${PYTHON_ABI}" "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; then
311 python2_enabled="1"
312 fi
313 if has "${PYTHON_ABI}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; then
314 python3_enabled="1"
315 fi
316
317 support_ABI="1"
318 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
319 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
320 support_ABI="0"
321 break
322 fi
323 done
324 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
325 done
326
327 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
328 die "USE_PYTHON variable does not enable any version of Python supported by ${CATEGORY}/${PF}"
329 fi
330
331 if [[ "${python2_enabled}" == "0" ]]; then
332 ewarn "USE_PYTHON variable does not enable any version of Python 2. This configuration is unsupported."
333 fi
334 if [[ "${python3_enabled}" == "0" ]]; then
335 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported."
336 fi
337 else
338 local python_version python2_version= python3_version= support_python_major_version
339
340 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
341
342 if has_version "=dev-lang/python-2*"; then
343 if [[ "$(readlink "${EPREFIX}/usr/bin/python2")" != "python2."* ]]; then
344 die "'${EPREFIX}/usr/bin/python2' is not valid symlink"
345 fi
346
347 python2_version="$("${EPREFIX}/usr/bin/python2" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
348
349 for PYTHON_ABI in "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; do
350 support_python_major_version="1"
351 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
352 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
353 support_python_major_version="0"
354 fi
355 done
356 [[ "${support_python_major_version}" == "1" ]] && break
357 done
358 if [[ "${support_python_major_version}" == "1" ]]; then
359 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
360 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
361 die "Active version of Python 2 is not supported by ${CATEGORY}/${PF}"
362 fi
363 done
364 else
365 python2_version=""
366 fi
367 fi
368
369 if has_version "=dev-lang/python-3*"; then
370 if [[ "$(readlink "${EPREFIX}/usr/bin/python3")" != "python3."* ]]; then
371 die "'${EPREFIX}/usr/bin/python3' is not valid symlink"
372 fi
373
374 python3_version="$("${EPREFIX}/usr/bin/python3" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
375
376 for PYTHON_ABI in "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; do
377 support_python_major_version="1"
378 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
379 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
380 support_python_major_version="0"
381 fi
382 done
383 [[ "${support_python_major_version}" == "1" ]] && break
384 done
385 if [[ "${support_python_major_version}" == "1" ]]; then
386 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
387 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
388 die "Active version of Python 3 is not supported by ${CATEGORY}/${PF}"
389 fi
390 done
391 else
392 python3_version=""
393 fi
394 fi
395
396 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
397 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python2' symlink"
398 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
399 die "Incorrect configuration of Python"
400 fi
401 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
402 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python3' symlink"
403 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
404 die "Incorrect configuration of Python"
405 fi
406
407 PYTHON_ABIS="${python2_version} ${python3_version}"
408 PYTHON_ABIS="${PYTHON_ABIS# }"
409 export PYTHON_ABIS="${PYTHON_ABIS% }"
410 fi
411 fi
412
413 if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
414 local PYTHON_ABI
415 for PYTHON_ABI in ${PYTHON_ABIS}; do
416 # Ensure that appropriate version of Python is installed.
417 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
418 die "dev-lang/python:${PYTHON_ABI} is not installed"
419 fi
420
421 # Ensure that EPYTHON variable is respected.
422 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
423 eerror "python: '$(type -p python)'"
424 eerror "ABI: '${ABI}'"
425 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
426 eerror "EPYTHON: '$(PYTHON)'"
427 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
428 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')'"
429 die "'python' does not respect EPYTHON variable"
430 fi
431 done
432 PYTHON_ABIS_SANITY_CHECKS="1"
433 fi
434}
435
436# @FUNCTION: python_execute_function
437# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
438# @DESCRIPTION:
439# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
440# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
441python_execute_function() {
442 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function i nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
443
444 while (($#)); do
445 case "$1" in
446 --action-message)
447 action_message_template="$2"
448 shift
449 ;;
450 -d|--default-function)
451 default_function="1"
452 ;;
453 --failure-message)
454 failure_message_template="$2"
455 shift
456 ;;
457 --nonfatal)
458 nonfatal="1"
459 ;;
460 -q|--quiet)
461 quiet="1"
462 ;;
463 -s|--separate-build-dirs)
464 separate_build_dirs="1"
465 ;;
466 --source-dir)
467 source_dir="$2"
468 shift
469 ;;
470 --)
471 shift
472 break
473 ;;
474 -*)
475 die "${FUNCNAME}(): Unrecognized option '$1'"
476 ;;
477 *)
478 break
479 ;;
480 esac
481 shift
482 done
483
484 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
485 die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
486 fi
487
488 if [[ "${default_function}" == "0" ]]; then
489 if [[ "$#" -eq 0 ]]; then
490 die "${FUNCNAME}(): Missing function name"
491 fi
492 function="$1"
493 shift
494
495 if [[ -z "$(type -t "${function}")" ]]; then
496 die "${FUNCNAME}(): '${function}' function is not defined"
497 fi
498 else
499 if [[ "$#" -ne 0 ]]; then
500 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
501 fi
502 if has "${EAPI:-0}" 0 1; then
503 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
504 fi
505
506 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
507 if has "${EAPI}" 2 3; then
508 python_default_function() {
509 econf
510 }
511 else
512 python_default_function() {
513 nonfatal econf
514 }
515 fi
516 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
517 python_default_function() {
518 emake
519 }
520 elif [[ "${EBUILD_PHASE}" == "test" ]]; then
521 python_default_function() {
522 if emake -j1 -n check &> /dev/null; then
523 emake -j1 check
524 elif emake -j1 -n test &> /dev/null; then
525 emake -j1 test
526 fi
527 }
528 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
529 python_default_function() {
530 emake DESTDIR="${D}" install
531 }
532 else
533 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
534 fi
535 function="python_default_function"
536 fi
537
538 for ((i = 1; i < "${#FUNCNAME[@]}"; i++)); do
539 if [[ "${FUNCNAME[${i}]}" == "python_execute_function" ]]; then
540 die "${FUNCNAME}(): Invalid call stack"
541 fi
542 done
543
544 if [[ "${quiet}" == "0" ]]; then
545 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
546 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
547 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
548 [[ "${EBUILD_PHASE}" == "configure" ]] && action="Configuration"
549 [[ "${EBUILD_PHASE}" == "compile" ]] && action="Building"
550 [[ "${EBUILD_PHASE}" == "test" ]] && action="Testing"
551 [[ "${EBUILD_PHASE}" == "install" ]] && action="Installation"
552 [[ "${EBUILD_PHASE}" == "preinst" ]] && action="Preinstallation"
553 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
554 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
555 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
556 fi
557
558 local RED GREEN BLUE NORMAL
559 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
560 RED=$'\e[1;31m'
561 GREEN=$'\e[1;32m'
562 BLUE=$'\e[1;34m'
563 NORMAL=$'\e[0m'
564 else
565 RED=
566 GREEN=
567 BLUE=
568 NORMAL=
569 fi
570
571 validate_PYTHON_ABIS
572 for PYTHON_ABI in ${PYTHON_ABIS}; do
573 if [[ "${quiet}" == "0" ]]; then
574 if [[ -n "${action_message_template}" ]]; then
575 action_message="$(eval echo -n "${action_message_template}")"
576 else
577 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..."
578 fi
579 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
580 fi
581
582 if [[ "${separate_build_dirs}" == "1" ]]; then
583 if [[ -n "${source_dir}" ]]; then
584 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
585 else
586 export BUILDDIR="${S}-${PYTHON_ABI}"
587 fi
588 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
589 else
590 export BUILDDIR="${S}"
591 fi
592
593 previous_directory="$(pwd)"
594 previous_directory_stack="$(dirs -p)"
595 previous_directory_stack_length="$(dirs -p | wc -l)"
596
597 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
598 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
599 else
600 EPYTHON="$(PYTHON)" "${function}" "$@"
601 fi
602
603 if [[ "$?" != "0" ]]; then
604 if [[ -n "${failure_message_template}" ]]; then
605 failure_message="$(eval echo -n "${failure_message_template}")"
606 else
607 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
608 fi
609
610 if [[ "${nonfatal}" == "1" ]]; then
611 if [[ "${quiet}" == "0" ]]; then
612 ewarn "${RED}${failure_message}${NORMAL}"
613 fi
614 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
615 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
616 local enabled_PYTHON_ABIS= other_PYTHON_ABI
617 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
618 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
619 done
620 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
621 fi
622 if [[ "${quiet}" == "0" ]]; then
623 ewarn "${RED}${failure_message}${NORMAL}"
624 fi
625 if [[ -z "${PYTHON_ABIS}" ]]; then
626 die "${function}() function failed with all enabled versions of Python"
627 fi
628 else
629 die "${failure_message}"
630 fi
631 fi
632
633 # Ensure that directory stack has not been decreased.
634 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
635 die "Directory stack decreased illegally"
636 fi
637
638 # Avoid side effects of earlier returning from the specified function.
639 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
640 popd > /dev/null || die "popd failed"
641 done
642
643 # Ensure that the bottom part of directory stack has not been changed. Restore
644 # previous directory (from before running of the specified function) before
645 # comparison of directory stacks to avoid mismatch of directory stacks after
646 # potential using of 'cd' to change current directory. Restoration of previous
647 # directory allows to safely use 'cd' to change current directory in the
648 # specified function without changing it back to original directory.
649 cd "${previous_directory}"
650 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
651 die "Directory stack changed illegally"
652 fi
653
654 if [[ "${separate_build_dirs}" == "1" ]]; then
655 popd > /dev/null || die "popd failed"
656 fi
657 unset BUILDDIR
658 done
659
660 if [[ "${default_function}" == "1" ]]; then
661 unset -f python_default_function
662 fi
663}
664
665# @FUNCTION: python_copy_sources
666# @USAGE: [--no-link] [--] [directory]
667# @DESCRIPTION:
668# Copy unpacked sources of given package to separate build directory for each Python ABI.
669python_copy_sources() {
670 local dir dirs=() no_link="0" PYTHON_ABI
671
672 while (($#)); do
673 case "$1" in
674 --no-link)
675 no_link="1"
676 ;;
677 --)
678 shift
679 break
680 ;;
681 -*)
682 die "${FUNCNAME}(): Unrecognized option '$1'"
683 ;;
684 *)
685 break
686 ;;
687 esac
688 shift
689 done
690
691 if [[ "$#" -eq 0 ]]; then
692 if [[ "${WORKDIR}" == "${S}" ]]; then
693 die "${FUNCNAME}() cannot be used"
694 fi
695 dirs="${S}"
696 else
697 dirs="$@"
698 fi
699
700 validate_PYTHON_ABIS
701 for PYTHON_ABI in ${PYTHON_ABIS}; do
702 for dir in "${dirs[@]}"; do
703 if [[ "${no_link}" == "1" ]]; then
704 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
705 else
706 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
707 fi
708 done
709 done
710}
711
712# @FUNCTION: python_set_build_dir_symlink
713# @USAGE: [directory="build"]
714# @DESCRIPTION:
715# Create build directory symlink.
716python_set_build_dir_symlink() {
717 local dir="$1"
718
719 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
720 [[ -z "${dir}" ]] && dir="build"
721
722 # Do not delete preexistent directories.
723 rm -f "${dir}" || die "Deletion of '${dir}' failed"
724 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
725}
726
727# @FUNCTION: python_generate_wrapper_scripts
728# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
729# @DESCRIPTION:
730# Generate wrapper scripts. Existing files are overwritten only with --force option.
731# If --respect-EPYTHON option is specified, then generated wrapper scripts will
732# respect EPYTHON variable at run time.
733python_generate_wrapper_scripts() {
734 _python_initialize_prefix_variables
735
736 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python3_enabled="0" respect_EPYTHON="0"
737
738 while (($#)); do
739 case "$1" in
740 -E|--respect-EPYTHON)
741 respect_EPYTHON="1"
742 ;;
743 -f|--force)
744 force="1"
745 ;;
746 -q|--quiet)
747 quiet="1"
748 ;;
749 --)
750 shift
751 break
752 ;;
753 -*)
754 die "${FUNCNAME}(): Unrecognized option '$1'"
755 ;;
756 *)
757 break
758 ;;
759 esac
760 shift
761 done
762
763 if [[ "$#" -eq 0 ]]; then
764 die "${FUNCNAME}(): Missing arguments"
765 fi
766
767 validate_PYTHON_ABIS
768 for PYTHON_ABI in "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; do
769 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
770 python2_enabled="1"
771 fi
772 done
773 for PYTHON_ABI in "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; do
774 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
775 python3_enabled="1"
776 fi
777 done
778
779 if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
780 eselect_python_option=
781 elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
782 eselect_python_option="--python2"
783 elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
784 eselect_python_option="--python3"
785 else
786 die "${FUNCNAME}(): Unsupported environment"
787 fi
788
789 for file in "$@"; do
790 if [[ -f "${file}" && "${force}" == "0" ]]; then
791 die "${FUNCNAME}(): '$1' already exists"
792 fi
793
794 if [[ "${quiet}" == "0" ]]; then
795 einfo "Generating '${file#${D%/}}' wrapper script"
796 fi
797
798 cat << EOF > "${file}"
799#!/usr/bin/env python
800# Gentoo '${file##*/}' wrapper script
801
802import os
803import re
804import subprocess
805import sys
806
807EPYTHON_re = re.compile(r"^python(\d+\.\d+)$")
808
809EOF
810 if [[ "$?" != "0" ]]; then
811 die "${FUNCNAME}(): Generation of '$1' failed"
812 fi
813 if [[ "${respect_EPYTHON}" == "1" ]]; then
814 cat << EOF >> "${file}"
815EPYTHON = os.environ.get("EPYTHON")
816if EPYTHON:
817 EPYTHON_matched = EPYTHON_re.match(EPYTHON)
818 if EPYTHON_matched:
819 PYTHON_ABI = EPYTHON_matched.group(1)
820 else:
821 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
822 sys.exit(1)
823else:
824 try:
825 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
826 if eselect_process.wait() != 0:
827 raise ValueError
828 except (OSError, ValueError):
829 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
830 sys.exit(1)
831
832 eselect_output = eselect_process.stdout.read()
833 if not isinstance(eselect_output, str):
834 # Python 3
835 eselect_output = eselect_output.decode()
836
837 EPYTHON_matched = EPYTHON_re.match(eselect_output)
838 if EPYTHON_matched:
839 PYTHON_ABI = EPYTHON_matched.group(1)
840 else:
841 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
842 sys.exit(1)
843EOF
844 if [[ "$?" != "0" ]]; then
845 die "${FUNCNAME}(): Generation of '$1' failed"
846 fi
847 else
848 cat << EOF >> "${file}"
849try:
850 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
851 if eselect_process.wait() != 0:
852 raise ValueError
853except (OSError, ValueError):
854 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
855 sys.exit(1)
856
857eselect_output = eselect_process.stdout.read()
858if not isinstance(eselect_output, str):
859 # Python 3
860 eselect_output = eselect_output.decode()
861
862EPYTHON_matched = EPYTHON_re.match(eselect_output)
863if EPYTHON_matched:
864 PYTHON_ABI = EPYTHON_matched.group(1)
865else:
866 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
867 sys.exit(1)
868EOF
869 if [[ "$?" != "0" ]]; then
870 die "${FUNCNAME}(): Generation of '$1' failed"
871 fi
872 fi
873 cat << EOF >> "${file}"
874
875os.environ["PYTHON_PROCESS_NAME"] = sys.argv[0]
876target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI)
877if not os.path.exists(target_executable):
878 sys.stderr.write("'%s' does not exist\n" % target_executable)
879 sys.exit(1)
880
881os.execv(target_executable, sys.argv)
882EOF
883 if [[ "$?" != "0" ]]; then
884 die "${FUNCNAME}(): Generation of '$1' failed"
885 fi
886 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed"
887 done
888}
889
890# ================================================================================================
891# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======
892# ================================================================================================
893
894# @FUNCTION: python_set_active_version
895# @USAGE: <Python_ABI|2|3>
896# @DESCRIPTION:
897# Set active version of Python.
898python_set_active_version() {
899 if [[ "$#" -ne 1 ]]; then
900 die "${FUNCNAME}() requires 1 argument"
901 fi
902
903 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
904 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
905 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
906 fi
907 export EPYTHON="$(PYTHON "$1")"
908 elif [[ "$1" == "2" ]]; then
909 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
910 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
911 fi
912 export EPYTHON="$(PYTHON -2)"
913 elif [[ "$1" == "3" ]]; then
914 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
915 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
916 fi
917 export EPYTHON="$(PYTHON -3)"
918 else
919 die "${FUNCNAME}(): Unrecognized argument '$1'"
920 fi
921
922 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
923 # so it does not need to be exported to subprocesses.
924 PYTHON_ABI="${EPYTHON#python}"
925 PYTHON_ABI="${PYTHON_ABI%%-*}"
926
927 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
928 PYTHON_REQUESTED_ACTIVE_VERSION="$1"
929}
930
931# @FUNCTION: python_need_rebuild
932# @DESCRIPTION: Run without arguments, specifies that the package should be
933# rebuilt after a python upgrade.
934# Do not use this function in ebuilds of packages supporting installation
935# for multiple versions of Python.
936python_need_rebuild() {
937 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
938}
939
940# ================================================================================================
941# ======================================= GETTER FUNCTIONS =======================================
942# ================================================================================================
943
944# @FUNCTION: PYTHON
945# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
946# @DESCRIPTION:
947# Print Python interpreter filename for specified Python ABI. If Python_ABI argument
948# is ommitted, then PYTHON_ABI environment variable must be set and is used.
949# If -2 option is specified, then active version of Python 2 is used.
950# If -3 option is specified, then active version of Python 3 is used.
951# If --active option is specified, then active version of Python is used.
952# Active version of Python can be set by python_set_active_version().
953# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
954# -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
955# If --ABI option is specified, then only specified Python ABI is printed instead of
956# Python interpreter filename.
957# --ABI and --absolute-path options cannot be specified simultaneously.
958PYTHON() {
959 local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" python2="0" python3="0" slot=
960
961 while (($#)); do
962 case "$1" in
963 -2)
964 python2="1"
965 ;;
966 -3)
967 python3="1"
968 ;;
969 --ABI)
970 ABI_output="1"
971 ;;
972 -A|--active)
973 active="1"
974 ;;
975 -a|--absolute-path)
976 absolute_path_output="1"
977 ;;
978 -f|--final-ABI)
979 final_ABI="1"
980 ;;
981 --)
982 shift
983 break
984 ;;
985 -*)
986 die "${FUNCNAME}(): Unrecognized option '$1'"
987 ;;
988 *)
989 break
990 ;;
991 esac
992 shift
993 done
994
995 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
996 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
997 fi
998
999 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then
1000 die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously"
1001 fi
1002
1003 if [[ "$#" -eq 0 ]]; then
1004 if [[ "${active}" == "1" ]]; then
1005 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1006 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
1007 fi
1008 slot="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
1009 elif [[ "${final_ABI}" == "1" ]]; then
1010 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
1011 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python"
1012 fi
1013 validate_PYTHON_ABIS
1014 slot="${PYTHON_ABIS##* }"
1015 elif [[ "${python2}" == "1" ]]; then
1016 slot="$(eselect python show --python2)"
1017 if [[ -z "${slot}" ]]; then
1018 die "${FUNCNAME}(): Active Python 2 interpreter not set"
1019 elif [[ "${slot}" != "python2."* ]]; then
1020 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1021 fi
1022 slot="${slot#python}"
1023 elif [[ "${python3}" == "1" ]]; then
1024 slot="$(eselect python show --python3)"
1025 if [[ -z "${slot}" ]]; then
1026 die "${FUNCNAME}(): Active Python 3 interpreter not set"
1027 elif [[ "${slot}" != "python3."* ]]; then
1028 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1029 fi
1030 slot="${slot#python}"
1031 elif [[ -n "${PYTHON_ABI}" ]]; then
1032 slot="${PYTHON_ABI}"
1033 else
1034 die "${FUNCNAME}(): Invalid usage"
1035 fi
1036 elif [[ "$#" -eq 1 ]]; then
1037 if [[ "${active}" == "1" ]]; then
1038 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
1039 fi
1040 if [[ "${final_ABI}" == "1" ]]; then
1041 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
1042 fi
1043 if [[ "${python2}" == "1" ]]; then
1044 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
1045 fi
1046 if [[ "${python3}" == "1" ]]; then
1047 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
1048 fi
1049 slot="$1"
1050 else
1051 die "${FUNCNAME}(): Invalid usage"
1052 fi
1053
1054 if [[ "${ABI_output}" == "1" ]]; then
1055 echo -n "${slot}"
1056 return
1057 elif [[ "${absolute_path_output}" == "1" ]]; then
1058 echo -n "${EPREFIX}/usr/bin/python${slot}"
1059 else
1060 echo -n "python${slot}"
1061 fi
1062
1063 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
1064 echo -n "-${ABI}"
1065 fi
1066}
1067
1068# @FUNCTION: python_get_includedir
1069# @USAGE: [-f|--final-ABI]
1070# @DESCRIPTION:
1071# Print path to Python include directory.
1072# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1073python_get_includedir() {
1074 local final_ABI="0"
1075
1076 while (($#)); do
1077 case "$1" in
1078 -f|--final-ABI)
1079 final_ABI="1"
1080 ;;
1081 -*)
1082 die "${FUNCNAME}(): Unrecognized option '$1'"
1083 ;;
1084 *)
1085 die "${FUNCNAME}(): Invalid usage"
1086 ;;
1087 esac
1088 shift
1089 done
1090
1091 if [[ "${final_ABI}" == "1" ]]; then
1092 echo "/usr/include/python$(PYTHON -f --ABI)"
1093 elif [[ -n "${PYTHON_ABI}" ]]; then
1094 echo "/usr/include/python${PYTHON_ABI}"
1095 else
1096 echo "/usr/include/python$(PYTHON -A --ABI)"
1097 fi
1098}
1099
1100# @FUNCTION: python_get_libdir
1101# @USAGE: [-f|--final-ABI]
1102# @DESCRIPTION:
1103# Print path to Python library directory.
1104# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1105python_get_libdir() {
1106 local final_ABI="0"
1107
1108 while (($#)); do
1109 case "$1" in
1110 -f|--final-ABI)
1111 final_ABI="1"
1112 ;;
1113 -*)
1114 die "${FUNCNAME}(): Unrecognized option '$1'"
1115 ;;
1116 *)
1117 die "${FUNCNAME}(): Invalid usage"
1118 ;;
1119 esac
1120 shift
1121 done
1122
1123 if [[ "${final_ABI}" == "1" ]]; then
1124 echo "/usr/$(get_libdir)/python$(PYTHON -f --ABI)"
1125 elif [[ -n "${PYTHON_ABI}" ]]; then
1126 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
1127 else
1128 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)"
1129 fi
1130}
1131
1132# @FUNCTION: python_get_sitedir
1133# @USAGE: [-f|--final-ABI]
1134# @DESCRIPTION:
1135# Print path to Python site-packages directory.
1136# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1137python_get_sitedir() {
1138 local options=()
1139
1140 while (($#)); do
1141 case "$1" in
1142 -f|--final-ABI)
1143 options+=("$1")
1144 ;;
1145 -*)
1146 die "${FUNCNAME}(): Unrecognized option '$1'"
1147 ;;
1148 *)
1149 die "${FUNCNAME}(): Invalid usage"
1150 ;;
1151 esac
1152 shift
1153 done
1154
1155 echo "$(python_get_libdir "${options[@]}")/site-packages"
1156}
1157
51# @FUNCTION: python_enable_pyc 1158# @FUNCTION: python_get_library
1159# @USAGE: [-f|--final-ABI] [-l|--linker-option]
52# @DESCRIPTION: 1160# @DESCRIPTION:
53# Tells python to automatically recompile modules to .pyc/.pyo if the 1161# Print path to Python library.
54# timestamps/version stamps change 1162# If --linker-option is specified, then "-l${library}" linker option is printed.
55python_enable_pyc() { 1163# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
56 unset PYTHON_DONTCOMPILE 1164python_get_library() {
57} 1165 local final_ABI="0" linker_option="0" python_version
58 1166
59python_disable_pyc 1167 while (($#)); do
1168 case "$1" in
1169 -f|--final-ABI)
1170 final_ABI="1"
1171 ;;
1172 -l|--linker-option)
1173 linker_option="1"
1174 ;;
1175 -*)
1176 die "${FUNCNAME}(): Unrecognized option '$1'"
1177 ;;
1178 *)
1179 die "${FUNCNAME}(): Invalid usage"
1180 ;;
1181 esac
1182 shift
1183 done
60 1184
1185 if [[ "${final_ABI}" == "1" ]]; then
1186 python_version="$(PYTHON -f --ABI)"
1187 elif [[ -n "${PYTHON_ABI}" ]]; then
1188 python_version="${PYTHON_ABI}"
1189 else
1190 python_version="$(PYTHON -A --ABI)"
1191 fi
1192
1193 if [[ "${linker_option}" == "1" ]]; then
1194 echo "-lpython${python_version}"
1195 else
1196 echo "/usr/$(get_libdir)/libpython${python_version}$(get_libname)"
1197 fi
1198}
1199
61# @FUNCTION: python_version 1200# @FUNCTION: python_get_version
1201# @USAGE: [-f|--final-ABI] [--major] [--minor] [--micro]
62# @DESCRIPTION: 1202# @DESCRIPTION:
63# Run without arguments and it will export the version of python 1203# Print Python version.
64# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 1204# --major, --minor and --micro options cannot be specified simultaneously.
65__python_version_extract() { 1205# If --major, --minor and --micro options are not specified, then "${major_version}.${minor_version}" is printed.
66 verstr=$1 1206# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
67 export PYVER_MAJOR=${verstr:0:1}
68 export PYVER_MINOR=${verstr:2:1}
69 if [ "${verstr:3}x" = ".x" ]; then
70 export PYVER_MICRO=${verstr:4}
71 fi
72 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
73}
74
75python_version() { 1207python_get_version() {
76 local tmpstr 1208 local final_ABI="0" major="0" minor="0" micro="0" python_command
77 python=${python:-/usr/bin/python}
78 tmpstr="$(${python} -V 2>&1 )"
79 export PYVER_ALL="${tmpstr#Python }"
80 __python_version_extract $PYVER_ALL
81}
82 1209
83# @FUNCTION: python_makesym 1210 while (($#)); do
84# @DESCRIPTION: 1211 case "$1" in
85# Run without arguments, it will create the /usr/bin/python symlinks 1212 -f|--final-ABI)
86# to the latest installed version 1213 final_ABI="1"
87python_makesym() { 1214 ;;
88 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]" 1215 --major)
89 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]" 1216 major="1"
90} 1217 ;;
1218 --minor)
1219 minor="1"
1220 ;;
1221 --micro)
1222 micro="1"
1223 ;;
1224 -*)
1225 die "${FUNCNAME}(): Unrecognized option '$1'"
1226 ;;
1227 *)
1228 die "${FUNCNAME}(): Invalid usage"
1229 ;;
1230 esac
1231 shift
1232 done
91 1233
92# @FUNCTION: python_tkinter_exists 1234 if [[ "$((${major} + ${minor} + ${micro}))" -gt 1 ]]; then
93# @DESCRIPTION: 1235 die "${FUNCNAME}(): '--major', '--minor' or '--micro' options cannot be specified simultaneously"
94# Run without arguments, checks if python was compiled with Tkinter 1236 fi
95# support. If not, prints an error message and dies. 1237
96python_tkinter_exists() { 1238 if [[ "${major}" == "1" ]]; then
97 if ! python -c "import Tkinter" >/dev/null 2>&1; then 1239 python_command="from sys import version_info; print(version_info[0])"
98 eerror "You need to recompile python with Tkinter support." 1240 elif [[ "${minor}" == "1" ]]; then
99 eerror "Try adding: 'dev-lang/python tk'" 1241 python_command="from sys import version_info; print(version_info[1])"
100 eerror "in to /etc/portage/package.use" 1242 elif [[ "${micro}" == "1" ]]; then
101 echo 1243 python_command="from sys import version_info; print(version_info[2])"
102 die "missing tkinter support with installed python" 1244 else
1245 python_command="from sys import version_info; print('.'.join([str(x) for x in version_info[:2]]))"
1246 fi
1247
1248 if [[ "${final_ABI}" == "1" ]]; then
1249 "$(PYTHON -f)" -c "${python_command}"
1250 else
1251 "$(PYTHON "${PYTHON_ABI--A}")" -c "${python_command}"
1252 fi
1253}
1254
1255# ================================================================================================
1256# =================================== MISCELLANEOUS FUNCTIONS ====================================
1257# ================================================================================================
1258
1259_python_implementation() {
1260 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1261 return 0
1262 else
1263 return 1
1264 fi
1265}
1266
1267_python_initialize_prefix_variables() {
1268 if has "${EAPI:-0}" 0 1 2; then
1269 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
1270 EROOT="${ROOT%/}${EPREFIX}/"
103 fi 1271 fi
1272 if [[ -n "${D}" && -z "${ED}" ]]; then
1273 ED="${D%/}${EPREFIX}/"
1274 fi
1275 fi
1276}
1277
1278# @FUNCTION: python_convert_shebangs
1279# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1280# @DESCRIPTION:
1281# Convert shebangs in specified files. Directories can be specified only with --recursive option.
1282python_convert_shebangs() {
1283 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
1284
1285 while (($#)); do
1286 case "$1" in
1287 -r|--recursive)
1288 recursive="1"
1289 ;;
1290 -q|--quiet)
1291 quiet="1"
1292 ;;
1293 -x|--only-executables)
1294 only_executables="1"
1295 ;;
1296 --)
1297 shift
1298 break
1299 ;;
1300 -*)
1301 die "${FUNCNAME}(): Unrecognized option '$1'"
1302 ;;
1303 *)
1304 break
1305 ;;
1306 esac
1307 shift
1308 done
1309
1310 if [[ "$#" -eq 0 ]]; then
1311 die "${FUNCNAME}(): Missing Python version and files or directories"
1312 elif [[ "$#" -eq 1 ]]; then
1313 die "${FUNCNAME}(): Missing files or directories"
1314 fi
1315
1316 python_version="$1"
1317 shift
1318
1319 for argument in "$@"; do
1320 if [[ ! -e "${argument}" ]]; then
1321 die "${FUNCNAME}(): '${argument}' does not exist"
1322 elif [[ -f "${argument}" ]]; then
1323 files+=("${argument}")
1324 elif [[ -d "${argument}" ]]; then
1325 if [[ "${recursive}" == "1" ]]; then
1326 if [[ "${only_executables}" == "1" ]]; then
1327 files+=($(find "${argument}" -perm /111 -type f))
1328 else
1329 files+=($(find "${argument}" -type f))
1330 fi
1331 else
1332 die "${FUNCNAME}(): '${argument}' is not a regular file"
1333 fi
1334 else
1335 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
1336 fi
1337 done
1338
1339 for file in "${files[@]}"; do
1340 file="${file#./}"
1341 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1342
1343 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1344 if [[ "${quiet}" == "0" ]]; then
1345 einfo "Converting shebang in '${file}'"
1346 fi
1347 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1348
1349 # Delete potential whitespace after "#!".
1350 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1351 fi
1352 done
104} 1353}
105 1354
106# @FUNCTION: python_mod_exists 1355# @FUNCTION: python_mod_exists
107# @USAGE: < module > 1356# @USAGE: <module>
108# @DESCRIPTION: 1357# @DESCRIPTION:
109# Run with the module name as an argument. it will check if a 1358# Run with the module name as an argument. It will check if a
110# python module is installed and loadable. it will return 1359# Python module is installed and loadable. It will return
111# TRUE(0) if the module exists, and FALSE(1) if the module does 1360# TRUE(0) if the module exists, and FALSE(1) if the module does
112# not exist. 1361# not exist.
113# 1362#
114# Example: 1363# Example:
115# if python_mod_exists gtk; then 1364# if python_mod_exists gtk; then
116# echo "gtk support enabled" 1365# echo "gtk support enabled"
117# fi 1366# fi
118python_mod_exists() { 1367python_mod_exists() {
119 [ -z "$1" ] && die "${FUNCTION} requires an argument!" 1368 if [[ "$#" -ne 1 ]]; then
120 if ! python -c "import $1" >/dev/null 2>&1; then 1369 die "${FUNCNAME}() requires 1 argument"
121 return 1 1370 fi
1371 "$(PYTHON "${PYTHON_ABI--A}")" -c "import $1" &> /dev/null
1372}
1373
1374# @FUNCTION: python_tkinter_exists
1375# @DESCRIPTION:
1376# Run without arguments, checks if Python was compiled with Tkinter
1377# support. If not, prints an error message and dies.
1378python_tkinter_exists() {
1379 if ! "$(PYTHON "${PYTHON_ABI--A}")" -c "from sys import version_info
1380if version_info[0] == 3:
1381 import tkinter
1382else:
1383 import Tkinter" &> /dev/null; then
1384 eerror "Python needs to be rebuilt with tkinter support enabled."
1385 eerror "Add the following line to '${EPREFIX}/etc/portage/package.use' and rebuild Python"
1386 eerror "dev-lang/python tk"
1387 die "Python installed without support for tkinter"
1388 fi
1389}
1390
1391# ================================================================================================
1392# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1393# ================================================================================================
1394
1395# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
1396# @DESCRIPTION:
1397# User-configurable verbosity of tests of Python modules.
1398# Supported values: 0, 1, 2, 3, 4.
1399PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
1400
1401# @FUNCTION: python_execute_nosetests
1402# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1403# @DESCRIPTION:
1404# Execute nosetests for all enabled versions of Python.
1405python_execute_nosetests() {
1406 local PYTHONPATH_template= separate_build_dirs=
1407
1408 while (($#)); do
1409 case "$1" in
1410 -P|--PYTHONPATH)
1411 PYTHONPATH_template="$2"
1412 shift
1413 ;;
1414 -s|--separate-build-dirs)
1415 separate_build_dirs="1"
1416 ;;
1417 --)
1418 shift
1419 break
1420 ;;
1421 -*)
1422 die "${FUNCNAME}(): Unrecognized option '$1'"
1423 ;;
1424 *)
1425 break
1426 ;;
1427 esac
1428 shift
1429 done
1430
1431 python_test_function() {
1432 local evaluated_PYTHONPATH=
1433
1434 if [[ -n "${PYTHONPATH_template}" ]]; then
1435 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1436 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1437 unset evaluated_PYTHONPATH
1438 fi
122 fi 1439 fi
123 return 0 1440
1441 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1442 echo PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1443 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1444 else
1445 echo nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1446 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1447 fi
1448 }
1449 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1450 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1451 else
1452 if [[ -n "${separate_build_dirs}" ]]; then
1453 die "${FUNCNAME}(): Invalid usage"
1454 fi
1455 python_test_function "$@"
1456 fi
1457}
1458
1459# @FUNCTION: python_execute_py.test
1460# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1461# @DESCRIPTION:
1462# Execute py.test for all enabled versions of Python.
1463python_execute_py.test() {
1464 local PYTHONPATH_template= separate_build_dirs=
1465
1466 while (($#)); do
1467 case "$1" in
1468 -P|--PYTHONPATH)
1469 PYTHONPATH_template="$2"
1470 shift
1471 ;;
1472 -s|--separate-build-dirs)
1473 separate_build_dirs="1"
1474 ;;
1475 --)
1476 shift
1477 break
1478 ;;
1479 -*)
1480 die "${FUNCNAME}(): Unrecognized option '$1'"
1481 ;;
1482 *)
1483 break
1484 ;;
1485 esac
1486 shift
1487 done
1488
1489 python_test_function() {
1490 local evaluated_PYTHONPATH=
1491
1492 if [[ -n "${PYTHONPATH_template}" ]]; then
1493 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1494 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1495 unset evaluated_PYTHONPATH
1496 fi
1497 fi
1498
1499 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1500 echo PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"
1501 PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"
1502 else
1503 echo py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"
1504 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"
1505 fi
1506 }
1507 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1508 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1509 else
1510 if [[ -n "${separate_build_dirs}" ]]; then
1511 die "${FUNCNAME}(): Invalid usage"
1512 fi
1513 python_test_function "$@"
1514 fi
1515}
1516
1517# @FUNCTION: python_execute_trial
1518# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1519# @DESCRIPTION:
1520# Execute trial for all enabled versions of Python.
1521python_execute_trial() {
1522 local PYTHONPATH_template= separate_build_dirs=
1523
1524 while (($#)); do
1525 case "$1" in
1526 -P|--PYTHONPATH)
1527 PYTHONPATH_template="$2"
1528 shift
1529 ;;
1530 -s|--separate-build-dirs)
1531 separate_build_dirs="1"
1532 ;;
1533 --)
1534 shift
1535 break
1536 ;;
1537 -*)
1538 die "${FUNCNAME}(): Unrecognized option '$1'"
1539 ;;
1540 *)
1541 break
1542 ;;
1543 esac
1544 shift
1545 done
1546
1547 python_test_function() {
1548 local evaluated_PYTHONPATH=
1549
1550 if [[ -n "${PYTHONPATH_template}" ]]; then
1551 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1552 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1553 unset evaluated_PYTHONPATH
1554 fi
1555 fi
1556
1557 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1558 echo PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1559 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1560 else
1561 echo trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1562 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1563 fi
1564 }
1565 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1566 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1567 else
1568 if [[ -n "${separate_build_dirs}" ]]; then
1569 die "${FUNCNAME}(): Invalid usage"
1570 fi
1571 python_test_function "$@"
1572 fi
1573}
1574
1575# ================================================================================================
1576# ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
1577# ================================================================================================
1578
1579# @FUNCTION: python_enable_pyc
1580# @DESCRIPTION:
1581# Tell Python to automatically recompile modules to .pyc/.pyo if the
1582# timestamps/version stamps have changed.
1583python_enable_pyc() {
1584 unset PYTHONDONTWRITEBYTECODE
1585}
1586
1587# @FUNCTION: python_disable_pyc
1588# @DESCRIPTION:
1589# Tell Python not to automatically recompile modules to .pyc/.pyo
1590# even if the timestamps/version stamps do not match. This is done
1591# to protect sandbox.
1592python_disable_pyc() {
1593 export PYTHONDONTWRITEBYTECODE="1"
1594}
1595
1596# @FUNCTION: python_mod_optimize
1597# @USAGE: [options] [directory|file]
1598# @DESCRIPTION:
1599# If no arguments supplied, it will recompile not recursively all modules
1600# under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages).
1601#
1602# If supplied with arguments, it will recompile all modules recursively
1603# in the supplied directory.
1604# This function should only be run in pkg_postinst().
1605#
1606# Options passed to this function are passed to compileall.py.
1607#
1608# Example:
1609# python_mod_optimize ctypesgencore
1610python_mod_optimize() {
1611 _python_initialize_prefix_variables
1612
1613 # Check if phase is pkg_postinst().
1614 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME}() should only be run in pkg_postinst()"
1615
1616 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1617 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
1618
1619 # Strip trailing slash from ROOT.
1620 root="${EROOT%/}"
1621
1622 # Respect ROOT and options passed to compileall.py.
1623 while (($#)); do
1624 case "$1" in
1625 -l|-f|-q)
1626 options+=("$1")
1627 ;;
1628 -d|-x)
1629 options+=("$1" "$2")
1630 shift
1631 ;;
1632 -*)
1633 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1634 ;;
1635 *)
1636 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1637 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1638 elif [[ "$1" =~ ^/ ]]; then
1639 if [[ -d "${root}/$1" ]]; then
1640 other_dirs+=("${root}/$1")
1641 elif [[ -f "${root}/$1" ]]; then
1642 other_files+=("${root}/$1")
1643 elif [[ -e "${root}/$1" ]]; then
1644 ewarn "'${root}/$1' is not a file or a directory!"
1645 else
1646 ewarn "'${root}/$1' does not exist!"
1647 fi
1648 else
1649 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1650 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
1651 site_packages_dirs+=("$1")
1652 break
1653 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
1654 site_packages_files+=("$1")
1655 break
1656 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
1657 ewarn "'$1' is not a file or a directory!"
1658 else
1659 ewarn "'$1' does not exist!"
1660 fi
1661 done
1662 fi
1663 ;;
1664 esac
1665 shift
1666 done
1667
1668 # Set additional options.
1669 options+=("-q")
1670
1671 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1672 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
1673 return_code="0"
1674 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
1675 if ((${#site_packages_dirs[@]})); then
1676 for dir in "${site_packages_dirs[@]}"; do
1677 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
1678 done
1679 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
1680 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
1681 fi
1682 if ((${#site_packages_files[@]})); then
1683 for file in "${site_packages_files[@]}"; do
1684 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
1685 done
1686 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
1687 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
1688 fi
1689 eend "${return_code}"
1690 fi
1691 unset site_packages_absolute_dirs site_packages_absolute_files
1692 done
1693
1694 # Restore previous value of PYTHON_ABI.
1695 if [[ -n "${previous_PYTHON_ABI}" ]]; then
1696 PYTHON_ABI="${previous_PYTHON_ABI}"
1697 else
1698 unset PYTHON_ABI
1699 fi
1700
1701 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1702 return_code="0"
1703 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)"
1704 if ((${#other_dirs[@]})); then
1705 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
1706 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
1707 fi
1708 if ((${#other_files[@]})); then
1709 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
1710 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
1711 fi
1712 eend "${return_code}"
1713 fi
1714 else
1715 local myroot mydirs=() myfiles=() myopts=() return_code="0"
1716
1717 # strip trailing slash
1718 myroot="${EROOT%/}"
1719
1720 # respect ROOT and options passed to compileall.py
1721 while (($#)); do
1722 case "$1" in
1723 -l|-f|-q)
1724 myopts+=("$1")
1725 ;;
1726 -d|-x)
1727 myopts+=("$1" "$2")
1728 shift
1729 ;;
1730 -*)
1731 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1732 ;;
1733 *)
1734 if [[ -d "${myroot}"/$1 ]]; then
1735 mydirs+=("${myroot}/$1")
1736 elif [[ -f "${myroot}"/$1 ]]; then
1737 # Files are passed to python_mod_compile which is ROOT-aware
1738 myfiles+=("$1")
1739 elif [[ -e "${myroot}/$1" ]]; then
1740 ewarn "${myroot}/$1 is not a file or directory!"
1741 else
1742 ewarn "${myroot}/$1 does not exist!"
1743 fi
1744 ;;
1745 esac
1746 shift
1747 done
1748
1749 # set additional opts
1750 myopts+=(-q)
1751
1752 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)"
1753 if ((${#mydirs[@]})); then
1754 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
1755 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
1756 fi
1757
1758 if ((${#myfiles[@]})); then
1759 python_mod_compile "${myfiles[@]}"
1760 fi
1761
1762 eend "${return_code}"
1763 fi
1764}
1765
1766# @FUNCTION: python_mod_cleanup
1767# @USAGE: [directory|file]
1768# @DESCRIPTION:
1769# Run with optional arguments, where arguments are Python modules. If none given,
1770# it will look in /usr/lib/python[0-9].[0-9].
1771#
1772# It will recursively scan all compiled Python modules in the directories and
1773# determine if they are orphaned (i.e. their corresponding .py files are missing.)
1774# If they are, then it will remove their corresponding .pyc and .pyo files.
1775#
1776# This function should only be run in pkg_postrm().
1777python_mod_cleanup() {
1778 _python_initialize_prefix_variables
1779
1780 local path py_file PYTHON_ABI SEARCH_PATH=() root
1781
1782 # Check if phase is pkg_postrm().
1783 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME}() should only be run in pkg_postrm()"
1784
1785 # Strip trailing slash from ROOT.
1786 root="${EROOT%/}"
1787
1788 if (($#)); then
1789 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1790 while (($#)); do
1791 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1792 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1793 elif [[ "$1" =~ ^/ ]]; then
1794 SEARCH_PATH+=("${root}/${1#/}")
1795 else
1796 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1797 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
1798 done
1799 fi
1800 shift
1801 done
1802 else
1803 SEARCH_PATH=("${@#/}")
1804 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
1805 fi
1806 else
1807 local dir sitedir
1808 for dir in "${root}"/usr/lib*; do
1809 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
1810 for sitedir in "${dir}"/python*/site-packages; do
1811 if [[ -d "${sitedir}" ]]; then
1812 SEARCH_PATH+=("${sitedir}")
1813 fi
1814 done
1815 fi
1816 done
1817 fi
1818
1819 local BLUE CYAN NORMAL
1820 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1821 BLUE=$'\e[1;34m'
1822 CYAN=$'\e[1;36m'
1823 NORMAL=$'\e[0m'
1824 else
1825 BLUE=
1826 CYAN=
1827 NORMAL=
1828 fi
1829
1830 for path in "${SEARCH_PATH[@]}"; do
1831 if [[ -d "${path}" ]]; then
1832 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
1833 py_file="${REPLY%[co]}"
1834 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
1835 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
1836 rm -f "${py_file}"[co]
1837 done
1838
1839 # Attempt to delete directories, which may be empty.
1840 find "${path}" -type d | sort -r | while read -r dir; do
1841 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
1842 done
1843 elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then
1844 einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1845 rm -f "${path}"[co]
1846 fi
1847 done
1848}
1849
1850# ================================================================================================
1851# ===================================== DEPRECATED FUNCTIONS =====================================
1852# ================================================================================================
1853
1854# @FUNCTION: python_version
1855# @DESCRIPTION:
1856# Run without arguments and it will export the version of python
1857# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
1858python_version() {
1859 if ! has "${EAPI:-0}" 0 1 2; then
1860 eerror "Use PYTHON() and/or python_get_*() instead of python_version()."
1861 die "${FUNCNAME}() cannot be used in this EAPI"
1862 fi
1863
1864 [[ -n "${PYVER}" ]] && return 0
1865 local tmpstr
1866 python="${python:-${EPREFIX}/usr/bin/python}"
1867 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
1868 export PYVER_ALL="${tmpstr#Python }"
1869 export PYVER_MAJOR="${PYVER_ALL:0:1}"
1870 export PYVER_MINOR="${PYVER_ALL:2:1}"
1871 if [[ "${PYVER_ALL:3:1}" == "." ]]; then
1872 export PYVER_MICRO="${PYVER_ALL:4}"
1873 fi
1874 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
124} 1875}
125 1876
126# @FUNCTION: python_mod_compile 1877# @FUNCTION: python_mod_compile
127# @USAGE: < file > [more files ...] 1878# @USAGE: <file> [more files ...]
128# @DESCRIPTION: 1879# @DESCRIPTION:
129# Given filenames, it will pre-compile the module's .pyc and .pyo. 1880# Given filenames, it will pre-compile the module's .pyc and .pyo.
130# This function should only be run in pkg_postinst() 1881# This function should only be run in pkg_postinst()
131# 1882#
132# Example: 1883# Example:
133# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 1884# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
134# 1885#
135python_mod_compile() { 1886python_mod_compile() {
136 local f myroot 1887 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1888 eerror "Use python_mod_optimize() instead of python_mod_compile()."
1889 die "${FUNCNAME}() cannot be used in this EAPI"
1890 fi
1891
1892 _python_initialize_prefix_variables
1893
1894 local f myroot myfiles=()
137 1895
138 # Check if phase is pkg_postinst() 1896 # Check if phase is pkg_postinst()
139 [[ ${EBUILD_PHASE} != postinst ]] &&\ 1897 [[ ${EBUILD_PHASE} != postinst ]] && die "${FUNCNAME}() should only be run in pkg_postinst()"
140 die "${FUNCNAME} should only be run in pkg_postinst()"
141
142 # allow compiling for older python versions
143 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
144 PYVER=${PYTHON_OVERRIDE_PYVER}
145 else
146 python_version
147 fi
148 1898
149 # strip trailing slash 1899 # strip trailing slash
150 myroot="${ROOT%/}" 1900 myroot="${EROOT%/}"
151 1901
152 # respect ROOT 1902 # respect ROOT
153 for f in $@; do 1903 for f in "$@"; do
154 [ -f "${myroot}/${f}" ] && myfiles="${myfiles} ${myroot}/${f}" 1904 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
155 done 1905 done
156 1906
157 if [ -n "${myfiles}" ]; then 1907 if ((${#myfiles[@]})); then
158 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py ${myfiles} 1908 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
159 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py ${myfiles} 1909 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
160 else 1910 else
161 ewarn "No files to compile!" 1911 ewarn "No files to compile!"
162 fi 1912 fi
163} 1913}
164
165# @FUNCTION: python_mod_optimize
166# @USAGE: [ path ]
167# @DESCRIPTION:
168# If no arguments supplied, it will recompile all modules under
169# sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..)
170# no recursively
171#
172# If supplied with arguments, it will recompile all modules recursively
173# in the supplied directory
174# This function should only be run in pkg_postinst()
175#
176# Options passed to this function are passed to compileall.py
177#
178# Example:
179# python_mod_optimize /usr/share/codegen
180python_mod_optimize() {
181 local mydirs myfiles myroot myopts
182
183 # Check if phase is pkg_postinst()
184 [[ ${EBUILD_PHASE} != postinst ]] &&\
185 die "${FUNCNAME} should only be run in pkg_postinst()"
186
187 # strip trailing slash
188 myroot="${ROOT%/}"
189
190 # respect ROOT and options passed to compileall.py
191 while [ $# -gt 0 ]; do
192 case $1 in
193 -l|-f|-q)
194 myopts="${myopts} $1"
195 ;;
196 -d|-x)
197 myopts="${myopts} $1 $2"
198 shift
199 ;;
200 -*)
201 ewarn "${FUNCNAME}: Ignoring compile option $1"
202 ;;
203 *)
204 [ ! -e "${myroot}/${1}" ] && ewarn "${myroot}/${1} doesn't exist!"
205 [ -d "${myroot}/${1#/}" ] && mydirs="${mydirs} ${myroot}/${1#/}"
206 # Files are passed to python_mod_compile which is ROOT-aware
207 [ -f "${myroot}/${1}" ] && myfiles="${myfiles} ${1}"
208 ;;
209 esac
210 shift
211 done
212
213 # allow compiling for older python versions
214 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
215 PYVER=${PYTHON_OVERRIDE_PYVER}
216 else
217 python_version
218 fi
219
220 # set additional opts
221 myopts="${myopts} -q"
222
223 ebegin "Byte compiling python modules for python-${PYVER} .."
224 if [ -n "${mydirs}" ]; then
225 python${PYVER} \
226 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \
227 ${myopts} ${mydirs}
228 python${PYVER} -O \
229 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \
230 ${myopts} ${mydirs}
231 fi
232
233 if [ -n "${myfiles}" ]; then
234 python_mod_compile ${myfiles}
235 fi
236
237 eend $?
238}
239
240# @FUNCTION: python_mod_cleanup
241# @USAGE: [ dir ]
242# @DESCRIPTION:
243# Run with optional arguments, where arguments are directories of
244# python modules. if none given, it will look in /usr/lib/python[0-9].[0-9]
245#
246# It will recursively scan all compiled python modules in the directories
247# and determine if they are orphaned (eg. their corresponding .py is missing.)
248# if they are, then it will remove their corresponding .pyc and .pyo
249#
250# This function should only be run in pkg_postrm()
251python_mod_cleanup() {
252 local SEARCH_PATH myroot
253
254 # Check if phase is pkg_postrm()
255 [[ ${EBUILD_PHASE} != postrm ]] &&\
256 die "${FUNCNAME} should only be run in pkg_postrm()"
257
258 # strip trailing slash
259 myroot="${ROOT%/}"
260
261 if [ $# -gt 0 ]; then
262 for path in $@; do
263 SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}"
264 done
265 else
266 for path in ${myroot}/usr/lib*/python*/site-packages; do
267 SEARCH_PATH="${SEARCH_PATH} ${path}"
268 done
269 fi
270
271 for path in ${SEARCH_PATH}; do
272 einfo "Cleaning orphaned Python bytecode from ${path} .."
273 for obj in $(find ${path} -name '*.py[co]'); do
274 src_py="${obj%[co]}"
275 if [ ! -f "${src_py}" ]; then
276 einfo "Purging ${src_py}[co]"
277 rm -f ${src_py}[co]
278 fi
279 done
280 # attempt to remove directories that maybe empty
281 for dir in $(find ${path} -type d | sort -r); do
282 rmdir ${dir} 2>/dev/null
283 done
284 done
285}

Legend:
Removed from v.1.41  
changed lines
  Added in v.1.87

  ViewVC Help
Powered by ViewVC 1.1.20