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

Diff of /eclass/python.eclass

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

Revision 1.104 Revision 1.116
1# Copyright 1999-2010 Gentoo Foundation 1# Copyright 1999-2011 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.104 2010/10/25 11:54:19 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.116 2011/07/04 11:27:53 djc Exp $
4 4
5# @ECLASS: python.eclass 5# @ECLASS: python.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# Gentoo Python Project <python@gentoo.org> 7# Gentoo Python Project <python@gentoo.org>
8# @BLURB: Eclass for Python packages 8# @BLURB: Eclass for Python packages
13 13
14if ! has "${EAPI:-0}" 0 1 2 3; then 14if ! has "${EAPI:-0}" 0 1 2 3; then
15 die "API of python.eclass in EAPI=\"${EAPI}\" not established" 15 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
16fi 16fi
17 17
18_CPYTHON2_SUPPORTED_ABIS=(2.4 2.5 2.6 2.7) 18_CPYTHON2_GLOBALLY_SUPPORTED_ABIS=(2.4 2.5 2.6 2.7)
19_CPYTHON3_SUPPORTED_ABIS=(3.0 3.1 3.2) 19_CPYTHON3_GLOBALLY_SUPPORTED_ABIS=(3.0 3.1 3.2 3.3)
20_JYTHON_SUPPORTED_ABIS=(2.5-jython) 20_JYTHON_GLOBALLY_SUPPORTED_ABIS=(2.5-jython)
21_PYTHON_SUPPORTED_ABIS=(${_CPYTHON2_SUPPORTED_ABIS[@]} ${_CPYTHON3_SUPPORTED_ABIS[@]} ${_JYTHON_SUPPORTED_ABIS[@]}) 21_PYTHON_GLOBALLY_SUPPORTED_ABIS=(${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]} ${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]} ${_JYTHON_GLOBALLY_SUPPORTED_ABIS[@]})
22 22
23# ================================================================================================ 23# ================================================================================================
24# ===================================== HANDLING OF METADATA ===================================== 24# ===================================== HANDLING OF METADATA =====================================
25# ================================================================================================ 25# ================================================================================================
26 26
27_python_set_IUSE() { 27_python_check_python_abi_matching() {
28 local PYTHON_ABI restricted_ABI restricted_ABIs support_ABI 28 local pattern patterns patterns_list="0" PYTHON_ABI
29 29
30 _PYTHON_ABIS_USE_FLAGS="" 30 while (($#)); do
31 restricted_ABIs="${RESTRICT_PYTHON_ABIS// /$'\n'}" 31 case "$1" in
32 32 --patterns-list)
33 for PYTHON_ABI in "${_PYTHON_SUPPORTED_ABIS[@]}"; do 33 patterns_list="1"
34 support_ABI="1" 34 ;;
35 while read restricted_ABI; do 35 --)
36 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then 36 shift
37 support_ABI="0"
38 break 37 break
38 ;;
39 -*)
40 die "${FUNCNAME}(): Unrecognized option '$1'"
41 ;;
42 *)
43 break
44 ;;
45 esac
46 shift
47 done
48
49 if [[ "$#" -ne 2 ]]; then
50 die "${FUNCNAME}() requires 2 arguments"
51 fi
52
53 PYTHON_ABI="$1"
54
55 if [[ "${patterns_list}" == "0" ]]; then
56 pattern="$2"
57
58 if [[ "${pattern}" == *"-cpython" ]]; then
59 [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+$ && "${PYTHON_ABI}" == ${pattern%-cpython} ]]
60 elif [[ "${pattern}" == *"-jython" ]]; then
61 [[ "${PYTHON_ABI}" == ${pattern} ]]
62 else
63 if [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
64 [[ "${PYTHON_ABI}" == ${pattern} ]]
65 elif [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+-jython$ ]]; then
66 [[ "${PYTHON_ABI%-jython}" == ${pattern} ]]
67 else
68 die "${FUNCNAME}(): Unrecognized Python ABI '${PYTHON_ABI}'"
69 fi
39 fi 70 fi
40 done <<< "${restricted_ABIs}" 71 else
41 [[ "${support_ABI}" == "1" ]] && _PYTHON_ABIS_USE_FLAGS+="${_PYTHON_ABIS_USE_FLAGS:+ }python_abis_${PYTHON_ABI}" 72 patterns="${2// /$'\n'}"
42 done
43 73
44 IUSE="${_PYTHON_ABIS_USE_FLAGS}" 74 while read pattern; do
45} 75 if _python_check_python_abi_matching "${PYTHON_ABI}" "${pattern}"; then
76 return 0
77 fi
78 done <<< "${patterns}"
46 79
47if ! has "${EAPI:-0}" 0 1 2 3 && [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 80 return 1
48 _python_set_IUSE
49fi 81 fi
50unset -f _python_set_IUSE 82}
51 83
52# @ECLASS-VARIABLE: PYTHON_DEPEND 84# @ECLASS-VARIABLE: PYTHON_DEPEND
53# @DESCRIPTION: 85# @DESCRIPTION:
54# Specification of dependency on dev-lang/python. 86# Specification of dependency on dev-lang/python.
55# Syntax: 87# Syntax:
90 fi 122 fi
91 fi 123 fi
92 124
93 if [[ "${major_version}" == "2" ]]; then 125 if [[ "${major_version}" == "2" ]]; then
94 python2="1" 126 python2="1"
95 python_versions=("${_CPYTHON2_SUPPORTED_ABIS[@]}") 127 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}")
96 python2_minimal_version="${minimal_version}" 128 python2_minimal_version="${minimal_version}"
97 python2_maximal_version="${maximal_version}" 129 python2_maximal_version="${maximal_version}"
98 elif [[ "${major_version}" == "3" ]]; then 130 elif [[ "${major_version}" == "3" ]]; then
99 python3="1" 131 python3="1"
100 python_versions=("${_CPYTHON3_SUPPORTED_ABIS[@]}") 132 python_versions=("${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
101 python3_minimal_version="${minimal_version}" 133 python3_minimal_version="${minimal_version}"
102 python3_maximal_version="${maximal_version}" 134 python3_maximal_version="${maximal_version}"
103 else 135 else
104 python_all="1" 136 python_all="1"
105 python_versions=("${_CPYTHON2_SUPPORTED_ABIS[@]}" "${_CPYTHON3_SUPPORTED_ABIS[@]}") 137 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
106 python_minimal_version="${minimal_version}" 138 python_minimal_version="${minimal_version}"
107 python_maximal_version="${maximal_version}" 139 python_maximal_version="${maximal_version}"
108 fi 140 fi
109 141
110 if [[ -n "${minimal_version}" ]] && ! has "${minimal_version}" "${python_versions[@]}"; then 142 if [[ -n "${minimal_version}" ]] && ! has "${minimal_version}" "${python_versions[@]}"; then
138 170
139 if [[ "${python_all}" == "1" ]]; then 171 if [[ "${python_all}" == "1" ]]; then
140 if [[ -z "${python_minimal_version}" && -z "${python_maximal_version}" ]]; then 172 if [[ -z "${python_minimal_version}" && -z "${python_maximal_version}" ]]; then
141 _PYTHON_ATOMS+=("dev-lang/python") 173 _PYTHON_ATOMS+=("dev-lang/python")
142 else 174 else
143 python_versions=("${_CPYTHON2_SUPPORTED_ABIS[@]}" "${_CPYTHON3_SUPPORTED_ABIS[@]}") 175 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
144 python_minimal_version="${python_minimal_version:-${python_versions[0]}}" 176 python_minimal_version="${python_minimal_version:-${python_versions[0]}}"
145 python_maximal_version="${python_maximal_version:-${python_versions[${#python_versions[@]}-1]}}" 177 python_maximal_version="${python_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
146 _append_accepted_versions_range 178 _append_accepted_versions_range
147 fi 179 fi
148 else 180 else
149 if [[ "${python3}" == "1" ]]; then 181 if [[ "${python3}" == "1" ]]; then
150 if [[ -z "${python3_minimal_version}" && -z "${python3_maximal_version}" ]]; then 182 if [[ -z "${python3_minimal_version}" && -z "${python3_maximal_version}" ]]; then
151 _PYTHON_ATOMS+=("=dev-lang/python-3*") 183 _PYTHON_ATOMS+=("=dev-lang/python-3*")
152 else 184 else
153 python_versions=("${_CPYTHON3_SUPPORTED_ABIS[@]}") 185 python_versions=("${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
154 python_minimal_version="${python3_minimal_version:-${python_versions[0]}}" 186 python_minimal_version="${python3_minimal_version:-${python_versions[0]}}"
155 python_maximal_version="${python3_maximal_version:-${python_versions[${#python_versions[@]}-1]}}" 187 python_maximal_version="${python3_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
156 _append_accepted_versions_range 188 _append_accepted_versions_range
157 fi 189 fi
158 fi 190 fi
159 if [[ "${python2}" == "1" ]]; then 191 if [[ "${python2}" == "1" ]]; then
160 if [[ -z "${python2_minimal_version}" && -z "${python2_maximal_version}" ]]; then 192 if [[ -z "${python2_minimal_version}" && -z "${python2_maximal_version}" ]]; then
161 _PYTHON_ATOMS+=("=dev-lang/python-2*") 193 _PYTHON_ATOMS+=("=dev-lang/python-2*")
162 else 194 else
163 python_versions=("${_CPYTHON2_SUPPORTED_ABIS[@]}") 195 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}")
164 python_minimal_version="${python2_minimal_version:-${python_versions[0]}}" 196 python_minimal_version="${python2_minimal_version:-${python_versions[0]}}"
165 python_maximal_version="${python2_maximal_version:-${python_versions[${#python_versions[@]}-1]}}" 197 python_maximal_version="${python2_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
166 _append_accepted_versions_range 198 _append_accepted_versions_range
167 fi 199 fi
168 fi 200 fi
237 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOMS _PYTHON_USE_WITH_ATOMS_ARRAY 269 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOMS _PYTHON_USE_WITH_ATOMS_ARRAY
238fi 270fi
239 271
240unset _PYTHON_ATOMS 272unset _PYTHON_ATOMS
241 273
242# @FUNCTION: python_abi_depend
243# @USAGE: [-e|--exclude-ABIs Python_ABIs] [-i|--include-ABIs Python_ABIs] [--] <dependency_atom> [dependency_atoms]
244# @DESCRIPTION:
245# Print dependency atoms with USE dependencies for Python ABIs added.
246# If --exclude-ABIs option is specified, then Python ABIs matching its argument are not used.
247# If --include-ABIs option is specified, then only Python ABIs matching its argument are used.
248# --exclude-ABIs and --include-ABIs options cannot be specified simultaneously.
249python_abi_depend() {
250 local atom atom_index atoms=() exclude_ABIs="0" excluded_ABI excluded_ABIs include_ABIs="0" included_ABI included_ABIs support_ABI USE_dependencies USE_flag USE_flag_index USE_flags=()
251
252 if has "${EAPI:-0}" 0 1 2 3; then
253 die "${FUNCNAME}() cannot be used in this EAPI"
254 fi
255
256 if [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
257 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
258 fi
259
260 while (($#)); do
261 case "$1" in
262 -e|--exclude-ABIs)
263 exclude_ABIs="1"
264 excluded_ABIs="${2// /$'\n'}"
265 shift
266 ;;
267 -i|--include-ABIs)
268 include_ABIs="1"
269 included_ABIs="${2// /$'\n'}"
270 shift
271 ;;
272 --)
273 shift
274 break
275 ;;
276 -*)
277 die "${FUNCNAME}(): Unrecognized option '$1'"
278 ;;
279 *)
280 break
281 ;;
282 esac
283 shift
284 done
285
286 if [[ "${exclude_ABIs}" == "1" && "${include_ABIs}" == "1" ]]; then
287 die "${FUNCNAME}(): '--exclude-ABIs' and '--include-ABIs' options cannot be specified simultaneously"
288 fi
289
290 if [[ "$#" -eq 0 ]]; then
291 die "${FUNCNAME}(): Missing dependency atoms"
292 fi
293
294 atoms=("$@")
295
296 if [[ "${exclude_ABIs}" == "0" && "${include_ABIs}" == "0" ]]; then
297 USE_dependencies="$(printf ",%s?" ${_PYTHON_ABIS_USE_FLAGS})"
298 USE_dependencies="${USE_dependencies#,}"
299
300 for atom_index in "${!atoms[@]}"; do
301 atom="${atoms[${atom_index}]}"
302
303 if [[ "${atom}" == *"["*"]" ]]; then
304 echo -n "${atom%]},"
305 else
306 echo -n "${atom}["
307 fi
308 echo -n "${USE_dependencies}]"
309
310 if [[ "${atom_index}" -ne $((${#atoms[@]} - 1)) ]]; then
311 echo -n " "
312 fi
313 done
314 else
315 if [[ "${exclude_ABIs}" == "1" ]]; then
316 for USE_flag in ${_PYTHON_ABIS_USE_FLAGS}; do
317 while read excluded_ABI; do
318 support_ABI="1"
319 if [[ "${USE_flag}" == python_abis_${excluded_ABI} ]]; then
320 support_ABI="0"
321 break
322 fi
323 done <<< "${excluded_ABIs}"
324 [[ "${support_ABI}" == "1" ]] && USE_flags+=("${USE_flag}")
325 done
326 elif [[ "${include_ABIs}" == "1" ]]; then
327 for USE_flag in ${_PYTHON_ABIS_USE_FLAGS}; do
328 while read included_ABI; do
329 support_ABI="0"
330 if [[ "${USE_flag}" == python_abis_${included_ABI} ]]; then
331 support_ABI="1"
332 break
333 fi
334 done <<< "${included_ABIs}"
335 [[ "${support_ABI}" == "1" ]] && USE_flags+=("${USE_flag}")
336 done
337 else
338 die "${FUNCNAME}(): Internal error"
339 fi
340
341 for USE_flag_index in "${!USE_flags[@]}"; do
342 USE_flag="${USE_flags[${USE_flag_index}]}"
343 USE_dependencies="${USE_flag}"
344
345 echo -n "${USE_flag}? ( "
346
347 for atom_index in "${!atoms[@]}"; do
348 atom="${atoms[${atom_index}]}"
349
350 if [[ "${atom}" == *"["*"]" ]]; then
351 echo -n "${atom%]},"
352 else
353 echo -n "${atom}["
354 fi
355 echo -n "${USE_dependencies}]"
356
357 if [[ "${atom_index}" -ne $((${#atoms[@]} - 1)) ]]; then
358 echo -n " "
359 fi
360 done
361
362 echo -n " )"
363
364 if [[ "${USE_flag_index}" -ne $((${#USE_flags[@]} - 1)) ]]; then
365 echo -n " "
366 fi
367 done
368 fi
369}
370
371# ================================================================================================ 274# ================================================================================================
372# =================================== MISCELLANEOUS FUNCTIONS ==================================== 275# =================================== MISCELLANEOUS FUNCTIONS ====================================
373# ================================================================================================ 276# ================================================================================================
374 277
375_python_implementation() { 278_python_implementation() {
492# @DESCRIPTION: 395# @DESCRIPTION:
493# Perform sanity checks and initialize environment. 396# Perform sanity checks and initialize environment.
494# 397#
495# This function is exported in EAPI 2 and 3 when PYTHON_USE_WITH or PYTHON_USE_WITH_OR variable 398# This function is exported in EAPI 2 and 3 when PYTHON_USE_WITH or PYTHON_USE_WITH_OR variable
496# is set and always in EAPI >=4. Calling of this function is mandatory in EAPI >=4. 399# is set and always in EAPI >=4. Calling of this function is mandatory in EAPI >=4.
497#
498# This function can be used only in pkg_setup() phase.
499python_pkg_setup() { 400python_pkg_setup() {
500 # Check if phase is pkg_setup(). 401 if [[ "${EBUILD_PHASE}" != "setup" ]]; then
501 [[ "${EBUILD_PHASE}" != "setup" ]] && die "${FUNCNAME}() can be used only in pkg_setup() phase" 402 die "${FUNCNAME}() can be used only in pkg_setup() phase"
403 fi
502 404
503 if [[ "$#" -ne 0 ]]; then 405 if [[ "$#" -ne 0 ]]; then
504 die "${FUNCNAME}() does not accept arguments" 406 die "${FUNCNAME}() does not accept arguments"
505 fi 407 fi
408
409 export JYTHON_SYSTEM_CACHEDIR="1"
410 addwrite "${EPREFIX}/var/cache/jython"
506 411
507 if _python_package_supporting_installation_for_multiple_python_abis; then 412 if _python_package_supporting_installation_for_multiple_python_abis; then
508 _python_calculate_PYTHON_ABIS 413 _python_calculate_PYTHON_ABIS
509 export EPYTHON="$(PYTHON -f)" 414 export EPYTHON="$(PYTHON -f)"
510 else 415 else
557 462
558if ! has "${EAPI:-0}" 0 1 2 3 || { has "${EAPI:-0}" 2 3 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; }; then 463if ! has "${EAPI:-0}" 0 1 2 3 || { has "${EAPI:-0}" 2 3 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; }; then
559 EXPORT_FUNCTIONS pkg_setup 464 EXPORT_FUNCTIONS pkg_setup
560fi 465fi
561 466
467_PYTHON_SHEBANG_BASE_PART_REGEX='^#![[:space:]]*([^[:space:]]*/usr/bin/env[[:space:]]+)?([^[:space:]]*/)?(jython|python)'
468
562# @FUNCTION: python_convert_shebangs 469# @FUNCTION: python_convert_shebangs
563# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories] 470# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_ABI|Python_version> <file|directory> [files|directories]
564# @DESCRIPTION: 471# @DESCRIPTION:
565# Convert shebangs in specified files. Directories can be specified only with --recursive option. 472# Convert shebangs in specified files. Directories can be specified only with --recursive option.
566python_convert_shebangs() { 473python_convert_shebangs() {
567 _python_check_python_pkg_setup_execution 474 _python_check_python_pkg_setup_execution
568 475
569 local argument file files=() only_executables="0" python_version quiet="0" recursive="0" 476 local argument file files=() only_executables="0" python_interpreter quiet="0" recursive="0"
570 477
571 while (($#)); do 478 while (($#)); do
572 case "$1" in 479 case "$1" in
573 -r|--recursive) 480 -r|--recursive)
574 recursive="1" 481 recursive="1"
597 die "${FUNCNAME}(): Missing Python version and files or directories" 504 die "${FUNCNAME}(): Missing Python version and files or directories"
598 elif [[ "$#" -eq 1 ]]; then 505 elif [[ "$#" -eq 1 ]]; then
599 die "${FUNCNAME}(): Missing files or directories" 506 die "${FUNCNAME}(): Missing files or directories"
600 fi 507 fi
601 508
602 python_version="$1" 509 if [[ -n "$(_python_get_implementation --ignore-invalid "$1")" ]]; then
510 python_interpreter="$(PYTHON "$1")"
511 else
512 python_interpreter="python$1"
513 fi
603 shift 514 shift
604 515
605 for argument in "$@"; do 516 for argument in "$@"; do
606 if [[ ! -e "${argument}" ]]; then 517 if [[ ! -e "${argument}" ]]; then
607 die "${FUNCNAME}(): '${argument}' does not exist" 518 die "${FUNCNAME}(): '${argument}' does not exist"
622 533
623 for file in "${files[@]}"; do 534 for file in "${files[@]}"; do
624 file="${file#./}" 535 file="${file#./}"
625 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue 536 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
626 537
627 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then 538 if [[ "$(head -n1 "${file}")" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX} ]]; then
628 [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue 539 [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue
629 540
630 if [[ "${quiet}" == "0" ]]; then 541 if [[ "${quiet}" == "0" ]]; then
631 einfo "Converting shebang in '${file}'" 542 einfo "Converting shebang in '${file}'"
632 fi 543 fi
633 544
634 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed" 545 sed -e "1s:^#![[:space:]]*\([^[:space:]]*/usr/bin/env[[:space:]]\)\?[[:space:]]*\([^[:space:]]*/\)\?\(jython\|python\)\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?\(\$\|[[:space:]].*\):#!\1\2${python_interpreter}\6:" -i "${file}" || die "Conversion of shebang in '${file}' failed"
635
636 # Delete potential whitespace after "#!".
637 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
638 fi 546 fi
639 done 547 done
640} 548}
641 549
642# @FUNCTION: python_clean_installation_image 550# @FUNCTION: python_clean_installation_image
643# @USAGE: [-q|--quiet] 551# @USAGE: [-q|--quiet]
644# @DESCRIPTION: 552# @DESCRIPTION:
645# Delete needless files in installation image. 553# Delete needless files in installation image.
554#
555# This function can be used only in src_install() phase.
646python_clean_installation_image() { 556python_clean_installation_image() {
557 if [[ "${EBUILD_PHASE}" != "install" ]]; then
558 die "${FUNCNAME}() can be used only in src_install() phase"
559 fi
560
647 _python_check_python_pkg_setup_execution 561 _python_check_python_pkg_setup_execution
648 _python_initialize_prefix_variables 562 _python_initialize_prefix_variables
649 563
650 local file files=() quiet="0" 564 local file files=() quiet="0"
651
652 # Check if phase is src_install().
653 [[ "${EBUILD_PHASE}" != "install" ]] && die "${FUNCNAME}() can be used only in src_install() phase"
654 565
655 while (($#)); do 566 while (($#)); do
656 case "$1" in 567 case "$1" in
657 -q|--quiet) 568 -q|--quiet)
658 quiet="1" 569 quiet="1"
709# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS 620# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
710# @DESCRIPTION: 621# @DESCRIPTION:
711# Set this in EAPI <= 4 to indicate that current package supports installation for 622# Set this in EAPI <= 4 to indicate that current package supports installation for
712# multiple Python ABIs. 623# multiple Python ABIs.
713 624
625# @ECLASS-VARIABLE: PYTHON_TESTS_RESTRICTED_ABIS
626# @DESCRIPTION:
627# Space-separated list of Python ABI patterns. Testing in Python ABIs matching any Python ABI
628# patterns specified in this list is skipped.
629
714# @ECLASS-VARIABLE: PYTHON_EXPORT_PHASE_FUNCTIONS 630# @ECLASS-VARIABLE: PYTHON_EXPORT_PHASE_FUNCTIONS
715# @DESCRIPTION: 631# @DESCRIPTION:
716# Set this to export phase functions for the following ebuild phases: 632# Set this to export phase functions for the following ebuild phases:
717# src_prepare, src_configure, src_compile, src_test, src_install. 633# src_prepare(), src_configure(), src_compile(), src_test(), src_install().
718if ! has "${EAPI:-0}" 0 1; then 634if ! has "${EAPI:-0}" 0 1; then
719 python_src_prepare() { 635 python_src_prepare() {
720 _python_check_python_pkg_setup_execution 636 if [[ "${EBUILD_PHASE}" != "prepare" ]]; then
637 die "${FUNCNAME}() can be used only in src_prepare() phase"
638 fi
721 639
722 if ! _python_package_supporting_installation_for_multiple_python_abis; then 640 if ! _python_package_supporting_installation_for_multiple_python_abis; then
723 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs" 641 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
724 fi 642 fi
725 643
644 _python_check_python_pkg_setup_execution
645
726 if [[ "$#" -ne 0 ]]; then 646 if [[ "$#" -ne 0 ]]; then
727 die "${FUNCNAME}() does not accept arguments" 647 die "${FUNCNAME}() does not accept arguments"
728 fi 648 fi
729 649
730 python_copy_sources 650 python_copy_sources
731 } 651 }
732 652
733 for python_default_function in src_configure src_compile src_test src_install; do 653 for python_default_function in src_configure src_compile src_test; do
734 eval "python_${python_default_function}() { 654 eval "python_${python_default_function}() {
735 _python_check_python_pkg_setup_execution 655 if [[ \"\${EBUILD_PHASE}\" != \"${python_default_function#src_}\" ]]; then
656 die \"\${FUNCNAME}() can be used only in ${python_default_function}() phase\"
657 fi
736 658
737 if ! _python_package_supporting_installation_for_multiple_python_abis; then 659 if ! _python_package_supporting_installation_for_multiple_python_abis; then
738 die \"\${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs\" 660 die \"\${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs\"
739 fi 661 fi
740 662
663 _python_check_python_pkg_setup_execution
664
741 python_execute_function -d -s -- \"\$@\" 665 python_execute_function -d -s -- \"\$@\"
742 }" 666 }"
743 done 667 done
744 unset python_default_function 668 unset python_default_function
745 669
670 python_src_install() {
671 if [[ "${EBUILD_PHASE}" != "install" ]]; then
672 die "${FUNCNAME}() can be used only in src_install() phase"
673 fi
674
675 if ! _python_package_supporting_installation_for_multiple_python_abis; then
676 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
677 fi
678
679 _python_check_python_pkg_setup_execution
680
681 if has "${EAPI:-0}" 0 1 2 3; then
682 python_execute_function -d -s -- "$@"
683 else
684 python_installation() {
685 emake DESTDIR="${T}/images/${PYTHON_ABI}" install "$@"
686 }
687 python_execute_function -s python_installation "$@"
688 unset python_installation
689
690 python_merge_intermediate_installation_images "${T}/images"
691 fi
692 }
693
746 if [[ -n "${PYTHON_EXPORT_PHASE_FUNCTIONS}" ]]; then 694 if [[ -n "${PYTHON_EXPORT_PHASE_FUNCTIONS}" ]]; then
747 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install 695 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
748 fi 696 fi
749fi 697fi
750 698
751if has "${EAPI:-0}" 0 1 2 3; then 699if has "${EAPI:-0}" 0 1 2 3 4; then
752 unset PYTHON_ABIS 700 unset PYTHON_ABIS
753fi 701fi
754 702
755_python_calculate_PYTHON_ABIS() { 703_python_calculate_PYTHON_ABIS() {
756 if ! _python_package_supporting_installation_for_multiple_python_abis; then 704 if ! _python_package_supporting_installation_for_multiple_python_abis; then
757 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs" 705 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
758 fi 706 fi
759 707
760 _python_initial_sanity_checks 708 _python_initial_sanity_checks
761 709
762 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then 710 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
763 local PYTHON_ABI restricted_ABI restricted_ABIs support_ABI supported_PYTHON_ABIS 711 local PYTHON_ABI restricted_ABI restricted_ABIs support_ABI supported_PYTHON_ABIS
764 712
765 restricted_ABIs="${RESTRICT_PYTHON_ABIS// /$'\n'}" 713 restricted_ABIs="${RESTRICT_PYTHON_ABIS// /$'\n'}"
766 714
767 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then 715 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
770 if [[ -z "${USE_PYTHON}" ]]; then 718 if [[ -z "${USE_PYTHON}" ]]; then
771 die "USE_PYTHON variable is empty" 719 die "USE_PYTHON variable is empty"
772 fi 720 fi
773 721
774 for PYTHON_ABI in ${USE_PYTHON}; do 722 for PYTHON_ABI in ${USE_PYTHON}; do
775 if ! has "${PYTHON_ABI}" ${_PYTHON_SUPPORTED_ABIS[@]}; then 723 if ! has "${PYTHON_ABI}" "${_PYTHON_GLOBALLY_SUPPORTED_ABIS[@]}"; then
776 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'" 724 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
777 fi 725 fi
778 726
779 if has "${PYTHON_ABI}" "${_CPYTHON2_SUPPORTED_ABIS[@]}" "${_CPYTHON3_SUPPORTED_ABIS[@]}"; then 727 if has "${PYTHON_ABI}" "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; then
780 cpython_enabled="1" 728 cpython_enabled="1"
781 fi 729 fi
782 730
783 support_ABI="1" 731 support_ABI="1"
784 while read restricted_ABI; do 732 while read restricted_ABI; do
785 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then 733 if _python_check_python_abi_matching "${PYTHON_ABI}" "${restricted_ABI}"; then
786 support_ABI="0" 734 support_ABI="0"
787 break 735 break
788 fi 736 fi
789 done <<< "${restricted_ABIs}" 737 done <<< "${restricted_ABIs}"
790 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}" 738 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
796 744
797 if [[ "${cpython_enabled}" == "0" ]]; then 745 if [[ "${cpython_enabled}" == "0" ]]; then
798 die "USE_PYTHON variable does not enable any CPython ABI" 746 die "USE_PYTHON variable does not enable any CPython ABI"
799 fi 747 fi
800 else 748 else
801 local python_version python2_version= python3_version= support_python_major_version 749 local python_version python2_version python3_version support_python_major_version
802 750
803 if ! has_version "dev-lang/python"; then 751 if ! has_version "dev-lang/python"; then
804 die "${FUNCNAME}(): 'dev-lang/python' is not installed" 752 die "${FUNCNAME}(): 'dev-lang/python' is not installed"
805 fi 753 fi
806 754
811 die "'${EPREFIX}/usr/bin/python2' is not valid symlink" 759 die "'${EPREFIX}/usr/bin/python2' is not valid symlink"
812 fi 760 fi
813 761
814 python2_version="$("${EPREFIX}/usr/bin/python2" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')" 762 python2_version="$("${EPREFIX}/usr/bin/python2" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
815 763
816 for PYTHON_ABI in "${_CPYTHON2_SUPPORTED_ABIS[@]}"; do 764 for PYTHON_ABI in "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}"; do
817 support_python_major_version="1" 765 support_python_major_version="1"
818 while read restricted_ABI; do 766 while read restricted_ABI; do
819 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then 767 if _python_check_python_abi_matching "${PYTHON_ABI}" "${restricted_ABI}"; then
820 support_python_major_version="0" 768 support_python_major_version="0"
821 fi 769 fi
822 done <<< "${restricted_ABIs}" 770 done <<< "${restricted_ABIs}"
823 [[ "${support_python_major_version}" == "1" ]] && break 771 [[ "${support_python_major_version}" == "1" ]] && break
824 done 772 done
825 if [[ "${support_python_major_version}" == "1" ]]; then 773 if [[ "${support_python_major_version}" == "1" ]]; then
826 while read restricted_ABI; do 774 while read restricted_ABI; do
827 if [[ "${python2_version}" == ${restricted_ABI} ]]; then 775 if _python_check_python_abi_matching "${python2_version}" "${restricted_ABI}"; then
828 die "Active version of Python 2 is not supported by ${CATEGORY}/${PF}" 776 die "Active version of CPython 2 is not supported by ${CATEGORY}/${PF}"
829 fi 777 fi
830 done <<< "${restricted_ABIs}" 778 done <<< "${restricted_ABIs}"
831 else 779 else
832 python2_version="" 780 python2_version=""
833 fi 781 fi
838 die "'${EPREFIX}/usr/bin/python3' is not valid symlink" 786 die "'${EPREFIX}/usr/bin/python3' is not valid symlink"
839 fi 787 fi
840 788
841 python3_version="$("${EPREFIX}/usr/bin/python3" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')" 789 python3_version="$("${EPREFIX}/usr/bin/python3" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
842 790
843 for PYTHON_ABI in "${_CPYTHON3_SUPPORTED_ABIS[@]}"; do 791 for PYTHON_ABI in "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; do
844 support_python_major_version="1" 792 support_python_major_version="1"
845 while read restricted_ABI; do 793 while read restricted_ABI; do
846 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then 794 if _python_check_python_abi_matching "${PYTHON_ABI}" "${restricted_ABI}"; then
847 support_python_major_version="0" 795 support_python_major_version="0"
848 fi 796 fi
849 done <<< "${restricted_ABIs}" 797 done <<< "${restricted_ABIs}"
850 [[ "${support_python_major_version}" == "1" ]] && break 798 [[ "${support_python_major_version}" == "1" ]] && break
851 done 799 done
852 if [[ "${support_python_major_version}" == "1" ]]; then 800 if [[ "${support_python_major_version}" == "1" ]]; then
853 while read restricted_ABI; do 801 while read restricted_ABI; do
854 if [[ "${python3_version}" == ${restricted_ABI} ]]; then 802 if _python_check_python_abi_matching "${python3_version}" "${restricted_ABI}"; then
855 die "Active version of Python 3 is not supported by ${CATEGORY}/${PF}" 803 die "Active version of CPython 3 is not supported by ${CATEGORY}/${PF}"
856 fi 804 fi
857 done <<< "${restricted_ABIs}" 805 done <<< "${restricted_ABIs}"
858 else 806 else
859 python3_version="" 807 python3_version=""
860 fi 808 fi
892 if [[ "${element}" =~ ^([[:alnum:]]|\.|-|\*|\[|\])+\ (\+|-)\ .+ ]]; then 840 if [[ "${element}" =~ ^([[:alnum:]]|\.|-|\*|\[|\])+\ (\+|-)\ .+ ]]; then
893 pattern="${element%% *}" 841 pattern="${element%% *}"
894 element="${element#* }" 842 element="${element#* }"
895 operator="${element%% *}" 843 operator="${element%% *}"
896 flags="${element#* }" 844 flags="${element#* }"
897 if [[ "${PYTHON_ABI}" == ${pattern} ]]; then 845 if _python_check_python_abi_matching "${PYTHON_ABI}" "${pattern}"; then
898 if [[ "${operator}" == "+" ]]; then 846 if [[ "${operator}" == "+" ]]; then
899 eval "export ${variable}+=\"\${variable:+ }${flags}\"" 847 eval "export ${variable}+=\"\${variable:+ }${flags}\""
900 elif [[ "${operator}" == "-" ]]; then 848 elif [[ "${operator}" == "-" ]]; then
901 flags="${flags// /$'\n'}" 849 flags="${flags// /$'\n'}"
902 old_value="${!variable// /$'\n'}" 850 old_value="${!variable// /$'\n'}"
936# @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] 884# @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]
937# @DESCRIPTION: 885# @DESCRIPTION:
938# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 886# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
939# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 887# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
940python_execute_function() { 888python_execute_function() {
941 _python_check_python_pkg_setup_execution
942
943 if ! _python_package_supporting_installation_for_multiple_python_abis; then 889 if ! _python_package_supporting_installation_for_multiple_python_abis; then
944 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs" 890 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
945 fi 891 fi
946 892
893 _python_check_python_pkg_setup_execution
947 _python_set_color_variables 894 _python_set_color_variables
948 895
949 local action action_message action_message_template= default_function="0" failure_message failure_message_template= final_ABI="0" function iterated_PYTHON_ABIS nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" return_code separate_build_dirs="0" source_dir= 896 local action action_message action_message_template default_function="0" failure_message failure_message_template final_ABI="0" function iterated_PYTHON_ABIS nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" return_code separate_build_dirs="0" source_dir
950 897
951 while (($#)); do 898 while (($#)); do
952 case "$1" in 899 case "$1" in
953 --action-message) 900 --action-message)
954 action_message_template="$2" 901 action_message_template="$2"
1066 iterated_PYTHON_ABIS="$(PYTHON -f --ABI)" 1013 iterated_PYTHON_ABIS="$(PYTHON -f --ABI)"
1067 else 1014 else
1068 iterated_PYTHON_ABIS="${PYTHON_ABIS}" 1015 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
1069 fi 1016 fi
1070 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do 1017 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
1018 if [[ "${EBUILD_PHASE}" == "test" ]] && _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${PYTHON_TESTS_RESTRICTED_ABIS}"; then
1019 if [[ "${quiet}" == "0" ]]; then
1020 echo " ${_GREEN}*${_NORMAL} ${_BLUE}Testing of ${CATEGORY}/${PF} with $(python_get_implementation) $(python_get_version) skipped${_NORMAL}"
1021 fi
1022 continue
1023 fi
1024
1071 _python_prepare_flags 1025 _python_prepare_flags
1072 1026
1073 if [[ "${quiet}" == "0" ]]; then 1027 if [[ "${quiet}" == "0" ]]; then
1074 if [[ -n "${action_message_template}" ]]; then 1028 if [[ -n "${action_message_template}" ]]; then
1075 eval "action_message=\"${action_message_template}\"" 1029 eval "action_message=\"${action_message_template}\""
1169# @FUNCTION: python_copy_sources 1123# @FUNCTION: python_copy_sources
1170# @USAGE: <directory="${S}"> [directory] 1124# @USAGE: <directory="${S}"> [directory]
1171# @DESCRIPTION: 1125# @DESCRIPTION:
1172# Copy unpacked sources of current package to separate build directory for each Python ABI. 1126# Copy unpacked sources of current package to separate build directory for each Python ABI.
1173python_copy_sources() { 1127python_copy_sources() {
1174 _python_check_python_pkg_setup_execution
1175
1176 if ! _python_package_supporting_installation_for_multiple_python_abis; then 1128 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1177 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs" 1129 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1178 fi 1130 fi
1131
1132 _python_check_python_pkg_setup_execution
1179 1133
1180 local dir dirs=() PYTHON_ABI 1134 local dir dirs=() PYTHON_ABI
1181 1135
1182 if [[ "$#" -eq 0 ]]; then 1136 if [[ "$#" -eq 0 ]]; then
1183 if [[ "${WORKDIR}" == "${S}" ]]; then 1137 if [[ "${WORKDIR}" == "${S}" ]]; then
1200# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files] 1154# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
1201# @DESCRIPTION: 1155# @DESCRIPTION:
1202# Generate wrapper scripts. Existing files are overwritten only with --force option. 1156# Generate wrapper scripts. Existing files are overwritten only with --force option.
1203# If --respect-EPYTHON option is specified, then generated wrapper scripts will 1157# If --respect-EPYTHON option is specified, then generated wrapper scripts will
1204# respect EPYTHON variable at run time. 1158# respect EPYTHON variable at run time.
1159#
1160# This function can be used only in src_install() phase.
1205python_generate_wrapper_scripts() { 1161python_generate_wrapper_scripts() {
1206 _python_check_python_pkg_setup_execution 1162 if [[ "${EBUILD_PHASE}" != "install" ]]; then
1163 die "${FUNCNAME}() can be used only in src_install() phase"
1164 fi
1207 1165
1208 if ! _python_package_supporting_installation_for_multiple_python_abis; then 1166 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1209 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs" 1167 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1210 fi 1168 fi
1211 1169
1170 _python_check_python_pkg_setup_execution
1212 _python_initialize_prefix_variables 1171 _python_initialize_prefix_variables
1213 1172
1214 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python3_enabled="0" respect_EPYTHON="0" 1173 local eselect_python_option file force="0" quiet="0" PYTHON_ABI PYTHON_ABIS_list python2_enabled="0" python3_enabled="0" respect_EPYTHON="0"
1215 1174
1216 while (($#)); do 1175 while (($#)); do
1217 case "$1" in 1176 case "$1" in
1218 -E|--respect-EPYTHON) 1177 -E|--respect-EPYTHON)
1219 respect_EPYTHON="1" 1178 respect_EPYTHON="1"
1241 if [[ "$#" -eq 0 ]]; then 1200 if [[ "$#" -eq 0 ]]; then
1242 die "${FUNCNAME}(): Missing arguments" 1201 die "${FUNCNAME}(): Missing arguments"
1243 fi 1202 fi
1244 1203
1245 _python_calculate_PYTHON_ABIS 1204 _python_calculate_PYTHON_ABIS
1246 for PYTHON_ABI in "${_CPYTHON2_SUPPORTED_ABIS[@]}"; do 1205 for PYTHON_ABI in "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}"; do
1247 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then 1206 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
1248 python2_enabled="1" 1207 python2_enabled="1"
1249 fi 1208 fi
1250 done 1209 done
1251 for PYTHON_ABI in "${_CPYTHON3_SUPPORTED_ABIS[@]}"; do 1210 for PYTHON_ABI in "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; do
1252 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then 1211 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
1253 python3_enabled="1" 1212 python3_enabled="1"
1254 fi 1213 fi
1255 done 1214 done
1256 1215
1262 eselect_python_option="--python3" 1221 eselect_python_option="--python3"
1263 else 1222 else
1264 die "${FUNCNAME}(): Unsupported environment" 1223 die "${FUNCNAME}(): Unsupported environment"
1265 fi 1224 fi
1266 1225
1226 PYTHON_ABIS_list="$("$(PYTHON -f)" -c "print(', '.join('\"%s\"' % x for x in reversed('${PYTHON_ABIS}'.split())))")"
1227
1267 for file in "$@"; do 1228 for file in "$@"; do
1268 if [[ -f "${file}" && "${force}" == "0" ]]; then 1229 if [[ -f "${file}" && "${force}" == "0" ]]; then
1269 die "${FUNCNAME}(): '$1' already exists" 1230 die "${FUNCNAME}(): '${file}' already exists"
1270 fi 1231 fi
1271 1232
1272 if [[ "${quiet}" == "0" ]]; then 1233 if [[ "${quiet}" == "0" ]]; then
1273 einfo "Generating '${file#${ED%/}}' wrapper script" 1234 einfo "Generating '${file#${ED%/}}' wrapper script"
1274 fi 1235 fi
1280import os 1241import os
1281import re 1242import re
1282import subprocess 1243import subprocess
1283import sys 1244import sys
1284 1245
1285EPYTHON_re = re.compile(r"^python(\d+\.\d+)$") 1246cpython_re = re.compile(r"^python(\d+\.\d+)$")
1247jython_re = re.compile(r"^jython(\d+\.\d+)$")
1286python_shebang_re = re.compile(r"^#! *(${EPREFIX}/usr/bin/python|(${EPREFIX})?/usr/bin/env +(${EPREFIX}/usr/bin/)?python)") 1248python_shebang_re = re.compile(r"^#! *(${EPREFIX}/usr/bin/python|(${EPREFIX})?/usr/bin/env +(${EPREFIX}/usr/bin/)?python)")
1287python_verification_output_re = re.compile("^GENTOO_PYTHON_TARGET_SCRIPT_PATH supported\n$") 1249python_verification_output_re = re.compile("^GENTOO_PYTHON_TARGET_SCRIPT_PATH supported\n$")
1250
1251def get_PYTHON_ABI(EPYTHON):
1252 cpython_matched = cpython_re.match(EPYTHON)
1253 jython_matched = jython_re.match(EPYTHON)
1254 if cpython_matched is not None:
1255 PYTHON_ABI = cpython_matched.group(1)
1256 elif jython_matched is not None:
1257 PYTHON_ABI = jython_matched.group(1) + "-jython"
1258 else:
1259 PYTHON_ABI = None
1260 return PYTHON_ABI
1288 1261
1289EOF 1262EOF
1290 if [[ "$?" != "0" ]]; then 1263 if [[ "$?" != "0" ]]; then
1291 die "${FUNCNAME}(): Generation of '$1' failed" 1264 die "${FUNCNAME}(): Generation of '$1' failed"
1292 fi 1265 fi
1293 if [[ "${respect_EPYTHON}" == "1" ]]; then 1266 if [[ "${respect_EPYTHON}" == "1" ]]; then
1294 cat << EOF >> "${file}" 1267 cat << EOF >> "${file}"
1295EPYTHON = os.environ.get("EPYTHON") 1268EPYTHON = os.environ.get("EPYTHON")
1296if EPYTHON: 1269if EPYTHON:
1297 EPYTHON_matched = EPYTHON_re.match(EPYTHON) 1270 PYTHON_ABI = get_PYTHON_ABI(EPYTHON)
1298 if EPYTHON_matched: 1271 if PYTHON_ABI is None:
1299 PYTHON_ABI = EPYTHON_matched.group(1)
1300 else:
1301 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON) 1272 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
1302 sys.exit(1) 1273 sys.exit(1)
1303else: 1274else:
1304 try: 1275 try:
1305 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE) 1276 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
1313 if not isinstance(EPYTHON, str): 1284 if not isinstance(EPYTHON, str):
1314 # Python 3 1285 # Python 3
1315 EPYTHON = EPYTHON.decode() 1286 EPYTHON = EPYTHON.decode()
1316 EPYTHON = EPYTHON.rstrip("\n") 1287 EPYTHON = EPYTHON.rstrip("\n")
1317 1288
1318 EPYTHON_matched = EPYTHON_re.match(EPYTHON) 1289 PYTHON_ABI = get_PYTHON_ABI(EPYTHON)
1319 if EPYTHON_matched: 1290 if PYTHON_ABI is None:
1320 PYTHON_ABI = EPYTHON_matched.group(1)
1321 else:
1322 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % EPYTHON) 1291 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % EPYTHON)
1323 sys.exit(1) 1292 sys.exit(1)
1293
1294wrapper_script_path = os.path.realpath(sys.argv[0])
1295target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
1296if not os.path.exists(target_executable_path):
1297 sys.stderr.write("'%s' does not exist\n" % target_executable_path)
1298 sys.exit(1)
1324EOF 1299EOF
1325 if [[ "$?" != "0" ]]; then 1300 if [[ "$?" != "0" ]]; then
1326 die "${FUNCNAME}(): Generation of '$1' failed" 1301 die "${FUNCNAME}(): Generation of '$1' failed"
1327 fi 1302 fi
1328 else 1303 else
1339if not isinstance(EPYTHON, str): 1314if not isinstance(EPYTHON, str):
1340 # Python 3 1315 # Python 3
1341 EPYTHON = EPYTHON.decode() 1316 EPYTHON = EPYTHON.decode()
1342EPYTHON = EPYTHON.rstrip("\n") 1317EPYTHON = EPYTHON.rstrip("\n")
1343 1318
1344EPYTHON_matched = EPYTHON_re.match(EPYTHON) 1319PYTHON_ABI = get_PYTHON_ABI(EPYTHON)
1345if EPYTHON_matched: 1320if PYTHON_ABI is None:
1346 PYTHON_ABI = EPYTHON_matched.group(1) 1321 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % EPYTHON)
1322 sys.exit(1)
1323
1324wrapper_script_path = os.path.realpath(sys.argv[0])
1325for PYTHON_ABI in [PYTHON_ABI, ${PYTHON_ABIS_list}]:
1326 target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
1327 if os.path.exists(target_executable_path):
1328 break
1347else: 1329else:
1348 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % EPYTHON) 1330 sys.stderr.write("No target script exists for '%s'\n" % wrapper_script_path)
1349 sys.exit(1) 1331 sys.exit(1)
1350EOF 1332EOF
1351 if [[ "$?" != "0" ]]; then 1333 if [[ "$?" != "0" ]]; then
1352 die "${FUNCNAME}(): Generation of '$1' failed" 1334 die "${FUNCNAME}(): Generation of '$1' failed"
1353 fi 1335 fi
1354 fi 1336 fi
1355 cat << EOF >> "${file}" 1337 cat << EOF >> "${file}"
1356
1357wrapper_script_path = os.path.realpath(sys.argv[0])
1358target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
1359os.environ["GENTOO_PYTHON_PROCESS_NAME"] = os.path.basename(sys.argv[0])
1360os.environ["GENTOO_PYTHON_WRAPPER_SCRIPT_PATH"] = sys.argv[0]
1361os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH"] = target_executable_path
1362if not os.path.exists(target_executable_path):
1363 sys.stderr.write("'%s' does not exist\n" % target_executable_path)
1364 sys.exit(1)
1365 1338
1366target_executable = open(target_executable_path, "rb") 1339target_executable = open(target_executable_path, "rb")
1367target_executable_first_line = target_executable.readline() 1340target_executable_first_line = target_executable.readline()
1368if not isinstance(target_executable_first_line, str): 1341if not isinstance(target_executable_first_line, str):
1369 # Python 3 1342 # Python 3
1370 target_executable_first_line = target_executable_first_line.decode("utf_8", "replace") 1343 target_executable_first_line = target_executable_first_line.decode("utf_8", "replace")
1371 1344
1372python_shebang_matched = python_shebang_re.match(target_executable_first_line) 1345python_shebang_matched = python_shebang_re.match(target_executable_first_line)
1373target_executable.close() 1346target_executable.close()
1374 1347
1375if python_shebang_matched: 1348if python_shebang_matched is not None:
1376 try: 1349 try:
1377 python_interpreter_path = "${EPREFIX}/usr/bin/%s" % EPYTHON 1350 python_interpreter_path = "${EPREFIX}/usr/bin/%s" % EPYTHON
1378 os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"] = "1" 1351 os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"] = "1"
1379 python_verification_process = subprocess.Popen([python_interpreter_path, "-c", "pass"], stdout=subprocess.PIPE) 1352 python_verification_process = subprocess.Popen([python_interpreter_path, "-c", "pass"], stdout=subprocess.PIPE)
1380 del os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"] 1353 del os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"]
1387 python_verification_output = python_verification_output.decode() 1360 python_verification_output = python_verification_output.decode()
1388 1361
1389 if not python_verification_output_re.match(python_verification_output): 1362 if not python_verification_output_re.match(python_verification_output):
1390 raise ValueError 1363 raise ValueError
1391 1364
1365 if cpython_re.match(EPYTHON) is not None:
1366 os.environ["GENTOO_PYTHON_PROCESS_NAME"] = os.path.basename(sys.argv[0])
1367 os.environ["GENTOO_PYTHON_WRAPPER_SCRIPT_PATH"] = sys.argv[0]
1368 os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH"] = target_executable_path
1369
1370 if hasattr(os, "execv"):
1392 os.execv(python_interpreter_path, [python_interpreter_path] + sys.argv) 1371 os.execv(python_interpreter_path, [python_interpreter_path] + sys.argv)
1372 else:
1373 sys.exit(subprocess.Popen([python_interpreter_path] + sys.argv).wait())
1374 except (KeyboardInterrupt, SystemExit):
1375 raise
1393 except: 1376 except:
1394 pass 1377 pass
1395 if "GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION" in os.environ: 1378 for variable in ("GENTOO_PYTHON_PROCESS_NAME", "GENTOO_PYTHON_WRAPPER_SCRIPT_PATH", "GENTOO_PYTHON_TARGET_SCRIPT_PATH", "GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"):
1396 del os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"] 1379 if variable in os.environ:
1380 del os.environ[variable]
1397 1381
1382if hasattr(os, "execv"):
1398os.execv(target_executable_path, sys.argv) 1383 os.execv(target_executable_path, sys.argv)
1384else:
1385 sys.exit(subprocess.Popen([target_executable_path] + sys.argv[1:]).wait())
1399EOF 1386EOF
1400 if [[ "$?" != "0" ]]; then 1387 if [[ "$?" != "0" ]]; then
1401 die "${FUNCNAME}(): Generation of '$1' failed" 1388 die "${FUNCNAME}(): Generation of '$1' failed"
1402 fi 1389 fi
1403 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed" 1390 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed"
1404 done 1391 done
1405} 1392}
1406 1393
1394# @ECLASS-VARIABLE: PYTHON_VERSIONED_SCRIPTS
1395# @DESCRIPTION:
1396# Array of regular expressions of paths to versioned Python scripts.
1397# Python scripts in /usr/bin and /usr/sbin are versioned by default.
1398
1399# @ECLASS-VARIABLE: PYTHON_VERSIONED_EXECUTABLES
1400# @DESCRIPTION:
1401# Array of regular expressions of paths to versioned executables (including Python scripts).
1402
1403# @ECLASS-VARIABLE: PYTHON_NONVERSIONED_EXECUTABLES
1404# @DESCRIPTION:
1405# Array of regular expressions of paths to nonversioned executables (including Python scripts).
1406
1407# @FUNCTION: python_merge_intermediate_installation_images
1408# @USAGE: [-q|--quiet] [--] <intermediate_installation_images_directory>
1409# @DESCRIPTION:
1410# Merge intermediate installation images into installation image.
1411#
1412# This function can be used only in src_install() phase.
1413python_merge_intermediate_installation_images() {
1414 if [[ "${EBUILD_PHASE}" != "install" ]]; then
1415 die "${FUNCNAME}() can be used only in src_install() phase"
1416 fi
1417
1418 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1419 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1420 fi
1421
1422 _python_check_python_pkg_setup_execution
1423 _python_initialize_prefix_variables
1424
1425 local b file files=() intermediate_installation_images_directory PYTHON_ABI quiet="0" regex shebang version_executable wrapper_scripts=() wrapper_scripts_set=()
1426
1427 while (($#)); do
1428 case "$1" in
1429 -q|--quiet)
1430 quiet="1"
1431 ;;
1432 --)
1433 shift
1434 break
1435 ;;
1436 -*)
1437 die "${FUNCNAME}(): Unrecognized option '$1'"
1438 ;;
1439 *)
1440 break
1441 ;;
1442 esac
1443 shift
1444 done
1445
1446 if [[ "$#" -ne 1 ]]; then
1447 die "${FUNCNAME}() requires 1 argument"
1448 fi
1449
1450 intermediate_installation_images_directory="$1"
1451
1452 if [[ ! -d "${intermediate_installation_images_directory}" ]]; then
1453 die "${FUNCNAME}(): Intermediate installation images directory '${intermediate_installation_images_directory}' does not exist"
1454 fi
1455
1456 _python_calculate_PYTHON_ABIS
1457 if [[ "$(PYTHON -f --ABI)" == 3.* ]]; then
1458 b="b"
1459 fi
1460
1461 while read -d $'\0' -r file; do
1462 files+=("${file}")
1463 done < <("$(PYTHON -f)" -c \
1464"import os
1465import sys
1466
1467if hasattr(sys.stdout, 'buffer'):
1468 # Python 3
1469 stdout = sys.stdout.buffer
1470else:
1471 # Python 2
1472 stdout = sys.stdout
1473
1474files_set = set()
1475
1476os.chdir(${b}'${intermediate_installation_images_directory}')
1477
1478for PYTHON_ABI in ${b}'${PYTHON_ABIS}'.split():
1479 for root, dirs, files in os.walk(PYTHON_ABI + ${b}'${EPREFIX}'):
1480 root = root[len(PYTHON_ABI + ${b}'${EPREFIX}')+1:]
1481 files_set.update(root + ${b}'/' + file for file in files)
1482
1483for file in sorted(files_set):
1484 stdout.write(file)
1485 stdout.write(${b}'\x00')" || die "${FUNCNAME}(): Failure of extraction of files in intermediate installation images")
1486
1487 for PYTHON_ABI in ${PYTHON_ABIS}; do
1488 if [[ ! -d "${intermediate_installation_images_directory}/${PYTHON_ABI}" ]]; then
1489 die "${FUNCNAME}(): Intermediate installation image for Python ABI '${PYTHON_ABI}' does not exist"
1490 fi
1491
1492 pushd "${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}" > /dev/null || die "pushd failed"
1493
1494 for file in "${files[@]}"; do
1495 version_executable="0"
1496 for regex in "/usr/bin/.*" "/usr/sbin/.*" "${PYTHON_VERSIONED_SCRIPTS[@]}"; do
1497 if [[ "/${file}" =~ ^${regex}$ ]]; then
1498 version_executable="1"
1499 break
1500 fi
1501 done
1502 for regex in "${PYTHON_VERSIONED_EXECUTABLES[@]}"; do
1503 if [[ "/${file}" =~ ^${regex}$ ]]; then
1504 version_executable="2"
1505 break
1506 fi
1507 done
1508 if [[ "${version_executable}" != "0" ]]; then
1509 for regex in "${PYTHON_NONVERSIONED_EXECUTABLES[@]}"; do
1510 if [[ "/${file}" =~ ^${regex}$ ]]; then
1511 version_executable="0"
1512 break
1513 fi
1514 done
1515 fi
1516
1517 [[ "${version_executable}" == "0" || ! -x "${file}" ]] && continue
1518
1519 shebang="$(head -n1 "${file}")" || die "Extraction of shebang from '${file}' failed"
1520
1521 if [[ "${version_executable}" == "2" ]]; then
1522 wrapper_scripts+=("${ED}${file}")
1523 elif [[ "${version_executable}" == "1" ]]; then
1524 if [[ "${shebang}" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX}([[:digit:]]+(\.[[:digit:]]+)?)?($|[[:space:]]+) ]]; then
1525 wrapper_scripts+=("${ED}${file}")
1526 else
1527 version_executable="0"
1528 fi
1529 fi
1530
1531 [[ "${version_executable}" == "0" ]] && continue
1532
1533 if [[ -e "${file}-${PYTHON_ABI}" ]]; then
1534 die "${FUNCNAME}(): '${EPREFIX}/${file}-${PYTHON_ABI}' already exists"
1535 fi
1536
1537 mv "${file}" "${file}-${PYTHON_ABI}" || die "Renaming of '${file}' failed"
1538
1539 if [[ "${shebang}" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX}[[:digit:]]*($|[[:space:]]+) ]]; then
1540 python_convert_shebangs $([[ "${quiet}" == "1" ]] && echo --quiet) "${PYTHON_ABI}" "${file}-${PYTHON_ABI}"
1541 fi
1542 done
1543
1544 popd > /dev/null || die "popd failed"
1545
1546 if ROOT="/" has_version sys-apps/coreutils; then
1547 cp -fr --preserve=all "${intermediate_installation_images_directory}/${PYTHON_ABI}/"* "${D}" || die "Merging of intermediate installation image for Python ABI '${PYTHON_ABI} into installation image failed"
1548 else
1549 cp -fpr "${intermediate_installation_images_directory}/${PYTHON_ABI}/"* "${D}" || die "Merging of intermediate installation image for Python ABI '${PYTHON_ABI} into installation image failed"
1550 fi
1551 done
1552
1553 rm -fr "${intermediate_installation_images_directory}"
1554
1555 if [[ "${#wrapper_scripts[@]}" -ge 1 ]]; then
1556 rm -f "${T}/python_wrapper_scripts"
1557
1558 for file in "${wrapper_scripts[@]}"; do
1559 echo -n "${file}" >> "${T}/python_wrapper_scripts"
1560 echo -en "\x00" >> "${T}/python_wrapper_scripts"
1561 done
1562
1563 while read -d $'\0' -r file; do
1564 wrapper_scripts_set+=("${file}")
1565 done < <("$(PYTHON -f)" -c \
1566"import sys
1567
1568if hasattr(sys.stdout, 'buffer'):
1569 # Python 3
1570 stdout = sys.stdout.buffer
1571else:
1572 # Python 2
1573 stdout = sys.stdout
1574
1575files = set(open('${T}/python_wrapper_scripts', 'rb').read().rstrip(${b}'\x00').split(${b}'\x00'))
1576
1577for file in sorted(files):
1578 stdout.write(file)
1579 stdout.write(${b}'\x00')" || die "${FUNCNAME}(): Failure of extraction of set of wrapper scripts")
1580
1581 python_generate_wrapper_scripts $([[ "${quiet}" == "1" ]] && echo --quiet) "${wrapper_scripts_set[@]}"
1582 fi
1583}
1584
1407# ================================================================================================ 1585# ================================================================================================
1408# ========= FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ========== 1586# ========= FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ==========
1409# ================================================================================================ 1587# ================================================================================================
1410 1588
1411unset EPYTHON PYTHON_ABI 1589unset EPYTHON PYTHON_ABI
1412 1590
1413# @FUNCTION: python_set_active_version 1591# @FUNCTION: python_set_active_version
1414# @USAGE: <CPython_ABI|2|3> 1592# @USAGE: <Python_ABI|2|3>
1415# @DESCRIPTION: 1593# @DESCRIPTION:
1416# Set specified version of CPython as active version of Python. 1594# Set locally active version of Python.
1595# If Python_ABI argument is specified, then version of Python corresponding to Python_ABI is used.
1596# If 2 argument is specified, then active version of CPython 2 is used.
1597# If 3 argument is specified, then active version of CPython 3 is used.
1417# 1598#
1418# This function can be used only in pkg_setup() phase. 1599# This function can be used only in pkg_setup() phase.
1419python_set_active_version() { 1600python_set_active_version() {
1420 # Check if phase is pkg_setup(). 1601 if [[ "${EBUILD_PHASE}" != "setup" ]]; then
1421 [[ "${EBUILD_PHASE}" != "setup" ]] && die "${FUNCNAME}() can be used only in pkg_setup() phase" 1602 die "${FUNCNAME}() can be used only in pkg_setup() phase"
1603 fi
1422 1604
1423 if _python_package_supporting_installation_for_multiple_python_abis; then 1605 if _python_package_supporting_installation_for_multiple_python_abis; then
1424 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs" 1606 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
1425 fi 1607 fi
1426 1608
1429 fi 1611 fi
1430 1612
1431 _python_initial_sanity_checks 1613 _python_initial_sanity_checks
1432 1614
1433 if [[ -z "${PYTHON_ABI}" ]]; then 1615 if [[ -z "${PYTHON_ABI}" ]]; then
1434 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then 1616 if [[ -n "$(_python_get_implementation --ignore-invalid "$1")" ]]; then
1435 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then 1617 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
1436 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed" 1618 # so it does not need to be exported to subprocesses.
1619 PYTHON_ABI="$1"
1620 if ! _python_implementation && ! has_version "$(python_get_implementational_package)"; then
1621 die "${FUNCNAME}(): '$(python_get_implementational_package)' is not installed"
1437 fi 1622 fi
1438 export EPYTHON="$(PYTHON "$1")" 1623 export EPYTHON="$(PYTHON "$1")"
1439 elif [[ "$1" == "2" ]]; then 1624 elif [[ "$1" == "2" ]]; then
1440 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then 1625 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
1441 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed" 1626 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
1442 fi 1627 fi
1443 export EPYTHON="$(PYTHON -2)" 1628 export EPYTHON="$(PYTHON -2)"
1629 PYTHON_ABI="${EPYTHON#python}"
1630 PYTHON_ABI="${PYTHON_ABI%%-*}"
1444 elif [[ "$1" == "3" ]]; then 1631 elif [[ "$1" == "3" ]]; then
1445 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then 1632 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
1446 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed" 1633 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
1447 fi 1634 fi
1448 export EPYTHON="$(PYTHON -3)" 1635 export EPYTHON="$(PYTHON -3)"
1636 PYTHON_ABI="${EPYTHON#python}"
1637 PYTHON_ABI="${PYTHON_ABI%%-*}"
1449 else 1638 else
1450 die "${FUNCNAME}(): Unrecognized argument '$1'" 1639 die "${FUNCNAME}(): Unrecognized argument '$1'"
1451 fi 1640 fi
1452
1453 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
1454 # so it does not need to be exported to subprocesses.
1455 PYTHON_ABI="${EPYTHON#python}"
1456 PYTHON_ABI="${PYTHON_ABI%%-*}"
1457 fi 1641 fi
1458 1642
1459 _python_final_sanity_checks 1643 _python_final_sanity_checks
1460 1644
1461 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable. 1645 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
1464 1648
1465# @FUNCTION: python_need_rebuild 1649# @FUNCTION: python_need_rebuild
1466# @DESCRIPTION: Mark current package for rebuilding by python-updater after 1650# @DESCRIPTION: Mark current package for rebuilding by python-updater after
1467# switching of active version of Python. 1651# switching of active version of Python.
1468python_need_rebuild() { 1652python_need_rebuild() {
1469 _python_check_python_pkg_setup_execution
1470
1471 if _python_package_supporting_installation_for_multiple_python_abis; then 1653 if _python_package_supporting_installation_for_multiple_python_abis; then
1472 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs" 1654 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
1473 fi 1655 fi
1656
1657 _python_check_python_pkg_setup_execution
1474 1658
1475 if [[ "$#" -ne 0 ]]; then 1659 if [[ "$#" -ne 0 ]]; then
1476 die "${FUNCNAME}() does not accept arguments" 1660 die "${FUNCNAME}() does not accept arguments"
1477 fi 1661 fi
1478 1662
1488sys.stdout.write(".".join(str(x) for x in sys.version_info[:2])) 1672sys.stdout.write(".".join(str(x) for x in sys.version_info[:2]))
1489if platform.system()[:4] == "Java": 1673if platform.system()[:4] == "Java":
1490 sys.stdout.write("-jython")' 1674 sys.stdout.write("-jython")'
1491 1675
1492_python_get_implementation() { 1676_python_get_implementation() {
1677 local ignore_invalid="0"
1678
1679 while (($#)); do
1680 case "$1" in
1681 --ignore-invalid)
1682 ignore_invalid="1"
1683 ;;
1684 --)
1685 shift
1686 break
1687 ;;
1688 -*)
1689 die "${FUNCNAME}(): Unrecognized option '$1'"
1690 ;;
1691 *)
1692 break
1693 ;;
1694 esac
1695 shift
1696 done
1697
1493 if [[ "$#" -ne 1 ]]; then 1698 if [[ "$#" -ne 1 ]]; then
1494 die "${FUNCNAME}() requires 1 argument" 1699 die "${FUNCNAME}() requires 1 argument"
1495 fi 1700 fi
1496 1701
1497 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then 1702 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
1498 echo "CPython" 1703 echo "CPython"
1499 elif [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+-jython$ ]]; then 1704 elif [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+-jython$ ]]; then
1500 echo "Jython" 1705 echo "Jython"
1501 else 1706 else
1707 if [[ "${ignore_invalid}" == "0" ]]; then
1502 die "${FUNCNAME}(): Unrecognized Python ABI '$1'" 1708 die "${FUNCNAME}(): Unrecognized Python ABI '$1'"
1709 fi
1503 fi 1710 fi
1504} 1711}
1505 1712
1506# @FUNCTION: PYTHON 1713# @FUNCTION: PYTHON
1507# @USAGE: [-2] [-3] [--ABI] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}"> 1714# @USAGE: [-2] [-3] [--ABI] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
1508# @DESCRIPTION: 1715# @DESCRIPTION:
1509# Print filename of Python interpreter for specified Python ABI. If Python_ABI argument 1716# Print filename of Python interpreter for specified Python ABI. If Python_ABI argument
1510# is ommitted, then PYTHON_ABI environment variable must be set and is used. 1717# is ommitted, then PYTHON_ABI environment variable must be set and is used.
1511# If -2 option is specified, then active version of Python 2 is used. 1718# If -2 option is specified, then active version of CPython 2 is used.
1512# If -3 option is specified, then active version of Python 3 is used. 1719# If -3 option is specified, then active version of CPython 3 is used.
1513# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used. 1720# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1514# -2, -3 and --final-ABI options and Python_ABI argument cannot be specified simultaneously. 1721# -2, -3 and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
1515# If --ABI option is specified, then only specified Python ABI is printed instead of 1722# If --ABI option is specified, then only specified Python ABI is printed instead of
1516# filename of Python interpreter. 1723# filename of Python interpreter.
1517# If --absolute-path option is specified, then absolute path to Python interpreter is printed. 1724# If --absolute-path option is specified, then absolute path to Python interpreter is printed.
1568 _python_calculate_PYTHON_ABIS 1775 _python_calculate_PYTHON_ABIS
1569 PYTHON_ABI="${PYTHON_ABIS##* }" 1776 PYTHON_ABI="${PYTHON_ABIS##* }"
1570 elif [[ "${python2}" == "1" ]]; then 1777 elif [[ "${python2}" == "1" ]]; then
1571 PYTHON_ABI="$(eselect python show --python2 --ABI)" 1778 PYTHON_ABI="$(eselect python show --python2 --ABI)"
1572 if [[ -z "${PYTHON_ABI}" ]]; then 1779 if [[ -z "${PYTHON_ABI}" ]]; then
1573 die "${FUNCNAME}(): Active version of Python 2 not set" 1780 die "${FUNCNAME}(): Active version of CPython 2 not set"
1574 elif [[ "${PYTHON_ABI}" != "2."* ]]; then 1781 elif [[ "${PYTHON_ABI}" != "2."* ]]; then
1575 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`" 1782 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1576 fi 1783 fi
1577 elif [[ "${python3}" == "1" ]]; then 1784 elif [[ "${python3}" == "1" ]]; then
1578 PYTHON_ABI="$(eselect python show --python3 --ABI)" 1785 PYTHON_ABI="$(eselect python show --python3 --ABI)"
1579 if [[ -z "${PYTHON_ABI}" ]]; then 1786 if [[ -z "${PYTHON_ABI}" ]]; then
1580 die "${FUNCNAME}(): Active version of Python 3 not set" 1787 die "${FUNCNAME}(): Active version of CPython 3 not set"
1581 elif [[ "${PYTHON_ABI}" != "3."* ]]; then 1788 elif [[ "${PYTHON_ABI}" != "3."* ]]; then
1582 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`" 1789 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1583 fi 1790 fi
1584 elif _python_package_supporting_installation_for_multiple_python_abis; then 1791 elif _python_package_supporting_installation_for_multiple_python_abis; then
1585 if ! _python_abi-specific_local_scope; then 1792 if ! _python_abi-specific_local_scope; then
1611 return 1818 return
1612 else 1819 else
1613 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1820 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1614 python_interpreter="python${PYTHON_ABI}" 1821 python_interpreter="python${PYTHON_ABI}"
1615 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1822 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1616 python_interpreter="jython-${PYTHON_ABI%-jython}" 1823 python_interpreter="jython${PYTHON_ABI%-jython}"
1617 fi 1824 fi
1618 1825
1619 if [[ "${absolute_path_output}" == "1" ]]; then 1826 if [[ "${absolute_path_output}" == "1" ]]; then
1620 echo -n "${EPREFIX}/usr/bin/${python_interpreter}" 1827 echo -n "${EPREFIX}/usr/bin/${python_interpreter}"
1621 else 1828 else
2050# python_execute_nosetests_pre_hook() and python_execute_nosetests_post_hook(), if they are defined. 2257# python_execute_nosetests_pre_hook() and python_execute_nosetests_post_hook(), if they are defined.
2051python_execute_nosetests() { 2258python_execute_nosetests() {
2052 _python_check_python_pkg_setup_execution 2259 _python_check_python_pkg_setup_execution
2053 _python_set_color_variables 2260 _python_set_color_variables
2054 2261
2055 local PYTHONPATH_template= separate_build_dirs= 2262 local PYTHONPATH_template separate_build_dirs
2056 2263
2057 while (($#)); do 2264 while (($#)); do
2058 case "$1" in 2265 case "$1" in
2059 -P|--PYTHONPATH) 2266 -P|--PYTHONPATH)
2060 PYTHONPATH_template="$2" 2267 PYTHONPATH_template="$2"
2114# python_execute_py.test_pre_hook() and python_execute_py.test_post_hook(), if they are defined. 2321# python_execute_py.test_pre_hook() and python_execute_py.test_post_hook(), if they are defined.
2115python_execute_py.test() { 2322python_execute_py.test() {
2116 _python_check_python_pkg_setup_execution 2323 _python_check_python_pkg_setup_execution
2117 _python_set_color_variables 2324 _python_set_color_variables
2118 2325
2119 local PYTHONPATH_template= separate_build_dirs= 2326 local PYTHONPATH_template separate_build_dirs
2120 2327
2121 while (($#)); do 2328 while (($#)); do
2122 case "$1" in 2329 case "$1" in
2123 -P|--PYTHONPATH) 2330 -P|--PYTHONPATH)
2124 PYTHONPATH_template="$2" 2331 PYTHONPATH_template="$2"
2178# calls python_execute_trial_pre_hook() and python_execute_trial_post_hook(), if they are defined. 2385# calls python_execute_trial_pre_hook() and python_execute_trial_post_hook(), if they are defined.
2179python_execute_trial() { 2386python_execute_trial() {
2180 _python_check_python_pkg_setup_execution 2387 _python_check_python_pkg_setup_execution
2181 _python_set_color_variables 2388 _python_set_color_variables
2182 2389
2183 local PYTHONPATH_template= separate_build_dirs= 2390 local PYTHONPATH_template separate_build_dirs
2184 2391
2185 while (($#)); do 2392 while (($#)); do
2186 case "$1" in 2393 case "$1" in
2187 -P|--PYTHONPATH) 2394 -P|--PYTHONPATH)
2188 PYTHONPATH_template="$2" 2395 PYTHONPATH_template="$2"
2328 if [[ "${dir}" == "__pycache__" ]]; then 2535 if [[ "${dir}" == "__pycache__" ]]; then
2329 base_module_name="${compiled_file##*/}" 2536 base_module_name="${compiled_file##*/}"
2330 base_module_name="${base_module_name%\$py.class}" 2537 base_module_name="${base_module_name%\$py.class}"
2331 py_file="${compiled_file%__pycache__/*}${base_module_name}.py" 2538 py_file="${compiled_file%__pycache__/*}${base_module_name}.py"
2332 else 2539 else
2333 py_file="${compiled_file%\$py.class}" 2540 py_file="${compiled_file%\$py.class}.py"
2334 fi 2541 fi
2335 if [[ "${EBUILD_PHASE}" == "postinst" ]]; then 2542 if [[ "${EBUILD_PHASE}" == "postinst" ]]; then
2336 [[ -f "${py_file}" && "${compiled_file}" -nt "${py_file}" ]] && continue 2543 [[ -f "${py_file}" && "${compiled_file}" -nt "${py_file}" ]] && continue
2337 else 2544 else
2338 [[ -f "${py_file}" ]] && continue 2545 [[ -f "${py_file}" ]] && continue
2363# Byte-compile specified Python modules. 2570# Byte-compile specified Python modules.
2364# -d, -f, -l, -q and -x options passed to this function are passed to compileall.py. 2571# -d, -f, -l, -q and -x options passed to this function are passed to compileall.py.
2365# 2572#
2366# This function can be used only in pkg_postinst() phase. 2573# This function can be used only in pkg_postinst() phase.
2367python_mod_optimize() { 2574python_mod_optimize() {
2575 if [[ "${EBUILD_PHASE}" != "postinst" ]]; then
2576 die "${FUNCNAME}() can be used only in pkg_postinst() phase"
2577 fi
2578
2368 _python_check_python_pkg_setup_execution 2579 _python_check_python_pkg_setup_execution
2369 _python_initialize_prefix_variables 2580 _python_initialize_prefix_variables
2370
2371 # Check if phase is pkg_postinst().
2372 [[ "${EBUILD_PHASE}" != "postinst" ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase"
2373 2581
2374 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis || _python_implementation || [[ "${CATEGORY}/${PN}" == "sys-apps/portage" ]]; then 2582 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis || _python_implementation || [[ "${CATEGORY}/${PN}" == "sys-apps/portage" ]]; then
2375 # PYTHON_ABI variable cannot be local in packages not supporting installation for multiple Python ABIs. 2583 # PYTHON_ABI variable cannot be local in packages not supporting installation for multiple Python ABIs.
2376 local allow_evaluated_non_sitedir_paths="0" dir dirs=() evaluated_dirs=() evaluated_files=() file files=() iterated_PYTHON_ABIS options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" return_code root site_packages_dirs=() site_packages_files=() 2584 local allow_evaluated_non_sitedir_paths="0" dir dirs=() evaluated_dirs=() evaluated_files=() file files=() iterated_PYTHON_ABIS options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" return_code root site_packages_dirs=() site_packages_files=()
2377 2585
2538 fi 2746 fi
2539 eend "${return_code}" 2747 eend "${return_code}"
2540 fi 2748 fi
2541 else 2749 else
2542 # Deprecated part of python_mod_optimize() 2750 # Deprecated part of python_mod_optimize()
2543 # ewarn 2751 ewarn
2544 # ewarn "Deprecation Warning: Usage of ${FUNCNAME}() in packages not supporting installation" 2752 ewarn "Deprecation Warning: Usage of ${FUNCNAME}() in packages not supporting installation"
2545 # ewarn "for multiple Python ABIs in EAPI <=2 is deprecated and will be disallowed on 2011-06-01." 2753 ewarn "for multiple Python ABIs in EAPI <=2 is deprecated and will be disallowed on 2011-08-01."
2546 # ewarn "Use EAPI >=3 and call ${FUNCNAME}() with paths having appropriate syntax." 2754 ewarn "Use EAPI >=3 and call ${FUNCNAME}() with paths having appropriate syntax."
2547 # ewarn "The ebuild needs to be fixed. Please report a bug, if it has not been already reported." 2755 ewarn "The ebuild needs to be fixed. Please report a bug, if it has not been already reported."
2548 # ewarn 2756 ewarn
2549 2757
2550 local myroot mydirs=() myfiles=() myopts=() return_code="0" 2758 local myroot mydirs=() myfiles=() myopts=() return_code="0"
2551 2759
2552 # strip trailing slash 2760 # strip trailing slash
2553 myroot="${EROOT%/}" 2761 myroot="${EROOT%/}"
2622# @DESCRIPTION: 2830# @DESCRIPTION:
2623# Delete orphaned byte-compiled Python modules corresponding to specified Python modules. 2831# Delete orphaned byte-compiled Python modules corresponding to specified Python modules.
2624# 2832#
2625# This function can be used only in pkg_postrm() phase. 2833# This function can be used only in pkg_postrm() phase.
2626python_mod_cleanup() { 2834python_mod_cleanup() {
2835 if [[ "${EBUILD_PHASE}" != "postrm" ]]; then
2836 die "${FUNCNAME}() can be used only in pkg_postrm() phase"
2837 fi
2838
2627 _python_check_python_pkg_setup_execution 2839 _python_check_python_pkg_setup_execution
2628 _python_initialize_prefix_variables 2840 _python_initialize_prefix_variables
2629 2841
2630 local allow_evaluated_non_sitedir_paths="0" dir iterated_PYTHON_ABIS PYTHON_ABI="${PYTHON_ABI}" root search_paths=() sitedir 2842 local allow_evaluated_non_sitedir_paths="0" dir iterated_PYTHON_ABIS PYTHON_ABI="${PYTHON_ABI}" root search_paths=() sitedir
2631
2632 # Check if phase is pkg_postrm().
2633 [[ "${EBUILD_PHASE}" != "postrm" ]] && die "${FUNCNAME}() can be used only in pkg_postrm() phase"
2634 2843
2635 if _python_package_supporting_installation_for_multiple_python_abis; then 2844 if _python_package_supporting_installation_for_multiple_python_abis; then
2636 if has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_ABIS}" ]]; then 2845 if has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_ABIS}" ]]; then
2637 die "${FUNCNAME}(): python_pkg_setup() or python_execute_function() not called" 2846 die "${FUNCNAME}(): python_pkg_setup() or python_execute_function() not called"
2638 fi 2847 fi
2702 fi 2911 fi
2703 shift 2912 shift
2704 done 2913 done
2705 else 2914 else
2706 # Deprecated part of python_mod_cleanup() 2915 # Deprecated part of python_mod_cleanup()
2707 # ewarn 2916 ewarn
2708 # ewarn "Deprecation Warning: Usage of ${FUNCNAME}() in packages not supporting installation" 2917 ewarn "Deprecation Warning: Usage of ${FUNCNAME}() in packages not supporting installation"
2709 # ewarn "for multiple Python ABIs in EAPI <=2 is deprecated and will be disallowed on 2011-06-01." 2918 ewarn "for multiple Python ABIs in EAPI <=2 is deprecated and will be disallowed on 2011-08-01."
2710 # ewarn "Use EAPI >=3 and call ${FUNCNAME}() with paths having appropriate syntax." 2919 ewarn "Use EAPI >=3 and call ${FUNCNAME}() with paths having appropriate syntax."
2711 # ewarn "The ebuild needs to be fixed. Please report a bug, if it has not been already reported." 2920 ewarn "The ebuild needs to be fixed. Please report a bug, if it has not been already reported."
2712 # ewarn 2921 ewarn
2713 2922
2714 search_paths=("${@#/}") 2923 search_paths=("${@#/}")
2715 search_paths=("${search_paths[@]/#/${root}/}") 2924 search_paths=("${search_paths[@]/#/${root}/}")
2716 fi 2925 fi
2717 2926
2719} 2928}
2720 2929
2721# ================================================================================================ 2930# ================================================================================================
2722# ===================================== DEPRECATED FUNCTIONS ===================================== 2931# ===================================== DEPRECATED FUNCTIONS =====================================
2723# ================================================================================================ 2932# ================================================================================================
2724
2725# Scheduled for deletion on 2011-01-01.
2726python_version() {
2727 eerror "Use PYTHON() instead of python variable. Use python_get_*() instead of PYVER* variables."
2728 die "${FUNCNAME}() is banned"
2729}
2730
2731# Scheduled for deletion on 2011-01-01.
2732python_mod_exists() {
2733 eerror "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()."
2734 die "${FUNCNAME}() is banned"
2735}
2736
2737# Scheduled for deletion on 2011-01-01.
2738python_tkinter_exists() {
2739 eerror "Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}()."
2740 die "${FUNCNAME}() is banned"
2741}
2742
2743# Scheduled for deletion on 2011-04-01.
2744python_mod_compile() {
2745 eerror "Use python_mod_optimize() instead of ${FUNCNAME}()."
2746 die "${FUNCNAME}() is banned"
2747}

Legend:
Removed from v.1.104  
changed lines
  Added in v.1.116

  ViewVC Help
Powered by ViewVC 1.1.20