1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # Removal on 2017-03-21.
7 # @ECLASS: python.eclass
9 # Gentoo Python Project <python@gentoo.org>
10 # @BLURB: Eclass for Python packages
12 # The python eclass contains miscellaneous, useful functions for Python packages.
14 # This eclass is DEPRECATED. Please use python-r1, python-single-r1
15 # or python-any-r1 instead.
17 if [[ ${EAPI} == 6 ]]; then
18 die "${ECLASS}.eclass is banned in EAPI ${EAPI}"
21 if [[ ${_PYTHON_UTILS_R1} ]]; then
22 die 'python.eclass can not be used with python-r1 suite eclasses.'
25 # Must call inherit before EXPORT_FUNCTIONS to avoid QA warning.
26 if [[ -z "${_PYTHON_ECLASS_INHERITED}" ]]; then
30 # Export pkg_setup every time to avoid issues with eclass inheritance order.
31 if ! has "${EAPI:-0}" 0 1 2 3 || { has "${EAPI:-0}" 2 3 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; }; then
32 EXPORT_FUNCTIONS pkg_setup
35 # Avoid processing this eclass more than once.
36 if [[ -z "${_PYTHON_ECLASS_INHERITED}" ]]; then
37 _PYTHON_ECLASS_INHERITED="1"
39 if ! has "${EAPI:-0}" 0 1 2 3 4 5; then
40 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
43 # Please do not add any new versions of Python here! Instead, please
44 # focus on converting packages to use the new eclasses.
46 _CPYTHON2_GLOBALLY_SUPPORTED_ABIS=(2.4 2.5 2.6 2.7)
47 _CPYTHON3_GLOBALLY_SUPPORTED_ABIS=(3.1 3.2 3.3)
48 _JYTHON_GLOBALLY_SUPPORTED_ABIS=(2.5-jython 2.7-jython)
49 _PYPY_GLOBALLY_SUPPORTED_ABIS=(2.7-pypy-1.7 2.7-pypy-1.8 2.7-pypy-1.9 2.7-pypy-2.0)
50 _PYTHON_GLOBALLY_SUPPORTED_ABIS=(${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]} ${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]} ${_JYTHON_GLOBALLY_SUPPORTED_ABIS[@]} ${_PYPY_GLOBALLY_SUPPORTED_ABIS[@]})
52 # ================================================================================================
53 # ===================================== HANDLING OF METADATA =====================================
54 # ================================================================================================
56 _PYTHON_ABI_PATTERN_REGEX="([[:alnum:]]|\.|-|\*|\[|\])+"
58 _python_check_python_abi_matching() {
59 local pattern patterns patterns_list="0" PYTHON_ABI
71 die "${FUNCNAME}(): Unrecognized option '$1'"
80 if [[ "$#" -ne 2 ]]; then
81 die "${FUNCNAME}() requires 2 arguments"
86 if [[ "${patterns_list}" == "0" ]]; then
89 if [[ "${pattern}" == *"-cpython" ]]; then
90 [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+$ && "${PYTHON_ABI}" == ${pattern%-cpython} ]]
91 elif [[ "${pattern}" == *"-jython" ]]; then
92 [[ "${PYTHON_ABI}" == ${pattern} ]]
93 elif [[ "${pattern}" == *"-pypy-"* ]]; then
94 [[ "${PYTHON_ABI}" == ${pattern} ]]
96 if [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
97 [[ "${PYTHON_ABI}" == ${pattern} ]]
98 elif [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+-jython$ ]]; then
99 [[ "${PYTHON_ABI%-jython}" == ${pattern} ]]
100 elif [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+-pypy-[[:digit:]]+\.[[:digit:]]+$ ]]; then
101 [[ "${PYTHON_ABI%-pypy-*}" == ${pattern} ]]
103 die "${FUNCNAME}(): Unrecognized Python ABI '${PYTHON_ABI}'"
107 patterns="${2// /$'\n'}"
109 while read pattern; do
110 if _python_check_python_abi_matching "${PYTHON_ABI}" "${pattern}"; then
113 done <<< "${patterns}"
119 _python_implementation() {
120 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
122 elif [[ "${CATEGORY}/${PN}" == "dev-java/jython" ]]; then
124 elif [[ "${CATEGORY}/${PN}" == "virtual/pypy" ]]; then
131 _python_package_supporting_installation_for_multiple_python_abis() {
132 [[ -n "${SUPPORT_PYTHON_ABIS}" ]]
135 # @ECLASS-VARIABLE: PYTHON_DEPEND
137 # Specification of dependency on dev-lang/python.
139 # PYTHON_DEPEND: [[!]USE_flag? ]<version_components_group>[ version_components_group]
140 # version_components_group: <major_version[:[minimal_version][:maximal_version]]>
141 # major_version: <2|3|*>
142 # minimal_version: <minimal_major_version.minimal_minor_version>
143 # maximal_version: <maximal_major_version.maximal_minor_version>
145 _python_parse_PYTHON_DEPEND() {
146 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
148 version_components_group_regex="(2|3|\*)(:([[:digit:]]+\.[[:digit:]]+)?(:([[:digit:]]+\.[[:digit:]]+)?)?)?"
149 version_components_groups="${PYTHON_DEPEND}"
151 if [[ "${version_components_groups}" =~ ^((\!)?[[:alnum:]_-]+\?\ )?${version_components_group_regex}(\ ${version_components_group_regex})?$ ]]; then
152 if [[ "${version_components_groups}" =~ ^(\!)?[[:alnum:]_-]+\? ]]; then
153 USE_flag="${version_components_groups%\? *}"
154 version_components_groups="${version_components_groups#* }"
156 if [[ "${version_components_groups}" =~ ("*".*" "|" *"|^2.*\ (2|\*)|^3.*\ (3|\*)) ]]; then
157 die "Invalid syntax of PYTHON_DEPEND: Incorrectly specified groups of versions"
160 version_components_groups="${version_components_groups// /$'\n'}"
161 while read version_components_group; do
162 major_version="${version_components_group:0:1}"
163 minimal_version="${version_components_group:2}"
164 minimal_version="${minimal_version%:*}"
165 maximal_version="${version_components_group:$((3 + ${#minimal_version}))}"
167 if [[ "${major_version}" =~ ^(2|3)$ ]]; then
168 if [[ -n "${minimal_version}" && "${major_version}" != "${minimal_version:0:1}" ]]; then
169 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' not in specified group of versions"
171 if [[ -n "${maximal_version}" && "${major_version}" != "${maximal_version:0:1}" ]]; then
172 die "Invalid syntax of PYTHON_DEPEND: Maximal version '${maximal_version}' not in specified group of versions"
176 if [[ "${major_version}" == "2" ]]; then
178 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}")
179 python2_minimal_version="${minimal_version}"
180 python2_maximal_version="${maximal_version}"
181 elif [[ "${major_version}" == "3" ]]; then
183 python_versions=("${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
184 python3_minimal_version="${minimal_version}"
185 python3_maximal_version="${maximal_version}"
188 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
189 python_minimal_version="${minimal_version}"
190 python_maximal_version="${maximal_version}"
193 if [[ -n "${minimal_version}" ]] && ! has "${minimal_version}" "${python_versions[@]}"; then
194 die "Invalid syntax of PYTHON_DEPEND: Unrecognized minimal version '${minimal_version}'"
196 if [[ -n "${maximal_version}" ]] && ! has "${maximal_version}" "${python_versions[@]}"; then
197 die "Invalid syntax of PYTHON_DEPEND: Unrecognized maximal version '${maximal_version}'"
200 if [[ -n "${minimal_version}" && -n "${maximal_version}" && "${minimal_version}" > "${maximal_version}" ]]; then
201 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' greater than maximal version '${maximal_version}'"
203 done <<< "${version_components_groups}"
207 _append_accepted_versions_range() {
208 local accepted_version="0" i
209 for ((i = "${#python_versions[@]}"; i >= 0; i--)); do
210 if [[ "${python_versions[${i}]}" == "${python_maximal_version}" ]]; then
213 if [[ "${accepted_version}" == "1" ]]; then
214 _PYTHON_ATOMS+=("=dev-lang/python-${python_versions[${i}]}*")
216 if [[ "${python_versions[${i}]}" == "${python_minimal_version}" ]]; then
222 if [[ "${python_all}" == "1" ]]; then
223 if [[ -z "${python_minimal_version}" && -z "${python_maximal_version}" ]]; then
224 _PYTHON_ATOMS+=("dev-lang/python")
226 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
227 python_minimal_version="${python_minimal_version:-${python_versions[0]}}"
228 python_maximal_version="${python_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
229 _append_accepted_versions_range
232 if [[ "${python3}" == "1" ]]; then
233 if [[ -z "${python3_minimal_version}" && -z "${python3_maximal_version}" ]]; then
234 _PYTHON_ATOMS+=("=dev-lang/python-3*")
236 python_versions=("${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
237 python_minimal_version="${python3_minimal_version:-${python_versions[0]}}"
238 python_maximal_version="${python3_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
239 _append_accepted_versions_range
242 if [[ "${python2}" == "1" ]]; then
243 if [[ -z "${python2_minimal_version}" && -z "${python2_maximal_version}" ]]; then
244 _PYTHON_ATOMS+=("=dev-lang/python-2*")
246 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}")
247 python_minimal_version="${python2_minimal_version:-${python_versions[0]}}"
248 python_maximal_version="${python2_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
249 _append_accepted_versions_range
254 unset -f _append_accepted_versions_range
256 if [[ "${#_PYTHON_ATOMS[@]}" -gt 1 ]]; then
257 DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${_PYTHON_ATOMS[@]} )${USE_flag:+ )}"
258 RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${_PYTHON_ATOMS[@]} )${USE_flag:+ )}"
260 DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }${_PYTHON_ATOMS[@]}${USE_flag:+ )}"
261 RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }${_PYTHON_ATOMS[@]}${USE_flag:+ )}"
264 die "Invalid syntax of PYTHON_DEPEND"
268 if _python_implementation; then
269 DEPEND=">=app-eselect/eselect-python-20091230"
271 PDEPEND="app-admin/python-updater"
274 if [[ -n "${PYTHON_DEPEND}" ]]; then
275 _python_parse_PYTHON_DEPEND
277 _PYTHON_ATOMS=("dev-lang/python")
279 unset -f _python_parse_PYTHON_DEPEND
281 if [[ -n "${NEED_PYTHON}" ]]; then
282 eerror "Use PYTHON_DEPEND variable instead of NEED_PYTHON variable."
283 die "NEED_PYTHON variable is banned"
286 # @ECLASS-VARIABLE: PYTHON_USE_WITH
288 # Set this to a space separated list of USE flags the Python slot in use must be built with.
290 # @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
292 # Set this to a space separated list of USE flags of which one must be turned on for the slot in use.
294 # @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
296 # Set this to a name of a USE flag if you need to make either PYTHON_USE_WITH or
297 # PYTHON_USE_WITH_OR atoms conditional under a USE flag.
299 if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
300 _PYTHON_USE_WITH_ATOMS_ARRAY=()
301 if [[ -n "${PYTHON_USE_WITH}" ]]; then
302 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
303 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${PYTHON_USE_WITH// /,}]")
305 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
306 for _USE_flag in ${PYTHON_USE_WITH_OR}; do
307 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
308 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${_USE_flag}]")
313 if [[ "${#_PYTHON_USE_WITH_ATOMS_ARRAY[@]}" -gt 1 ]]; then
314 _PYTHON_USE_WITH_ATOMS="|| ( ${_PYTHON_USE_WITH_ATOMS_ARRAY[@]} )"
316 _PYTHON_USE_WITH_ATOMS="${_PYTHON_USE_WITH_ATOMS_ARRAY[@]}"
318 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
319 _PYTHON_USE_WITH_ATOMS="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOMS} )"
321 DEPEND+="${DEPEND:+ }${_PYTHON_USE_WITH_ATOMS}"
322 RDEPEND+="${RDEPEND:+ }${_PYTHON_USE_WITH_ATOMS}"
323 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOMS _PYTHON_USE_WITH_ATOMS_ARRAY
328 # ================================================================================================
329 # =================================== MISCELLANEOUS FUNCTIONS ====================================
330 # ================================================================================================
332 _python_abi-specific_local_scope() {
333 [[ " ${FUNCNAME[@]:2} " =~ " "(_python_final_sanity_checks|python_execute_function|python_mod_optimize|python_mod_cleanup)" " ]]
336 _python_initialize_prefix_variables() {
337 if has "${EAPI:-0}" 0 1 2; then
338 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
339 EROOT="${ROOT%/}${EPREFIX}/"
341 if [[ -n "${D}" && -z "${ED}" ]]; then
342 ED="${D%/}${EPREFIX}/"
347 unset PYTHON_SANITY_CHECKS_EXECUTED PYTHON_SKIP_SANITY_CHECKS
349 _python_initial_sanity_checks() {
353 _python_final_sanity_checks() {
354 if ! _python_implementation && [[ "$(declare -p PYTHON_SANITY_CHECKS_EXECUTED 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS_EXECUTED="* || " ${FUNCNAME[@]:1} " =~ " "(python_set_active_version|python_pkg_setup)" " && -z "${PYTHON_SKIP_SANITY_CHECKS}" ]]; then
355 local PYTHON_ABI="${PYTHON_ABI}"
356 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI}}; do
357 # Ensure that appropriate version of Python is installed.
358 if ! has_version "$(python_get_implementational_package)"; then
359 die "$(python_get_implementational_package) is not installed"
362 # Ensure that EPYTHON variable is respected.
363 if [[ "$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" != "${PYTHON_ABI}" ]]; then
364 eerror "Path to 'python': '$(type -p python)'"
365 eerror "ABI: '${ABI}'"
366 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
367 eerror "EPYTHON: '$(PYTHON)'"
368 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
369 eerror "Locally active version of Python: '$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")'"
370 die "'python' does not respect EPYTHON variable"
374 PYTHON_SANITY_CHECKS_EXECUTED="1"
377 # @ECLASS-VARIABLE: PYTHON_COLORS
379 # User-configurable colored output.
380 PYTHON_COLORS="${PYTHON_COLORS:-0}"
382 _python_set_color_variables() {
383 if [[ "${PYTHON_COLORS}" != "0" && "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
400 _python_check_python_pkg_setup_execution() {
401 [[ " ${FUNCNAME[@]:1} " =~ " "(python_set_active_version|python_pkg_setup)" " ]] && return
403 if ! has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_PKG_SETUP_EXECUTED}" ]]; then
404 die "python_pkg_setup() not called"
408 # @FUNCTION: python_pkg_setup
410 # Perform sanity checks and initialize environment.
412 # This function is exported in EAPI 2 and 3 when PYTHON_USE_WITH or PYTHON_USE_WITH_OR variable
413 # is set and always in EAPI >=4. Calling of this function is mandatory in EAPI >=4.
415 if [[ "${EBUILD_PHASE}" != "setup" ]]; then
416 die "${FUNCNAME}() can be used only in pkg_setup() phase"
419 if [[ "$#" -ne 0 ]]; then
420 die "${FUNCNAME}() does not accept arguments"
423 export JYTHON_SYSTEM_CACHEDIR="1"
424 addwrite "${EPREFIX}/var/cache/jython"
426 if _python_package_supporting_installation_for_multiple_python_abis; then
427 _python_calculate_PYTHON_ABIS
428 export EPYTHON="$(PYTHON -f)"
430 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
433 if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; then
434 if [[ "${PYTHON_USE_WITH_OPT}" ]]; then
435 if [[ "${PYTHON_USE_WITH_OPT}" == !* ]]; then
436 use ${PYTHON_USE_WITH_OPT#!} && return
438 use !${PYTHON_USE_WITH_OPT} && return
442 python_pkg_setup_check_USE_flags() {
443 local python_atom USE_flag
444 python_atom="$(python_get_implementational_package)"
446 for USE_flag in ${PYTHON_USE_WITH}; do
447 if ! has_version "${python_atom}[${USE_flag}]"; then
448 eerror "Please rebuild ${python_atom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
449 die "Please rebuild ${python_atom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
453 for USE_flag in ${PYTHON_USE_WITH_OR}; do
454 if has_version "${python_atom}[${USE_flag}]"; then
459 if [[ ${PYTHON_USE_WITH_OR} ]]; then
460 eerror "Please rebuild ${python_atom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
461 die "Please rebuild ${python_atom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
465 if _python_package_supporting_installation_for_multiple_python_abis; then
466 PYTHON_SKIP_SANITY_CHECKS="1" python_execute_function -q python_pkg_setup_check_USE_flags
468 python_pkg_setup_check_USE_flags
471 unset -f python_pkg_setup_check_USE_flags
474 PYTHON_PKG_SETUP_EXECUTED="1"
477 _PYTHON_SHEBANG_BASE_PART_REGEX='^#![[:space:]]*([^[:space:]]*/usr/bin/env[[:space:]]+)?([^[:space:]]*/)?(jython|pypy-c|python)'
479 # @FUNCTION: python_convert_shebangs
480 # @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_ABI|Python_version> <file|directory> [files|directories]
482 # Convert shebangs in specified files. Directories can be specified only with --recursive option.
483 python_convert_shebangs() {
484 _python_check_python_pkg_setup_execution
486 local argument file files=() only_executables="0" python_interpreter quiet="0" recursive="0" shebangs_converted="0"
496 -x|--only-executables)
504 die "${FUNCNAME}(): Unrecognized option '$1'"
513 if [[ "$#" -eq 0 ]]; then
514 die "${FUNCNAME}(): Missing Python version and files or directories"
515 elif [[ "$#" -eq 1 ]]; then
516 die "${FUNCNAME}(): Missing files or directories"
519 if [[ -n "$(_python_get_implementation --ignore-invalid "$1")" ]]; then
520 python_interpreter="$(PYTHON "$1")"
522 python_interpreter="python$1"
526 for argument in "$@"; do
527 if [[ ! -e "${argument}" ]]; then
528 die "${FUNCNAME}(): '${argument}' does not exist"
529 elif [[ -f "${argument}" ]]; then
530 files+=("${argument}")
531 elif [[ -d "${argument}" ]]; then
532 if [[ "${recursive}" == "1" ]]; then
533 while read -d $'\0' -r file; do
535 done < <(find "${argument}" $([[ "${only_executables}" == "1" ]] && echo -perm /111) -type f -print0)
537 die "${FUNCNAME}(): '${argument}' is not a regular file"
540 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
544 for file in "${files[@]}"; do
546 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
548 if [[ "$(head -n1 "${file}")" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX} ]]; then
549 [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue
551 shebangs_converted="1"
553 if [[ "${quiet}" == "0" ]]; then
554 einfo "Converting shebang in '${file}'"
557 sed -e "1s:^#![[:space:]]*\([^[:space:]]*/usr/bin/env[[:space:]]\)\?[[:space:]]*\([^[:space:]]*/\)\?\(jython\|pypy-c\|python\)\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?\(\$\|[[:space:]].*\):#!\1\2${python_interpreter}\6:" -i "${file}" || die "Conversion of shebang in '${file}' failed"
561 if [[ "${shebangs_converted}" == "0" ]]; then
562 ewarn "${FUNCNAME}(): Python scripts not found"
566 # @FUNCTION: python_clean_py-compile_files
567 # @USAGE: [-q|--quiet]
569 # Clean py-compile files to disable byte-compilation.
570 python_clean_py-compile_files() {
571 _python_check_python_pkg_setup_execution
573 local file files=() quiet="0"
581 die "${FUNCNAME}(): Unrecognized option '$1'"
584 die "${FUNCNAME}(): Invalid usage"
590 while read -d $'\0' -r file; do
591 files+=("${file#./}")
592 done < <(find -name py-compile -type f -print0)
594 for file in "${files[@]}"; do
595 if [[ "${quiet}" == "0" ]]; then
596 einfo "Cleaning '${file}' file"
598 echo "#!/bin/sh" > "${file}"
602 # @FUNCTION: python_clean_installation_image
603 # @USAGE: [-q|--quiet]
605 # Delete needless files in installation image.
607 # This function can be used only in src_install() phase.
608 python_clean_installation_image() {
609 if [[ "${EBUILD_PHASE}" != "install" ]]; then
610 die "${FUNCNAME}() can be used only in src_install() phase"
613 _python_check_python_pkg_setup_execution
614 _python_initialize_prefix_variables
616 local file files=() quiet="0"
624 die "${FUNCNAME}(): Unrecognized option '$1'"
627 die "${FUNCNAME}(): Invalid usage"
633 while read -d $'\0' -r file; do
635 done < <(find "${ED}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -type f -print0)
637 if [[ "${#files[@]}" -gt 0 ]]; then
638 if [[ "${quiet}" == "0" ]]; then
639 ewarn "Deleting byte-compiled Python modules needlessly generated by build system:"
641 for file in "${files[@]}"; do
642 if [[ "${quiet}" == "0" ]]; then
647 # Delete empty __pycache__ directories.
648 if [[ "${file%/*}" == *"/__pycache__" ]]; then
649 rmdir "${file%/*}" 2> /dev/null
654 python_clean_sitedirs() {
655 if [[ -d "${ED}$(python_get_sitedir)" ]]; then
656 find "${ED}$(python_get_sitedir)" "(" -name "*.c" -o -name "*.h" -o -name "*.la" ")" -type f -print0 | xargs -0 rm -f
659 if _python_package_supporting_installation_for_multiple_python_abis; then
660 python_execute_function -q python_clean_sitedirs
662 python_clean_sitedirs
665 unset -f python_clean_sitedirs
668 # ================================================================================================
669 # =========== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ============
670 # ================================================================================================
672 # @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
674 # Set this in EAPI <= 4 to indicate that current package supports installation for
675 # multiple Python ABIs.
677 # @ECLASS-VARIABLE: PYTHON_TESTS_RESTRICTED_ABIS
679 # Space-separated list of Python ABI patterns. Testing in Python ABIs matching any Python ABI
680 # patterns specified in this list is skipped.
682 # @ECLASS-VARIABLE: PYTHON_EXPORT_PHASE_FUNCTIONS
684 # Set this to export phase functions for the following ebuild phases:
685 # src_prepare(), src_configure(), src_compile(), src_test(), src_install().
686 if ! has "${EAPI:-0}" 0 1; then
687 python_src_prepare() {
688 if [[ "${EBUILD_PHASE}" != "prepare" ]]; then
689 die "${FUNCNAME}() can be used only in src_prepare() phase"
692 if ! _python_package_supporting_installation_for_multiple_python_abis; then
693 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
696 _python_check_python_pkg_setup_execution
698 if [[ "$#" -ne 0 ]]; then
699 die "${FUNCNAME}() does not accept arguments"
705 for python_default_function in src_configure src_compile src_test; do
706 eval "python_${python_default_function}() {
707 if [[ \"\${EBUILD_PHASE}\" != \"${python_default_function#src_}\" ]]; then
708 die \"\${FUNCNAME}() can be used only in ${python_default_function}() phase\"
711 if ! _python_package_supporting_installation_for_multiple_python_abis; then
712 die \"\${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs\"
715 _python_check_python_pkg_setup_execution
717 python_execute_function -d -s -- \"\$@\"
720 unset python_default_function
722 python_src_install() {
723 if [[ "${EBUILD_PHASE}" != "install" ]]; then
724 die "${FUNCNAME}() can be used only in src_install() phase"
727 if ! _python_package_supporting_installation_for_multiple_python_abis; then
728 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
731 _python_check_python_pkg_setup_execution
733 if has "${EAPI:-0}" 0 1 2 3; then
734 python_execute_function -d -s -- "$@"
736 python_installation() {
737 emake DESTDIR="${T}/images/${PYTHON_ABI}" install "$@"
739 python_execute_function -s python_installation "$@"
740 unset python_installation
742 python_merge_intermediate_installation_images "${T}/images"
746 if [[ -n "${PYTHON_EXPORT_PHASE_FUNCTIONS}" ]]; then
747 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
753 _python_calculate_PYTHON_ABIS() {
754 if ! _python_package_supporting_installation_for_multiple_python_abis; then
755 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
758 _python_initial_sanity_checks
760 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]]; then
763 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
764 local cpython_enabled="0"
766 if [[ -z "${USE_PYTHON}" ]]; then
767 die "USE_PYTHON variable is empty"
770 for PYTHON_ABI in ${USE_PYTHON}; do
771 if ! has "${PYTHON_ABI}" "${_PYTHON_GLOBALLY_SUPPORTED_ABIS[@]}"; then
772 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
775 if has "${PYTHON_ABI}" "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; then
779 if ! _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${RESTRICT_PYTHON_ABIS}"; then
780 export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
784 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
785 die "USE_PYTHON variable does not enable any Python ABI supported by ${CATEGORY}/${PF}"
788 if [[ "${cpython_enabled}" == "0" ]]; then
789 die "USE_PYTHON variable does not enable any CPython ABI"
792 local python_version python2_version python3_version support_python_major_version
794 if ! has_version "dev-lang/python"; then
795 die "${FUNCNAME}(): 'dev-lang/python' is not installed"
798 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
800 if has_version "=dev-lang/python-2*"; then
801 python2_version="$("${EPREFIX}/usr/bin/python2" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
803 support_python_major_version="0"
804 for PYTHON_ABI in "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}"; do
805 if ! _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${RESTRICT_PYTHON_ABIS}"; then
806 support_python_major_version="1"
810 if [[ "${support_python_major_version}" == "1" ]]; then
811 if _python_check_python_abi_matching --patterns-list "${python2_version}" "${RESTRICT_PYTHON_ABIS}"; then
812 die "Active version of CPython 2 is not supported by ${CATEGORY}/${PF}"
819 if has_version "=dev-lang/python-3*"; then
820 python3_version="$("${EPREFIX}/usr/bin/python3" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
822 support_python_major_version="0"
823 for PYTHON_ABI in "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; do
824 if ! _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${RESTRICT_PYTHON_ABIS}"; then
825 support_python_major_version="1"
829 if [[ "${support_python_major_version}" == "1" ]]; then
830 if _python_check_python_abi_matching --patterns-list "${python3_version}" "${RESTRICT_PYTHON_ABIS}"; then
831 die "Active version of CPython 3 is not supported by ${CATEGORY}/${PF}"
838 if [[ -z "${python2_version}" && -z "${python3_version}" ]]; then
839 eerror "${CATEGORY}/${PF} requires at least one of the following packages:"
840 for PYTHON_ABI in "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; do
841 if ! _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${RESTRICT_PYTHON_ABIS}"; then
842 eerror " dev-lang/python:${PYTHON_ABI}"
845 die "No supported version of CPython installed"
848 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
849 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python2' symlink"
850 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
851 die "Incorrect configuration of Python"
853 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
854 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python3' symlink"
855 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
856 die "Incorrect configuration of Python"
859 PYTHON_ABIS="${python2_version} ${python3_version}"
860 PYTHON_ABIS="${PYTHON_ABIS# }"
861 export PYTHON_ABIS="${PYTHON_ABIS% }"
865 _python_final_sanity_checks
868 _python_prepare_flags() {
869 local array=() deleted_flag element flags new_value old_flag old_value operator pattern prefix variable
871 for variable in CPPFLAGS CFLAGS CXXFLAGS LDFLAGS; do
872 eval "_PYTHON_SAVED_${variable}=\"\${!variable}\""
873 for prefix in PYTHON_USER_ PYTHON_; do
874 if [[ "$(declare -p ${prefix}${variable} 2> /dev/null)" == "declare -a ${prefix}${variable}="* ]]; then
875 eval "array=(\"\${${prefix}${variable}[@]}\")"
876 for element in "${array[@]}"; do
877 if [[ "${element}" =~ ^${_PYTHON_ABI_PATTERN_REGEX}\ (\+|-)\ .+ ]]; then
878 pattern="${element%% *}"
879 element="${element#* }"
880 operator="${element%% *}"
881 flags="${element#* }"
882 if _python_check_python_abi_matching "${PYTHON_ABI}" "${pattern}"; then
883 if [[ "${operator}" == "+" ]]; then
884 eval "export ${variable}+=\"\${variable:+ }${flags}\""
885 elif [[ "${operator}" == "-" ]]; then
886 flags="${flags// /$'\n'}"
887 old_value="${!variable// /$'\n'}"
889 while read old_flag; do
890 while read deleted_flag; do
891 if [[ "${old_flag}" == ${deleted_flag} ]]; then
895 new_value+="${new_value:+ }${old_flag}"
896 done <<< "${old_value}"
897 eval "export ${variable}=\"\${new_value}\""
901 die "Element '${element}' of ${prefix}${variable} array has invalid syntax"
904 elif [[ -n "$(declare -p ${prefix}${variable} 2> /dev/null)" ]]; then
905 die "${prefix}${variable} should be indexed array"
911 _python_restore_flags() {
914 for variable in CPPFLAGS CFLAGS CXXFLAGS LDFLAGS; do
915 eval "${variable}=\"\${_PYTHON_SAVED_${variable}}\""
916 unset _PYTHON_SAVED_${variable}
920 # @FUNCTION: python_execute_function
921 # @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]
923 # Execute specified function for each value of PYTHON_ABIS, optionally passing additional
924 # arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
925 python_execute_function() {
926 if ! _python_package_supporting_installation_for_multiple_python_abis; then
927 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
930 _python_check_python_pkg_setup_execution
931 _python_set_color_variables
933 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
938 action_message_template="$2"
941 -d|--default-function)
945 failure_message_template="$2"
957 -s|--separate-build-dirs)
958 separate_build_dirs="1"
969 die "${FUNCNAME}(): Unrecognized option '$1'"
978 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
979 die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
982 if [[ "${default_function}" == "0" ]]; then
983 if [[ "$#" -eq 0 ]]; then
984 die "${FUNCNAME}(): Missing function name"
989 if [[ -z "$(type -t "${function}")" ]]; then
990 die "${FUNCNAME}(): '${function}' function is not defined"
993 if has "${EAPI:-0}" 0 1; then
994 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
997 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
998 if has "${EAPI}" 2 3; then
999 python_default_function() {
1003 python_default_function() {
1007 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
1008 python_default_function() {
1011 elif [[ "${EBUILD_PHASE}" == "test" ]]; then
1012 python_default_function() {
1013 # Stolen from portage's _eapi0_src_test()
1014 local emake_cmd="${MAKE:-make} ${MAKEOPTS} ${EXTRA_EMAKE}"
1015 if ${emake_cmd} -j1 -n check &> /dev/null; then
1016 ${emake_cmd} -j1 check "$@"
1017 elif ${emake_cmd} -j1 -n test &> /dev/null; then
1018 ${emake_cmd} -j1 test "$@"
1021 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
1022 python_default_function() {
1023 emake DESTDIR="${D}" install "$@"
1026 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
1028 function="python_default_function"
1031 # Ensure that python_execute_function() cannot be directly or indirectly called by python_execute_function().
1032 if _python_abi-specific_local_scope; then
1033 die "${FUNCNAME}(): Invalid call stack"
1036 if [[ "${quiet}" == "0" ]]; then
1037 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
1038 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
1039 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
1040 [[ "${EBUILD_PHASE}" == "configure" ]] && action="Configuration"
1041 [[ "${EBUILD_PHASE}" == "compile" ]] && action="Building"
1042 [[ "${EBUILD_PHASE}" == "test" ]] && action="Testing"
1043 [[ "${EBUILD_PHASE}" == "install" ]] && action="Installation"
1044 [[ "${EBUILD_PHASE}" == "preinst" ]] && action="Preinstallation"
1045 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
1046 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
1047 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
1050 _python_calculate_PYTHON_ABIS
1051 if [[ "${final_ABI}" == "1" ]]; then
1052 iterated_PYTHON_ABIS="$(PYTHON -f --ABI)"
1054 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
1056 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
1057 if [[ "${EBUILD_PHASE}" == "test" ]] && _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${PYTHON_TESTS_RESTRICTED_ABIS}"; then
1058 if [[ "${quiet}" == "0" ]]; then
1059 echo " ${_GREEN}*${_NORMAL} ${_BLUE}Testing of ${CATEGORY}/${PF} with $(python_get_implementation_and_version) skipped${_NORMAL}"
1064 _python_prepare_flags
1066 if [[ "${quiet}" == "0" ]]; then
1067 if [[ -n "${action_message_template}" ]]; then
1068 eval "action_message=\"${action_message_template}\""
1070 action_message="${action} of ${CATEGORY}/${PF} with $(python_get_implementation_and_version)..."
1072 echo " ${_GREEN}*${_NORMAL} ${_BLUE}${action_message}${_NORMAL}"
1075 if [[ "${separate_build_dirs}" == "1" ]]; then
1076 if [[ -n "${source_dir}" ]]; then
1077 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
1079 export BUILDDIR="${S}-${PYTHON_ABI}"
1081 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
1083 export BUILDDIR="${S}"
1086 previous_directory="$(pwd)"
1087 previous_directory_stack="$(dirs -p)"
1088 previous_directory_stack_length="$(dirs -p | wc -l)"
1090 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
1091 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
1093 EPYTHON="$(PYTHON)" "${function}" "$@"
1098 _python_restore_flags
1100 if [[ "${return_code}" -ne 0 ]]; then
1101 if [[ -n "${failure_message_template}" ]]; then
1102 eval "failure_message=\"${failure_message_template}\""
1104 failure_message="${action} failed with $(python_get_implementation_and_version) in ${function}() function"
1107 if [[ "${nonfatal}" == "1" ]]; then
1108 if [[ "${quiet}" == "0" ]]; then
1109 ewarn "${failure_message}"
1111 elif [[ "${final_ABI}" == "0" ]] && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
1112 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
1113 local enabled_PYTHON_ABIS= other_PYTHON_ABI
1114 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
1115 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
1117 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
1119 if [[ "${quiet}" == "0" ]]; then
1120 ewarn "${failure_message}"
1122 if [[ -z "${PYTHON_ABIS}" ]]; then
1123 die "${function}() function failed with all enabled Python ABIs"
1126 die "${failure_message}"
1130 # Ensure that directory stack has not been decreased.
1131 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
1132 die "Directory stack decreased illegally"
1135 # Avoid side effects of earlier returning from the specified function.
1136 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
1137 popd > /dev/null || die "popd failed"
1140 # Ensure that the bottom part of directory stack has not been changed. Restore
1141 # previous directory (from before running of the specified function) before
1142 # comparison of directory stacks to avoid mismatch of directory stacks after
1143 # potential using of 'cd' to change current directory. Restoration of previous
1144 # directory allows to safely use 'cd' to change current directory in the
1145 # specified function without changing it back to original directory.
1146 cd "${previous_directory}"
1147 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
1148 die "Directory stack changed illegally"
1151 if [[ "${separate_build_dirs}" == "1" ]]; then
1152 popd > /dev/null || die "popd failed"
1157 if [[ "${default_function}" == "1" ]]; then
1158 unset -f python_default_function
1162 # @FUNCTION: python_copy_sources
1163 # @USAGE: <directory="${S}"> [directory]
1165 # Copy unpacked sources of current package to separate build directory for each Python ABI.
1166 python_copy_sources() {
1167 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1168 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1171 _python_check_python_pkg_setup_execution
1173 local dir dirs=() PYTHON_ABI
1175 if [[ "$#" -eq 0 ]]; then
1176 if [[ "${WORKDIR}" == "${S}" ]]; then
1177 die "${FUNCNAME}() cannot be used with current value of S variable"
1184 _python_calculate_PYTHON_ABIS
1185 for PYTHON_ABI in ${PYTHON_ABIS}; do
1186 for dir in "${dirs[@]}"; do
1187 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
1192 # @FUNCTION: python_generate_wrapper_scripts
1193 # @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
1195 # Generate wrapper scripts. Existing files are overwritten only with --force option.
1196 # If --respect-EPYTHON option is specified, then generated wrapper scripts will
1197 # respect EPYTHON variable at run time.
1199 # This function can be used only in src_install() phase.
1200 python_generate_wrapper_scripts() {
1201 if [[ "${EBUILD_PHASE}" != "install" ]]; then
1202 die "${FUNCNAME}() can be used only in src_install() phase"
1205 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1206 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1209 _python_check_python_pkg_setup_execution
1210 _python_initialize_prefix_variables
1212 local eselect_python_option file force="0" quiet="0" PYTHON_ABI PYTHON_ABIS_list python2_enabled="0" python3_enabled="0" respect_EPYTHON="0"
1216 -E|--respect-EPYTHON)
1230 die "${FUNCNAME}(): Unrecognized option '$1'"
1239 if [[ "$#" -eq 0 ]]; then
1240 die "${FUNCNAME}(): Missing arguments"
1243 _python_calculate_PYTHON_ABIS
1244 for PYTHON_ABI in "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}"; do
1245 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
1249 for PYTHON_ABI in "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; do
1250 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
1255 if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
1256 eselect_python_option=
1257 elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
1258 eselect_python_option="--python2"
1259 elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
1260 eselect_python_option="--python3"
1262 die "${FUNCNAME}(): Unsupported environment"
1265 PYTHON_ABIS_list="$("$(PYTHON -f)" -c "print(', '.join('\"%s\"' % x for x in reversed('${PYTHON_ABIS}'.split())))")"
1267 for file in "$@"; do
1268 if [[ -f "${file}" && "${force}" == "0" ]]; then
1269 die "${FUNCNAME}(): '${file}' already exists"
1272 if [[ "${quiet}" == "0" ]]; then
1273 einfo "Generating '${file#${ED%/}}' wrapper script"
1276 cat << EOF > "${file}"
1277 #!/usr/bin/env python
1278 # Gentoo '${file##*/}' wrapper script generated by python_generate_wrapper_scripts()
1285 cpython_ABI_re = re.compile(r"^(\d+\.\d+)$")
1286 jython_ABI_re = re.compile(r"^(\d+\.\d+)-jython$")
1287 pypy_ABI_re = re.compile(r"^\d+\.\d+-pypy-(\d+\.\d+)$")
1288 cpython_interpreter_re = re.compile(r"^python(\d+\.\d+)$")
1289 jython_interpreter_re = re.compile(r"^jython(\d+\.\d+)$")
1290 pypy_interpreter_re = re.compile(r"^pypy-c(\d+\.\d+)$")
1291 cpython_shebang_re = re.compile(r"^#![ \t]*(?:${EPREFIX}/usr/bin/python|(?:${EPREFIX})?/usr/bin/env[ \t]+(?:${EPREFIX}/usr/bin/)?python)")
1292 python_shebang_options_re = re.compile(r"^#![ \t]*${EPREFIX}/usr/bin/(?:jython|pypy-c|python)(?:\d+(?:\.\d+)?)?[ \t]+(-\S)")
1293 python_verification_output_re = re.compile("^GENTOO_PYTHON_TARGET_SCRIPT_PATH supported\n$")
1295 #pypy_versions_mapping = {
1304 def get_PYTHON_ABI(python_interpreter):
1305 cpython_matched = cpython_interpreter_re.match(python_interpreter)
1306 jython_matched = jython_interpreter_re.match(python_interpreter)
1307 pypy_matched = pypy_interpreter_re.match(python_interpreter)
1308 if cpython_matched is not None:
1309 PYTHON_ABI = cpython_matched.group(1)
1310 elif jython_matched is not None:
1311 PYTHON_ABI = jython_matched.group(1) + "-jython"
1312 elif pypy_matched is not None:
1313 #PYTHON_ABI = pypy_versions_mapping[pypy_matched.group(1)] + "-pypy-" + pypy_matched.group(1)
1314 PYTHON_ABI = "2.7-pypy-" + pypy_matched.group(1)
1319 def get_python_interpreter(PYTHON_ABI):
1320 cpython_matched = cpython_ABI_re.match(PYTHON_ABI)
1321 jython_matched = jython_ABI_re.match(PYTHON_ABI)
1322 pypy_matched = pypy_ABI_re.match(PYTHON_ABI)
1323 if cpython_matched is not None:
1324 python_interpreter = "python" + cpython_matched.group(1)
1325 elif jython_matched is not None:
1326 python_interpreter = "jython" + jython_matched.group(1)
1327 elif pypy_matched is not None:
1328 python_interpreter = "pypy-c" + pypy_matched.group(1)
1330 python_interpreter = None
1331 return python_interpreter
1334 if [[ "$?" != "0" ]]; then
1335 die "${FUNCNAME}(): Generation of '$1' failed"
1337 if [[ "${respect_EPYTHON}" == "1" ]]; then
1338 cat << EOF >> "${file}"
1339 python_interpreter = os.environ.get("EPYTHON")
1340 if python_interpreter:
1341 PYTHON_ABI = get_PYTHON_ABI(python_interpreter)
1342 if PYTHON_ABI is None:
1343 sys.stderr.write("%s: EPYTHON variable has unrecognized value '%s'\n" % (sys.argv[0], python_interpreter))
1347 environment = os.environ.copy()
1348 environment["ROOT"] = "/"
1349 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], env=environment, stdout=subprocess.PIPE)
1350 if eselect_process.wait() != 0:
1352 except (OSError, ValueError):
1353 sys.stderr.write("%s: Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n" % sys.argv[0])
1356 python_interpreter = eselect_process.stdout.read()
1357 if not isinstance(python_interpreter, str):
1359 python_interpreter = python_interpreter.decode()
1360 python_interpreter = python_interpreter.rstrip("\n")
1362 PYTHON_ABI = get_PYTHON_ABI(python_interpreter)
1363 if PYTHON_ABI is None:
1364 sys.stderr.write("%s: 'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % (sys.argv[0], python_interpreter))
1367 wrapper_script_path = os.path.realpath(sys.argv[0])
1368 target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
1369 if not os.path.exists(target_executable_path):
1370 sys.stderr.write("%s: '%s' does not exist\n" % (sys.argv[0], target_executable_path))
1373 if [[ "$?" != "0" ]]; then
1374 die "${FUNCNAME}(): Generation of '$1' failed"
1377 cat << EOF >> "${file}"
1379 environment = os.environ.copy()
1380 environment["ROOT"] = "/"
1381 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], env=environment, stdout=subprocess.PIPE)
1382 if eselect_process.wait() != 0:
1384 except (OSError, ValueError):
1385 sys.stderr.write("%s: Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n" % sys.argv[0])
1388 python_interpreter = eselect_process.stdout.read()
1389 if not isinstance(python_interpreter, str):
1391 python_interpreter = python_interpreter.decode()
1392 python_interpreter = python_interpreter.rstrip("\n")
1394 PYTHON_ABI = get_PYTHON_ABI(python_interpreter)
1395 if PYTHON_ABI is None:
1396 sys.stderr.write("%s: 'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % (sys.argv[0], python_interpreter))
1399 wrapper_script_path = os.path.realpath(sys.argv[0])
1400 for PYTHON_ABI in [PYTHON_ABI, ${PYTHON_ABIS_list}]:
1401 target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
1402 if os.path.exists(target_executable_path):
1405 sys.stderr.write("%s: No target script exists for '%s'\n" % (sys.argv[0], wrapper_script_path))
1408 python_interpreter = get_python_interpreter(PYTHON_ABI)
1409 if python_interpreter is None:
1410 sys.stderr.write("%s: Unrecognized Python ABI '%s'\n" % (sys.argv[0], PYTHON_ABI))
1413 if [[ "$?" != "0" ]]; then
1414 die "${FUNCNAME}(): Generation of '$1' failed"
1417 cat << EOF >> "${file}"
1419 target_executable = open(target_executable_path, "rb")
1420 target_executable_first_line = target_executable.readline()
1421 target_executable.close()
1422 if not isinstance(target_executable_first_line, str):
1424 target_executable_first_line = target_executable_first_line.decode("utf_8", "replace")
1427 python_shebang_options_matched = python_shebang_options_re.match(target_executable_first_line)
1428 if python_shebang_options_matched is not None:
1429 options = [python_shebang_options_matched.group(1)]
1431 cpython_shebang_matched = cpython_shebang_re.match(target_executable_first_line)
1433 if cpython_shebang_matched is not None:
1435 python_interpreter_path = "${EPREFIX}/usr/bin/%s" % python_interpreter
1436 os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"] = "1"
1437 python_verification_process = subprocess.Popen([python_interpreter_path, "-c", "pass"], stdout=subprocess.PIPE)
1438 del os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"]
1439 if python_verification_process.wait() != 0:
1442 python_verification_output = python_verification_process.stdout.read()
1443 if not isinstance(python_verification_output, str):
1445 python_verification_output = python_verification_output.decode()
1447 if not python_verification_output_re.match(python_verification_output):
1450 if cpython_interpreter_re.match(python_interpreter) is not None:
1451 os.environ["GENTOO_PYTHON_PROCESS_NAME"] = os.path.basename(sys.argv[0])
1452 os.environ["GENTOO_PYTHON_WRAPPER_SCRIPT_PATH"] = sys.argv[0]
1453 os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH"] = target_executable_path
1455 if hasattr(os, "execv"):
1456 os.execv(python_interpreter_path, [python_interpreter_path] + options + sys.argv)
1458 sys.exit(subprocess.Popen([python_interpreter_path] + options + sys.argv).wait())
1459 except (KeyboardInterrupt, SystemExit):
1463 for variable in ("GENTOO_PYTHON_PROCESS_NAME", "GENTOO_PYTHON_WRAPPER_SCRIPT_PATH", "GENTOO_PYTHON_TARGET_SCRIPT_PATH", "GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"):
1464 if variable in os.environ:
1465 del os.environ[variable]
1467 if hasattr(os, "execv"):
1468 os.execv(target_executable_path, sys.argv)
1470 sys.exit(subprocess.Popen([target_executable_path] + sys.argv[1:]).wait())
1472 if [[ "$?" != "0" ]]; then
1473 die "${FUNCNAME}(): Generation of '$1' failed"
1475 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed"
1479 # @ECLASS-VARIABLE: PYTHON_VERSIONED_SCRIPTS
1481 # Array of regular expressions of paths to versioned Python scripts.
1482 # Python scripts in /usr/bin and /usr/sbin are versioned by default.
1484 # @ECLASS-VARIABLE: PYTHON_VERSIONED_EXECUTABLES
1486 # Array of regular expressions of paths to versioned executables (including Python scripts).
1488 # @ECLASS-VARIABLE: PYTHON_NONVERSIONED_EXECUTABLES
1490 # Array of regular expressions of paths to nonversioned executables (including Python scripts).
1492 # @FUNCTION: python_merge_intermediate_installation_images
1493 # @USAGE: [-q|--quiet] [--] <intermediate_installation_images_directory>
1495 # Merge intermediate installation images into installation image.
1497 # This function can be used only in src_install() phase.
1498 python_merge_intermediate_installation_images() {
1499 if [[ "${EBUILD_PHASE}" != "install" ]]; then
1500 die "${FUNCNAME}() can be used only in src_install() phase"
1503 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1504 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1507 _python_check_python_pkg_setup_execution
1508 _python_initialize_prefix_variables
1510 local absolute_file b file files=() intermediate_installation_images_directory PYTHON_ABI quiet="0" regex shebang version_executable wrapper_scripts=() wrapper_scripts_set=()
1522 die "${FUNCNAME}(): Unrecognized option '$1'"
1531 if [[ "$#" -ne 1 ]]; then
1532 die "${FUNCNAME}() requires 1 argument"
1535 intermediate_installation_images_directory="$1"
1537 if [[ ! -d "${intermediate_installation_images_directory}" ]]; then
1538 die "${FUNCNAME}(): Intermediate installation images directory '${intermediate_installation_images_directory}' does not exist"
1541 _python_calculate_PYTHON_ABIS
1542 if [[ "$(PYTHON -f --ABI)" == 3.* ]]; then
1546 while read -d $'\0' -r file; do
1548 done < <("$(PYTHON -f)" -c \
1552 if hasattr(sys.stdout, 'buffer'):
1554 stdout = sys.stdout.buffer
1561 os.chdir(${b}'${intermediate_installation_images_directory}')
1563 for PYTHON_ABI in ${b}'${PYTHON_ABIS}'.split():
1564 for root, dirs, files in os.walk(PYTHON_ABI + ${b}'${EPREFIX}'):
1565 root = root[len(PYTHON_ABI + ${b}'${EPREFIX}')+1:]
1566 files_set.update(root + ${b}'/' + file for file in files)
1568 for file in sorted(files_set):
1570 stdout.write(${b}'\x00')" || die "${FUNCNAME}(): Failure of extraction of files in intermediate installation images")
1572 for PYTHON_ABI in ${PYTHON_ABIS}; do
1573 if [[ ! -d "${intermediate_installation_images_directory}/${PYTHON_ABI}" ]]; then
1574 die "${FUNCNAME}(): Intermediate installation image for Python ABI '${PYTHON_ABI}' does not exist"
1577 pushd "${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}" > /dev/null || die "pushd failed"
1579 for file in "${files[@]}"; do
1580 version_executable="0"
1581 for regex in "/usr/bin/.*" "/usr/sbin/.*" "${PYTHON_VERSIONED_SCRIPTS[@]}"; do
1582 if [[ "/${file}" =~ ^${regex}$ ]]; then
1583 version_executable="1"
1587 for regex in "${PYTHON_VERSIONED_EXECUTABLES[@]}"; do
1588 if [[ "/${file}" =~ ^${regex}$ ]]; then
1589 version_executable="2"
1593 if [[ "${version_executable}" != "0" ]]; then
1594 for regex in "${PYTHON_NONVERSIONED_EXECUTABLES[@]}"; do
1595 if [[ "/${file}" =~ ^${regex}$ ]]; then
1596 version_executable="0"
1602 [[ "${version_executable}" == "0" ]] && continue
1604 if [[ -L "${file}" ]]; then
1605 absolute_file="$(readlink "${file}")"
1606 if [[ "${absolute_file}" == /* ]]; then
1607 absolute_file="${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}/${absolute_file##/}"
1609 if [[ "${file}" == */* ]]; then
1610 absolute_file="${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}/${file%/*}/${absolute_file}"
1612 absolute_file="${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}/${absolute_file}"
1616 absolute_file="${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}/${file}"
1619 [[ ! -x "${absolute_file}" ]] && continue
1621 shebang="$(head -n1 "${absolute_file}")" || die "Extraction of shebang from '${absolute_file}' failed"
1623 if [[ "${version_executable}" == "2" ]]; then
1624 wrapper_scripts+=("${ED}${file}")
1625 elif [[ "${version_executable}" == "1" ]]; then
1626 if [[ "${shebang}" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX}([[:digit:]]+(\.[[:digit:]]+)?)?($|[[:space:]]+) ]]; then
1627 wrapper_scripts+=("${ED}${file}")
1629 version_executable="0"
1633 [[ "${version_executable}" == "0" ]] && continue
1635 if [[ -e "${file}-${PYTHON_ABI}" ]]; then
1636 die "${FUNCNAME}(): '${EPREFIX}/${file}-${PYTHON_ABI}' already exists"
1639 mv "${file}" "${file}-${PYTHON_ABI}" || die "Renaming of '${file}' failed"
1641 if [[ "${shebang}" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX}[[:digit:]]*($|[[:space:]]+) ]]; then
1642 if [[ -L "${file}-${PYTHON_ABI}" ]]; then
1643 python_convert_shebangs $([[ "${quiet}" == "1" ]] && echo --quiet) "${PYTHON_ABI}" "${absolute_file}"
1645 python_convert_shebangs $([[ "${quiet}" == "1" ]] && echo --quiet) "${PYTHON_ABI}" "${file}-${PYTHON_ABI}"
1650 popd > /dev/null || die "popd failed"
1652 # This is per bug #390691, without the duplication refactor, and with
1653 # the 3-way structure per comment #6. This enable users with old
1654 # coreutils to upgrade a lot easier (you need to upgrade python+portage
1655 # before coreutils can be upgraded).
1656 if ROOT="/" has_version '>=sys-apps/coreutils-6.9.90'; then
1657 cp -fr --preserve=all --no-preserve=context "${intermediate_installation_images_directory}/${PYTHON_ABI}/"* "${D}" || die "Merging of intermediate installation image for Python ABI '${PYTHON_ABI} into installation image failed"
1658 elif ROOT="/" has_version sys-apps/coreutils; then
1659 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"
1661 cp -fpr "${intermediate_installation_images_directory}/${PYTHON_ABI}/"* "${D}" || die "Merging of intermediate installation image for Python ABI '${PYTHON_ABI} into installation image failed"
1665 rm -fr "${intermediate_installation_images_directory}"
1667 if [[ "${#wrapper_scripts[@]}" -ge 1 ]]; then
1668 rm -f "${T}/python_wrapper_scripts"
1670 for file in "${wrapper_scripts[@]}"; do
1671 echo -n "${file}" >> "${T}/python_wrapper_scripts"
1672 echo -en "\x00" >> "${T}/python_wrapper_scripts"
1675 while read -d $'\0' -r file; do
1676 wrapper_scripts_set+=("${file}")
1677 done < <("$(PYTHON -f)" -c \
1680 if hasattr(sys.stdout, 'buffer'):
1682 stdout = sys.stdout.buffer
1687 python_wrapper_scripts_file = open('${T}/python_wrapper_scripts', 'rb')
1688 files = set(python_wrapper_scripts_file.read().rstrip(${b}'\x00').split(${b}'\x00'))
1689 python_wrapper_scripts_file.close()
1691 for file in sorted(files):
1693 stdout.write(${b}'\x00')" || die "${FUNCNAME}(): Failure of extraction of set of wrapper scripts")
1695 python_generate_wrapper_scripts $([[ "${quiet}" == "1" ]] && echo --quiet) "${wrapper_scripts_set[@]}"
1699 # ================================================================================================
1700 # ========= FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ==========
1701 # ================================================================================================
1703 unset EPYTHON PYTHON_ABI
1705 # @FUNCTION: python_set_active_version
1706 # @USAGE: <Python_ABI|2|3>
1708 # Set locally active version of Python.
1709 # If Python_ABI argument is specified, then version of Python corresponding to Python_ABI is used.
1710 # If 2 argument is specified, then active version of CPython 2 is used.
1711 # If 3 argument is specified, then active version of CPython 3 is used.
1713 # This function can be used only in pkg_setup() phase.
1714 python_set_active_version() {
1715 if [[ "${EBUILD_PHASE}" != "setup" ]]; then
1716 die "${FUNCNAME}() can be used only in pkg_setup() phase"
1719 if _python_package_supporting_installation_for_multiple_python_abis; then
1720 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
1723 if [[ "$#" -ne 1 ]]; then
1724 die "${FUNCNAME}() requires 1 argument"
1727 _python_initial_sanity_checks
1729 if [[ -z "${PYTHON_ABI}" ]]; then
1730 if [[ -n "$(_python_get_implementation --ignore-invalid "$1")" ]]; then
1731 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
1732 # so it does not need to be exported to subprocesses.
1734 if ! _python_implementation && ! has_version "$(python_get_implementational_package)"; then
1735 die "${FUNCNAME}(): '$(python_get_implementational_package)' is not installed"
1737 export EPYTHON="$(PYTHON "$1")"
1738 elif [[ "$1" == "2" ]]; then
1739 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
1740 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
1742 export EPYTHON="$(PYTHON -2)"
1743 PYTHON_ABI="${EPYTHON#python}"
1744 PYTHON_ABI="${PYTHON_ABI%%-*}"
1745 elif [[ "$1" == "3" ]]; then
1746 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
1747 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
1749 export EPYTHON="$(PYTHON -3)"
1750 PYTHON_ABI="${EPYTHON#python}"
1751 PYTHON_ABI="${PYTHON_ABI%%-*}"
1753 die "${FUNCNAME}(): Unrecognized argument '$1'"
1757 _python_final_sanity_checks
1759 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
1760 PYTHON_REQUESTED_ACTIVE_VERSION="$1"
1763 # @FUNCTION: python_need_rebuild
1765 # Mark current package for rebuilding by python-updater after
1766 # switching of active version of Python.
1767 python_need_rebuild() {
1768 if _python_package_supporting_installation_for_multiple_python_abis; then
1769 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
1772 _python_check_python_pkg_setup_execution
1774 if [[ "$#" -ne 0 ]]; then
1775 die "${FUNCNAME}() does not accept arguments"
1778 export PYTHON_NEED_REBUILD="$(PYTHON --ABI)"
1781 # ================================================================================================
1782 # ======================================= GETTER FUNCTIONS =======================================
1783 # ================================================================================================
1785 _PYTHON_ABI_EXTRACTION_COMMAND=\
1788 sys.stdout.write(".".join(str(x) for x in sys.version_info[:2]))
1789 if platform.system()[:4] == "Java":
1790 sys.stdout.write("-jython")
1791 elif hasattr(platform, "python_implementation") and platform.python_implementation() == "PyPy":
1792 sys.stdout.write("-pypy-" + ".".join(str(x) for x in sys.pypy_version_info[:2]))'
1794 _python_get_implementation() {
1795 local ignore_invalid="0"
1807 die "${FUNCNAME}(): Unrecognized option '$1'"
1816 if [[ "$#" -ne 1 ]]; then
1817 die "${FUNCNAME}() requires 1 argument"
1820 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
1822 elif [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+-jython$ ]]; then
1824 elif [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+-pypy-[[:digit:]]+\.[[:digit:]]+$ ]]; then
1827 if [[ "${ignore_invalid}" == "0" ]]; then
1828 die "${FUNCNAME}(): Unrecognized Python ABI '$1'"
1834 # @USAGE: [-2] [-3] [--ABI] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
1836 # Print filename of Python interpreter for specified Python ABI. If Python_ABI argument
1837 # is ommitted, then PYTHON_ABI environment variable must be set and is used.
1838 # If -2 option is specified, then active version of CPython 2 is used.
1839 # If -3 option is specified, then active version of CPython 3 is used.
1840 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1841 # -2, -3 and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
1842 # If --ABI option is specified, then only specified Python ABI is printed instead of
1843 # filename of Python interpreter.
1844 # If --absolute-path option is specified, then absolute path to Python interpreter is printed.
1845 # --ABI and --absolute-path options cannot be specified simultaneously.
1847 _python_check_python_pkg_setup_execution
1849 local ABI_output="0" absolute_path_output="0" final_ABI="0" PYTHON_ABI="${PYTHON_ABI}" python_interpreter python2="0" python3="0"
1863 absolute_path_output="1"
1873 die "${FUNCNAME}(): Unrecognized option '$1'"
1882 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
1883 die "${FUNCNAME}(): '--ABI' and '--absolute-path' options cannot be specified simultaneously"
1886 if [[ "$((${python2} + ${python3} + ${final_ABI}))" -gt 1 ]]; then
1887 die "${FUNCNAME}(): '-2', '-3' or '--final-ABI' options cannot be specified simultaneously"
1890 if [[ "$#" -eq 0 ]]; then
1891 if [[ "${final_ABI}" == "1" ]]; then
1892 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1893 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1895 _python_calculate_PYTHON_ABIS
1896 PYTHON_ABI="${PYTHON_ABIS##* }"
1897 elif [[ "${python2}" == "1" ]]; then
1898 PYTHON_ABI="$(ROOT="/" eselect python show --python2 --ABI)"
1899 if [[ -z "${PYTHON_ABI}" ]]; then
1900 die "${FUNCNAME}(): Active version of CPython 2 not set"
1901 elif [[ "${PYTHON_ABI}" != "2."* ]]; then
1902 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1904 elif [[ "${python3}" == "1" ]]; then
1905 PYTHON_ABI="$(ROOT="/" eselect python show --python3 --ABI)"
1906 if [[ -z "${PYTHON_ABI}" ]]; then
1907 die "${FUNCNAME}(): Active version of CPython 3 not set"
1908 elif [[ "${PYTHON_ABI}" != "3."* ]]; then
1909 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1911 elif _python_package_supporting_installation_for_multiple_python_abis; then
1912 if ! _python_abi-specific_local_scope; then
1913 die "${FUNCNAME}() should be used in ABI-specific local scope"
1916 PYTHON_ABI="$("${EPREFIX}/usr/bin/python" -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")"
1917 if [[ -z "${PYTHON_ABI}" ]]; then
1918 die "${FUNCNAME}(): Failure of extraction of locally active version of Python"
1921 elif [[ "$#" -eq 1 ]]; then
1922 if [[ "${final_ABI}" == "1" ]]; then
1923 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
1925 if [[ "${python2}" == "1" ]]; then
1926 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
1928 if [[ "${python3}" == "1" ]]; then
1929 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
1933 die "${FUNCNAME}(): Invalid usage"
1936 if [[ "${ABI_output}" == "1" ]]; then
1937 echo -n "${PYTHON_ABI}"
1940 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1941 python_interpreter="python${PYTHON_ABI}"
1942 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1943 python_interpreter="jython${PYTHON_ABI%-jython}"
1944 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
1945 python_interpreter="pypy-c${PYTHON_ABI#*-pypy-}"
1948 if [[ "${absolute_path_output}" == "1" ]]; then
1949 echo -n "${EPREFIX}/usr/bin/${python_interpreter}"
1951 echo -n "${python_interpreter}"
1955 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
1960 # @FUNCTION: python_get_implementation
1961 # @USAGE: [-f|--final-ABI]
1963 # Print name of Python implementation.
1964 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1965 python_get_implementation() {
1966 _python_check_python_pkg_setup_execution
1968 local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
1976 die "${FUNCNAME}(): Unrecognized option '$1'"
1979 die "${FUNCNAME}(): Invalid usage"
1985 if [[ "${final_ABI}" == "1" ]]; then
1986 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1987 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1989 PYTHON_ABI="$(PYTHON -f --ABI)"
1991 if _python_package_supporting_installation_for_multiple_python_abis; then
1992 if ! _python_abi-specific_local_scope; then
1993 die "${FUNCNAME}() should be used in ABI-specific local scope"
1996 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2000 echo "$(_python_get_implementation "${PYTHON_ABI}")"
2003 # @FUNCTION: python_get_implementational_package
2004 # @USAGE: [-f|--final-ABI]
2006 # Print category, name and slot of package providing Python implementation.
2007 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2008 python_get_implementational_package() {
2009 _python_check_python_pkg_setup_execution
2011 local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
2019 die "${FUNCNAME}(): Unrecognized option '$1'"
2022 die "${FUNCNAME}(): Invalid usage"
2028 if [[ "${final_ABI}" == "1" ]]; then
2029 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2030 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2032 PYTHON_ABI="$(PYTHON -f --ABI)"
2034 if _python_package_supporting_installation_for_multiple_python_abis; then
2035 if ! _python_abi-specific_local_scope; then
2036 die "${FUNCNAME}() should be used in ABI-specific local scope"
2039 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2043 if [[ "${EAPI:-0}" == "0" ]]; then
2044 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2045 echo "=dev-lang/python-${PYTHON_ABI}*"
2046 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2047 echo "=dev-java/jython-${PYTHON_ABI%-jython}*"
2048 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2049 echo "=virtual/pypy-${PYTHON_ABI#*-pypy-}*"
2052 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2053 echo "dev-lang/python:${PYTHON_ABI}"
2054 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2055 echo "dev-java/jython:${PYTHON_ABI%-jython}"
2056 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2057 echo "virtual/pypy:${PYTHON_ABI#*-pypy-}"
2062 # @FUNCTION: python_get_includedir
2063 # @USAGE: [-b|--base-path] [-f|--final-ABI]
2065 # Print path to Python include directory.
2066 # If --base-path option is specified, then path not prefixed with "/" is printed.
2067 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2068 python_get_includedir() {
2069 _python_check_python_pkg_setup_execution
2071 local base_path="0" final_ABI="0" prefix PYTHON_ABI="${PYTHON_ABI}"
2082 die "${FUNCNAME}(): Unrecognized option '$1'"
2085 die "${FUNCNAME}(): Invalid usage"
2091 if [[ "${base_path}" == "0" ]]; then
2095 if [[ "${final_ABI}" == "1" ]]; then
2096 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2097 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2099 PYTHON_ABI="$(PYTHON -f --ABI)"
2101 if _python_package_supporting_installation_for_multiple_python_abis; then
2102 if ! _python_abi-specific_local_scope; then
2103 die "${FUNCNAME}() should be used in ABI-specific local scope"
2106 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2110 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2111 echo "${prefix}usr/include/python${PYTHON_ABI}"
2112 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2113 echo "${prefix}usr/share/jython-${PYTHON_ABI%-jython}/Include"
2114 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2115 echo "${prefix}usr/$(get_libdir)/pypy${PYTHON_ABI#*-pypy-}/include"
2119 # @FUNCTION: python_get_libdir
2120 # @USAGE: [-b|--base-path] [-f|--final-ABI]
2122 # Print path to Python standard library directory.
2123 # If --base-path option is specified, then path not prefixed with "/" is printed.
2124 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2125 python_get_libdir() {
2126 _python_check_python_pkg_setup_execution
2128 local base_path="0" final_ABI="0" prefix PYTHON_ABI="${PYTHON_ABI}"
2139 die "${FUNCNAME}(): Unrecognized option '$1'"
2142 die "${FUNCNAME}(): Invalid usage"
2148 if [[ "${base_path}" == "0" ]]; then
2152 if [[ "${final_ABI}" == "1" ]]; then
2153 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2154 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2156 PYTHON_ABI="$(PYTHON -f --ABI)"
2158 if _python_package_supporting_installation_for_multiple_python_abis; then
2159 if ! _python_abi-specific_local_scope; then
2160 die "${FUNCNAME}() should be used in ABI-specific local scope"
2163 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2167 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2168 echo "${prefix}usr/$(get_libdir)/python${PYTHON_ABI}"
2169 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2170 echo "${prefix}usr/share/jython-${PYTHON_ABI%-jython}/Lib"
2171 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2172 die "${FUNCNAME}(): PyPy has multiple standard library directories"
2176 # @FUNCTION: python_get_sitedir
2177 # @USAGE: [-b|--base-path] [-f|--final-ABI]
2179 # Print path to Python site-packages directory.
2180 # If --base-path option is specified, then path not prefixed with "/" is printed.
2181 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2182 python_get_sitedir() {
2183 _python_check_python_pkg_setup_execution
2185 local base_path="0" final_ABI="0" prefix PYTHON_ABI="${PYTHON_ABI}"
2196 die "${FUNCNAME}(): Unrecognized option '$1'"
2199 die "${FUNCNAME}(): Invalid usage"
2205 if [[ "${base_path}" == "0" ]]; then
2209 if [[ "${final_ABI}" == "1" ]]; then
2210 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2211 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2213 PYTHON_ABI="$(PYTHON -f --ABI)"
2215 if _python_package_supporting_installation_for_multiple_python_abis; then
2216 if ! _python_abi-specific_local_scope; then
2217 die "${FUNCNAME}() should be used in ABI-specific local scope"
2220 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2224 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2225 echo "${prefix}usr/$(get_libdir)/python${PYTHON_ABI}/site-packages"
2226 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2227 echo "${prefix}usr/share/jython-${PYTHON_ABI%-jython}/Lib/site-packages"
2228 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2229 echo "${prefix}usr/$(get_libdir)/pypy${PYTHON_ABI#*-pypy-}/site-packages"
2233 # @FUNCTION: python_get_library
2234 # @USAGE: [-b|--base-path] [-f|--final-ABI] [-l|--linker-option]
2236 # Print path to Python library.
2237 # If --base-path option is specified, then path not prefixed with "/" is printed.
2238 # If --linker-option is specified, then "-l${library}" linker option is printed.
2239 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2240 python_get_library() {
2241 _python_check_python_pkg_setup_execution
2243 local base_path="0" final_ABI="0" linker_option="0" prefix PYTHON_ABI="${PYTHON_ABI}"
2257 die "${FUNCNAME}(): Unrecognized option '$1'"
2260 die "${FUNCNAME}(): Invalid usage"
2266 if [[ "${base_path}" == "0" ]]; then
2270 if [[ "${base_path}" == "1" && "${linker_option}" == "1" ]]; then
2271 die "${FUNCNAME}(): '--base-path' and '--linker-option' options cannot be specified simultaneously"
2274 if [[ "${final_ABI}" == "1" ]]; then
2275 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2276 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2278 PYTHON_ABI="$(PYTHON -f --ABI)"
2280 if _python_package_supporting_installation_for_multiple_python_abis; then
2281 if ! _python_abi-specific_local_scope; then
2282 die "${FUNCNAME}() should be used in ABI-specific local scope"
2285 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2289 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2290 if [[ "${linker_option}" == "1" ]]; then
2291 echo "-lpython${PYTHON_ABI}"
2293 echo "${prefix}usr/$(get_libdir)/libpython${PYTHON_ABI}$(get_libname)"
2295 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2296 die "${FUNCNAME}(): Jython does not have shared library"
2297 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2298 die "${FUNCNAME}(): PyPy does not have shared library"
2302 # @FUNCTION: python_get_version
2303 # @USAGE: [-f|--final-ABI] [-l|--language] [--full] [--major] [--minor] [--micro]
2305 # Print version of Python implementation.
2306 # --full, --major, --minor and --micro options cannot be specified simultaneously.
2307 # If --full, --major, --minor and --micro options are not specified, then "${major_version}.${minor_version}" is printed.
2308 # If --language option is specified, then version of Python language is printed.
2309 # --language and --full options cannot be specified simultaneously.
2310 # --language and --micro options cannot be specified simultaneously.
2311 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2312 python_get_version() {
2313 _python_check_python_pkg_setup_execution
2315 local final_ABI="0" language="0" language_version full="0" major="0" minor="0" micro="0" PYTHON_ABI="${PYTHON_ABI}" python_command
2338 die "${FUNCNAME}(): Unrecognized option '$1'"
2341 die "${FUNCNAME}(): Invalid usage"
2347 if [[ "${final_ABI}" == "1" ]]; then
2348 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2349 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2352 if _python_package_supporting_installation_for_multiple_python_abis && ! _python_abi-specific_local_scope; then
2353 die "${FUNCNAME}() should be used in ABI-specific local scope"
2357 if [[ "$((${full} + ${major} + ${minor} + ${micro}))" -gt 1 ]]; then
2358 die "${FUNCNAME}(): '--full', '--major', '--minor' or '--micro' options cannot be specified simultaneously"
2361 if [[ "${language}" == "1" ]]; then
2362 if [[ "${final_ABI}" == "1" ]]; then
2363 PYTHON_ABI="$(PYTHON -f --ABI)"
2364 elif [[ -z "${PYTHON_ABI}" ]]; then
2365 PYTHON_ABI="$(PYTHON --ABI)"
2367 language_version="${PYTHON_ABI%%-*}"
2368 if [[ "${full}" == "1" ]]; then
2369 die "${FUNCNAME}(): '--language' and '--full' options cannot be specified simultaneously"
2370 elif [[ "${major}" == "1" ]]; then
2371 echo "${language_version%.*}"
2372 elif [[ "${minor}" == "1" ]]; then
2373 echo "${language_version#*.}"
2374 elif [[ "${micro}" == "1" ]]; then
2375 die "${FUNCNAME}(): '--language' and '--micro' options cannot be specified simultaneously"
2377 echo "${language_version}"
2380 if [[ "${full}" == "1" ]]; then
2381 python_command="import sys; print('.'.join(str(x) for x in getattr(sys, 'pypy_version_info', sys.version_info)[:3]))"
2382 elif [[ "${major}" == "1" ]]; then
2383 python_command="import sys; print(getattr(sys, 'pypy_version_info', sys.version_info)[0])"
2384 elif [[ "${minor}" == "1" ]]; then
2385 python_command="import sys; print(getattr(sys, 'pypy_version_info', sys.version_info)[1])"
2386 elif [[ "${micro}" == "1" ]]; then
2387 python_command="import sys; print(getattr(sys, 'pypy_version_info', sys.version_info)[2])"
2389 if [[ -n "${PYTHON_ABI}" && "${final_ABI}" == "0" ]]; then
2390 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2391 echo "${PYTHON_ABI}"
2392 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2393 echo "${PYTHON_ABI%-jython}"
2394 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2395 echo "${PYTHON_ABI#*-pypy-}"
2399 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:2]))"
2402 if [[ "${final_ABI}" == "1" ]]; then
2403 "$(PYTHON -f)" -c "${python_command}"
2405 "$(PYTHON ${PYTHON_ABI})" -c "${python_command}"
2410 # @FUNCTION: python_get_implementation_and_version
2411 # @USAGE: [-f|--final-ABI]
2413 # Print name and version of Python implementation.
2414 # If version of Python implementation is not bound to version of Python language, then
2415 # version of Python language is additionally printed.
2416 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2417 python_get_implementation_and_version() {
2418 _python_check_python_pkg_setup_execution
2420 local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
2428 die "${FUNCNAME}(): Unrecognized option '$1'"
2431 die "${FUNCNAME}(): Invalid usage"
2437 if [[ "${final_ABI}" == "1" ]]; then
2438 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2439 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2441 PYTHON_ABI="$(PYTHON -f --ABI)"
2443 if _python_package_supporting_installation_for_multiple_python_abis; then
2444 if ! _python_abi-specific_local_scope; then
2445 die "${FUNCNAME}() should be used in ABI-specific local scope"
2448 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2452 if [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+-[[:alnum:]]+-[[:digit:]]+\.[[:digit:]]+$ ]]; then
2453 echo "$(_python_get_implementation "${PYTHON_ABI}") ${PYTHON_ABI##*-} (Python ${PYTHON_ABI%%-*})"
2455 echo "$(_python_get_implementation "${PYTHON_ABI}") ${PYTHON_ABI%%-*}"
2459 # ================================================================================================
2460 # ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
2461 # ================================================================================================
2463 # @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
2465 # User-configurable verbosity of tests of Python modules.
2466 # Supported values: 0, 1, 2, 3, 4.
2467 PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
2469 _python_test_hook() {
2470 if [[ "$#" -ne 1 ]]; then
2471 die "${FUNCNAME}() requires 1 argument"
2474 if _python_package_supporting_installation_for_multiple_python_abis && [[ "$(type -t "${_PYTHON_TEST_FUNCTION}_$1_hook")" == "function" ]]; then
2475 "${_PYTHON_TEST_FUNCTION}_$1_hook"
2479 # @FUNCTION: python_execute_nosetests
2480 # @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
2482 # Execute nosetests for all enabled Python ABIs.
2483 # In ebuilds of packages supporting installation for multiple Python ABIs, this function calls
2484 # python_execute_nosetests_pre_hook() and python_execute_nosetests_post_hook(), if they are defined.
2485 python_execute_nosetests() {
2486 _python_check_python_pkg_setup_execution
2487 _python_set_color_variables
2489 local PYTHONPATH_template separate_build_dirs
2494 PYTHONPATH_template="$2"
2497 -s|--separate-build-dirs)
2498 separate_build_dirs="1"
2505 die "${FUNCNAME}(): Unrecognized option '$1'"
2514 python_test_function() {
2515 local evaluated_PYTHONPATH
2517 eval "evaluated_PYTHONPATH=\"${PYTHONPATH_template}\""
2519 _PYTHON_TEST_FUNCTION="python_execute_nosetests" _python_test_hook pre
2521 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
2522 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
2523 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
2525 echo ${_BOLD}nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
2526 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
2529 _PYTHON_TEST_FUNCTION="python_execute_nosetests" _python_test_hook post
2531 if _python_package_supporting_installation_for_multiple_python_abis; then
2532 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
2534 if [[ -n "${separate_build_dirs}" ]]; then
2535 die "${FUNCNAME}(): Invalid usage"
2537 python_test_function "$@" || die "Testing failed"
2540 unset -f python_test_function
2543 # @FUNCTION: python_execute_py.test
2544 # @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
2546 # Execute py.test for all enabled Python ABIs.
2547 # In ebuilds of packages supporting installation for multiple Python ABIs, this function calls
2548 # python_execute_py.test_pre_hook() and python_execute_py.test_post_hook(), if they are defined.
2549 python_execute_py.test() {
2550 _python_check_python_pkg_setup_execution
2551 _python_set_color_variables
2553 local PYTHONPATH_template separate_build_dirs
2558 PYTHONPATH_template="$2"
2561 -s|--separate-build-dirs)
2562 separate_build_dirs="1"
2569 die "${FUNCNAME}(): Unrecognized option '$1'"
2578 python_test_function() {
2579 local evaluated_PYTHONPATH
2581 eval "evaluated_PYTHONPATH=\"${PYTHONPATH_template}\""
2583 _PYTHON_TEST_FUNCTION="python_execute_py.test" _python_test_hook pre
2585 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
2586 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"${_NORMAL}
2587 PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@" || return "$?"
2589 echo ${_BOLD}py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"${_NORMAL}
2590 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" || return "$?"
2593 _PYTHON_TEST_FUNCTION="python_execute_py.test" _python_test_hook post
2595 if _python_package_supporting_installation_for_multiple_python_abis; then
2596 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
2598 if [[ -n "${separate_build_dirs}" ]]; then
2599 die "${FUNCNAME}(): Invalid usage"
2601 python_test_function "$@" || die "Testing failed"
2604 unset -f python_test_function
2607 # @FUNCTION: python_execute_trial
2608 # @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
2610 # Execute trial for all enabled Python ABIs.
2611 # In ebuilds of packages supporting installation for multiple Python ABIs, this function
2612 # calls python_execute_trial_pre_hook() and python_execute_trial_post_hook(), if they are defined.
2613 python_execute_trial() {
2614 _python_check_python_pkg_setup_execution
2615 _python_set_color_variables
2617 local PYTHONPATH_template separate_build_dirs
2622 PYTHONPATH_template="$2"
2625 -s|--separate-build-dirs)
2626 separate_build_dirs="1"
2633 die "${FUNCNAME}(): Unrecognized option '$1'"
2642 python_test_function() {
2643 local evaluated_PYTHONPATH
2645 eval "evaluated_PYTHONPATH=\"${PYTHONPATH_template}\""
2647 _PYTHON_TEST_FUNCTION="python_execute_trial" _python_test_hook pre
2649 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
2650 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
2651 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
2653 echo ${_BOLD}trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
2654 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
2657 _PYTHON_TEST_FUNCTION="python_execute_trial" _python_test_hook post
2659 if _python_package_supporting_installation_for_multiple_python_abis; then
2660 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
2662 if [[ -n "${separate_build_dirs}" ]]; then
2663 die "${FUNCNAME}(): Invalid usage"
2665 python_test_function "$@" || die "Testing failed"
2668 unset -f python_test_function
2671 # ================================================================================================
2672 # ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
2673 # ================================================================================================
2675 # @FUNCTION: python_enable_pyc
2677 # Tell Python to automatically recompile modules to .pyc/.pyo if the
2678 # timestamps/version stamps have changed.
2679 python_enable_pyc() {
2680 _python_check_python_pkg_setup_execution
2682 if [[ "$#" -ne 0 ]]; then
2683 die "${FUNCNAME}() does not accept arguments"
2686 unset PYTHONDONTWRITEBYTECODE
2689 # @FUNCTION: python_disable_pyc
2691 # Tell Python not to automatically recompile modules to .pyc/.pyo
2692 # even if the timestamps/version stamps do not match. This is done
2693 # to protect sandbox.
2694 python_disable_pyc() {
2695 _python_check_python_pkg_setup_execution
2697 if [[ "$#" -ne 0 ]]; then
2698 die "${FUNCNAME}() does not accept arguments"
2701 export PYTHONDONTWRITEBYTECODE="1"
2705 [[ -z ${PORTAGE_VERBOSE} ]] || echo "$@"
2708 _python_clean_compiled_modules() {
2709 _python_initialize_prefix_variables
2710 _python_set_color_variables
2712 [[ "${FUNCNAME[1]}" =~ ^(python_mod_optimize|python_mod_cleanup)$ ]] || die "${FUNCNAME}(): Invalid usage"
2714 local base_module_name compiled_file compiled_files=() dir path py_file root
2716 # Strip trailing slash from EROOT.
2719 for path in "$@"; do
2721 if [[ -d "${path}" ]]; then
2722 while read -d $'\0' -r compiled_file; do
2723 compiled_files+=("${compiled_file}")
2724 done < <(find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0)
2726 if [[ "${EBUILD_PHASE}" == "postrm" ]]; then
2727 # Delete empty child directories.
2728 find "${path}" -type d | sort -r | while read -r dir; do
2729 if rmdir "${dir}" 2> /dev/null; then
2730 _python_vecho "<<< ${dir}"
2734 elif [[ "${path}" == *.py ]]; then
2735 base_module_name="${path##*/}"
2736 base_module_name="${base_module_name%.py}"
2737 if [[ -d "${path%/*}/__pycache__" ]]; then
2738 while read -d $'\0' -r compiled_file; do
2739 compiled_files+=("${compiled_file}")
2740 done < <(find "${path%/*}/__pycache__" "(" -name "${base_module_name}.*.py[co]" -o -name "${base_module_name}\$py.class" ")" -print0)
2742 compiled_files+=("${path}c" "${path}o" "${path%.py}\$py.class")
2745 for compiled_file in "${compiled_files[@]}"; do
2746 [[ ! -f "${compiled_file}" ]] && continue
2747 dir="${compiled_file%/*}"
2749 if [[ "${compiled_file}" == *.py[co] ]]; then
2750 if [[ "${dir}" == "__pycache__" ]]; then
2751 base_module_name="${compiled_file##*/}"
2752 base_module_name="${base_module_name%.*py[co]}"
2753 base_module_name="${base_module_name%.*}"
2754 py_file="${compiled_file%__pycache__/*}${base_module_name}.py"
2756 py_file="${compiled_file%[co]}"
2758 if [[ "${EBUILD_PHASE}" == "postinst" ]]; then
2759 [[ -f "${py_file}" && "${compiled_file}" -nt "${py_file}" ]] && continue
2761 [[ -f "${py_file}" ]] && continue
2763 _python_vecho "<<< ${compiled_file%[co]}[co]"
2764 rm -f "${compiled_file%[co]}"[co]
2765 elif [[ "${compiled_file}" == *\$py.class ]]; then
2766 if [[ "${dir}" == "__pycache__" ]]; then
2767 base_module_name="${compiled_file##*/}"
2768 base_module_name="${base_module_name%\$py.class}"
2769 py_file="${compiled_file%__pycache__/*}${base_module_name}.py"
2771 py_file="${compiled_file%\$py.class}.py"
2773 if [[ "${EBUILD_PHASE}" == "postinst" ]]; then
2774 [[ -f "${py_file}" && "${compiled_file}" -nt "${py_file}" ]] && continue
2776 [[ -f "${py_file}" ]] && continue
2778 _python_vecho "<<< ${compiled_file}"
2779 rm -f "${compiled_file}"
2781 die "${FUNCNAME}(): Unrecognized file type: '${compiled_file}'"
2784 # Delete empty parent directories.
2785 dir="${compiled_file%/*}"
2786 while [[ "${dir}" != "${root}" ]]; do
2787 if rmdir "${dir}" 2> /dev/null; then
2788 _python_vecho "<<< ${dir}"
2798 # @FUNCTION: python_mod_optimize
2799 # @USAGE: [--allow-evaluated-non-sitedir-paths] [-d directory] [-f] [-l] [-q] [-x regular_expression] [--] <file|directory> [files|directories]
2801 # Byte-compile specified Python modules.
2802 # -d, -f, -l, -q and -x options passed to this function are passed to compileall.py.
2804 # This function can be used only in pkg_postinst() phase.
2805 python_mod_optimize() {
2806 if [[ "${EBUILD_PHASE}" != "postinst" ]]; then
2807 die "${FUNCNAME}() can be used only in pkg_postinst() phase"
2810 _python_check_python_pkg_setup_execution
2811 _python_initialize_prefix_variables
2813 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis || _python_implementation || [[ "${CATEGORY}/${PN}" == "sys-apps/portage" ]]; then
2814 # PYTHON_ABI variable cannot be local in packages not supporting installation for multiple Python ABIs.
2815 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=() stderr stderr_line
2817 if _python_package_supporting_installation_for_multiple_python_abis; then
2818 if has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_ABIS}" ]]; then
2819 die "${FUNCNAME}(): python_pkg_setup() or python_execute_function() not called"
2821 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
2823 if has "${EAPI:-0}" 0 1 2 3; then
2824 iterated_PYTHON_ABIS="${PYTHON_ABI:=$(PYTHON --ABI)}"
2826 iterated_PYTHON_ABIS="${PYTHON_ABI}"
2830 # Strip trailing slash from EROOT.
2835 --allow-evaluated-non-sitedir-paths)
2836 allow_evaluated_non_sitedir_paths="1"
2842 options+=("$1" "$2")
2850 die "${FUNCNAME}(): Unrecognized option '$1'"
2859 if [[ "${allow_evaluated_non_sitedir_paths}" == "1" ]] && ! _python_package_supporting_installation_for_multiple_python_abis; then
2860 die "${FUNCNAME}(): '--allow-evaluated-non-sitedir-paths' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2863 if [[ "$#" -eq 0 ]]; then
2864 die "${FUNCNAME}(): Missing files or directories"
2868 if [[ "$1" =~ ^($|(\.|\.\.|/)($|/)) ]]; then
2869 die "${FUNCNAME}(): Invalid argument '$1'"
2870 elif ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
2871 die "${FUNCNAME}(): Paths of directories / files in site-packages directories must be relative to site-packages directories"
2872 elif [[ "$1" =~ ^/ ]]; then
2873 if _python_package_supporting_installation_for_multiple_python_abis; then
2874 if [[ "${allow_evaluated_non_sitedir_paths}" != "1" ]]; then
2875 die "${FUNCNAME}(): Absolute paths cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
2877 if [[ "$1" != *\$* ]]; then
2878 die "${FUNCNAME}(): '$1' has invalid syntax"
2880 if [[ "$1" == *.py ]]; then
2881 evaluated_files+=("$1")
2883 evaluated_dirs+=("$1")
2886 if [[ -d "${root}$1" ]]; then
2887 other_dirs+=("${root}$1")
2888 elif [[ -f "${root}$1" ]]; then
2889 other_files+=("${root}$1")
2890 elif [[ -e "${root}$1" ]]; then
2891 eerror "${FUNCNAME}(): '${root}$1' is not a regular file or a directory"
2893 eerror "${FUNCNAME}(): '${root}$1' does not exist"
2897 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
2898 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
2899 site_packages_dirs+=("$1")
2901 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
2902 site_packages_files+=("$1")
2904 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
2905 eerror "${FUNCNAME}(): '$1' is not a regular file or a directory"
2907 eerror "${FUNCNAME}(): '$1' does not exist"
2914 # Set additional options.
2917 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
2918 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})) || ((${#evaluated_dirs[@]})) || ((${#evaluated_files[@]})); then
2921 ebegin "Compilation and optimization of Python modules for $(python_get_implementation_and_version)"
2922 if ((${#site_packages_dirs[@]})) || ((${#evaluated_dirs[@]})); then
2923 for dir in "${site_packages_dirs[@]}"; do
2924 dirs+=("${root}$(python_get_sitedir)/${dir}")
2926 for dir in "${evaluated_dirs[@]}"; do
2927 eval "dirs+=(\"\${root}${dir}\")"
2929 stderr+="${stderr:+$'\n'}$("$(PYTHON)" -m compileall "${options[@]}" "${dirs[@]}" 2>&1)" || return_code="1"
2930 if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then
2931 "$(PYTHON)" -O -m compileall "${options[@]}" "${dirs[@]}" &> /dev/null || return_code="1"
2933 _python_clean_compiled_modules "${dirs[@]}"
2935 if ((${#site_packages_files[@]})) || ((${#evaluated_files[@]})); then
2936 for file in "${site_packages_files[@]}"; do
2937 files+=("${root}$(python_get_sitedir)/${file}")
2939 for file in "${evaluated_files[@]}"; do
2940 eval "files+=(\"\${root}${file}\")"
2942 stderr+="${stderr:+$'\n'}$("$(PYTHON)" -m py_compile "${files[@]}" 2>&1)" || return_code="1"
2943 if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then
2944 "$(PYTHON)" -O -m py_compile "${files[@]}" &> /dev/null || return_code="1"
2946 _python_clean_compiled_modules "${files[@]}"
2948 eend "${return_code}"
2949 if [[ -n "${stderr}" ]]; then
2950 eerror "Syntax errors / warnings in Python modules for $(python_get_implementation_and_version):" &> /dev/null
2951 while read stderr_line; do
2952 eerror " ${stderr_line}"
2953 done <<< "${stderr}"
2959 if _python_package_supporting_installation_for_multiple_python_abis; then
2960 # Restore previous value of PYTHON_ABI.
2961 if [[ -n "${previous_PYTHON_ABI}" ]]; then
2962 PYTHON_ABI="${previous_PYTHON_ABI}"
2968 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
2971 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation_and_version)"
2972 if ((${#other_dirs[@]})); then
2973 stderr+="${stderr:+$'\n'}$("$(PYTHON ${PYTHON_ABI})" -m compileall "${options[@]}" "${other_dirs[@]}" 2>&1)" || return_code="1"
2974 if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then
2975 "$(PYTHON ${PYTHON_ABI})" -O -m compileall "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
2977 _python_clean_compiled_modules "${other_dirs[@]}"
2979 if ((${#other_files[@]})); then
2980 stderr+="${stderr:+$'\n'}$("$(PYTHON ${PYTHON_ABI})" -m py_compile "${other_files[@]}" 2>&1)" || return_code="1"
2981 if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then
2982 "$(PYTHON ${PYTHON_ABI})" -O -m py_compile "${other_files[@]}" &> /dev/null || return_code="1"
2984 _python_clean_compiled_modules "${other_files[@]}"
2986 eend "${return_code}"
2987 if [[ -n "${stderr}" ]]; then
2988 eerror "Syntax errors / warnings in Python modules placed outside of site-packages directories for $(python_get_implementation_and_version):" &> /dev/null
2989 while read stderr_line; do
2990 eerror " ${stderr_line}"
2991 done <<< "${stderr}"
2995 # Deprecated part of python_mod_optimize()
2997 local myroot mydirs=() myfiles=() myopts=() return_code="0"
2999 # strip trailing slash
3002 # respect EROOT and options passed to compileall.py
3017 die "${FUNCNAME}(): Unrecognized option '$1'"
3026 if [[ "$#" -eq 0 ]]; then
3027 die "${FUNCNAME}(): Missing files or directories"
3031 if [[ "$1" =~ ^($|(\.|\.\.|/)($|/)) ]]; then
3032 die "${FUNCNAME}(): Invalid argument '$1'"
3033 elif [[ -d "${myroot}/${1#/}" ]]; then
3034 mydirs+=("${myroot}/${1#/}")
3035 elif [[ -f "${myroot}/${1#/}" ]]; then
3036 myfiles+=("${myroot}/${1#/}")
3037 elif [[ -e "${myroot}/${1#/}" ]]; then
3038 eerror "${FUNCNAME}(): ${myroot}/${1#/} is not a regular file or directory"
3040 eerror "${FUNCNAME}(): ${myroot}/${1#/} does not exist"
3045 # set additional opts
3048 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
3050 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
3051 if ((${#mydirs[@]})); then
3052 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
3053 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
3054 _python_clean_compiled_modules "${mydirs[@]}"
3057 if ((${#myfiles[@]})); then
3058 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" || return_code="1"
3059 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null || return_code="1"
3060 _python_clean_compiled_modules "${myfiles[@]}"
3063 eend "${return_code}"
3067 # @FUNCTION: python_mod_cleanup
3068 # @USAGE: [--allow-evaluated-non-sitedir-paths] [--] <file|directory> [files|directories]
3070 # Delete orphaned byte-compiled Python modules corresponding to specified Python modules.
3072 # This function can be used only in pkg_postrm() phase.
3073 python_mod_cleanup() {
3074 if [[ "${EBUILD_PHASE}" != "postrm" ]]; then
3075 die "${FUNCNAME}() can be used only in pkg_postrm() phase"
3078 _python_check_python_pkg_setup_execution
3079 _python_initialize_prefix_variables
3081 local allow_evaluated_non_sitedir_paths="0" dir iterated_PYTHON_ABIS PYTHON_ABI="${PYTHON_ABI}" root search_paths=() sitedir
3083 if _python_package_supporting_installation_for_multiple_python_abis; then
3084 if has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_ABIS}" ]]; then
3085 die "${FUNCNAME}(): python_pkg_setup() or python_execute_function() not called"
3087 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
3089 if has "${EAPI:-0}" 0 1 2 3; then
3090 iterated_PYTHON_ABIS="${PYTHON_ABI:-$(PYTHON --ABI)}"
3092 iterated_PYTHON_ABIS="${PYTHON_ABI}"
3096 # Strip trailing slash from EROOT.
3101 --allow-evaluated-non-sitedir-paths)
3102 allow_evaluated_non_sitedir_paths="1"
3109 die "${FUNCNAME}(): Unrecognized option '$1'"
3118 if [[ "${allow_evaluated_non_sitedir_paths}" == "1" ]] && ! _python_package_supporting_installation_for_multiple_python_abis; then
3119 die "${FUNCNAME}(): '--allow-evaluated-non-sitedir-paths' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
3122 if [[ "$#" -eq 0 ]]; then
3123 die "${FUNCNAME}(): Missing files or directories"
3126 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis || _python_implementation || [[ "${CATEGORY}/${PN}" == "sys-apps/portage" ]]; then
3128 if [[ "$1" =~ ^($|(\.|\.\.|/)($|/)) ]]; then
3129 die "${FUNCNAME}(): Invalid argument '$1'"
3130 elif ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
3131 die "${FUNCNAME}(): Paths of directories / files in site-packages directories must be relative to site-packages directories"
3132 elif [[ "$1" =~ ^/ ]]; then
3133 if _python_package_supporting_installation_for_multiple_python_abis; then
3134 if [[ "${allow_evaluated_non_sitedir_paths}" != "1" ]]; then
3135 die "${FUNCNAME}(): Absolute paths cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
3137 if [[ "$1" != *\$* ]]; then
3138 die "${FUNCNAME}(): '$1' has invalid syntax"
3140 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
3141 eval "search_paths+=(\"\${root}$1\")"
3144 search_paths+=("${root}$1")
3147 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
3148 search_paths+=("${root}$(python_get_sitedir)/$1")
3154 # Deprecated part of python_mod_cleanup()
3156 search_paths=("${@#/}")
3157 search_paths=("${search_paths[@]/#/${root}/}")
3160 _python_clean_compiled_modules "${search_paths[@]}"
3163 # ================================================================================================
3164 # ===================================== DEPRECATED FUNCTIONS =====================================
3165 # ================================================================================================
3167 fi # _PYTHON_ECLASS_INHERITED