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

Diff of /eclass/python.eclass

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

Revision 1.89 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.89 2010/02/14 18:53:11 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
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
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##* }"
1065 if [[ -z "${PYTHON_ABI}" ]]; then 1014 if [[ -z "${PYTHON_ABI}" ]]; then
1066 die "${FUNCNAME}(): Active Python 3 interpreter not set" 1015 die "${FUNCNAME}(): Active Python 3 interpreter not set"
1067 elif [[ "${PYTHON_ABI}" != "3."* ]]; then 1016 elif [[ "${PYTHON_ABI}" != "3."* ]]; then
1068 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`" 1017 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1069 fi 1018 fi
1019 elif [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
1020 PYTHON_ABI="$("${EPREFIX}/usr/bin/python" -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")"
1070 elif [[ -z "${PYTHON_ABI}" ]]; then 1021 elif [[ -z "${PYTHON_ABI}" ]]; then
1071 die "${FUNCNAME}(): Invalid usage: Python ABI not specified" 1022 die "${FUNCNAME}(): Invalid usage: Python ABI not specified"
1072 fi 1023 fi
1073 elif [[ "$#" -eq 1 ]]; then 1024 elif [[ "$#" -eq 1 ]]; then
1074 if [[ "${active}" == "1" ]]; then
1075 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
1076 fi
1077 if [[ "${final_ABI}" == "1" ]]; then 1025 if [[ "${final_ABI}" == "1" ]]; then
1078 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"
1079 fi 1027 fi
1080 if [[ "${python2}" == "1" ]]; then 1028 if [[ "${python2}" == "1" ]]; then
1081 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously" 1029 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
1134 done 1082 done
1135 1083
1136 if [[ "${final_ABI}" == "1" ]]; then 1084 if [[ "${final_ABI}" == "1" ]]; then
1137 PYTHON_ABI="$(PYTHON -f --ABI)" 1085 PYTHON_ABI="$(PYTHON -f --ABI)"
1138 elif [[ -z "${PYTHON_ABI}" ]]; then 1086 elif [[ -z "${PYTHON_ABI}" ]]; then
1139 PYTHON_ABI="$(PYTHON -A --ABI)" 1087 PYTHON_ABI="$(PYTHON --ABI)"
1140 fi 1088 fi
1141 1089
1142 echo "$(_python_get_implementation "${PYTHON_ABI}")" 1090 echo "$(_python_get_implementation "${PYTHON_ABI}")"
1143} 1091}
1144 1092
1166 done 1114 done
1167 1115
1168 if [[ "${final_ABI}" == "1" ]]; then 1116 if [[ "${final_ABI}" == "1" ]]; then
1169 PYTHON_ABI="$(PYTHON -f --ABI)" 1117 PYTHON_ABI="$(PYTHON -f --ABI)"
1170 elif [[ -z "${PYTHON_ABI}" ]]; then 1118 elif [[ -z "${PYTHON_ABI}" ]]; then
1171 PYTHON_ABI="$(PYTHON -A --ABI)" 1119 PYTHON_ABI="$(PYTHON --ABI)"
1172 fi 1120 fi
1173 1121
1174 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1122 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1175 echo "dev-lang/python:${PYTHON_ABI}" 1123 echo "dev-lang/python:${PYTHON_ABI}"
1176 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1124 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1202 done 1150 done
1203 1151
1204 if [[ "${final_ABI}" == "1" ]]; then 1152 if [[ "${final_ABI}" == "1" ]]; then
1205 PYTHON_ABI="$(PYTHON -f --ABI)" 1153 PYTHON_ABI="$(PYTHON -f --ABI)"
1206 elif [[ -z "${PYTHON_ABI}" ]]; then 1154 elif [[ -z "${PYTHON_ABI}" ]]; then
1207 PYTHON_ABI="$(PYTHON -A --ABI)" 1155 PYTHON_ABI="$(PYTHON --ABI)"
1208 fi 1156 fi
1209 1157
1210 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1158 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1211 echo "/usr/include/python${PYTHON_ABI}" 1159 echo "/usr/include/python${PYTHON_ABI}"
1212 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1160 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1238 done 1186 done
1239 1187
1240 if [[ "${final_ABI}" == "1" ]]; then 1188 if [[ "${final_ABI}" == "1" ]]; then
1241 PYTHON_ABI="$(PYTHON -f --ABI)" 1189 PYTHON_ABI="$(PYTHON -f --ABI)"
1242 elif [[ -z "${PYTHON_ABI}" ]]; then 1190 elif [[ -z "${PYTHON_ABI}" ]]; then
1243 PYTHON_ABI="$(PYTHON -A --ABI)" 1191 PYTHON_ABI="$(PYTHON --ABI)"
1244 fi 1192 fi
1245 1193
1246 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1194 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1247 echo "/usr/$(get_libdir)/python${PYTHON_ABI}" 1195 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
1248 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1196 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1304 done 1252 done
1305 1253
1306 if [[ "${final_ABI}" == "1" ]]; then 1254 if [[ "${final_ABI}" == "1" ]]; then
1307 PYTHON_ABI="$(PYTHON -f --ABI)" 1255 PYTHON_ABI="$(PYTHON -f --ABI)"
1308 elif [[ -z "${PYTHON_ABI}" ]]; then 1256 elif [[ -z "${PYTHON_ABI}" ]]; then
1309 PYTHON_ABI="$(PYTHON -A --ABI)" 1257 PYTHON_ABI="$(PYTHON --ABI)"
1310 fi 1258 fi
1311 1259
1312 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1260 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1313 if [[ "${linker_option}" == "1" ]]; then 1261 if [[ "${linker_option}" == "1" ]]; then
1314 echo "-lpython${PYTHON_ABI}" 1262 echo "-lpython${PYTHON_ABI}"
1319 die "${FUNCNAME}(): Jython does not have shared library" 1267 die "${FUNCNAME}(): Jython does not have shared library"
1320 fi 1268 fi
1321} 1269}
1322 1270
1323# @FUNCTION: python_get_version 1271# @FUNCTION: python_get_version
1324# @USAGE: [-f|--final-ABI] [--major] [--minor] [--micro] 1272# @USAGE: [-f|--final-ABI] [--full] [--major] [--minor] [--micro]
1325# @DESCRIPTION: 1273# @DESCRIPTION:
1326# Print Python version. 1274# Print Python version.
1327# --major, --minor and --micro options cannot be specified simultaneously. 1275# --full, --major, --minor and --micro options cannot be specified simultaneously.
1328# 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.
1329# 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.
1330python_get_version() { 1278python_get_version() {
1331 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
1332 1280
1333 while (($#)); do 1281 while (($#)); do
1334 case "$1" in 1282 case "$1" in
1335 -f|--final-ABI) 1283 -f|--final-ABI)
1336 final_ABI="1" 1284 final_ABI="1"
1337 ;; 1285 ;;
1286 --full)
1287 full="1"
1288 ;;
1338 --major) 1289 --major)
1339 major="1" 1290 major="1"
1340 ;; 1291 ;;
1341 --minor) 1292 --minor)
1342 minor="1" 1293 minor="1"
1352 ;; 1303 ;;
1353 esac 1304 esac
1354 shift 1305 shift
1355 done 1306 done
1356 1307
1357 if [[ "$((${major} + ${minor} + ${micro}))" -gt 1 ]]; then 1308 if [[ "$((${full} + ${major} + ${minor} + ${micro}))" -gt 1 ]]; then
1358 die "${FUNCNAME}(): '--major', '--minor' or '--micro' options cannot be specified simultaneously" 1309 die "${FUNCNAME}(): '--full', '--major', '--minor' or '--micro' options cannot be specified simultaneously"
1359 fi 1310 fi
1360 1311
1312 if [[ "${full}" == "1" ]]; then
1313 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:3]))"
1361 if [[ "${major}" == "1" ]]; then 1314 elif [[ "${major}" == "1" ]]; then
1362 python_command="from sys import version_info; print(version_info[0])" 1315 python_command="from sys import version_info; print(version_info[0])"
1363 elif [[ "${minor}" == "1" ]]; then 1316 elif [[ "${minor}" == "1" ]]; then
1364 python_command="from sys import version_info; print(version_info[1])" 1317 python_command="from sys import version_info; print(version_info[1])"
1365 elif [[ "${micro}" == "1" ]]; then 1318 elif [[ "${micro}" == "1" ]]; then
1366 python_command="from sys import version_info; print(version_info[2])" 1319 python_command="from sys import version_info; print(version_info[2])"
1367 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
1368 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]))"
1369 fi 1330 fi
1370 1331
1371 if [[ "${final_ABI}" == "1" ]]; then 1332 if [[ "${final_ABI}" == "1" ]]; then
1372 "$(PYTHON -f)" -c "${python_command}" 1333 "$(PYTHON -f)" -c "${python_command}"
1373 else 1334 else
1374 "$(PYTHON "${PYTHON_ABI--A}")" -c "${python_command}" 1335 "$(PYTHON ${PYTHON_ABI})" -c "${python_command}"
1375 fi 1336 fi
1376} 1337}
1377 1338
1378# ================================================================================================ 1339# ================================================================================================
1379# =================================== MISCELLANEOUS FUNCTIONS ==================================== 1340# =================================== MISCELLANEOUS FUNCTIONS ====================================
1395 EROOT="${ROOT%/}${EPREFIX}/" 1356 EROOT="${ROOT%/}${EPREFIX}/"
1396 fi 1357 fi
1397 if [[ -n "${D}" && -z "${ED}" ]]; then 1358 if [[ -n "${D}" && -z "${ED}" ]]; then
1398 ED="${D%/}${EPREFIX}/" 1359 ED="${D%/}${EPREFIX}/"
1399 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=
1400 fi 1421 fi
1401} 1422}
1402 1423
1403# @FUNCTION: python_convert_shebangs 1424# @FUNCTION: python_convert_shebangs
1404# @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]
1475 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed" 1496 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1476 fi 1497 fi
1477 done 1498 done
1478} 1499}
1479 1500
1480# @FUNCTION: python_mod_exists
1481# @USAGE: <module>
1482# @DESCRIPTION:
1483# Run with the module name as an argument. It will check if a
1484# Python module is installed and loadable. It will return
1485# TRUE(0) if the module exists, and FALSE(1) if the module does
1486# not exist.
1487#
1488# Example:
1489# if python_mod_exists gtk; then
1490# echo "gtk support enabled"
1491# fi
1492python_mod_exists() {
1493 if [[ "$#" -ne 1 ]]; then
1494 die "${FUNCNAME}() requires 1 argument"
1495 fi
1496 "$(PYTHON "${PYTHON_ABI--A}")" -c "import $1" &> /dev/null
1497}
1498
1499# @FUNCTION: python_tkinter_exists
1500# @DESCRIPTION:
1501# Run without arguments, checks if Python was compiled with Tkinter
1502# support. If not, prints an error message and dies.
1503python_tkinter_exists() {
1504 if ! "$(PYTHON "${PYTHON_ABI--A}")" -c "from sys import version_info
1505if version_info[0] == 3:
1506 import tkinter
1507else:
1508 import Tkinter" &> /dev/null; then
1509 eerror "Python needs to be rebuilt with tkinter support enabled."
1510 eerror "Add the following line to '${EPREFIX}/etc/portage/package.use' and rebuild Python"
1511 eerror "dev-lang/python tk"
1512 die "Python installed without support for tkinter"
1513 fi
1514}
1515
1516# ================================================================================================ 1501# ================================================================================================
1517# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================ 1502# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1518# ================================================================================================ 1503# ================================================================================================
1519 1504
1520# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY 1505# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
1521# @DESCRIPTION: 1506# @DESCRIPTION:
1522# User-configurable verbosity of tests of Python modules. 1507# User-configurable verbosity of tests of Python modules.
1523# Supported values: 0, 1, 2, 3, 4. 1508# Supported values: 0, 1, 2, 3, 4.
1524PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}" 1509PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
1525 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
1526# @FUNCTION: python_execute_nosetests 1521# @FUNCTION: python_execute_nosetests
1527# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments] 1522# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1528# @DESCRIPTION: 1523# @DESCRIPTION:
1529# 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.
1530python_execute_nosetests() { 1527python_execute_nosetests() {
1528 _python_set_color_variables
1529
1531 local PYTHONPATH_template= separate_build_dirs= 1530 local PYTHONPATH_template= separate_build_dirs=
1532 1531
1533 while (($#)); do 1532 while (($#)); do
1534 case "$1" in 1533 case "$1" in
1535 -P|--PYTHONPATH) 1534 -P|--PYTHONPATH)
1561 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then 1560 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1562 unset evaluated_PYTHONPATH 1561 unset evaluated_PYTHONPATH
1563 fi 1562 fi
1564 fi 1563 fi
1565 1564
1565 _python_test_hook pre
1566
1566 if [[ -n "${evaluated_PYTHONPATH}" ]]; then 1567 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1567 echo PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1568 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
1568 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1569 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
1569 else 1570 else
1570 echo nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1571 echo ${_BOLD}nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
1571 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1572 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
1572 fi 1573 fi
1574
1575 _python_test_hook post
1573 } 1576 }
1574 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1577 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1575 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@" 1578 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1576 else 1579 else
1577 if [[ -n "${separate_build_dirs}" ]]; then 1580 if [[ -n "${separate_build_dirs}" ]]; then
1585 1588
1586# @FUNCTION: python_execute_py.test 1589# @FUNCTION: python_execute_py.test
1587# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments] 1590# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1588# @DESCRIPTION: 1591# @DESCRIPTION:
1589# 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.
1590python_execute_py.test() { 1595python_execute_py.test() {
1596 _python_set_color_variables
1597
1591 local PYTHONPATH_template= separate_build_dirs= 1598 local PYTHONPATH_template= separate_build_dirs=
1592 1599
1593 while (($#)); do 1600 while (($#)); do
1594 case "$1" in 1601 case "$1" in
1595 -P|--PYTHONPATH) 1602 -P|--PYTHONPATH)
1621 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then 1628 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1622 unset evaluated_PYTHONPATH 1629 unset evaluated_PYTHONPATH
1623 fi 1630 fi
1624 fi 1631 fi
1625 1632
1633 _python_test_hook pre
1634
1626 if [[ -n "${evaluated_PYTHONPATH}" ]]; then 1635 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1627 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}
1628 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 "$?"
1629 else 1638 else
1630 echo py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" 1639 echo ${_BOLD}py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"${_NORMAL}
1631 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" 1640 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" || return "$?"
1632 fi 1641 fi
1642
1643 _python_test_hook post
1633 } 1644 }
1634 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1645 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1635 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@" 1646 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1636 else 1647 else
1637 if [[ -n "${separate_build_dirs}" ]]; then 1648 if [[ -n "${separate_build_dirs}" ]]; then
1645 1656
1646# @FUNCTION: python_execute_trial 1657# @FUNCTION: python_execute_trial
1647# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments] 1658# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1648# @DESCRIPTION: 1659# @DESCRIPTION:
1649# 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.
1650python_execute_trial() { 1663python_execute_trial() {
1664 _python_set_color_variables
1665
1651 local PYTHONPATH_template= separate_build_dirs= 1666 local PYTHONPATH_template= separate_build_dirs=
1652 1667
1653 while (($#)); do 1668 while (($#)); do
1654 case "$1" in 1669 case "$1" in
1655 -P|--PYTHONPATH) 1670 -P|--PYTHONPATH)
1681 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then 1696 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1682 unset evaluated_PYTHONPATH 1697 unset evaluated_PYTHONPATH
1683 fi 1698 fi
1684 fi 1699 fi
1685 1700
1701 _python_test_hook pre
1702
1686 if [[ -n "${evaluated_PYTHONPATH}" ]]; then 1703 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1687 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}
1688 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1705 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
1689 else 1706 else
1690 echo trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1707 echo ${_BOLD}trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
1691 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1708 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
1692 fi 1709 fi
1710
1711 _python_test_hook post
1693 } 1712 }
1694 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1713 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1695 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@" 1714 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1696 else 1715 else
1697 if [[ -n "${separate_build_dirs}" ]]; then 1716 if [[ -n "${separate_build_dirs}" ]]; then
1730# If no arguments supplied, it will recompile not recursively all modules 1749# If no arguments supplied, it will recompile not recursively all modules
1731# 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).
1732# 1751#
1733# If supplied with arguments, it will recompile all modules recursively 1752# If supplied with arguments, it will recompile all modules recursively
1734# in the supplied directory. 1753# in the supplied directory.
1735# This function should only be run in pkg_postinst().
1736# 1754#
1737# Options passed to this function are passed to compileall.py. 1755# Options passed to this function are passed to compileall.py.
1738# 1756#
1739# Example: 1757# This function can be used only in pkg_postinst() phase.
1740# python_mod_optimize ctypesgencore
1741python_mod_optimize() { 1758python_mod_optimize() {
1742 _python_initialize_prefix_variables 1759 _python_initialize_prefix_variables
1743 1760
1744 # Check if phase is pkg_postinst(). 1761 # Check if phase is pkg_postinst().
1745 [[ ${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"
1746 1763
1747 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
1748 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=()
1749 1766
1750 # Strip trailing slash from ROOT. 1767 # Strip trailing slash from ROOT.
1751 root="${EROOT%/}" 1768 root="${EROOT%/}"
1752 1769
1753 # Respect ROOT and options passed to compileall.py. 1770 # Respect ROOT and options passed to compileall.py.
1775 ewarn "'${root}/$1' is not a file or a directory!" 1792 ewarn "'${root}/$1' is not a file or a directory!"
1776 else 1793 else
1777 ewarn "'${root}/$1' does not exist!" 1794 ewarn "'${root}/$1' does not exist!"
1778 fi 1795 fi
1779 else 1796 else
1780 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1797 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
1781 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then 1798 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
1782 site_packages_dirs+=("$1") 1799 site_packages_dirs+=("$1")
1783 break 1800 break
1784 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then 1801 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
1785 site_packages_files+=("$1") 1802 site_packages_files+=("$1")
1797 done 1814 done
1798 1815
1799 # Set additional options. 1816 # Set additional options.
1800 options+=("-q") 1817 options+=("-q")
1801 1818
1802 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1819 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
1803 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 1820 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
1804 return_code="0" 1821 return_code="0"
1805 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)"
1806 if ((${#site_packages_dirs[@]})); then 1823 if ((${#site_packages_dirs[@]})); then
1807 for dir in "${site_packages_dirs[@]}"; do 1824 for dir in "${site_packages_dirs[@]}"; do
1835 1852
1836 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1853 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1837 return_code="0" 1854 return_code="0"
1838 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation) $(python_get_version)" 1855 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation) $(python_get_version)"
1839 if ((${#other_dirs[@]})); then 1856 if ((${#other_dirs[@]})); then
1840 "$(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"
1841 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON -A --ABI)}")" != "Jython" ]]; then 1858 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
1842 "$(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"
1843 fi 1860 fi
1844 fi 1861 fi
1845 if ((${#other_files[@]})); then 1862 if ((${#other_files[@]})); then
1846 "$(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"
1847 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON -A --ABI)}")" != "Jython" ]]; then 1864 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
1848 "$(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"
1849 fi 1866 fi
1850 fi 1867 fi
1851 eend "${return_code}" 1868 eend "${return_code}"
1852 fi 1869 fi
1853 else 1870 else
1888 # set additional opts 1905 # set additional opts
1889 myopts+=(-q) 1906 myopts+=(-q)
1890 1907
1891 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)" 1908 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
1892 if ((${#mydirs[@]})); then 1909 if ((${#mydirs[@]})); then
1893 "$(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"
1894 "$(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"
1895 fi 1912 fi
1896 1913
1897 if ((${#myfiles[@]})); then 1914 if ((${#myfiles[@]})); then
1898 python_mod_compile "${myfiles[@]}" 1915 python_mod_compile "${myfiles[@]}"
1899 fi 1916 fi
1910# 1927#
1911# 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
1912# 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.)
1913# 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.
1914# 1931#
1915# This function should only be run in pkg_postrm(). 1932# This function can be used only in pkg_postrm() phase.
1916python_mod_cleanup() { 1933python_mod_cleanup() {
1917 _python_initialize_prefix_variables 1934 _python_initialize_prefix_variables
1935 _python_set_color_variables
1918 1936
1919 local path py_file PYTHON_ABI SEARCH_PATH=() root 1937 local path py_file PYTHON_ABI="${PYTHON_ABI}" SEARCH_PATH=() root
1920 1938
1921 # Check if phase is pkg_postrm(). 1939 # Check if phase is pkg_postrm().
1922 [[ ${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"
1923 1941
1924 # Strip trailing slash from ROOT. 1942 # Strip trailing slash from ROOT.
1925 root="${EROOT%/}" 1943 root="${EROOT%/}"
1926 1944
1927 if (($#)); then 1945 if (($#)); then
1930 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
1931 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"
1932 elif [[ "$1" =~ ^/ ]]; then 1950 elif [[ "$1" =~ ^/ ]]; then
1933 SEARCH_PATH+=("${root}/${1#/}") 1951 SEARCH_PATH+=("${root}/${1#/}")
1934 else 1952 else
1935 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1953 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
1936 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 1954 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
1937 done 1955 done
1938 fi 1956 fi
1939 shift 1957 shift
1940 done 1958 done
1958 SEARCH_PATH+=("${sitedir}") 1976 SEARCH_PATH+=("${sitedir}")
1959 fi 1977 fi
1960 done 1978 done
1961 fi 1979 fi
1962 1980
1963 local BLUE CYAN NORMAL
1964 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1965 BLUE=$'\e[1;34m'
1966 CYAN=$'\e[1;36m'
1967 NORMAL=$'\e[0m'
1968 else
1969 BLUE=
1970 CYAN=
1971 NORMAL=
1972 fi
1973
1974 for path in "${SEARCH_PATH[@]}"; do 1981 for path in "${SEARCH_PATH[@]}"; do
1975 if [[ -d "${path}" ]]; then 1982 if [[ -d "${path}" ]]; then
1976 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
1977 if [[ "${REPLY}" == *[co] ]]; then 1984 if [[ "${REPLY}" == *[co] ]]; then
1978 py_file="${REPLY%[co]}" 1985 py_file="${REPLY%[co]}"
1979 [[ -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
1980 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}" 1987 einfo "${_BLUE}<<< ${py_file}[co]${_NORMAL}"
1981 rm -f "${py_file}"[co] 1988 rm -f "${py_file}"[co]
1982 elif [[ "${REPLY}" == *\$py.class ]]; then 1989 elif [[ "${REPLY}" == *\$py.class ]]; then
1983 py_file="${REPLY%\$py.class}.py" 1990 py_file="${REPLY%\$py.class}.py"
1984 [[ -f "${py_file}" || ! -f "${py_file%.py}\$py.class" ]] && continue 1991 [[ -f "${py_file}" || ! -f "${py_file%.py}\$py.class" ]] && continue
1985 einfo "${BLUE}<<< ${py_file%.py}\$py.class${NORMAL}" 1992 einfo "${_BLUE}<<< ${py_file%.py}\$py.class${_NORMAL}"
1986 rm -f "${py_file%.py}\$py.class" 1993 rm -f "${py_file%.py}\$py.class"
1987 fi 1994 fi
1988 done 1995 done
1989 1996
1990 # Attempt to delete directories, which may be empty. 1997 # Attempt to delete directories, which may be empty.
1991 find "${path}" -type d | sort -r | while read -r dir; do 1998 find "${path}" -type d | sort -r | while read -r dir; do
1992 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}" 1999 rmdir "${dir}" 2>/dev/null && einfo "${_CYAN}<<< ${dir}${_NORMAL}"
1993 done 2000 done
1994 elif [[ "${path}" == *.py && ! -f "${path}" ]]; then 2001 elif [[ "${path}" == *.py && ! -f "${path}" ]]; then
1995 if [[ (-f "${path}c" || -f "${path}o") ]]; then 2002 if [[ (-f "${path}c" || -f "${path}o") ]]; then
1996 einfo "${BLUE}<<< ${path}[co]${NORMAL}" 2003 einfo "${_BLUE}<<< ${path}[co]${_NORMAL}"
1997 rm -f "${path}"[co] 2004 rm -f "${path}"[co]
1998 fi 2005 fi
1999 if [[ -f "${path%.py}\$py.class" ]]; then 2006 if [[ -f "${path%.py}\$py.class" ]]; then
2000 einfo "${BLUE}<<< ${path%.py}\$py.class${NORMAL}" 2007 einfo "${_BLUE}<<< ${path%.py}\$py.class${_NORMAL}"
2001 rm -f "${path%.py}\$py.class" 2008 rm -f "${path%.py}\$py.class"
2002 fi 2009 fi
2003 fi 2010 fi
2004 done 2011 done
2005} 2012}
2011# @FUNCTION: python_version 2018# @FUNCTION: python_version
2012# @DESCRIPTION: 2019# @DESCRIPTION:
2013# Run without arguments and it will export the version of python 2020# Run without arguments and it will export the version of python
2014# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 2021# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
2015python_version() { 2022python_version() {
2016 if ! has "${EAPI:-0}" 0 1 2; then 2023 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
2017 eerror "Use PYTHON() and/or python_get_*() instead of ${FUNCNAME}()." 2024 eerror "Use PYTHON() and/or python_get_*() instead of ${FUNCNAME}()."
2018 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
2019 fi 2033 fi
2020 2034
2021 [[ -n "${PYVER}" ]] && return 0 2035 [[ -n "${PYVER}" ]] && return 0
2022 local tmpstr 2036 local tmpstr
2023 python="${python:-${EPREFIX}/usr/bin/python}" 2037 python="${python:-${EPREFIX}/usr/bin/python}"
2029 export PYVER_MICRO="${PYVER_ALL:4}" 2043 export PYVER_MICRO="${PYVER_ALL:4}"
2030 fi 2044 fi
2031 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}" 2045 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
2032} 2046}
2033 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
2034# @FUNCTION: python_mod_compile 2106# @FUNCTION: python_mod_compile
2035# @USAGE: <file> [more files ...] 2107# @USAGE: <file> [more files ...]
2036# @DESCRIPTION: 2108# @DESCRIPTION:
2037# 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.
2038# This function should only be run in pkg_postinst() 2110# This function can be used only in pkg_postinst() phase.
2039# 2111#
2040# Example: 2112# Example:
2041# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 2113# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
2042# 2114#
2043python_mod_compile() { 2115python_mod_compile() {
2049 _python_initialize_prefix_variables 2121 _python_initialize_prefix_variables
2050 2122
2051 local f myroot myfiles=() 2123 local f myroot myfiles=()
2052 2124
2053 # Check if phase is pkg_postinst() 2125 # Check if phase is pkg_postinst()
2054 [[ ${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"
2055 2127
2056 # strip trailing slash 2128 # strip trailing slash
2057 myroot="${EROOT%/}" 2129 myroot="${EROOT%/}"
2058 2130
2059 # respect ROOT 2131 # respect ROOT
2060 for f in "$@"; do 2132 for f in "$@"; do
2061 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") 2133 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
2062 done 2134 done
2063 2135
2064 if ((${#myfiles[@]})); then 2136 if ((${#myfiles[@]})); then
2065 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" 2137 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
2066 "$(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
2067 else 2139 else
2068 ewarn "No files to compile!" 2140 ewarn "No files to compile!"
2069 fi 2141 fi
2070} 2142}

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

  ViewVC Help
Powered by ViewVC 1.1.20