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

Diff of /eclass/python.eclass

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

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

Legend:
Removed from v.1.53  
changed lines
  Added in v.1.97

  ViewVC Help
Powered by ViewVC 1.1.20