| 1 | # Copyright 1999-2011 Gentoo Foundation |
1 | # Copyright 1999-2012 Gentoo Foundation |
| 2 | # Distributed under the terms of the GNU General Public License v2 |
2 | # Distributed under the terms of the GNU General Public License v2 |
| 3 | # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.137 2011/10/07 10:55:51 djc Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.150 2012/02/13 23:50:12 zmedico Exp $ |
| 4 | |
4 | |
| 5 | # @ECLASS: python.eclass |
5 | # @ECLASS: python.eclass |
| 6 | # @MAINTAINER: |
6 | # @MAINTAINER: |
| 7 | # Gentoo Python Project <python@gentoo.org> |
7 | # Gentoo Python Project <python@gentoo.org> |
| 8 | # @BLURB: Eclass for Python packages |
8 | # @BLURB: Eclass for Python packages |
| 9 | # @DESCRIPTION: |
9 | # @DESCRIPTION: |
| 10 | # The python eclass contains miscellaneous, useful functions for Python packages. |
10 | # The python eclass contains miscellaneous, useful functions for Python packages. |
| 11 | |
11 | |
|
|
12 | # Must call inherit before EXPORT_FUNCTIONS to avoid QA warning. |
|
|
13 | if [[ -z "${_PYTHON_ECLASS_INHERITED}" ]]; then |
| 12 | inherit multilib |
14 | inherit multilib |
|
|
15 | fi |
| 13 | |
16 | |
|
|
17 | # Export pkg_setup every time to avoid issues with eclass inheritance order. |
|
|
18 | if ! has "${EAPI:-0}" 0 1 2 3 || { has "${EAPI:-0}" 2 3 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; }; then |
|
|
19 | EXPORT_FUNCTIONS pkg_setup |
|
|
20 | fi |
|
|
21 | |
|
|
22 | # Avoid processing this eclass more than once. |
|
|
23 | if [[ -z "${_PYTHON_ECLASS_INHERITED}" ]]; then |
|
|
24 | _PYTHON_ECLASS_INHERITED="1" |
|
|
25 | |
| 14 | if ! has "${EAPI:-0}" 0 1 2 3; then |
26 | if ! has "${EAPI:-0}" 0 1 2 3 4; then |
| 15 | die "API of python.eclass in EAPI=\"${EAPI}\" not established" |
27 | die "API of python.eclass in EAPI=\"${EAPI}\" not established" |
| 16 | fi |
28 | fi |
| 17 | |
29 | |
| 18 | _CPYTHON2_GLOBALLY_SUPPORTED_ABIS=(2.4 2.5 2.6 2.7) |
30 | _CPYTHON2_GLOBALLY_SUPPORTED_ABIS=(2.4 2.5 2.6 2.7) |
| 19 | _CPYTHON3_GLOBALLY_SUPPORTED_ABIS=(3.1 3.2) |
31 | _CPYTHON3_GLOBALLY_SUPPORTED_ABIS=(3.1 3.2) |
| 20 | _JYTHON_GLOBALLY_SUPPORTED_ABIS=(2.5-jython) |
32 | _JYTHON_GLOBALLY_SUPPORTED_ABIS=(2.5-jython) |
| 21 | _PYPY_GLOBALLY_SUPPORTED_ABIS=(2.7-pypy-1.5) |
33 | _PYPY_GLOBALLY_SUPPORTED_ABIS=(2.7-pypy-1.7 2.7-pypy-1.8) |
| 22 | _PYTHON_GLOBALLY_SUPPORTED_ABIS=(${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]} ${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]} ${_JYTHON_GLOBALLY_SUPPORTED_ABIS[@]} ${_PYPY_GLOBALLY_SUPPORTED_ABIS[@]}) |
34 | _PYTHON_GLOBALLY_SUPPORTED_ABIS=(${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]} ${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]} ${_JYTHON_GLOBALLY_SUPPORTED_ABIS[@]} ${_PYPY_GLOBALLY_SUPPORTED_ABIS[@]}) |
| 23 | |
35 | |
| 24 | # ================================================================================================ |
36 | # ================================================================================================ |
| 25 | # ===================================== HANDLING OF METADATA ===================================== |
37 | # ===================================== HANDLING OF METADATA ===================================== |
| 26 | # ================================================================================================ |
38 | # ================================================================================================ |
| … | |
… | |
| 384 | _CYAN= |
396 | _CYAN= |
| 385 | _NORMAL= |
397 | _NORMAL= |
| 386 | fi |
398 | fi |
| 387 | } |
399 | } |
| 388 | |
400 | |
| 389 | unset PYTHON_PKG_SETUP_EXECUTED |
|
|
| 390 | |
|
|
| 391 | _python_check_python_pkg_setup_execution() { |
401 | _python_check_python_pkg_setup_execution() { |
| 392 | [[ " ${FUNCNAME[@]:1} " =~ " "(python_set_active_version|python_pkg_setup)" " ]] && return |
402 | [[ " ${FUNCNAME[@]:1} " =~ " "(python_set_active_version|python_pkg_setup)" " ]] && return |
| 393 | |
403 | |
| 394 | if ! has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_PKG_SETUP_EXECUTED}" ]]; then |
404 | if ! has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_PKG_SETUP_EXECUTED}" ]]; then |
| 395 | die "python_pkg_setup() not called" |
405 | die "python_pkg_setup() not called" |
| … | |
… | |
| 463 | fi |
473 | fi |
| 464 | |
474 | |
| 465 | PYTHON_PKG_SETUP_EXECUTED="1" |
475 | PYTHON_PKG_SETUP_EXECUTED="1" |
| 466 | } |
476 | } |
| 467 | |
477 | |
| 468 | if ! has "${EAPI:-0}" 0 1 2 3 || { has "${EAPI:-0}" 2 3 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; }; then |
|
|
| 469 | EXPORT_FUNCTIONS pkg_setup |
|
|
| 470 | fi |
|
|
| 471 | |
|
|
| 472 | _PYTHON_SHEBANG_BASE_PART_REGEX='^#![[:space:]]*([^[:space:]]*/usr/bin/env[[:space:]]+)?([^[:space:]]*/)?(jython|pypy-c|python)' |
478 | _PYTHON_SHEBANG_BASE_PART_REGEX='^#![[:space:]]*([^[:space:]]*/usr/bin/env[[:space:]]+)?([^[:space:]]*/)?(jython|pypy-c|python)' |
| 473 | |
479 | |
| 474 | # @FUNCTION: python_convert_shebangs |
480 | # @FUNCTION: python_convert_shebangs |
| 475 | # @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_ABI|Python_version> <file|directory> [files|directories] |
481 | # @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_ABI|Python_version> <file|directory> [files|directories] |
| 476 | # @DESCRIPTION: |
482 | # @DESCRIPTION: |
| 477 | # Convert shebangs in specified files. Directories can be specified only with --recursive option. |
483 | # Convert shebangs in specified files. Directories can be specified only with --recursive option. |
| 478 | python_convert_shebangs() { |
484 | python_convert_shebangs() { |
| 479 | _python_check_python_pkg_setup_execution |
485 | _python_check_python_pkg_setup_execution |
| 480 | |
486 | |
| 481 | local argument file files=() only_executables="0" python_interpreter quiet="0" recursive="0" |
487 | local argument file files=() only_executables="0" python_interpreter quiet="0" recursive="0" shebangs_converted="0" |
| 482 | |
488 | |
| 483 | while (($#)); do |
489 | while (($#)); do |
| 484 | case "$1" in |
490 | case "$1" in |
| 485 | -r|--recursive) |
491 | -r|--recursive) |
| 486 | recursive="1" |
492 | recursive="1" |
| … | |
… | |
| 541 | [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue |
547 | [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue |
| 542 | |
548 | |
| 543 | if [[ "$(head -n1 "${file}")" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX} ]]; then |
549 | if [[ "$(head -n1 "${file}")" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX} ]]; then |
| 544 | [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue |
550 | [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue |
| 545 | |
551 | |
|
|
552 | shebangs_converted="1" |
|
|
553 | |
| 546 | if [[ "${quiet}" == "0" ]]; then |
554 | if [[ "${quiet}" == "0" ]]; then |
| 547 | einfo "Converting shebang in '${file}'" |
555 | einfo "Converting shebang in '${file}'" |
| 548 | fi |
556 | fi |
| 549 | |
557 | |
| 550 | 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" |
558 | 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" |
| 551 | fi |
559 | fi |
|
|
560 | done |
|
|
561 | |
|
|
562 | if [[ "${shebangs_converted}" == "0" ]]; then |
|
|
563 | ewarn "${FUNCNAME}(): Python scripts not found" |
|
|
564 | fi |
|
|
565 | } |
|
|
566 | |
|
|
567 | # @FUNCTION: python_clean_py-compile_files |
|
|
568 | # @USAGE: [-q|--quiet] |
|
|
569 | # @DESCRIPTION: |
|
|
570 | # Clean py-compile files to disable byte-compilation. |
|
|
571 | python_clean_py-compile_files() { |
|
|
572 | _python_check_python_pkg_setup_execution |
|
|
573 | |
|
|
574 | local file files=() quiet="0" |
|
|
575 | |
|
|
576 | while (($#)); do |
|
|
577 | case "$1" in |
|
|
578 | -q|--quiet) |
|
|
579 | quiet="1" |
|
|
580 | ;; |
|
|
581 | -*) |
|
|
582 | die "${FUNCNAME}(): Unrecognized option '$1'" |
|
|
583 | ;; |
|
|
584 | *) |
|
|
585 | die "${FUNCNAME}(): Invalid usage" |
|
|
586 | ;; |
|
|
587 | esac |
|
|
588 | shift |
|
|
589 | done |
|
|
590 | |
|
|
591 | while read -d $'\0' -r file; do |
|
|
592 | files+=("${file#./}") |
|
|
593 | done < <(find -name py-compile -type f -print0) |
|
|
594 | |
|
|
595 | for file in "${files[@]}"; do |
|
|
596 | if [[ "${quiet}" == "0" ]]; then |
|
|
597 | einfo "Cleaning '${file}' file" |
|
|
598 | fi |
|
|
599 | echo "#!/bin/sh" > "${file}" |
| 552 | done |
600 | done |
| 553 | } |
601 | } |
| 554 | |
602 | |
| 555 | # @FUNCTION: python_clean_installation_image |
603 | # @FUNCTION: python_clean_installation_image |
| 556 | # @USAGE: [-q|--quiet] |
604 | # @USAGE: [-q|--quiet] |
| … | |
… | |
| 1592 | fi |
1640 | fi |
| 1593 | done |
1641 | done |
| 1594 | |
1642 | |
| 1595 | popd > /dev/null || die "popd failed" |
1643 | popd > /dev/null || die "popd failed" |
| 1596 | |
1644 | |
|
|
1645 | # This is per bug #390691, without the duplication refactor, and with |
|
|
1646 | # the 3-way structure per comment #6. This enable users with old |
|
|
1647 | # coreutils to upgrade a lot easier (you need to upgrade python+portage |
|
|
1648 | # before coreutils can be upgraded). |
| 1597 | if ROOT="/" has_version sys-apps/coreutils; then |
1649 | if ROOT="/" has_version '>=sys-apps/coreutils-6.9.90'; then |
| 1598 | 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" |
1650 | 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" |
|
|
1651 | elif ROOT="/" has_version sys-apps/coreutils; then |
|
|
1652 | 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" |
| 1599 | else |
1653 | else |
| 1600 | cp -fpr "${intermediate_installation_images_directory}/${PYTHON_ABI}/"* "${D}" || die "Merging of intermediate installation image for Python ABI '${PYTHON_ABI} into installation image failed" |
1654 | cp -fpr "${intermediate_installation_images_directory}/${PYTHON_ABI}/"* "${D}" || die "Merging of intermediate installation image for Python ABI '${PYTHON_ABI} into installation image failed" |
| 1601 | fi |
1655 | fi |
| 1602 | done |
1656 | done |
| 1603 | |
1657 | |
| … | |
… | |
| 2860 | done |
2914 | done |
| 2861 | for dir in "${evaluated_dirs[@]}"; do |
2915 | for dir in "${evaluated_dirs[@]}"; do |
| 2862 | eval "dirs+=(\"\${root}${dir}\")" |
2916 | eval "dirs+=(\"\${root}${dir}\")" |
| 2863 | done |
2917 | done |
| 2864 | stderr+="${stderr:+$'\n'}$("$(PYTHON)" -m compileall "${options[@]}" "${dirs[@]}" 2>&1)" || return_code="1" |
2918 | stderr+="${stderr:+$'\n'}$("$(PYTHON)" -m compileall "${options[@]}" "${dirs[@]}" 2>&1)" || return_code="1" |
| 2865 | if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then |
2919 | if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then |
| 2866 | "$(PYTHON)" -O -m compileall "${options[@]}" "${dirs[@]}" &> /dev/null || return_code="1" |
2920 | "$(PYTHON)" -O -m compileall "${options[@]}" "${dirs[@]}" &> /dev/null || return_code="1" |
| 2867 | fi |
2921 | fi |
| 2868 | _python_clean_compiled_modules "${dirs[@]}" |
2922 | _python_clean_compiled_modules "${dirs[@]}" |
| 2869 | fi |
2923 | fi |
| 2870 | if ((${#site_packages_files[@]})) || ((${#evaluated_files[@]})); then |
2924 | if ((${#site_packages_files[@]})) || ((${#evaluated_files[@]})); then |
| … | |
… | |
| 2873 | done |
2927 | done |
| 2874 | for file in "${evaluated_files[@]}"; do |
2928 | for file in "${evaluated_files[@]}"; do |
| 2875 | eval "files+=(\"\${root}${file}\")" |
2929 | eval "files+=(\"\${root}${file}\")" |
| 2876 | done |
2930 | done |
| 2877 | stderr+="${stderr:+$'\n'}$("$(PYTHON)" -m py_compile "${files[@]}" 2>&1)" || return_code="1" |
2931 | stderr+="${stderr:+$'\n'}$("$(PYTHON)" -m py_compile "${files[@]}" 2>&1)" || return_code="1" |
| 2878 | if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then |
2932 | if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then |
| 2879 | "$(PYTHON)" -O -m py_compile "${files[@]}" &> /dev/null || return_code="1" |
2933 | "$(PYTHON)" -O -m py_compile "${files[@]}" &> /dev/null || return_code="1" |
| 2880 | fi |
2934 | fi |
| 2881 | _python_clean_compiled_modules "${files[@]}" |
2935 | _python_clean_compiled_modules "${files[@]}" |
| 2882 | fi |
2936 | fi |
| 2883 | eend "${return_code}" |
2937 | eend "${return_code}" |
| … | |
… | |
| 2904 | return_code="0" |
2958 | return_code="0" |
| 2905 | stderr="" |
2959 | stderr="" |
| 2906 | ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation_and_version)" |
2960 | ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation_and_version)" |
| 2907 | if ((${#other_dirs[@]})); then |
2961 | if ((${#other_dirs[@]})); then |
| 2908 | stderr+="${stderr:+$'\n'}$("$(PYTHON ${PYTHON_ABI})" -m compileall "${options[@]}" "${other_dirs[@]}" 2>&1)" || return_code="1" |
2962 | stderr+="${stderr:+$'\n'}$("$(PYTHON ${PYTHON_ABI})" -m compileall "${options[@]}" "${other_dirs[@]}" 2>&1)" || return_code="1" |
| 2909 | if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then |
2963 | if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then |
| 2910 | "$(PYTHON ${PYTHON_ABI})" -O -m compileall "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" |
2964 | "$(PYTHON ${PYTHON_ABI})" -O -m compileall "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" |
| 2911 | fi |
2965 | fi |
| 2912 | _python_clean_compiled_modules "${other_dirs[@]}" |
2966 | _python_clean_compiled_modules "${other_dirs[@]}" |
| 2913 | fi |
2967 | fi |
| 2914 | if ((${#other_files[@]})); then |
2968 | if ((${#other_files[@]})); then |
| 2915 | stderr+="${stderr:+$'\n'}$("$(PYTHON ${PYTHON_ABI})" -m py_compile "${other_files[@]}" 2>&1)" || return_code="1" |
2969 | stderr+="${stderr:+$'\n'}$("$(PYTHON ${PYTHON_ABI})" -m py_compile "${other_files[@]}" 2>&1)" || return_code="1" |
| 2916 | if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then |
2970 | if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then |
| 2917 | "$(PYTHON ${PYTHON_ABI})" -O -m py_compile "${other_files[@]}" &> /dev/null || return_code="1" |
2971 | "$(PYTHON ${PYTHON_ABI})" -O -m py_compile "${other_files[@]}" &> /dev/null || return_code="1" |
| 2918 | fi |
2972 | fi |
| 2919 | _python_clean_compiled_modules "${other_files[@]}" |
2973 | _python_clean_compiled_modules "${other_files[@]}" |
| 2920 | fi |
2974 | fi |
| 2921 | eend "${return_code}" |
2975 | eend "${return_code}" |
| … | |
… | |
| 3108 | } |
3162 | } |
| 3109 | |
3163 | |
| 3110 | # ================================================================================================ |
3164 | # ================================================================================================ |
| 3111 | # ===================================== DEPRECATED FUNCTIONS ===================================== |
3165 | # ===================================== DEPRECATED FUNCTIONS ===================================== |
| 3112 | # ================================================================================================ |
3166 | # ================================================================================================ |
|
|
3167 | |
|
|
3168 | fi # _PYTHON_ECLASS_INHERITED |