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

Diff of /eclass/python.eclass

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

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

Legend:
Removed from v.1.39  
changed lines
  Added in v.1.94

  ViewVC Help
Powered by ViewVC 1.1.20