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

Diff of /eclass/python.eclass

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

Revision 1.88 Revision 1.93
1# Copyright 1999-2010 Gentoo Foundation 1# Copyright 1999-2010 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.88 2010/02/11 18:52:44 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.93 2010/03/12 18:27:01 betelgeuse 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
150 else 150 else
151 die "Invalid syntax of PYTHON_DEPEND" 151 die "Invalid syntax of PYTHON_DEPEND"
152 fi 152 fi
153} 153}
154 154
155DEPEND=">=app-admin/eselect-python-20090804" 155DEPEND=">=app-admin/eselect-python-20091230"
156RDEPEND="${DEPEND}" 156RDEPEND="${DEPEND}"
157 157
158if [[ -n "${PYTHON_DEPEND}" && -n "${NEED_PYTHON}" ]]; then 158if [[ -n "${PYTHON_DEPEND}" && -n "${NEED_PYTHON}" ]]; then
159 die "PYTHON_DEPEND and NEED_PYTHON cannot be set simultaneously" 159 die "PYTHON_DEPEND and NEED_PYTHON cannot be set simultaneously"
160elif [[ -n "${PYTHON_DEPEND}" ]]; then 160elif [[ -n "${PYTHON_DEPEND}" ]]; then
188# @DESCRIPTION: 188# @DESCRIPTION:
189# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 189# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
190# are respected. Only exported if one of those variables is set. 190# are respected. Only exported if one of those variables is set.
191if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 191if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
192 python_pkg_setup() { 192 python_pkg_setup() {
193 # Check if phase is pkg_setup().
194 [[ "${EBUILD_PHASE}" != "setup" ]] && die "${FUNCNAME}() can be used only in pkg_setup() phase"
195
193 python_pkg_setup_fail() { 196 python_pkg_setup_fail() {
194 eerror "${1}" 197 eerror "${1}"
195 die "${1}" 198 die "${1}"
196 } 199 }
197 200
227 unset -f python_pkg_setup_check_USE_flags python_pkg_setup_fail 230 unset -f python_pkg_setup_check_USE_flags python_pkg_setup_fail
228 } 231 }
229 232
230 EXPORT_FUNCTIONS pkg_setup 233 EXPORT_FUNCTIONS pkg_setup
231 234
232 _PYTHON_USE_WITH_ATOM="" 235 _PYTHON_USE_WITH_ATOMS_ARRAY=()
233 if [[ -n "${PYTHON_USE_WITH}" ]]; then 236 if [[ -n "${PYTHON_USE_WITH}" ]]; then
234 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do 237 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
235 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 238 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]")
236 done 239 done
237 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
238 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then 240 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
239 for _USE_flag in ${PYTHON_USE_WITH_OR}; do 241 for _USE_flag in ${PYTHON_USE_WITH_OR}; do
240 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do 242 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
241 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${_USE_flag}]" 243 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${_USE_flag}]")
242 done 244 done
243 done 245 done
244 unset _USE_flag 246 unset _USE_flag
247 fi
248 if [[ "${#_PYTHON_USE_WITH_ATOMS_ARRAY[@]}" -gt 1 ]]; then
245 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )" 249 _PYTHON_USE_WITH_ATOMS="|| ( ${_PYTHON_USE_WITH_ATOMS_ARRAY[@]} )"
250 else
251 _PYTHON_USE_WITH_ATOMS="${_PYTHON_USE_WITH_ATOMS_ARRAY[@]}"
246 fi 252 fi
247 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then 253 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
248 _PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOM} )" 254 _PYTHON_USE_WITH_ATOMS="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOMS} )"
249 fi 255 fi
250 DEPEND+=" ${_PYTHON_USE_WITH_ATOM}" 256 DEPEND+=" ${_PYTHON_USE_WITH_ATOMS}"
251 RDEPEND+=" ${_PYTHON_USE_WITH_ATOM}" 257 RDEPEND+=" ${_PYTHON_USE_WITH_ATOMS}"
252 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOM 258 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOMS _PYTHON_USE_WITH_ATOMS_ARRAY
253fi 259fi
254 260
255unset _PYTHON_ATOMS 261unset _PYTHON_ATOMS
256 262
257# ================================================================================================ 263# ================================================================================================
261# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS 267# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
262# @DESCRIPTION: 268# @DESCRIPTION:
263# Set this in EAPI <= 4 to indicate that current package supports installation for 269# Set this in EAPI <= 4 to indicate that current package supports installation for
264# multiple versions of Python. 270# multiple versions of Python.
265 271
266# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS 272# @ECLASS-VARIABLE: PYTHON_EXPORT_PHASE_FUNCTIONS
267# @DESCRIPTION: 273# @DESCRIPTION:
268# Set this to define default functions for the following ebuild phases: 274# Set this to export phase functions for the following ebuild phases:
269# src_prepare, src_configure, src_compile, src_test, src_install. 275# src_prepare, src_configure, src_compile, src_test, src_install.
270if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then 276if ! has "${EAPI:-0}" 0 1 && [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
271 python_src_prepare() { 277 python_src_prepare() {
272 python_copy_sources 278 python_copy_sources
273 } 279 }
274 280
275 for python_default_function in src_configure src_compile src_test src_install; do 281 for python_default_function in src_configure src_compile src_test src_install; do
276 eval "python_${python_default_function}() { python_execute_function -d -s; }" 282 eval "python_${python_default_function}() { python_execute_function -d -s; }"
277 done 283 done
278 unset python_default_function 284 unset python_default_function
279 285
286 if [[ -n "${PYTHON_EXPORT_PHASE_FUNCTIONS}" ]]; then
280 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install 287 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
288 fi
281fi 289fi
282 290
283unset PYTHON_ABIS 291unset PYTHON_ABIS
284unset PYTHON_ABIS_SANITY_CHECKS
285 292
286# @FUNCTION: validate_PYTHON_ABIS 293# @FUNCTION: validate_PYTHON_ABIS
287# @DESCRIPTION: 294# @DESCRIPTION:
288# Ensure that PYTHON_ABIS variable has valid value. 295# Ensure that PYTHON_ABIS variable has valid value.
289# This function usually should not be directly called in ebuilds. 296# This function usually should not be directly called in ebuilds.
291 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable. 298 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable.
292 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 299 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
293 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable" 300 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
294 fi 301 fi
295 302
296 # Ensure that /usr/bin/python and /usr/bin/python-config are valid. 303 _python_initial_sanity_checks
297 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
298 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
299 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
300 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
301 fi
302 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
303 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
304 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
305 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
306 fi
307 304
308 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5. 305 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
309 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then 306 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
310 local PYTHON_ABI restricted_ABI support_ABI supported_PYTHON_ABIS= 307 local PYTHON_ABI restricted_ABI support_ABI supported_PYTHON_ABIS=
311 PYTHON_ABI_SUPPORTED_VALUES="${_CPYTHON2_SUPPORTED_ABIS[@]} ${_CPYTHON3_SUPPORTED_ABIS[@]} ${_JYTHON_SUPPORTED_ABIS[@]}" 308 PYTHON_ABI_SUPPORTED_VALUES="${_CPYTHON2_SUPPORTED_ABIS[@]} ${_CPYTHON3_SUPPORTED_ABIS[@]} ${_JYTHON_SUPPORTED_ABIS[@]}"
426 PYTHON_ABIS="${PYTHON_ABIS# }" 423 PYTHON_ABIS="${PYTHON_ABIS# }"
427 export PYTHON_ABIS="${PYTHON_ABIS% }" 424 export PYTHON_ABIS="${PYTHON_ABIS% }"
428 fi 425 fi
429 fi 426 fi
430 427
431 if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then 428 _python_final_sanity_checks
432 local PYTHON_ABI
433 for PYTHON_ABI in ${PYTHON_ABIS}; do
434 # Ensure that appropriate version of Python is installed.
435 if ! has_version "$(python_get_implementational_package)"; then
436 die "$(python_get_implementational_package) is not installed"
437 fi
438
439 # Ensure that EPYTHON variable is respected.
440 if [[ "$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" != "${PYTHON_ABI}" ]]; then
441 eerror "python: '$(type -p python)'"
442 eerror "ABI: '${ABI}'"
443 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
444 eerror "EPYTHON: '$(PYTHON)'"
445 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
446 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")'"
447 die "'python' does not respect EPYTHON variable"
448 fi
449 done
450 PYTHON_ABIS_SANITY_CHECKS="1"
451 fi
452} 429}
453 430
454# @FUNCTION: python_execute_function 431# @FUNCTION: python_execute_function
455# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments] 432# @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]
456# @DESCRIPTION: 433# @DESCRIPTION:
457# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 434# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
458# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 435# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
459python_execute_function() { 436python_execute_function() {
460 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function i nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir= 437 _python_set_color_variables
438
439 local action action_message action_message_template= default_function="0" failure_message failure_message_template= final_ABI="0" function i iterated_PYTHON_ABIS nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
461 440
462 while (($#)); do 441 while (($#)); do
463 case "$1" in 442 case "$1" in
464 --action-message) 443 --action-message)
465 action_message_template="$2" 444 action_message_template="$2"
469 default_function="1" 448 default_function="1"
470 ;; 449 ;;
471 --failure-message) 450 --failure-message)
472 failure_message_template="$2" 451 failure_message_template="$2"
473 shift 452 shift
453 ;;
454 -f|--final-ABI)
455 final_ABI="1"
474 ;; 456 ;;
475 --nonfatal) 457 --nonfatal)
476 nonfatal="1" 458 nonfatal="1"
477 ;; 459 ;;
478 -q|--quiet) 460 -q|--quiet)
571 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation" 553 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
572 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation" 554 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
573 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation" 555 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
574 fi 556 fi
575 557
576 local RED GREEN BLUE NORMAL 558 validate_PYTHON_ABIS
577 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then 559 if [[ "${final_ABI}" == "1" ]]; then
578 RED=$'\e[1;31m' 560 iterated_PYTHON_ABIS="$(PYTHON -f --ABI)"
579 GREEN=$'\e[1;32m'
580 BLUE=$'\e[1;34m'
581 NORMAL=$'\e[0m'
582 else 561 else
583 RED= 562 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
584 GREEN=
585 BLUE=
586 NORMAL=
587 fi 563 fi
588
589 validate_PYTHON_ABIS
590 for PYTHON_ABI in ${PYTHON_ABIS}; do 564 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
591 if [[ "${quiet}" == "0" ]]; then 565 if [[ "${quiet}" == "0" ]]; then
592 if [[ -n "${action_message_template}" ]]; then 566 if [[ -n "${action_message_template}" ]]; then
593 action_message="$(eval echo -n "${action_message_template}")" 567 action_message="$(eval echo -n "${action_message_template}")"
594 else 568 else
595 action_message="${action} of ${CATEGORY}/${PF} with $(python_get_implementation) $(python_get_version)..." 569 action_message="${action} of ${CATEGORY}/${PF} with $(python_get_implementation) $(python_get_version)..."
596 fi 570 fi
597 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 571 echo " ${_GREEN}*${_NORMAL} ${_BLUE}${action_message}${_NORMAL}"
598 fi 572 fi
599 573
600 if [[ "${separate_build_dirs}" == "1" ]]; then 574 if [[ "${separate_build_dirs}" == "1" ]]; then
601 if [[ -n "${source_dir}" ]]; then 575 if [[ -n "${source_dir}" ]]; then
602 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}" 576 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
625 failure_message="${action} failed with $(python_get_implementation) $(python_get_version) in ${function}() function" 599 failure_message="${action} failed with $(python_get_implementation) $(python_get_version) in ${function}() function"
626 fi 600 fi
627 601
628 if [[ "${nonfatal}" == "1" ]]; then 602 if [[ "${nonfatal}" == "1" ]]; then
629 if [[ "${quiet}" == "0" ]]; then 603 if [[ "${quiet}" == "0" ]]; then
630 ewarn "${RED}${failure_message}${NORMAL}" 604 ewarn "${_RED}${failure_message}${_NORMAL}"
631 fi 605 fi
632 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 606 elif [[ "${final_ABI}" == "0" ]] && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
633 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then 607 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
634 local enabled_PYTHON_ABIS= other_PYTHON_ABI 608 local enabled_PYTHON_ABIS= other_PYTHON_ABI
635 for other_PYTHON_ABI in ${PYTHON_ABIS}; do 609 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
636 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}" 610 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
637 done 611 done
638 export PYTHON_ABIS="${enabled_PYTHON_ABIS}" 612 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
639 fi 613 fi
640 if [[ "${quiet}" == "0" ]]; then 614 if [[ "${quiet}" == "0" ]]; then
641 ewarn "${RED}${failure_message}${NORMAL}" 615 ewarn "${_RED}${failure_message}${_NORMAL}"
642 fi 616 fi
643 if [[ -z "${PYTHON_ABIS}" ]]; then 617 if [[ -z "${PYTHON_ABIS}" ]]; then
644 die "${function}() function failed with all enabled versions of Python" 618 die "${function}() function failed with all enabled versions of Python"
645 fi 619 fi
646 else 620 else
679 unset -f python_default_function 653 unset -f python_default_function
680 fi 654 fi
681} 655}
682 656
683# @FUNCTION: python_copy_sources 657# @FUNCTION: python_copy_sources
684# @USAGE: [--no-link] [--] [directory] 658# @USAGE: <directory="${S}"> [directory]
685# @DESCRIPTION: 659# @DESCRIPTION:
686# Copy unpacked sources of current package to separate build directory for each Python ABI. 660# Copy unpacked sources of current package to separate build directory for each Python ABI.
687python_copy_sources() { 661python_copy_sources() {
688 local dir dirs=() no_link="0" PYTHON_ABI 662 local dir dirs=() PYTHON_ABI
689
690 while (($#)); do
691 case "$1" in
692 --no-link)
693 no_link="1"
694 ;;
695 --)
696 shift
697 break
698 ;;
699 -*)
700 die "${FUNCNAME}(): Unrecognized option '$1'"
701 ;;
702 *)
703 break
704 ;;
705 esac
706 shift
707 done
708 663
709 if [[ "$#" -eq 0 ]]; then 664 if [[ "$#" -eq 0 ]]; then
710 if [[ "${WORKDIR}" == "${S}" ]]; then 665 if [[ "${WORKDIR}" == "${S}" ]]; then
711 die "${FUNCNAME}() cannot be used" 666 die "${FUNCNAME}() cannot be used"
712 fi 667 fi
713 dirs="${S}" 668 dirs=("${S}")
714 else 669 else
715 dirs="$@" 670 dirs=("$@")
716 fi 671 fi
717 672
718 validate_PYTHON_ABIS 673 validate_PYTHON_ABIS
719 for PYTHON_ABI in ${PYTHON_ABIS}; do 674 for PYTHON_ABI in ${PYTHON_ABIS}; do
720 for dir in "${dirs[@]}"; do 675 for dir in "${dirs[@]}"; do
721 if [[ "${no_link}" == "1" ]]; then
722 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed" 676 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
723 else
724 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
725 fi
726 done 677 done
727 done 678 done
728} 679}
729 680
730# @FUNCTION: python_set_build_dir_symlink 681# @FUNCTION: python_set_build_dir_symlink
731# @USAGE: [directory="build"] 682# @USAGE: <directory="build">
732# @DESCRIPTION: 683# @DESCRIPTION:
733# Create build directory symlink. 684# Create build directory symlink.
734python_set_build_dir_symlink() { 685python_set_build_dir_symlink() {
735 local dir="$1" 686 local dir="$1"
736 687
888 die "${FUNCNAME}(): Generation of '$1' failed" 839 die "${FUNCNAME}(): Generation of '$1' failed"
889 fi 840 fi
890 fi 841 fi
891 cat << EOF >> "${file}" 842 cat << EOF >> "${file}"
892 843
893os.environ["PYTHON_PROCESS_NAME"] = sys.argv[0] 844os.environ["PYTHON_SCRIPT_NAME"] = sys.argv[0]
894target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI) 845target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI)
895if not os.path.exists(target_executable): 846if not os.path.exists(target_executable):
896 sys.stderr.write("'%s' does not exist\n" % target_executable) 847 sys.stderr.write("'%s' does not exist\n" % target_executable)
897 sys.exit(1) 848 sys.exit(1)
898 849
920 871
921 if [[ "$#" -ne 1 ]]; then 872 if [[ "$#" -ne 1 ]]; then
922 die "${FUNCNAME}() requires 1 argument" 873 die "${FUNCNAME}() requires 1 argument"
923 fi 874 fi
924 875
876 _python_initial_sanity_checks
877
925 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then 878 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
926 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then 879 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
927 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed" 880 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
928 fi 881 fi
929 export EPYTHON="$(PYTHON "$1")" 882 export EPYTHON="$(PYTHON "$1")"
944 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses, 897 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
945 # so it does not need to be exported to subprocesses. 898 # so it does not need to be exported to subprocesses.
946 PYTHON_ABI="${EPYTHON#python}" 899 PYTHON_ABI="${EPYTHON#python}"
947 PYTHON_ABI="${PYTHON_ABI%%-*}" 900 PYTHON_ABI="${PYTHON_ABI%%-*}"
948 901
902 _python_final_sanity_checks
903
949 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable. 904 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
950 PYTHON_REQUESTED_ACTIVE_VERSION="$1" 905 PYTHON_REQUESTED_ACTIVE_VERSION="$1"
951} 906}
952 907
953# @FUNCTION: python_need_rebuild 908# @FUNCTION: python_need_rebuild
954# @DESCRIPTION: Mark current package for rebuilding by python-updater after 909# @DESCRIPTION: Mark current package for rebuilding by python-updater after
955# switching of active version of Python. 910# switching of active version of Python.
956python_need_rebuild() { 911python_need_rebuild() {
912 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
913 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
914 fi
915
957 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)" 916 export PYTHON_NEED_REBUILD="$(PYTHON --ABI)"
958} 917}
959 918
960# ================================================================================================ 919# ================================================================================================
961# ======================================= GETTER FUNCTIONS ======================================= 920# ======================================= GETTER FUNCTIONS =======================================
962# ================================================================================================ 921# ================================================================================================
980 die "${FUNCNAME}(): Unrecognized Python ABI '$1'" 939 die "${FUNCNAME}(): Unrecognized Python ABI '$1'"
981 fi 940 fi
982} 941}
983 942
984# @FUNCTION: PYTHON 943# @FUNCTION: PYTHON
985# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}"> 944# @USAGE: [-2] [-3] [--ABI] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
986# @DESCRIPTION: 945# @DESCRIPTION:
987# Print filename of Python interpreter for specified Python ABI. If Python_ABI argument 946# Print filename of Python interpreter for specified Python ABI. If Python_ABI argument
988# is ommitted, then PYTHON_ABI environment variable must be set and is used. 947# is ommitted, then PYTHON_ABI environment variable must be set and is used.
989# If -2 option is specified, then active version of Python 2 is used. 948# If -2 option is specified, then active version of Python 2 is used.
990# If -3 option is specified, then active version of Python 3 is used. 949# If -3 option is specified, then active version of Python 3 is used.
991# If --active option is specified, then active version of Python is used.
992# Active version of Python can be set by python_set_active_version().
993# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used. 950# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
994# -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously. 951# -2, -3 and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
995# If --ABI option is specified, then only specified Python ABI is printed instead of 952# If --ABI option is specified, then only specified Python ABI is printed instead of
996# filename of Python interpreter. 953# filename of Python interpreter.
997# If --absolute-path option is specified, then absolute path to Python interpreter is printed. 954# If --absolute-path option is specified, then absolute path to Python interpreter is printed.
998# --ABI and --absolute-path options cannot be specified simultaneously. 955# --ABI and --absolute-path options cannot be specified simultaneously.
999PYTHON() { 956PYTHON() {
1000 local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" PYTHON_ABI="${PYTHON_ABI}" python_interpreter python2="0" python3="0" 957 local ABI_output="0" absolute_path_output="0" final_ABI="0" PYTHON_ABI="${PYTHON_ABI}" python_interpreter python2="0" python3="0"
1001 958
1002 while (($#)); do 959 while (($#)); do
1003 case "$1" in 960 case "$1" in
1004 -2) 961 -2)
1005 python2="1" 962 python2="1"
1008 python3="1" 965 python3="1"
1009 ;; 966 ;;
1010 --ABI) 967 --ABI)
1011 ABI_output="1" 968 ABI_output="1"
1012 ;; 969 ;;
1013 -A|--active)
1014 active="1"
1015 ;;
1016 -a|--absolute-path) 970 -a|--absolute-path)
1017 absolute_path_output="1" 971 absolute_path_output="1"
1018 ;; 972 ;;
1019 -f|--final-ABI) 973 -f|--final-ABI)
1020 final_ABI="1" 974 final_ABI="1"
1035 989
1036 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then 990 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
1037 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously" 991 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
1038 fi 992 fi
1039 993
1040 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then 994 if [[ "$((${python2} + ${python3} + ${final_ABI}))" -gt 1 ]]; then
1041 die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously" 995 die "${FUNCNAME}(): '-2', '-3' or '--final-ABI' options cannot be specified simultaneously"
1042 fi 996 fi
1043 997
1044 if [[ "$#" -eq 0 ]]; then 998 if [[ "$#" -eq 0 ]]; then
1045 if [[ "${active}" == "1" ]]; then
1046 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1047 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
1048 fi
1049 PYTHON_ABI="$("${EPREFIX}/usr/bin/python" -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")"
1050 elif [[ "${final_ABI}" == "1" ]]; then 999 if [[ "${final_ABI}" == "1" ]]; then
1051 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 1000 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
1052 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python" 1001 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python"
1053 fi 1002 fi
1054 validate_PYTHON_ABIS 1003 validate_PYTHON_ABIS
1055 PYTHON_ABI="${PYTHON_ABIS##* }" 1004 PYTHON_ABI="${PYTHON_ABIS##* }"
1056 elif [[ "${python2}" == "1" ]]; then 1005 elif [[ "${python2}" == "1" ]]; then
1057 # PYTHON_ABI="$(eselect python show --python2 --ABI)" 1006 PYTHON_ABI="$(eselect python show --python2 --ABI)"
1058 PYTHON_ABI="$(eselect python show --python2)"
1059 if [[ -z "${PYTHON_ABI}" ]]; then 1007 if [[ -z "${PYTHON_ABI}" ]]; then
1060 die "${FUNCNAME}(): Active Python 2 interpreter not set" 1008 die "${FUNCNAME}(): Active Python 2 interpreter not set"
1061 elif [[ "${PYTHON_ABI}" != "python2."* ]]; then 1009 elif [[ "${PYTHON_ABI}" != "2."* ]]; then
1062 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`" 1010 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1063 fi 1011 fi
1064 PYTHON_ABI="${PYTHON_ABI#python}"
1065 elif [[ "${python3}" == "1" ]]; then 1012 elif [[ "${python3}" == "1" ]]; then
1066 # PYTHON_ABI="$(eselect python show --python3 --ABI)" 1013 PYTHON_ABI="$(eselect python show --python3 --ABI)"
1067 PYTHON_ABI="$(eselect python show --python3)"
1068 if [[ -z "${PYTHON_ABI}" ]]; then 1014 if [[ -z "${PYTHON_ABI}" ]]; then
1069 die "${FUNCNAME}(): Active Python 3 interpreter not set" 1015 die "${FUNCNAME}(): Active Python 3 interpreter not set"
1070 elif [[ "${PYTHON_ABI}" != "python3."* ]]; then 1016 elif [[ "${PYTHON_ABI}" != "3."* ]]; then
1071 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`" 1017 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1072 fi 1018 fi
1073 PYTHON_ABI="${PYTHON_ABI#python}" 1019 elif [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
1020 PYTHON_ABI="$("${EPREFIX}/usr/bin/python" -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")"
1074 elif [[ -z "${PYTHON_ABI}" ]]; then 1021 elif [[ -z "${PYTHON_ABI}" ]]; then
1075 die "${FUNCNAME}(): Invalid usage: Python ABI not specified" 1022 die "${FUNCNAME}(): Invalid usage: Python ABI not specified"
1076 fi 1023 fi
1077 elif [[ "$#" -eq 1 ]]; then 1024 elif [[ "$#" -eq 1 ]]; then
1078 if [[ "${active}" == "1" ]]; then
1079 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
1080 fi
1081 if [[ "${final_ABI}" == "1" ]]; then 1025 if [[ "${final_ABI}" == "1" ]]; then
1082 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously" 1026 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
1083 fi 1027 fi
1084 if [[ "${python2}" == "1" ]]; then 1028 if [[ "${python2}" == "1" ]]; then
1085 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously" 1029 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
1138 done 1082 done
1139 1083
1140 if [[ "${final_ABI}" == "1" ]]; then 1084 if [[ "${final_ABI}" == "1" ]]; then
1141 PYTHON_ABI="$(PYTHON -f --ABI)" 1085 PYTHON_ABI="$(PYTHON -f --ABI)"
1142 elif [[ -z "${PYTHON_ABI}" ]]; then 1086 elif [[ -z "${PYTHON_ABI}" ]]; then
1143 PYTHON_ABI="$(PYTHON -A --ABI)" 1087 PYTHON_ABI="$(PYTHON --ABI)"
1144 fi 1088 fi
1145 1089
1146 echo "$(_python_get_implementation "${PYTHON_ABI}")" 1090 echo "$(_python_get_implementation "${PYTHON_ABI}")"
1147} 1091}
1148 1092
1170 done 1114 done
1171 1115
1172 if [[ "${final_ABI}" == "1" ]]; then 1116 if [[ "${final_ABI}" == "1" ]]; then
1173 PYTHON_ABI="$(PYTHON -f --ABI)" 1117 PYTHON_ABI="$(PYTHON -f --ABI)"
1174 elif [[ -z "${PYTHON_ABI}" ]]; then 1118 elif [[ -z "${PYTHON_ABI}" ]]; then
1175 PYTHON_ABI="$(PYTHON -A --ABI)" 1119 PYTHON_ABI="$(PYTHON --ABI)"
1176 fi 1120 fi
1177 1121
1178 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1122 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1179 echo "dev-lang/python:${PYTHON_ABI}" 1123 echo "dev-lang/python:${PYTHON_ABI}"
1180 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1124 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1206 done 1150 done
1207 1151
1208 if [[ "${final_ABI}" == "1" ]]; then 1152 if [[ "${final_ABI}" == "1" ]]; then
1209 PYTHON_ABI="$(PYTHON -f --ABI)" 1153 PYTHON_ABI="$(PYTHON -f --ABI)"
1210 elif [[ -z "${PYTHON_ABI}" ]]; then 1154 elif [[ -z "${PYTHON_ABI}" ]]; then
1211 PYTHON_ABI="$(PYTHON -A --ABI)" 1155 PYTHON_ABI="$(PYTHON --ABI)"
1212 fi 1156 fi
1213 1157
1214 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1158 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1215 echo "/usr/include/python${PYTHON_ABI}" 1159 echo "/usr/include/python${PYTHON_ABI}"
1216 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1160 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1242 done 1186 done
1243 1187
1244 if [[ "${final_ABI}" == "1" ]]; then 1188 if [[ "${final_ABI}" == "1" ]]; then
1245 PYTHON_ABI="$(PYTHON -f --ABI)" 1189 PYTHON_ABI="$(PYTHON -f --ABI)"
1246 elif [[ -z "${PYTHON_ABI}" ]]; then 1190 elif [[ -z "${PYTHON_ABI}" ]]; then
1247 PYTHON_ABI="$(PYTHON -A --ABI)" 1191 PYTHON_ABI="$(PYTHON --ABI)"
1248 fi 1192 fi
1249 1193
1250 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1194 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1251 echo "/usr/$(get_libdir)/python${PYTHON_ABI}" 1195 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
1252 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1196 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1308 done 1252 done
1309 1253
1310 if [[ "${final_ABI}" == "1" ]]; then 1254 if [[ "${final_ABI}" == "1" ]]; then
1311 PYTHON_ABI="$(PYTHON -f --ABI)" 1255 PYTHON_ABI="$(PYTHON -f --ABI)"
1312 elif [[ -z "${PYTHON_ABI}" ]]; then 1256 elif [[ -z "${PYTHON_ABI}" ]]; then
1313 PYTHON_ABI="$(PYTHON -A --ABI)" 1257 PYTHON_ABI="$(PYTHON --ABI)"
1314 fi 1258 fi
1315 1259
1316 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1260 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1317 if [[ "${linker_option}" == "1" ]]; then 1261 if [[ "${linker_option}" == "1" ]]; then
1318 echo "-lpython${PYTHON_ABI}" 1262 echo "-lpython${PYTHON_ABI}"
1323 die "${FUNCNAME}(): Jython does not have shared library" 1267 die "${FUNCNAME}(): Jython does not have shared library"
1324 fi 1268 fi
1325} 1269}
1326 1270
1327# @FUNCTION: python_get_version 1271# @FUNCTION: python_get_version
1328# @USAGE: [-f|--final-ABI] [--major] [--minor] [--micro] 1272# @USAGE: [-f|--final-ABI] [--full] [--major] [--minor] [--micro]
1329# @DESCRIPTION: 1273# @DESCRIPTION:
1330# Print Python version. 1274# Print Python version.
1331# --major, --minor and --micro options cannot be specified simultaneously. 1275# --full, --major, --minor and --micro options cannot be specified simultaneously.
1332# If --major, --minor and --micro options are not specified, then "${major_version}.${minor_version}" is printed. 1276# If --full, --major, --minor and --micro options are not specified, then "${major_version}.${minor_version}" is printed.
1333# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used. 1277# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1334python_get_version() { 1278python_get_version() {
1335 local final_ABI="0" major="0" minor="0" micro="0" python_command 1279 local final_ABI="0" full="0" major="0" minor="0" micro="0" python_command
1336 1280
1337 while (($#)); do 1281 while (($#)); do
1338 case "$1" in 1282 case "$1" in
1339 -f|--final-ABI) 1283 -f|--final-ABI)
1340 final_ABI="1" 1284 final_ABI="1"
1341 ;; 1285 ;;
1286 --full)
1287 full="1"
1288 ;;
1342 --major) 1289 --major)
1343 major="1" 1290 major="1"
1344 ;; 1291 ;;
1345 --minor) 1292 --minor)
1346 minor="1" 1293 minor="1"
1356 ;; 1303 ;;
1357 esac 1304 esac
1358 shift 1305 shift
1359 done 1306 done
1360 1307
1361 if [[ "$((${major} + ${minor} + ${micro}))" -gt 1 ]]; then 1308 if [[ "$((${full} + ${major} + ${minor} + ${micro}))" -gt 1 ]]; then
1362 die "${FUNCNAME}(): '--major', '--minor' or '--micro' options cannot be specified simultaneously" 1309 die "${FUNCNAME}(): '--full', '--major', '--minor' or '--micro' options cannot be specified simultaneously"
1363 fi 1310 fi
1364 1311
1312 if [[ "${full}" == "1" ]]; then
1313 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:3]))"
1365 if [[ "${major}" == "1" ]]; then 1314 elif [[ "${major}" == "1" ]]; then
1366 python_command="from sys import version_info; print(version_info[0])" 1315 python_command="from sys import version_info; print(version_info[0])"
1367 elif [[ "${minor}" == "1" ]]; then 1316 elif [[ "${minor}" == "1" ]]; then
1368 python_command="from sys import version_info; print(version_info[1])" 1317 python_command="from sys import version_info; print(version_info[1])"
1369 elif [[ "${micro}" == "1" ]]; then 1318 elif [[ "${micro}" == "1" ]]; then
1370 python_command="from sys import version_info; print(version_info[2])" 1319 python_command="from sys import version_info; print(version_info[2])"
1371 else 1320 else
1321 if [[ -n "${PYTHON_ABI}" && "${final_ABI}" == "0" ]]; then
1322 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1323 echo "${PYTHON_ABI}"
1324 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1325 echo "${PYTHON_ABI%-jython}"
1326 fi
1327 return
1328 fi
1372 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:2]))" 1329 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:2]))"
1373 fi 1330 fi
1374 1331
1375 if [[ "${final_ABI}" == "1" ]]; then 1332 if [[ "${final_ABI}" == "1" ]]; then
1376 "$(PYTHON -f)" -c "${python_command}" 1333 "$(PYTHON -f)" -c "${python_command}"
1377 else 1334 else
1378 "$(PYTHON "${PYTHON_ABI--A}")" -c "${python_command}" 1335 "$(PYTHON ${PYTHON_ABI})" -c "${python_command}"
1379 fi 1336 fi
1380} 1337}
1381 1338
1382# ================================================================================================ 1339# ================================================================================================
1383# =================================== MISCELLANEOUS FUNCTIONS ==================================== 1340# =================================== MISCELLANEOUS FUNCTIONS ====================================
1399 EROOT="${ROOT%/}${EPREFIX}/" 1356 EROOT="${ROOT%/}${EPREFIX}/"
1400 fi 1357 fi
1401 if [[ -n "${D}" && -z "${ED}" ]]; then 1358 if [[ -n "${D}" && -z "${ED}" ]]; then
1402 ED="${D%/}${EPREFIX}/" 1359 ED="${D%/}${EPREFIX}/"
1403 fi 1360 fi
1361 fi
1362}
1363
1364unset PYTHON_SANITY_CHECKS
1365
1366_python_initial_sanity_checks() {
1367 if [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1368 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
1369 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
1370 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
1371 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1372 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
1373 fi
1374 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
1375 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
1376 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1377 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
1378 fi
1379 fi
1380}
1381
1382_python_final_sanity_checks() {
1383 if ! _python_implementation && [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1384 local PYTHON_ABI="${PYTHON_ABI}"
1385 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI}}; do
1386 # Ensure that appropriate version of Python is installed.
1387 if ! has_version "$(python_get_implementational_package)"; then
1388 die "$(python_get_implementational_package) is not installed"
1389 fi
1390
1391 # Ensure that EPYTHON variable is respected.
1392 if [[ "$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" != "${PYTHON_ABI}" ]]; then
1393 eerror "python: '$(type -p python)'"
1394 eerror "ABI: '${ABI}'"
1395 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
1396 eerror "EPYTHON: '$(PYTHON)'"
1397 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
1398 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")'"
1399 die "'python' does not respect EPYTHON variable"
1400 fi
1401 done
1402 fi
1403 PYTHON_SANITY_CHECKS="1"
1404}
1405
1406_python_set_color_variables() {
1407 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1408 _BOLD=$'\e[1m'
1409 _RED=$'\e[1;31m'
1410 _GREEN=$'\e[1;32m'
1411 _BLUE=$'\e[1;34m'
1412 _CYAN=$'\e[1;36m'
1413 _NORMAL=$'\e[0m'
1414 else
1415 _BOLD=
1416 _RED=
1417 _GREEN=
1418 _BLUE=
1419 _CYAN=
1420 _NORMAL=
1404 fi 1421 fi
1405} 1422}
1406 1423
1407# @FUNCTION: python_convert_shebangs 1424# @FUNCTION: python_convert_shebangs
1408# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories] 1425# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1479 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed" 1496 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1480 fi 1497 fi
1481 done 1498 done
1482} 1499}
1483 1500
1484# @FUNCTION: python_mod_exists
1485# @USAGE: <module>
1486# @DESCRIPTION:
1487# Run with the module name as an argument. It will check if a
1488# Python module is installed and loadable. It will return
1489# TRUE(0) if the module exists, and FALSE(1) if the module does
1490# not exist.
1491#
1492# Example:
1493# if python_mod_exists gtk; then
1494# echo "gtk support enabled"
1495# fi
1496python_mod_exists() {
1497 if [[ "$#" -ne 1 ]]; then
1498 die "${FUNCNAME}() requires 1 argument"
1499 fi
1500 "$(PYTHON "${PYTHON_ABI--A}")" -c "import $1" &> /dev/null
1501}
1502
1503# @FUNCTION: python_tkinter_exists
1504# @DESCRIPTION:
1505# Run without arguments, checks if Python was compiled with Tkinter
1506# support. If not, prints an error message and dies.
1507python_tkinter_exists() {
1508 if ! "$(PYTHON "${PYTHON_ABI--A}")" -c "from sys import version_info
1509if version_info[0] == 3:
1510 import tkinter
1511else:
1512 import Tkinter" &> /dev/null; then
1513 eerror "Python needs to be rebuilt with tkinter support enabled."
1514 eerror "Add the following line to '${EPREFIX}/etc/portage/package.use' and rebuild Python"
1515 eerror "dev-lang/python tk"
1516 die "Python installed without support for tkinter"
1517 fi
1518}
1519
1520# ================================================================================================ 1501# ================================================================================================
1521# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================ 1502# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1522# ================================================================================================ 1503# ================================================================================================
1523 1504
1524# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY 1505# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
1525# @DESCRIPTION: 1506# @DESCRIPTION:
1526# User-configurable verbosity of tests of Python modules. 1507# User-configurable verbosity of tests of Python modules.
1527# Supported values: 0, 1, 2, 3, 4. 1508# Supported values: 0, 1, 2, 3, 4.
1528PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}" 1509PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
1529 1510
1511_python_test_hook() {
1512 if [[ "$#" -ne 1 ]]; then
1513 die "${FUNCNAME}() requires 1 argument"
1514 fi
1515
1516 if [[ -n "${SUPPORT_PYTHON_ABIS}" && "$(type -t "${FUNCNAME[3]}_$1_hook")" == "function" ]]; then
1517 "${FUNCNAME[3]}_$1_hook"
1518 fi
1519}
1520
1530# @FUNCTION: python_execute_nosetests 1521# @FUNCTION: python_execute_nosetests
1531# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments] 1522# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1532# @DESCRIPTION: 1523# @DESCRIPTION:
1533# Execute nosetests for all enabled versions of Python. 1524# Execute nosetests for all enabled versions of Python.
1525# In ebuilds of packages supporting installation for multiple versions of Python, this function
1526# calls python_execute_nosetests_pre_hook() and python_execute_nosetests_post_hook(), if they are defined.
1534python_execute_nosetests() { 1527python_execute_nosetests() {
1528 _python_set_color_variables
1529
1535 local PYTHONPATH_template= separate_build_dirs= 1530 local PYTHONPATH_template= separate_build_dirs=
1536 1531
1537 while (($#)); do 1532 while (($#)); do
1538 case "$1" in 1533 case "$1" in
1539 -P|--PYTHONPATH) 1534 -P|--PYTHONPATH)
1565 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then 1560 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1566 unset evaluated_PYTHONPATH 1561 unset evaluated_PYTHONPATH
1567 fi 1562 fi
1568 fi 1563 fi
1569 1564
1565 _python_test_hook pre
1566
1570 if [[ -n "${evaluated_PYTHONPATH}" ]]; then 1567 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1571 echo PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1568 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
1572 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1569 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
1573 else 1570 else
1574 echo nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1571 echo ${_BOLD}nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
1575 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1572 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
1576 fi 1573 fi
1574
1575 _python_test_hook post
1577 } 1576 }
1578 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1577 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1579 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@" 1578 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1580 else 1579 else
1581 if [[ -n "${separate_build_dirs}" ]]; then 1580 if [[ -n "${separate_build_dirs}" ]]; then
1589 1588
1590# @FUNCTION: python_execute_py.test 1589# @FUNCTION: python_execute_py.test
1591# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments] 1590# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1592# @DESCRIPTION: 1591# @DESCRIPTION:
1593# Execute py.test for all enabled versions of Python. 1592# Execute py.test for all enabled versions of Python.
1593# In ebuilds of packages supporting installation for multiple versions of Python, this function
1594# calls python_execute_py.test_pre_hook() and python_execute_py.test_post_hook(), if they are defined.
1594python_execute_py.test() { 1595python_execute_py.test() {
1596 _python_set_color_variables
1597
1595 local PYTHONPATH_template= separate_build_dirs= 1598 local PYTHONPATH_template= separate_build_dirs=
1596 1599
1597 while (($#)); do 1600 while (($#)); do
1598 case "$1" in 1601 case "$1" in
1599 -P|--PYTHONPATH) 1602 -P|--PYTHONPATH)
1625 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then 1628 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1626 unset evaluated_PYTHONPATH 1629 unset evaluated_PYTHONPATH
1627 fi 1630 fi
1628 fi 1631 fi
1629 1632
1633 _python_test_hook pre
1634
1630 if [[ -n "${evaluated_PYTHONPATH}" ]]; then 1635 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1631 echo PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@" 1636 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"${_NORMAL}
1632 PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@" 1637 PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@" || return "$?"
1633 else 1638 else
1634 echo py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" 1639 echo ${_BOLD}py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"${_NORMAL}
1635 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" 1640 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" || return "$?"
1636 fi 1641 fi
1642
1643 _python_test_hook post
1637 } 1644 }
1638 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1645 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1639 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@" 1646 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1640 else 1647 else
1641 if [[ -n "${separate_build_dirs}" ]]; then 1648 if [[ -n "${separate_build_dirs}" ]]; then
1649 1656
1650# @FUNCTION: python_execute_trial 1657# @FUNCTION: python_execute_trial
1651# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments] 1658# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1652# @DESCRIPTION: 1659# @DESCRIPTION:
1653# Execute trial for all enabled versions of Python. 1660# Execute trial for all enabled versions of Python.
1661# In ebuilds of packages supporting installation for multiple versions of Python, this function
1662# calls python_execute_trial_pre_hook() and python_execute_trial_post_hook(), if they are defined.
1654python_execute_trial() { 1663python_execute_trial() {
1664 _python_set_color_variables
1665
1655 local PYTHONPATH_template= separate_build_dirs= 1666 local PYTHONPATH_template= separate_build_dirs=
1656 1667
1657 while (($#)); do 1668 while (($#)); do
1658 case "$1" in 1669 case "$1" in
1659 -P|--PYTHONPATH) 1670 -P|--PYTHONPATH)
1685 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then 1696 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1686 unset evaluated_PYTHONPATH 1697 unset evaluated_PYTHONPATH
1687 fi 1698 fi
1688 fi 1699 fi
1689 1700
1701 _python_test_hook pre
1702
1690 if [[ -n "${evaluated_PYTHONPATH}" ]]; then 1703 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1691 echo PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1704 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
1692 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1705 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
1693 else 1706 else
1694 echo trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1707 echo ${_BOLD}trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
1695 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1708 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
1696 fi 1709 fi
1710
1711 _python_test_hook post
1697 } 1712 }
1698 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1713 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1699 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@" 1714 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1700 else 1715 else
1701 if [[ -n "${separate_build_dirs}" ]]; then 1716 if [[ -n "${separate_build_dirs}" ]]; then
1734# If no arguments supplied, it will recompile not recursively all modules 1749# If no arguments supplied, it will recompile not recursively all modules
1735# under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages). 1750# under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages).
1736# 1751#
1737# If supplied with arguments, it will recompile all modules recursively 1752# If supplied with arguments, it will recompile all modules recursively
1738# in the supplied directory. 1753# in the supplied directory.
1739# This function should only be run in pkg_postinst().
1740# 1754#
1741# Options passed to this function are passed to compileall.py. 1755# Options passed to this function are passed to compileall.py.
1742# 1756#
1743# Example: 1757# This function can be used only in pkg_postinst() phase.
1744# python_mod_optimize ctypesgencore
1745python_mod_optimize() { 1758python_mod_optimize() {
1746 _python_initialize_prefix_variables 1759 _python_initialize_prefix_variables
1747 1760
1748 # Check if phase is pkg_postinst(). 1761 # Check if phase is pkg_postinst().
1749 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME}() should only be run in pkg_postinst()" 1762 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase"
1750 1763
1751 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1764 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1752 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=() 1765 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI="${PYTHON_ABI}" return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
1753 1766
1754 # Strip trailing slash from ROOT. 1767 # Strip trailing slash from ROOT.
1755 root="${EROOT%/}" 1768 root="${EROOT%/}"
1756 1769
1757 # Respect ROOT and options passed to compileall.py. 1770 # Respect ROOT and options passed to compileall.py.
1779 ewarn "'${root}/$1' is not a file or a directory!" 1792 ewarn "'${root}/$1' is not a file or a directory!"
1780 else 1793 else
1781 ewarn "'${root}/$1' does not exist!" 1794 ewarn "'${root}/$1' does not exist!"
1782 fi 1795 fi
1783 else 1796 else
1784 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1797 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
1785 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then 1798 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
1786 site_packages_dirs+=("$1") 1799 site_packages_dirs+=("$1")
1787 break 1800 break
1788 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then 1801 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
1789 site_packages_files+=("$1") 1802 site_packages_files+=("$1")
1801 done 1814 done
1802 1815
1803 # Set additional options. 1816 # Set additional options.
1804 options+=("-q") 1817 options+=("-q")
1805 1818
1806 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1819 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
1807 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 1820 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
1808 return_code="0" 1821 return_code="0"
1809 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)" 1822 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
1810 if ((${#site_packages_dirs[@]})); then 1823 if ((${#site_packages_dirs[@]})); then
1811 for dir in "${site_packages_dirs[@]}"; do 1824 for dir in "${site_packages_dirs[@]}"; do
1837 unset PYTHON_ABI 1850 unset PYTHON_ABI
1838 fi 1851 fi
1839 1852
1840 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1853 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1841 return_code="0" 1854 return_code="0"
1842 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)" 1855 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation) $(python_get_version)"
1843 if ((${#other_dirs[@]})); then 1856 if ((${#other_dirs[@]})); then
1844 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 1857 "$(PYTHON ${PYTHON_ABI})" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
1845 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then 1858 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
1846 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" 1859 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
1847 fi 1860 fi
1848 fi 1861 fi
1849 if ((${#other_files[@]})); then 1862 if ((${#other_files[@]})); then
1850 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 1863 "$(PYTHON ${PYTHON_ABI})" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
1851 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then 1864 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
1852 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1" 1865 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
1853 fi 1866 fi
1854 fi 1867 fi
1855 eend "${return_code}" 1868 eend "${return_code}"
1856 fi 1869 fi
1857 else 1870 else
1890 done 1903 done
1891 1904
1892 # set additional opts 1905 # set additional opts
1893 myopts+=(-q) 1906 myopts+=(-q)
1894 1907
1895 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)" 1908 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
1896 if ((${#mydirs[@]})); then 1909 if ((${#mydirs[@]})); then
1897 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1" 1910 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
1898 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1" 1911 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
1899 fi 1912 fi
1900 1913
1901 if ((${#myfiles[@]})); then 1914 if ((${#myfiles[@]})); then
1902 python_mod_compile "${myfiles[@]}" 1915 python_mod_compile "${myfiles[@]}"
1903 fi 1916 fi
1914# 1927#
1915# It will recursively scan all compiled Python modules in the directories and 1928# It will recursively scan all compiled Python modules in the directories and
1916# determine if they are orphaned (i.e. their corresponding .py files are missing.) 1929# determine if they are orphaned (i.e. their corresponding .py files are missing.)
1917# If they are, then it will remove their corresponding .pyc and .pyo files. 1930# If they are, then it will remove their corresponding .pyc and .pyo files.
1918# 1931#
1919# This function should only be run in pkg_postrm(). 1932# This function can be used only in pkg_postrm() phase.
1920python_mod_cleanup() { 1933python_mod_cleanup() {
1921 _python_initialize_prefix_variables 1934 _python_initialize_prefix_variables
1935 _python_set_color_variables
1922 1936
1923 local path py_file PYTHON_ABI SEARCH_PATH=() root 1937 local path py_file PYTHON_ABI="${PYTHON_ABI}" SEARCH_PATH=() root
1924 1938
1925 # Check if phase is pkg_postrm(). 1939 # Check if phase is pkg_postrm().
1926 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME}() should only be run in pkg_postrm()" 1940 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME}() can be used only in pkg_postrm() phase"
1927 1941
1928 # Strip trailing slash from ROOT. 1942 # Strip trailing slash from ROOT.
1929 root="${EROOT%/}" 1943 root="${EROOT%/}"
1930 1944
1931 if (($#)); then 1945 if (($#)); then
1934 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1948 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1935 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories" 1949 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1936 elif [[ "$1" =~ ^/ ]]; then 1950 elif [[ "$1" =~ ^/ ]]; then
1937 SEARCH_PATH+=("${root}/${1#/}") 1951 SEARCH_PATH+=("${root}/${1#/}")
1938 else 1952 else
1939 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1953 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
1940 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 1954 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
1941 done 1955 done
1942 fi 1956 fi
1943 shift 1957 shift
1944 done 1958 done
1962 SEARCH_PATH+=("${sitedir}") 1976 SEARCH_PATH+=("${sitedir}")
1963 fi 1977 fi
1964 done 1978 done
1965 fi 1979 fi
1966 1980
1967 local BLUE CYAN NORMAL
1968 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1969 BLUE=$'\e[1;34m'
1970 CYAN=$'\e[1;36m'
1971 NORMAL=$'\e[0m'
1972 else
1973 BLUE=
1974 CYAN=
1975 NORMAL=
1976 fi
1977
1978 for path in "${SEARCH_PATH[@]}"; do 1981 for path in "${SEARCH_PATH[@]}"; do
1979 if [[ -d "${path}" ]]; then 1982 if [[ -d "${path}" ]]; then
1980 find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0 | while read -rd ''; do 1983 find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0 | while read -rd ''; do
1981 if [[ "${REPLY}" == *[co] ]]; then 1984 if [[ "${REPLY}" == *[co] ]]; then
1982 py_file="${REPLY%[co]}" 1985 py_file="${REPLY%[co]}"
1983 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue 1986 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
1984 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}" 1987 einfo "${_BLUE}<<< ${py_file}[co]${_NORMAL}"
1985 rm -f "${py_file}"[co] 1988 rm -f "${py_file}"[co]
1986 elif [[ "${REPLY}" == *\$py.class ]]; then 1989 elif [[ "${REPLY}" == *\$py.class ]]; then
1987 py_file="${REPLY%\$py.class}.py" 1990 py_file="${REPLY%\$py.class}.py"
1988 [[ -f "${py_file}" || ! -f "${py_file%.py}\$py.class" ]] && continue 1991 [[ -f "${py_file}" || ! -f "${py_file%.py}\$py.class" ]] && continue
1989 einfo "${BLUE}<<< ${py_file%.py}\$py.class${NORMAL}" 1992 einfo "${_BLUE}<<< ${py_file%.py}\$py.class${_NORMAL}"
1990 rm -f "${py_file%.py}\$py.class" 1993 rm -f "${py_file%.py}\$py.class"
1991 fi 1994 fi
1992 done 1995 done
1993 1996
1994 # Attempt to delete directories, which may be empty. 1997 # Attempt to delete directories, which may be empty.
1995 find "${path}" -type d | sort -r | while read -r dir; do 1998 find "${path}" -type d | sort -r | while read -r dir; do
1996 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}" 1999 rmdir "${dir}" 2>/dev/null && einfo "${_CYAN}<<< ${dir}${_NORMAL}"
1997 done 2000 done
1998 elif [[ "${path}" == *.py && ! -f "${path}" ]]; then 2001 elif [[ "${path}" == *.py && ! -f "${path}" ]]; then
1999 if [[ (-f "${path}c" || -f "${path}o") ]]; then 2002 if [[ (-f "${path}c" || -f "${path}o") ]]; then
2000 einfo "${BLUE}<<< ${path}[co]${NORMAL}" 2003 einfo "${_BLUE}<<< ${path}[co]${_NORMAL}"
2001 rm -f "${path}"[co] 2004 rm -f "${path}"[co]
2002 fi 2005 fi
2003 if [[ -f "${path%.py}\$py.class" ]]; then 2006 if [[ -f "${path%.py}\$py.class" ]]; then
2004 einfo "${BLUE}<<< ${path%.py}\$py.class${NORMAL}" 2007 einfo "${_BLUE}<<< ${path%.py}\$py.class${_NORMAL}"
2005 rm -f "${path%.py}\$py.class" 2008 rm -f "${path%.py}\$py.class"
2006 fi 2009 fi
2007 fi 2010 fi
2008 done 2011 done
2009} 2012}
2015# @FUNCTION: python_version 2018# @FUNCTION: python_version
2016# @DESCRIPTION: 2019# @DESCRIPTION:
2017# Run without arguments and it will export the version of python 2020# Run without arguments and it will export the version of python
2018# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 2021# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
2019python_version() { 2022python_version() {
2020 if ! has "${EAPI:-0}" 0 1 2; then 2023 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
2021 eerror "Use PYTHON() and/or python_get_*() instead of ${FUNCNAME}()." 2024 eerror "Use PYTHON() and/or python_get_*() instead of ${FUNCNAME}()."
2022 die "${FUNCNAME}() cannot be used in this EAPI" 2025 die "${FUNCNAME}() cannot be used in this EAPI"
2026 fi
2027
2028 if [[ "${FUNCNAME[1]}" != "distutils_python_version" ]]; then
2029 einfo
2030 einfo "Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01."
2031 einfo "Use PYTHON() instead of python variable. Use python_get_*() instead of PYVER* variables."
2032 einfo
2023 fi 2033 fi
2024 2034
2025 [[ -n "${PYVER}" ]] && return 0 2035 [[ -n "${PYVER}" ]] && return 0
2026 local tmpstr 2036 local tmpstr
2027 python="${python:-${EPREFIX}/usr/bin/python}" 2037 python="${python:-${EPREFIX}/usr/bin/python}"
2033 export PYVER_MICRO="${PYVER_ALL:4}" 2043 export PYVER_MICRO="${PYVER_ALL:4}"
2034 fi 2044 fi
2035 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}" 2045 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
2036} 2046}
2037 2047
2048# @FUNCTION: python_mod_exists
2049# @USAGE: <module>
2050# @DESCRIPTION:
2051# Run with the module name as an argument. It will check if a
2052# Python module is installed and loadable. It will return
2053# TRUE(0) if the module exists, and FALSE(1) if the module does
2054# not exist.
2055#
2056# Example:
2057# if python_mod_exists gtk; then
2058# echo "gtk support enabled"
2059# fi
2060python_mod_exists() {
2061 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
2062 eerror "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()."
2063 die "${FUNCNAME}() cannot be used in this EAPI"
2064 fi
2065
2066 einfo
2067 einfo "Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01."
2068 einfo "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()."
2069 einfo
2070
2071 if [[ "$#" -ne 1 ]]; then
2072 die "${FUNCNAME}() requires 1 argument"
2073 fi
2074 "$(PYTHON ${PYTHON_ABI})" -c "import $1" &> /dev/null
2075}
2076
2077# @FUNCTION: python_tkinter_exists
2078# @DESCRIPTION:
2079# Run without arguments, checks if Python was compiled with Tkinter
2080# support. If not, prints an error message and dies.
2081python_tkinter_exists() {
2082 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
2083 eerror "Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}()."
2084 die "${FUNCNAME}() cannot be used in this EAPI"
2085 fi
2086
2087 if [[ "${FUNCNAME[1]}" != "distutils_python_tkinter" ]]; then
2088 einfo
2089 einfo "Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01."
2090 einfo "Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}()."
2091 einfo
2092 fi
2093
2094 if ! "$(PYTHON ${PYTHON_ABI})" -c "from sys import version_info
2095if version_info[0] == 3:
2096 import tkinter
2097else:
2098 import Tkinter" &> /dev/null; then
2099 eerror "Python needs to be rebuilt with tkinter support enabled."
2100 eerror "Add the following line to '${EPREFIX}/etc/portage/package.use' and rebuild Python"
2101 eerror "dev-lang/python tk"
2102 die "Python installed without support for tkinter"
2103 fi
2104}
2105
2038# @FUNCTION: python_mod_compile 2106# @FUNCTION: python_mod_compile
2039# @USAGE: <file> [more files ...] 2107# @USAGE: <file> [more files ...]
2040# @DESCRIPTION: 2108# @DESCRIPTION:
2041# Given filenames, it will pre-compile the module's .pyc and .pyo. 2109# Given filenames, it will pre-compile the module's .pyc and .pyo.
2042# This function should only be run in pkg_postinst() 2110# This function can be used only in pkg_postinst() phase.
2043# 2111#
2044# Example: 2112# Example:
2045# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 2113# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
2046# 2114#
2047python_mod_compile() { 2115python_mod_compile() {
2053 _python_initialize_prefix_variables 2121 _python_initialize_prefix_variables
2054 2122
2055 local f myroot myfiles=() 2123 local f myroot myfiles=()
2056 2124
2057 # Check if phase is pkg_postinst() 2125 # Check if phase is pkg_postinst()
2058 [[ ${EBUILD_PHASE} != postinst ]] && die "${FUNCNAME}() should only be run in pkg_postinst()" 2126 [[ ${EBUILD_PHASE} != postinst ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase"
2059 2127
2060 # strip trailing slash 2128 # strip trailing slash
2061 myroot="${EROOT%/}" 2129 myroot="${EROOT%/}"
2062 2130
2063 # respect ROOT 2131 # respect ROOT
2064 for f in "$@"; do 2132 for f in "$@"; do
2065 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") 2133 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
2066 done 2134 done
2067 2135
2068 if ((${#myfiles[@]})); then 2136 if ((${#myfiles[@]})); then
2069 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" 2137 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
2070 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null 2138 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
2071 else 2139 else
2072 ewarn "No files to compile!" 2140 ewarn "No files to compile!"
2073 fi 2141 fi
2074} 2142}

Legend:
Removed from v.1.88  
changed lines
  Added in v.1.93

  ViewVC Help
Powered by ViewVC 1.1.20