/[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.95
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.95 2010/03/20 17:59:40 arfrever 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
198 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return 201 if [[ "${PYTHON_USE_WITH_OPT}" ]]; then
202 if [[ "${PYTHON_USE_WITH_OPT}" == !* ]]; then
203 use ${PYTHON_USE_WITH_OPT#!} && return
204 else
205 use !${PYTHON_USE_WITH_OPT} && return
206 fi
207 fi
199 208
200 python_pkg_setup_check_USE_flags() { 209 python_pkg_setup_check_USE_flags() {
201 local pyatom use 210 local pyatom use
202 pyatom="$(python_get_implementational_package)" 211 pyatom="$(python_get_implementational_package)"
203 212
216 if [[ ${PYTHON_USE_WITH_OR} ]]; then 225 if [[ ${PYTHON_USE_WITH_OR} ]]; then
217 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}" 226 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
218 fi 227 fi
219 } 228 }
220 229
221 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 230 if _python_package_supporting_installation_for_multiple_python_abis; then
222 python_execute_function -q python_pkg_setup_check_USE_flags 231 python_execute_function -q python_pkg_setup_check_USE_flags
223 else 232 else
224 python_pkg_setup_check_USE_flags 233 python_pkg_setup_check_USE_flags
225 fi 234 fi
226 235
227 unset -f python_pkg_setup_check_USE_flags python_pkg_setup_fail 236 unset -f python_pkg_setup_check_USE_flags python_pkg_setup_fail
228 } 237 }
229 238
230 EXPORT_FUNCTIONS pkg_setup 239 EXPORT_FUNCTIONS pkg_setup
231 240
232 _PYTHON_USE_WITH_ATOM="" 241 _PYTHON_USE_WITH_ATOMS_ARRAY=()
233 if [[ -n "${PYTHON_USE_WITH}" ]]; then 242 if [[ -n "${PYTHON_USE_WITH}" ]]; then
234 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do 243 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
235 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 244 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]")
236 done 245 done
237 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
238 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then 246 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
239 for _USE_flag in ${PYTHON_USE_WITH_OR}; do 247 for _USE_flag in ${PYTHON_USE_WITH_OR}; do
240 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do 248 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
241 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${_USE_flag}]" 249 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${_USE_flag}]")
242 done 250 done
243 done 251 done
244 unset _USE_flag 252 unset _USE_flag
253 fi
254 if [[ "${#_PYTHON_USE_WITH_ATOMS_ARRAY[@]}" -gt 1 ]]; then
245 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )" 255 _PYTHON_USE_WITH_ATOMS="|| ( ${_PYTHON_USE_WITH_ATOMS_ARRAY[@]} )"
256 else
257 _PYTHON_USE_WITH_ATOMS="${_PYTHON_USE_WITH_ATOMS_ARRAY[@]}"
246 fi 258 fi
247 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then 259 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
248 _PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOM} )" 260 _PYTHON_USE_WITH_ATOMS="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOMS} )"
249 fi 261 fi
250 DEPEND+=" ${_PYTHON_USE_WITH_ATOM}" 262 DEPEND+=" ${_PYTHON_USE_WITH_ATOMS}"
251 RDEPEND+=" ${_PYTHON_USE_WITH_ATOM}" 263 RDEPEND+=" ${_PYTHON_USE_WITH_ATOMS}"
252 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOM 264 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOMS _PYTHON_USE_WITH_ATOMS_ARRAY
253fi 265fi
254 266
255unset _PYTHON_ATOMS 267unset _PYTHON_ATOMS
256 268
257# ================================================================================================ 269# ================================================================================================
258# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======== 270# =========== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ============
259# ================================================================================================ 271# ================================================================================================
260 272
261# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS 273# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
262# @DESCRIPTION: 274# @DESCRIPTION:
263# Set this in EAPI <= 4 to indicate that current package supports installation for 275# Set this in EAPI <= 4 to indicate that current package supports installation for
264# multiple versions of Python. 276# multiple Python ABIs.
265 277
266# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS 278# @ECLASS-VARIABLE: PYTHON_EXPORT_PHASE_FUNCTIONS
267# @DESCRIPTION: 279# @DESCRIPTION:
268# Set this to define default functions for the following ebuild phases: 280# Set this to export phase functions for the following ebuild phases:
269# src_prepare, src_configure, src_compile, src_test, src_install. 281# src_prepare, src_configure, src_compile, src_test, src_install.
270if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then 282if ! has "${EAPI:-0}" 0 1; then
271 python_src_prepare() { 283 python_src_prepare() {
284 if ! _python_package_supporting_installation_for_multiple_python_abis; then
285 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
286 fi
287
272 python_copy_sources 288 python_copy_sources
273 } 289 }
274 290
275 for python_default_function in src_configure src_compile src_test src_install; do 291 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; }" 292 eval "python_${python_default_function}() {
293 if ! _python_package_supporting_installation_for_multiple_python_abis; then
294 die \"\${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs\"
295 fi
296
297 python_execute_function -d -s \"\$@\"
298 }"
277 done 299 done
278 unset python_default_function 300 unset python_default_function
279 301
302 if [[ -n "${PYTHON_EXPORT_PHASE_FUNCTIONS}" ]]; then
280 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install 303 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
304 fi
281fi 305fi
282 306
283unset PYTHON_ABIS 307unset PYTHON_ABIS
284unset PYTHON_ABIS_SANITY_CHECKS
285 308
286# @FUNCTION: validate_PYTHON_ABIS 309# @FUNCTION: validate_PYTHON_ABIS
287# @DESCRIPTION: 310# @DESCRIPTION:
288# Ensure that PYTHON_ABIS variable has valid value. 311# Ensure that PYTHON_ABIS variable has valid value.
289# This function usually should not be directly called in ebuilds. 312# This function usually should not be directly called in ebuilds.
290validate_PYTHON_ABIS() { 313validate_PYTHON_ABIS() {
291 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable. 314 if ! _python_package_supporting_installation_for_multiple_python_abis; then
292 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 315 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
293 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
294 fi 316 fi
295 317
296 # Ensure that /usr/bin/python and /usr/bin/python-config are valid. 318 _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 319
308 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5. 320 # 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 321 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= 322 local PYTHON_ABI restricted_ABI support_ABI supported_PYTHON_ABIS=
311 PYTHON_ABI_SUPPORTED_VALUES="${_CPYTHON2_SUPPORTED_ABIS[@]} ${_CPYTHON3_SUPPORTED_ABIS[@]} ${_JYTHON_SUPPORTED_ABIS[@]}" 323 PYTHON_ABI_SUPPORTED_VALUES="${_CPYTHON2_SUPPORTED_ABIS[@]} ${_CPYTHON3_SUPPORTED_ABIS[@]} ${_JYTHON_SUPPORTED_ABIS[@]}"
312 324
313 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then 325 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
314 local python2_enabled="0" python3_enabled="0" 326 local cpython_enabled="0"
315 327
316 if [[ -z "${USE_PYTHON}" ]]; then 328 if [[ -z "${USE_PYTHON}" ]]; then
317 die "USE_PYTHON variable is empty" 329 die "USE_PYTHON variable is empty"
318 fi 330 fi
319 331
320 for PYTHON_ABI in ${USE_PYTHON}; do 332 for PYTHON_ABI in ${USE_PYTHON}; do
321 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 333 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
322 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'" 334 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
323 fi 335 fi
324 336
325 if has "${PYTHON_ABI}" "${_CPYTHON2_SUPPORTED_ABIS[@]}"; then 337 if has "${PYTHON_ABI}" "${_CPYTHON2_SUPPORTED_ABIS[@]}" "${_CPYTHON3_SUPPORTED_ABIS[@]}"; then
326 python2_enabled="1" 338 cpython_enabled="1"
327 fi
328 if has "${PYTHON_ABI}" "${_CPYTHON3_SUPPORTED_ABIS[@]}"; then
329 python3_enabled="1"
330 fi 339 fi
331 340
332 support_ABI="1" 341 support_ABI="1"
333 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 342 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
334 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then 343 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
338 done 347 done
339 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}" 348 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
340 done 349 done
341 350
342 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then 351 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
343 die "USE_PYTHON variable does not enable any version of Python supported by ${CATEGORY}/${PF}" 352 die "USE_PYTHON variable does not enable any Python ABI supported by ${CATEGORY}/${PF}"
344 fi 353 fi
345 354
346 if [[ "${python2_enabled}" == "0" ]]; then 355 if [[ "${cpython_enabled}" == "0" ]]; then
347 ewarn "USE_PYTHON variable does not enable any version of Python 2. This configuration is unsupported."
348 fi
349 if [[ "${python3_enabled}" == "0" ]]; then
350 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported."
351 fi
352 if [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "0" ]]; then
353 die "USE_PYTHON variable does not enable any version of CPython" 356 die "USE_PYTHON variable does not enable any CPython ABI"
354 fi 357 fi
355 else 358 else
356 local python_version python2_version= python3_version= support_python_major_version 359 local python_version python2_version= python3_version= support_python_major_version
357 360
358 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')" 361 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
426 PYTHON_ABIS="${PYTHON_ABIS# }" 429 PYTHON_ABIS="${PYTHON_ABIS# }"
427 export PYTHON_ABIS="${PYTHON_ABIS% }" 430 export PYTHON_ABIS="${PYTHON_ABIS% }"
428 fi 431 fi
429 fi 432 fi
430 433
431 if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then 434 _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} 435}
453 436
454# @FUNCTION: python_execute_function 437# @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] 438# @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: 439# @DESCRIPTION:
457# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 440# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
458# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 441# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
459python_execute_function() { 442python_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= 443 if ! _python_package_supporting_installation_for_multiple_python_abis; then
444 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
445 fi
446
447 _python_set_color_variables
448
449 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 450
462 while (($#)); do 451 while (($#)); do
463 case "$1" in 452 case "$1" in
464 --action-message) 453 --action-message)
465 action_message_template="$2" 454 action_message_template="$2"
470 ;; 459 ;;
471 --failure-message) 460 --failure-message)
472 failure_message_template="$2" 461 failure_message_template="$2"
473 shift 462 shift
474 ;; 463 ;;
464 -f|--final-ABI)
465 final_ABI="1"
466 ;;
475 --nonfatal) 467 --nonfatal)
476 nonfatal="1" 468 nonfatal="1"
477 ;; 469 ;;
478 -q|--quiet) 470 -q|--quiet)
479 quiet="1" 471 quiet="1"
512 504
513 if [[ -z "$(type -t "${function}")" ]]; then 505 if [[ -z "$(type -t "${function}")" ]]; then
514 die "${FUNCNAME}(): '${function}' function is not defined" 506 die "${FUNCNAME}(): '${function}' function is not defined"
515 fi 507 fi
516 else 508 else
517 if [[ "$#" -ne 0 ]]; then
518 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
519 fi
520 if has "${EAPI:-0}" 0 1; then 509 if has "${EAPI:-0}" 0 1; then
521 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI" 510 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
522 fi 511 fi
523 512
524 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 513 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
525 if has "${EAPI}" 2 3; then 514 if has "${EAPI}" 2 3; then
526 python_default_function() { 515 python_default_function() {
527 econf 516 econf "$@"
528 } 517 }
529 else 518 else
530 python_default_function() { 519 python_default_function() {
531 nonfatal econf 520 nonfatal econf "$@"
532 } 521 }
533 fi 522 fi
534 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then 523 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
535 python_default_function() { 524 python_default_function() {
536 emake 525 emake "$@"
537 } 526 }
538 elif [[ "${EBUILD_PHASE}" == "test" ]]; then 527 elif [[ "${EBUILD_PHASE}" == "test" ]]; then
539 python_default_function() { 528 python_default_function() {
540 if emake -j1 -n check &> /dev/null; then 529 if emake -j1 -n check &> /dev/null; then
541 emake -j1 check 530 emake -j1 check "$@"
542 elif emake -j1 -n test &> /dev/null; then 531 elif emake -j1 -n test &> /dev/null; then
543 emake -j1 test 532 emake -j1 test "$@"
544 fi 533 fi
545 } 534 }
546 elif [[ "${EBUILD_PHASE}" == "install" ]]; then 535 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
547 python_default_function() { 536 python_default_function() {
548 emake DESTDIR="${D}" install 537 emake DESTDIR="${D}" install "$@"
549 } 538 }
550 else 539 else
551 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase" 540 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
552 fi 541 fi
553 function="python_default_function" 542 function="python_default_function"
554 fi 543 fi
555 544
545 # Ensure that python_execute_function() cannot be directly or indirectly called by python_execute_function().
556 for ((i = 1; i < "${#FUNCNAME[@]}"; i++)); do 546 for ((i = 1; i < "${#FUNCNAME[@]}"; i++)); do
557 if [[ "${FUNCNAME[${i}]}" == "${FUNCNAME}" ]]; then 547 if [[ "${FUNCNAME[${i}]}" == "${FUNCNAME}" ]]; then
558 die "${FUNCNAME}(): Invalid call stack" 548 die "${FUNCNAME}(): Invalid call stack"
559 fi 549 fi
560 done 550 done
571 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation" 561 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
572 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation" 562 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
573 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation" 563 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
574 fi 564 fi
575 565
576 local RED GREEN BLUE NORMAL 566 validate_PYTHON_ABIS
577 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then 567 if [[ "${final_ABI}" == "1" ]]; then
578 RED=$'\e[1;31m' 568 iterated_PYTHON_ABIS="$(PYTHON -f --ABI)"
579 GREEN=$'\e[1;32m'
580 BLUE=$'\e[1;34m'
581 NORMAL=$'\e[0m'
582 else 569 else
583 RED= 570 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
584 GREEN=
585 BLUE=
586 NORMAL=
587 fi 571 fi
588
589 validate_PYTHON_ABIS
590 for PYTHON_ABI in ${PYTHON_ABIS}; do 572 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
591 if [[ "${quiet}" == "0" ]]; then 573 if [[ "${quiet}" == "0" ]]; then
592 if [[ -n "${action_message_template}" ]]; then 574 if [[ -n "${action_message_template}" ]]; then
593 action_message="$(eval echo -n "${action_message_template}")" 575 action_message="$(eval echo -n "${action_message_template}")"
594 else 576 else
595 action_message="${action} of ${CATEGORY}/${PF} with $(python_get_implementation) $(python_get_version)..." 577 action_message="${action} of ${CATEGORY}/${PF} with $(python_get_implementation) $(python_get_version)..."
596 fi 578 fi
597 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 579 echo " ${_GREEN}*${_NORMAL} ${_BLUE}${action_message}${_NORMAL}"
598 fi 580 fi
599 581
600 if [[ "${separate_build_dirs}" == "1" ]]; then 582 if [[ "${separate_build_dirs}" == "1" ]]; then
601 if [[ -n "${source_dir}" ]]; then 583 if [[ -n "${source_dir}" ]]; then
602 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}" 584 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
625 failure_message="${action} failed with $(python_get_implementation) $(python_get_version) in ${function}() function" 607 failure_message="${action} failed with $(python_get_implementation) $(python_get_version) in ${function}() function"
626 fi 608 fi
627 609
628 if [[ "${nonfatal}" == "1" ]]; then 610 if [[ "${nonfatal}" == "1" ]]; then
629 if [[ "${quiet}" == "0" ]]; then 611 if [[ "${quiet}" == "0" ]]; then
630 ewarn "${RED}${failure_message}${NORMAL}" 612 ewarn "${_RED}${failure_message}${_NORMAL}"
631 fi 613 fi
632 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 614 elif [[ "${final_ABI}" == "0" ]] && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
633 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then 615 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
634 local enabled_PYTHON_ABIS= other_PYTHON_ABI 616 local enabled_PYTHON_ABIS= other_PYTHON_ABI
635 for other_PYTHON_ABI in ${PYTHON_ABIS}; do 617 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
636 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}" 618 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
637 done 619 done
638 export PYTHON_ABIS="${enabled_PYTHON_ABIS}" 620 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
639 fi 621 fi
640 if [[ "${quiet}" == "0" ]]; then 622 if [[ "${quiet}" == "0" ]]; then
641 ewarn "${RED}${failure_message}${NORMAL}" 623 ewarn "${_RED}${failure_message}${_NORMAL}"
642 fi 624 fi
643 if [[ -z "${PYTHON_ABIS}" ]]; then 625 if [[ -z "${PYTHON_ABIS}" ]]; then
644 die "${function}() function failed with all enabled versions of Python" 626 die "${function}() function failed with all enabled Python ABIs"
645 fi 627 fi
646 else 628 else
647 die "${failure_message}" 629 die "${failure_message}"
648 fi 630 fi
649 fi 631 fi
679 unset -f python_default_function 661 unset -f python_default_function
680 fi 662 fi
681} 663}
682 664
683# @FUNCTION: python_copy_sources 665# @FUNCTION: python_copy_sources
684# @USAGE: [--no-link] [--] [directory] 666# @USAGE: <directory="${S}"> [directory]
685# @DESCRIPTION: 667# @DESCRIPTION:
686# Copy unpacked sources of current package to separate build directory for each Python ABI. 668# Copy unpacked sources of current package to separate build directory for each Python ABI.
687python_copy_sources() { 669python_copy_sources() {
670 if ! _python_package_supporting_installation_for_multiple_python_abis; then
671 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
672 fi
673
688 local dir dirs=() no_link="0" PYTHON_ABI 674 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 675
709 if [[ "$#" -eq 0 ]]; then 676 if [[ "$#" -eq 0 ]]; then
710 if [[ "${WORKDIR}" == "${S}" ]]; then 677 if [[ "${WORKDIR}" == "${S}" ]]; then
711 die "${FUNCNAME}() cannot be used" 678 die "${FUNCNAME}() cannot be used"
712 fi 679 fi
713 dirs="${S}" 680 dirs=("${S%/}")
714 else 681 else
715 dirs="$@" 682 dirs=("$@")
716 fi 683 fi
717 684
718 validate_PYTHON_ABIS 685 validate_PYTHON_ABIS
719 for PYTHON_ABI in ${PYTHON_ABIS}; do 686 for PYTHON_ABI in ${PYTHON_ABIS}; do
720 for dir in "${dirs[@]}"; do 687 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" 688 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 689 done
727 done 690 done
728}
729
730# @FUNCTION: python_set_build_dir_symlink
731# @USAGE: [directory="build"]
732# @DESCRIPTION:
733# Create build directory symlink.
734python_set_build_dir_symlink() {
735 local dir="$1"
736
737 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
738 [[ -z "${dir}" ]] && dir="build"
739
740 # Do not delete preexistent directories.
741 rm -f "${dir}" || die "Deletion of '${dir}' failed"
742 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
743} 691}
744 692
745# @FUNCTION: python_generate_wrapper_scripts 693# @FUNCTION: python_generate_wrapper_scripts
746# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files] 694# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
747# @DESCRIPTION: 695# @DESCRIPTION:
748# Generate wrapper scripts. Existing files are overwritten only with --force option. 696# Generate wrapper scripts. Existing files are overwritten only with --force option.
749# If --respect-EPYTHON option is specified, then generated wrapper scripts will 697# If --respect-EPYTHON option is specified, then generated wrapper scripts will
750# respect EPYTHON variable at run time. 698# respect EPYTHON variable at run time.
751python_generate_wrapper_scripts() { 699python_generate_wrapper_scripts() {
700 if ! _python_package_supporting_installation_for_multiple_python_abis; then
701 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
702 fi
703
752 _python_initialize_prefix_variables 704 _python_initialize_prefix_variables
753 705
754 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python3_enabled="0" respect_EPYTHON="0" 706 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python3_enabled="0" respect_EPYTHON="0"
755 707
756 while (($#)); do 708 while (($#)); do
808 if [[ -f "${file}" && "${force}" == "0" ]]; then 760 if [[ -f "${file}" && "${force}" == "0" ]]; then
809 die "${FUNCNAME}(): '$1' already exists" 761 die "${FUNCNAME}(): '$1' already exists"
810 fi 762 fi
811 763
812 if [[ "${quiet}" == "0" ]]; then 764 if [[ "${quiet}" == "0" ]]; then
813 einfo "Generating '${file#${D%/}}' wrapper script" 765 einfo "Generating '${file#${ED%/}}' wrapper script"
814 fi 766 fi
815 767
816 cat << EOF > "${file}" 768 cat << EOF > "${file}"
817#!/usr/bin/env python 769#!/usr/bin/env python
818# Gentoo '${file##*/}' wrapper script 770# Gentoo '${file##*/}' wrapper script generated by python_generate_wrapper_scripts()
819 771
820import os 772import os
821import re 773import re
822import subprocess 774import subprocess
823import sys 775import sys
824 776
825EPYTHON_re = re.compile(r"^python(\d+\.\d+)$") 777EPYTHON_re = re.compile(r"^python(\d+\.\d+)$")
778python_shebang_re = re.compile(r"^#! *(${EPREFIX}/usr/bin/python|(${EPREFIX})?/usr/bin/env +(${EPREFIX}/usr/bin/)?python)")
779python_verification_output_re = re.compile("^GENTOO_PYTHON_TARGET_SCRIPT_PATH supported\n$")
826 780
827EOF 781EOF
828 if [[ "$?" != "0" ]]; then 782 if [[ "$?" != "0" ]]; then
829 die "${FUNCNAME}(): Generation of '$1' failed" 783 die "${FUNCNAME}(): Generation of '$1' failed"
830 fi 784 fi
845 raise ValueError 799 raise ValueError
846 except (OSError, ValueError): 800 except (OSError, ValueError):
847 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n") 801 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
848 sys.exit(1) 802 sys.exit(1)
849 803
850 eselect_output = eselect_process.stdout.read() 804 EPYTHON = eselect_process.stdout.read()
851 if not isinstance(eselect_output, str): 805 if not isinstance(EPYTHON, str):
852 # Python 3 806 # Python 3
853 eselect_output = eselect_output.decode() 807 EPYTHON = EPYTHON.decode()
808 EPYTHON = EPYTHON.rstrip("\n")
854 809
855 EPYTHON_matched = EPYTHON_re.match(eselect_output) 810 EPYTHON_matched = EPYTHON_re.match(EPYTHON)
856 if EPYTHON_matched: 811 if EPYTHON_matched:
857 PYTHON_ABI = EPYTHON_matched.group(1) 812 PYTHON_ABI = EPYTHON_matched.group(1)
858 else: 813 else:
859 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output) 814 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % EPYTHON)
860 sys.exit(1) 815 sys.exit(1)
861EOF 816EOF
862 if [[ "$?" != "0" ]]; then 817 if [[ "$?" != "0" ]]; then
863 die "${FUNCNAME}(): Generation of '$1' failed" 818 die "${FUNCNAME}(): Generation of '$1' failed"
864 fi 819 fi
870 raise ValueError 825 raise ValueError
871except (OSError, ValueError): 826except (OSError, ValueError):
872 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n") 827 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
873 sys.exit(1) 828 sys.exit(1)
874 829
875eselect_output = eselect_process.stdout.read() 830EPYTHON = eselect_process.stdout.read()
876if not isinstance(eselect_output, str): 831if not isinstance(EPYTHON, str):
877 # Python 3 832 # Python 3
878 eselect_output = eselect_output.decode() 833 EPYTHON = EPYTHON.decode()
834EPYTHON = EPYTHON.rstrip("\n")
879 835
880EPYTHON_matched = EPYTHON_re.match(eselect_output) 836EPYTHON_matched = EPYTHON_re.match(EPYTHON)
881if EPYTHON_matched: 837if EPYTHON_matched:
882 PYTHON_ABI = EPYTHON_matched.group(1) 838 PYTHON_ABI = EPYTHON_matched.group(1)
883else: 839else:
884 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output) 840 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % EPYTHON)
885 sys.exit(1) 841 sys.exit(1)
886EOF 842EOF
887 if [[ "$?" != "0" ]]; then 843 if [[ "$?" != "0" ]]; then
888 die "${FUNCNAME}(): Generation of '$1' failed" 844 die "${FUNCNAME}(): Generation of '$1' failed"
889 fi 845 fi
890 fi 846 fi
891 cat << EOF >> "${file}" 847 cat << EOF >> "${file}"
892 848
849wrapper_script_path = os.path.realpath(sys.argv[0])
850target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
893os.environ["PYTHON_SCRIPT_NAME"] = sys.argv[0] 851os.environ["GENTOO_PYTHON_WRAPPER_SCRIPT_PATH"] = sys.argv[0]
894target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI) 852os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH"] = target_executable_path
895if not os.path.exists(target_executable): 853if not os.path.exists(target_executable_path):
896 sys.stderr.write("'%s' does not exist\n" % target_executable) 854 sys.stderr.write("'%s' does not exist\n" % target_executable_path)
897 sys.exit(1) 855 sys.exit(1)
898 856
857target_executable = open(target_executable_path, "rb")
858target_executable_first_line = target_executable.readline()
859if not isinstance(target_executable_first_line, str):
860 # Python 3
861 target_executable_first_line = target_executable_first_line.decode("utf_8", "replace")
862
863python_shebang_matched = python_shebang_re.match(target_executable_first_line)
864target_executable.close()
865
866if python_shebang_matched:
867 try:
868 python_interpreter_path = "${EPREFIX}/usr/bin/%s" % EPYTHON
869 os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"] = "1"
870 python_verification_process = subprocess.Popen([python_interpreter_path, "-c", "pass"], stdout=subprocess.PIPE)
871 del os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"]
872 if python_verification_process.wait() != 0:
873 raise ValueError
874
875 python_verification_output = python_verification_process.stdout.read()
876 if not isinstance(python_verification_output, str):
877 # Python 3
878 python_verification_output = python_verification_output.decode()
879
880 if not python_verification_output_re.match(python_verification_output):
881 raise ValueError
882
883 os.execv(python_interpreter_path, [python_interpreter_path] + sys.argv)
884 except:
885 pass
886 if "GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION" in os.environ:
887 del os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"]
888
899os.execv(target_executable, sys.argv) 889os.execv(target_executable_path, sys.argv)
900EOF 890EOF
901 if [[ "$?" != "0" ]]; then 891 if [[ "$?" != "0" ]]; then
902 die "${FUNCNAME}(): Generation of '$1' failed" 892 die "${FUNCNAME}(): Generation of '$1' failed"
903 fi 893 fi
904 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed" 894 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed"
905 done 895 done
906} 896}
907 897
908# ================================================================================================ 898# ================================================================================================
909# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ====== 899# ========= FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ==========
910# ================================================================================================ 900# ================================================================================================
911 901
912# @FUNCTION: python_set_active_version 902# @FUNCTION: python_set_active_version
913# @USAGE: <CPython_ABI|2|3> 903# @USAGE: <CPython_ABI|2|3>
914# @DESCRIPTION: 904# @DESCRIPTION:
915# Set specified version of CPython as active version of Python. 905# Set specified version of CPython as active version of Python.
916python_set_active_version() { 906python_set_active_version() {
917 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 907 if _python_package_supporting_installation_for_multiple_python_abis; then
918 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple versions of Python" 908 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
919 fi 909 fi
920 910
921 if [[ "$#" -ne 1 ]]; then 911 if [[ "$#" -ne 1 ]]; then
922 die "${FUNCNAME}() requires 1 argument" 912 die "${FUNCNAME}() requires 1 argument"
923 fi 913 fi
914
915 _python_initial_sanity_checks
924 916
925 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then 917 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
926 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then 918 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
927 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed" 919 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
928 fi 920 fi
944 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses, 936 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
945 # so it does not need to be exported to subprocesses. 937 # so it does not need to be exported to subprocesses.
946 PYTHON_ABI="${EPYTHON#python}" 938 PYTHON_ABI="${EPYTHON#python}"
947 PYTHON_ABI="${PYTHON_ABI%%-*}" 939 PYTHON_ABI="${PYTHON_ABI%%-*}"
948 940
941 _python_final_sanity_checks
942
949 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable. 943 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
950 PYTHON_REQUESTED_ACTIVE_VERSION="$1" 944 PYTHON_REQUESTED_ACTIVE_VERSION="$1"
951} 945}
952 946
953# @FUNCTION: python_need_rebuild 947# @FUNCTION: python_need_rebuild
954# @DESCRIPTION: Mark current package for rebuilding by python-updater after 948# @DESCRIPTION: Mark current package for rebuilding by python-updater after
955# switching of active version of Python. 949# switching of active version of Python.
956python_need_rebuild() { 950python_need_rebuild() {
951 if _python_package_supporting_installation_for_multiple_python_abis; then
952 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
953 fi
954
957 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)" 955 export PYTHON_NEED_REBUILD="$(PYTHON --ABI)"
958} 956}
959 957
960# ================================================================================================ 958# ================================================================================================
961# ======================================= GETTER FUNCTIONS ======================================= 959# ======================================= GETTER FUNCTIONS =======================================
962# ================================================================================================ 960# ================================================================================================
980 die "${FUNCNAME}(): Unrecognized Python ABI '$1'" 978 die "${FUNCNAME}(): Unrecognized Python ABI '$1'"
981 fi 979 fi
982} 980}
983 981
984# @FUNCTION: PYTHON 982# @FUNCTION: PYTHON
985# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}"> 983# @USAGE: [-2] [-3] [--ABI] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
986# @DESCRIPTION: 984# @DESCRIPTION:
987# Print filename of Python interpreter for specified Python ABI. If Python_ABI argument 985# 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. 986# 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. 987# 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. 988# 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. 989# 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. 990# -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 991# If --ABI option is specified, then only specified Python ABI is printed instead of
996# filename of Python interpreter. 992# filename of Python interpreter.
997# If --absolute-path option is specified, then absolute path to Python interpreter is printed. 993# If --absolute-path option is specified, then absolute path to Python interpreter is printed.
998# --ABI and --absolute-path options cannot be specified simultaneously. 994# --ABI and --absolute-path options cannot be specified simultaneously.
999PYTHON() { 995PYTHON() {
1000 local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" PYTHON_ABI="${PYTHON_ABI}" python_interpreter python2="0" python3="0" 996 local ABI_output="0" absolute_path_output="0" final_ABI="0" PYTHON_ABI="${PYTHON_ABI}" python_interpreter python2="0" python3="0"
1001 997
1002 while (($#)); do 998 while (($#)); do
1003 case "$1" in 999 case "$1" in
1004 -2) 1000 -2)
1005 python2="1" 1001 python2="1"
1008 python3="1" 1004 python3="1"
1009 ;; 1005 ;;
1010 --ABI) 1006 --ABI)
1011 ABI_output="1" 1007 ABI_output="1"
1012 ;; 1008 ;;
1013 -A|--active)
1014 active="1"
1015 ;;
1016 -a|--absolute-path) 1009 -a|--absolute-path)
1017 absolute_path_output="1" 1010 absolute_path_output="1"
1018 ;; 1011 ;;
1019 -f|--final-ABI) 1012 -f|--final-ABI)
1020 final_ABI="1" 1013 final_ABI="1"
1035 1028
1036 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then 1029 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
1037 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously" 1030 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
1038 fi 1031 fi
1039 1032
1040 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then 1033 if [[ "$((${python2} + ${python3} + ${final_ABI}))" -gt 1 ]]; then
1041 die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously" 1034 die "${FUNCNAME}(): '-2', '-3' or '--final-ABI' options cannot be specified simultaneously"
1042 fi 1035 fi
1043 1036
1044 if [[ "$#" -eq 0 ]]; then 1037 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 1038 if [[ "${final_ABI}" == "1" ]]; then
1051 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 1039 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1052 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python" 1040 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1053 fi 1041 fi
1054 validate_PYTHON_ABIS 1042 validate_PYTHON_ABIS
1055 PYTHON_ABI="${PYTHON_ABIS##* }" 1043 PYTHON_ABI="${PYTHON_ABIS##* }"
1056 elif [[ "${python2}" == "1" ]]; then 1044 elif [[ "${python2}" == "1" ]]; then
1057 PYTHON_ABI="$(eselect python show --python2 --ABI)" 1045 PYTHON_ABI="$(eselect python show --python2 --ABI)"
1065 if [[ -z "${PYTHON_ABI}" ]]; then 1053 if [[ -z "${PYTHON_ABI}" ]]; then
1066 die "${FUNCNAME}(): Active Python 3 interpreter not set" 1054 die "${FUNCNAME}(): Active Python 3 interpreter not set"
1067 elif [[ "${PYTHON_ABI}" != "3."* ]]; then 1055 elif [[ "${PYTHON_ABI}" != "3."* ]]; then
1068 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`" 1056 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1069 fi 1057 fi
1058 elif ! _python_package_supporting_installation_for_multiple_python_abis; then
1059 PYTHON_ABI="$("${EPREFIX}/usr/bin/python" -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")"
1070 elif [[ -z "${PYTHON_ABI}" ]]; then 1060 elif [[ -z "${PYTHON_ABI}" ]]; then
1071 die "${FUNCNAME}(): Invalid usage: Python ABI not specified" 1061 die "${FUNCNAME}(): Invalid usage: ${FUNCNAME}() should be used in ABI-specific local scope"
1072 fi 1062 fi
1073 elif [[ "$#" -eq 1 ]]; then 1063 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 1064 if [[ "${final_ABI}" == "1" ]]; then
1078 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously" 1065 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
1079 fi 1066 fi
1080 if [[ "${python2}" == "1" ]]; then 1067 if [[ "${python2}" == "1" ]]; then
1081 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously" 1068 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
1132 esac 1119 esac
1133 shift 1120 shift
1134 done 1121 done
1135 1122
1136 if [[ "${final_ABI}" == "1" ]]; then 1123 if [[ "${final_ABI}" == "1" ]]; then
1124 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1125 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1126 fi
1137 PYTHON_ABI="$(PYTHON -f --ABI)" 1127 PYTHON_ABI="$(PYTHON -f --ABI)"
1138 elif [[ -z "${PYTHON_ABI}" ]]; then 1128 elif [[ -z "${PYTHON_ABI}" ]]; then
1139 PYTHON_ABI="$(PYTHON -A --ABI)" 1129 PYTHON_ABI="$(PYTHON --ABI)"
1140 fi 1130 fi
1141 1131
1142 echo "$(_python_get_implementation "${PYTHON_ABI}")" 1132 echo "$(_python_get_implementation "${PYTHON_ABI}")"
1143} 1133}
1144 1134
1164 esac 1154 esac
1165 shift 1155 shift
1166 done 1156 done
1167 1157
1168 if [[ "${final_ABI}" == "1" ]]; then 1158 if [[ "${final_ABI}" == "1" ]]; then
1159 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1160 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1161 fi
1169 PYTHON_ABI="$(PYTHON -f --ABI)" 1162 PYTHON_ABI="$(PYTHON -f --ABI)"
1170 elif [[ -z "${PYTHON_ABI}" ]]; then 1163 elif [[ -z "${PYTHON_ABI}" ]]; then
1171 PYTHON_ABI="$(PYTHON -A --ABI)" 1164 PYTHON_ABI="$(PYTHON --ABI)"
1172 fi 1165 fi
1173 1166
1174 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1167 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1175 echo "dev-lang/python:${PYTHON_ABI}" 1168 echo "dev-lang/python:${PYTHON_ABI}"
1176 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1169 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1200 esac 1193 esac
1201 shift 1194 shift
1202 done 1195 done
1203 1196
1204 if [[ "${final_ABI}" == "1" ]]; then 1197 if [[ "${final_ABI}" == "1" ]]; then
1198 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1199 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1200 fi
1205 PYTHON_ABI="$(PYTHON -f --ABI)" 1201 PYTHON_ABI="$(PYTHON -f --ABI)"
1206 elif [[ -z "${PYTHON_ABI}" ]]; then 1202 elif [[ -z "${PYTHON_ABI}" ]]; then
1207 PYTHON_ABI="$(PYTHON -A --ABI)" 1203 PYTHON_ABI="$(PYTHON --ABI)"
1208 fi 1204 fi
1209 1205
1210 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1206 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1211 echo "/usr/include/python${PYTHON_ABI}" 1207 echo "/usr/include/python${PYTHON_ABI}"
1212 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1208 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1236 esac 1232 esac
1237 shift 1233 shift
1238 done 1234 done
1239 1235
1240 if [[ "${final_ABI}" == "1" ]]; then 1236 if [[ "${final_ABI}" == "1" ]]; then
1237 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1238 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1239 fi
1241 PYTHON_ABI="$(PYTHON -f --ABI)" 1240 PYTHON_ABI="$(PYTHON -f --ABI)"
1242 elif [[ -z "${PYTHON_ABI}" ]]; then 1241 elif [[ -z "${PYTHON_ABI}" ]]; then
1243 PYTHON_ABI="$(PYTHON -A --ABI)" 1242 PYTHON_ABI="$(PYTHON --ABI)"
1244 fi 1243 fi
1245 1244
1246 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1245 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1247 echo "/usr/$(get_libdir)/python${PYTHON_ABI}" 1246 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
1248 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then 1247 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1259 local options=() 1258 local options=()
1260 1259
1261 while (($#)); do 1260 while (($#)); do
1262 case "$1" in 1261 case "$1" in
1263 -f|--final-ABI) 1262 -f|--final-ABI)
1263 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1264 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1265 fi
1264 options+=("$1") 1266 options+=("$1")
1265 ;; 1267 ;;
1266 -*) 1268 -*)
1267 die "${FUNCNAME}(): Unrecognized option '$1'" 1269 die "${FUNCNAME}(): Unrecognized option '$1'"
1268 ;; 1270 ;;
1302 esac 1304 esac
1303 shift 1305 shift
1304 done 1306 done
1305 1307
1306 if [[ "${final_ABI}" == "1" ]]; then 1308 if [[ "${final_ABI}" == "1" ]]; then
1309 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1310 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1311 fi
1307 PYTHON_ABI="$(PYTHON -f --ABI)" 1312 PYTHON_ABI="$(PYTHON -f --ABI)"
1308 elif [[ -z "${PYTHON_ABI}" ]]; then 1313 elif [[ -z "${PYTHON_ABI}" ]]; then
1309 PYTHON_ABI="$(PYTHON -A --ABI)" 1314 PYTHON_ABI="$(PYTHON --ABI)"
1310 fi 1315 fi
1311 1316
1312 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then 1317 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1313 if [[ "${linker_option}" == "1" ]]; then 1318 if [[ "${linker_option}" == "1" ]]; then
1314 echo "-lpython${PYTHON_ABI}" 1319 echo "-lpython${PYTHON_ABI}"
1319 die "${FUNCNAME}(): Jython does not have shared library" 1324 die "${FUNCNAME}(): Jython does not have shared library"
1320 fi 1325 fi
1321} 1326}
1322 1327
1323# @FUNCTION: python_get_version 1328# @FUNCTION: python_get_version
1324# @USAGE: [-f|--final-ABI] [--major] [--minor] [--micro] 1329# @USAGE: [-f|--final-ABI] [--full] [--major] [--minor] [--micro]
1325# @DESCRIPTION: 1330# @DESCRIPTION:
1326# Print Python version. 1331# Print Python version.
1327# --major, --minor and --micro options cannot be specified simultaneously. 1332# --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. 1333# 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. 1334# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1330python_get_version() { 1335python_get_version() {
1331 local final_ABI="0" major="0" minor="0" micro="0" python_command 1336 local final_ABI="0" full="0" major="0" minor="0" micro="0" python_command
1332 1337
1333 while (($#)); do 1338 while (($#)); do
1334 case "$1" in 1339 case "$1" in
1335 -f|--final-ABI) 1340 -f|--final-ABI)
1336 final_ABI="1" 1341 final_ABI="1"
1337 ;; 1342 ;;
1343 --full)
1344 full="1"
1345 ;;
1338 --major) 1346 --major)
1339 major="1" 1347 major="1"
1340 ;; 1348 ;;
1341 --minor) 1349 --minor)
1342 minor="1" 1350 minor="1"
1352 ;; 1360 ;;
1353 esac 1361 esac
1354 shift 1362 shift
1355 done 1363 done
1356 1364
1357 if [[ "$((${major} + ${minor} + ${micro}))" -gt 1 ]]; then 1365 if [[ "$((${full} + ${major} + ${minor} + ${micro}))" -gt 1 ]]; then
1358 die "${FUNCNAME}(): '--major', '--minor' or '--micro' options cannot be specified simultaneously" 1366 die "${FUNCNAME}(): '--full', '--major', '--minor' or '--micro' options cannot be specified simultaneously"
1359 fi 1367 fi
1360 1368
1369 if [[ "${full}" == "1" ]]; then
1370 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:3]))"
1361 if [[ "${major}" == "1" ]]; then 1371 elif [[ "${major}" == "1" ]]; then
1362 python_command="from sys import version_info; print(version_info[0])" 1372 python_command="from sys import version_info; print(version_info[0])"
1363 elif [[ "${minor}" == "1" ]]; then 1373 elif [[ "${minor}" == "1" ]]; then
1364 python_command="from sys import version_info; print(version_info[1])" 1374 python_command="from sys import version_info; print(version_info[1])"
1365 elif [[ "${micro}" == "1" ]]; then 1375 elif [[ "${micro}" == "1" ]]; then
1366 python_command="from sys import version_info; print(version_info[2])" 1376 python_command="from sys import version_info; print(version_info[2])"
1367 else 1377 else
1378 if [[ -n "${PYTHON_ABI}" && "${final_ABI}" == "0" ]]; then
1379 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1380 echo "${PYTHON_ABI}"
1381 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1382 echo "${PYTHON_ABI%-jython}"
1383 fi
1384 return
1385 fi
1368 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:2]))" 1386 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:2]))"
1369 fi 1387 fi
1370 1388
1371 if [[ "${final_ABI}" == "1" ]]; then 1389 if [[ "${final_ABI}" == "1" ]]; then
1390 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1391 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1392 fi
1372 "$(PYTHON -f)" -c "${python_command}" 1393 "$(PYTHON -f)" -c "${python_command}"
1373 else 1394 else
1374 "$(PYTHON "${PYTHON_ABI--A}")" -c "${python_command}" 1395 "$(PYTHON ${PYTHON_ABI})" -c "${python_command}"
1375 fi 1396 fi
1376} 1397}
1377 1398
1378# ================================================================================================ 1399# ================================================================================================
1379# =================================== MISCELLANEOUS FUNCTIONS ==================================== 1400# =================================== MISCELLANEOUS FUNCTIONS ====================================
1387 else 1408 else
1388 return 1 1409 return 1
1389 fi 1410 fi
1390} 1411}
1391 1412
1413_python_package_supporting_installation_for_multiple_python_abis() {
1414 if [[ "${EBUILD_PHASE}" == "depend" ]]; then
1415 die "${FUNCNAME}() cannot be used in global scope"
1416 fi
1417
1418 if has "${EAPI:-0}" 0 1 2 3 4; then
1419 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1420 return 0
1421 else
1422 return 1
1423 fi
1424 else
1425 die "${FUNCNAME}(): Support for EAPI=\"${EAPI}\" not implemented"
1426 fi
1427}
1428
1392_python_initialize_prefix_variables() { 1429_python_initialize_prefix_variables() {
1393 if has "${EAPI:-0}" 0 1 2; then 1430 if has "${EAPI:-0}" 0 1 2; then
1394 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then 1431 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
1395 EROOT="${ROOT%/}${EPREFIX}/" 1432 EROOT="${ROOT%/}${EPREFIX}/"
1396 fi 1433 fi
1397 if [[ -n "${D}" && -z "${ED}" ]]; then 1434 if [[ -n "${D}" && -z "${ED}" ]]; then
1398 ED="${D%/}${EPREFIX}/" 1435 ED="${D%/}${EPREFIX}/"
1399 fi 1436 fi
1437 fi
1438}
1439
1440unset PYTHON_SANITY_CHECKS
1441
1442_python_initial_sanity_checks() {
1443 if [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1444 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
1445 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
1446 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
1447 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1448 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
1449 fi
1450 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
1451 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
1452 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1453 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
1454 fi
1455 fi
1456}
1457
1458_python_final_sanity_checks() {
1459 if ! _python_implementation && [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1460 local PYTHON_ABI="${PYTHON_ABI}"
1461 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI}}; do
1462 # Ensure that appropriate version of Python is installed.
1463 if ! has_version "$(python_get_implementational_package)"; then
1464 die "$(python_get_implementational_package) is not installed"
1465 fi
1466
1467 # Ensure that EPYTHON variable is respected.
1468 if [[ "$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" != "${PYTHON_ABI}" ]]; then
1469 eerror "python: '$(type -p python)'"
1470 eerror "ABI: '${ABI}'"
1471 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
1472 eerror "EPYTHON: '$(PYTHON)'"
1473 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
1474 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")'"
1475 die "'python' does not respect EPYTHON variable"
1476 fi
1477 done
1478 fi
1479 PYTHON_SANITY_CHECKS="1"
1480}
1481
1482_python_set_color_variables() {
1483 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1484 _BOLD=$'\e[1m'
1485 _RED=$'\e[1;31m'
1486 _GREEN=$'\e[1;32m'
1487 _BLUE=$'\e[1;34m'
1488 _CYAN=$'\e[1;36m'
1489 _NORMAL=$'\e[0m'
1490 else
1491 _BOLD=
1492 _RED=
1493 _GREEN=
1494 _BLUE=
1495 _CYAN=
1496 _NORMAL=
1400 fi 1497 fi
1401} 1498}
1402 1499
1403# @FUNCTION: python_convert_shebangs 1500# @FUNCTION: python_convert_shebangs
1404# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories] 1501# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1446 die "${FUNCNAME}(): '${argument}' does not exist" 1543 die "${FUNCNAME}(): '${argument}' does not exist"
1447 elif [[ -f "${argument}" ]]; then 1544 elif [[ -f "${argument}" ]]; then
1448 files+=("${argument}") 1545 files+=("${argument}")
1449 elif [[ -d "${argument}" ]]; then 1546 elif [[ -d "${argument}" ]]; then
1450 if [[ "${recursive}" == "1" ]]; then 1547 if [[ "${recursive}" == "1" ]]; then
1451 if [[ "${only_executables}" == "1" ]]; then 1548 while read -d $'\0' -r file; do
1452 files+=($(find "${argument}" -perm /111 -type f)) 1549 files+=("${file}")
1453 else 1550 done < <(find "${argument}" $([[ "${only_executables}" == "1" ]] && echo -perm /111) -type f -print0)
1454 files+=($(find "${argument}" -type f))
1455 fi
1456 else 1551 else
1457 die "${FUNCNAME}(): '${argument}' is not a regular file" 1552 die "${FUNCNAME}(): '${argument}' is not a regular file"
1458 fi 1553 fi
1459 else 1554 else
1460 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory" 1555 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
1464 for file in "${files[@]}"; do 1559 for file in "${files[@]}"; do
1465 file="${file#./}" 1560 file="${file#./}"
1466 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue 1561 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1467 1562
1468 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then 1563 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1564 [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue
1565
1469 if [[ "${quiet}" == "0" ]]; then 1566 if [[ "${quiet}" == "0" ]]; then
1470 einfo "Converting shebang in '${file}'" 1567 einfo "Converting shebang in '${file}'"
1471 fi 1568 fi
1569
1472 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed" 1570 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1473 1571
1474 # Delete potential whitespace after "#!". 1572 # Delete potential whitespace after "#!".
1475 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed" 1573 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1476 fi 1574 fi
1477 done 1575 done
1478} 1576}
1479 1577
1480# @FUNCTION: python_mod_exists 1578# @FUNCTION: python_clean_sitedirs
1481# @USAGE: <module>
1482# @DESCRIPTION: 1579# @DESCRIPTION:
1483# Run with the module name as an argument. It will check if a 1580# Delete needless files in site-packages directories in ${ED}.
1484# Python module is installed and loadable. It will return 1581python_clean_sitedirs() {
1485# TRUE(0) if the module exists, and FALSE(1) if the module does 1582 _python_initialize_prefix_variables
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 1583
1499# @FUNCTION: python_tkinter_exists 1584 find "${ED}"usr/$(get_libdir)/python*/site-packages "(" -name "*.c" -o -name "*.h" -o -name "*.la" ")" -type f -print0 | xargs -0 rm -f
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} 1585}
1515 1586
1516# ================================================================================================ 1587# ================================================================================================
1517# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================ 1588# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1518# ================================================================================================ 1589# ================================================================================================
1521# @DESCRIPTION: 1592# @DESCRIPTION:
1522# User-configurable verbosity of tests of Python modules. 1593# User-configurable verbosity of tests of Python modules.
1523# Supported values: 0, 1, 2, 3, 4. 1594# Supported values: 0, 1, 2, 3, 4.
1524PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}" 1595PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
1525 1596
1597_python_test_hook() {
1598 if [[ "$#" -ne 1 ]]; then
1599 die "${FUNCNAME}() requires 1 argument"
1600 fi
1601
1602 if _python_package_supporting_installation_for_multiple_python_abis && [[ "$(type -t "${FUNCNAME[3]}_$1_hook")" == "function" ]]; then
1603 "${FUNCNAME[3]}_$1_hook"
1604 fi
1605}
1606
1526# @FUNCTION: python_execute_nosetests 1607# @FUNCTION: python_execute_nosetests
1527# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments] 1608# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1528# @DESCRIPTION: 1609# @DESCRIPTION:
1529# Execute nosetests for all enabled versions of Python. 1610# Execute nosetests for all enabled Python ABIs.
1611# In ebuilds of packages supporting installation for multiple Python ABIs, this function calls
1612# python_execute_nosetests_pre_hook() and python_execute_nosetests_post_hook(), if they are defined.
1530python_execute_nosetests() { 1613python_execute_nosetests() {
1614 _python_set_color_variables
1615
1531 local PYTHONPATH_template= separate_build_dirs= 1616 local PYTHONPATH_template= separate_build_dirs=
1532 1617
1533 while (($#)); do 1618 while (($#)); do
1534 case "$1" in 1619 case "$1" in
1535 -P|--PYTHONPATH) 1620 -P|--PYTHONPATH)
1561 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then 1646 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1562 unset evaluated_PYTHONPATH 1647 unset evaluated_PYTHONPATH
1563 fi 1648 fi
1564 fi 1649 fi
1565 1650
1651 _python_test_hook pre
1652
1566 if [[ -n "${evaluated_PYTHONPATH}" ]]; then 1653 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1567 echo PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1654 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
1568 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1655 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
1569 else 1656 else
1570 echo nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1657 echo ${_BOLD}nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
1571 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" 1658 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
1572 fi 1659 fi
1660
1661 _python_test_hook post
1573 } 1662 }
1574 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1663 if _python_package_supporting_installation_for_multiple_python_abis; then
1575 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@" 1664 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1576 else 1665 else
1577 if [[ -n "${separate_build_dirs}" ]]; then 1666 if [[ -n "${separate_build_dirs}" ]]; then
1578 die "${FUNCNAME}(): Invalid usage" 1667 die "${FUNCNAME}(): Invalid usage"
1579 fi 1668 fi
1584} 1673}
1585 1674
1586# @FUNCTION: python_execute_py.test 1675# @FUNCTION: python_execute_py.test
1587# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments] 1676# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1588# @DESCRIPTION: 1677# @DESCRIPTION:
1589# Execute py.test for all enabled versions of Python. 1678# Execute py.test for all enabled Python ABIs.
1679# In ebuilds of packages supporting installation for multiple Python ABIs, this function calls
1680# python_execute_py.test_pre_hook() and python_execute_py.test_post_hook(), if they are defined.
1590python_execute_py.test() { 1681python_execute_py.test() {
1682 _python_set_color_variables
1683
1591 local PYTHONPATH_template= separate_build_dirs= 1684 local PYTHONPATH_template= separate_build_dirs=
1592 1685
1593 while (($#)); do 1686 while (($#)); do
1594 case "$1" in 1687 case "$1" in
1595 -P|--PYTHONPATH) 1688 -P|--PYTHONPATH)
1621 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then 1714 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1622 unset evaluated_PYTHONPATH 1715 unset evaluated_PYTHONPATH
1623 fi 1716 fi
1624 fi 1717 fi
1625 1718
1719 _python_test_hook pre
1720
1626 if [[ -n "${evaluated_PYTHONPATH}" ]]; then 1721 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1627 echo PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@" 1722 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) "$@" 1723 PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@" || return "$?"
1629 else 1724 else
1630 echo py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" 1725 echo ${_BOLD}py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"${_NORMAL}
1631 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" 1726 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" || return "$?"
1632 fi 1727 fi
1728
1729 _python_test_hook post
1633 } 1730 }
1634 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1731 if _python_package_supporting_installation_for_multiple_python_abis; then
1635 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@" 1732 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1636 else 1733 else
1637 if [[ -n "${separate_build_dirs}" ]]; then 1734 if [[ -n "${separate_build_dirs}" ]]; then
1638 die "${FUNCNAME}(): Invalid usage" 1735 die "${FUNCNAME}(): Invalid usage"
1639 fi 1736 fi
1644} 1741}
1645 1742
1646# @FUNCTION: python_execute_trial 1743# @FUNCTION: python_execute_trial
1647# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments] 1744# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1648# @DESCRIPTION: 1745# @DESCRIPTION:
1649# Execute trial for all enabled versions of Python. 1746# Execute trial for all enabled Python ABIs.
1747# In ebuilds of packages supporting installation for multiple Python ABIs, this function
1748# calls python_execute_trial_pre_hook() and python_execute_trial_post_hook(), if they are defined.
1650python_execute_trial() { 1749python_execute_trial() {
1750 _python_set_color_variables
1751
1651 local PYTHONPATH_template= separate_build_dirs= 1752 local PYTHONPATH_template= separate_build_dirs=
1652 1753
1653 while (($#)); do 1754 while (($#)); do
1654 case "$1" in 1755 case "$1" in
1655 -P|--PYTHONPATH) 1756 -P|--PYTHONPATH)
1681 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then 1782 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1682 unset evaluated_PYTHONPATH 1783 unset evaluated_PYTHONPATH
1683 fi 1784 fi
1684 fi 1785 fi
1685 1786
1787 _python_test_hook pre
1788
1686 if [[ -n "${evaluated_PYTHONPATH}" ]]; then 1789 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1687 echo PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1790 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) "$@" 1791 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
1689 else 1792 else
1690 echo trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1793 echo ${_BOLD}trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
1691 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" 1794 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
1692 fi 1795 fi
1796
1797 _python_test_hook post
1693 } 1798 }
1694 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1799 if _python_package_supporting_installation_for_multiple_python_abis; then
1695 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@" 1800 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1696 else 1801 else
1697 if [[ -n "${separate_build_dirs}" ]]; then 1802 if [[ -n "${separate_build_dirs}" ]]; then
1698 die "${FUNCNAME}(): Invalid usage" 1803 die "${FUNCNAME}(): Invalid usage"
1699 fi 1804 fi
1730# If no arguments supplied, it will recompile not recursively all modules 1835# 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). 1836# under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages).
1732# 1837#
1733# If supplied with arguments, it will recompile all modules recursively 1838# If supplied with arguments, it will recompile all modules recursively
1734# in the supplied directory. 1839# in the supplied directory.
1735# This function should only be run in pkg_postinst().
1736# 1840#
1737# Options passed to this function are passed to compileall.py. 1841# Options passed to this function are passed to compileall.py.
1738# 1842#
1739# Example: 1843# This function can be used only in pkg_postinst() phase.
1740# python_mod_optimize ctypesgencore
1741python_mod_optimize() { 1844python_mod_optimize() {
1742 _python_initialize_prefix_variables 1845 _python_initialize_prefix_variables
1743 1846
1744 # Check if phase is pkg_postinst(). 1847 # Check if phase is pkg_postinst().
1745 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME}() should only be run in pkg_postinst()" 1848 [[ "${EBUILD_PHASE}" != "postinst" ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase"
1746 1849
1747 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1850 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_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=() 1851 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 1852
1750 # Strip trailing slash from ROOT. 1853 # Strip trailing slash from ROOT.
1751 root="${EROOT%/}" 1854 root="${EROOT%/}"
1752 1855
1753 # Respect ROOT and options passed to compileall.py. 1856 # Respect ROOT and options passed to compileall.py.
1775 ewarn "'${root}/$1' is not a file or a directory!" 1878 ewarn "'${root}/$1' is not a file or a directory!"
1776 else 1879 else
1777 ewarn "'${root}/$1' does not exist!" 1880 ewarn "'${root}/$1' does not exist!"
1778 fi 1881 fi
1779 else 1882 else
1780 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1883 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
1781 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then 1884 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
1782 site_packages_dirs+=("$1") 1885 site_packages_dirs+=("$1")
1783 break 1886 break
1784 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then 1887 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
1785 site_packages_files+=("$1") 1888 site_packages_files+=("$1")
1797 done 1900 done
1798 1901
1799 # Set additional options. 1902 # Set additional options.
1800 options+=("-q") 1903 options+=("-q")
1801 1904
1802 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1905 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
1803 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 1906 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
1804 return_code="0" 1907 return_code="0"
1805 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)"
1806 if ((${#site_packages_dirs[@]})); then 1909 if ((${#site_packages_dirs[@]})); then
1807 for dir in "${site_packages_dirs[@]}"; do 1910 for dir in "${site_packages_dirs[@]}"; do
1835 1938
1836 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1939 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1837 return_code="0" 1940 return_code="0"
1838 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation) $(python_get_version)" 1941 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 1942 if ((${#other_dirs[@]})); then
1840 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 1943 "$(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 1944 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" 1945 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
1843 fi 1946 fi
1844 fi 1947 fi
1845 if ((${#other_files[@]})); then 1948 if ((${#other_files[@]})); then
1846 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 1949 "$(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 1950 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" 1951 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
1849 fi 1952 fi
1850 fi 1953 fi
1851 eend "${return_code}" 1954 eend "${return_code}"
1852 fi 1955 fi
1853 else 1956 else
1888 # set additional opts 1991 # set additional opts
1889 myopts+=(-q) 1992 myopts+=(-q)
1890 1993
1891 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)" 1994 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
1892 if ((${#mydirs[@]})); then 1995 if ((${#mydirs[@]})); then
1893 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1" 1996 "$(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" 1997 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
1895 fi 1998 fi
1896 1999
1897 if ((${#myfiles[@]})); then 2000 if ((${#myfiles[@]})); then
1898 python_mod_compile "${myfiles[@]}" 2001 python_mod_compile "${myfiles[@]}"
1899 fi 2002 fi
1910# 2013#
1911# It will recursively scan all compiled Python modules in the directories and 2014# 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.) 2015# 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. 2016# If they are, then it will remove their corresponding .pyc and .pyo files.
1914# 2017#
1915# This function should only be run in pkg_postrm(). 2018# This function can be used only in pkg_postrm() phase.
1916python_mod_cleanup() { 2019python_mod_cleanup() {
1917 _python_initialize_prefix_variables 2020 _python_initialize_prefix_variables
2021 _python_set_color_variables
1918 2022
1919 local path py_file PYTHON_ABI SEARCH_PATH=() root 2023 local path py_file PYTHON_ABI="${PYTHON_ABI}" SEARCH_PATH=() root
1920 2024
1921 # Check if phase is pkg_postrm(). 2025 # Check if phase is pkg_postrm().
1922 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME}() should only be run in pkg_postrm()" 2026 [[ "${EBUILD_PHASE}" != "postrm" ]] && die "${FUNCNAME}() can be used only in pkg_postrm() phase"
1923 2027
1924 # Strip trailing slash from ROOT. 2028 # Strip trailing slash from ROOT.
1925 root="${EROOT%/}" 2029 root="${EROOT%/}"
1926 2030
1927 if (($#)); then 2031 if (($#)); then
1928 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 2032 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
1929 while (($#)); do 2033 while (($#)); do
1930 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 2034 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" 2035 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1932 elif [[ "$1" =~ ^/ ]]; then 2036 elif [[ "$1" =~ ^/ ]]; then
1933 SEARCH_PATH+=("${root}/${1#/}") 2037 SEARCH_PATH+=("${root}/${1#/}")
1934 else 2038 else
1935 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 2039 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
1936 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 2040 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
1937 done 2041 done
1938 fi 2042 fi
1939 shift 2043 shift
1940 done 2044 done
1958 SEARCH_PATH+=("${sitedir}") 2062 SEARCH_PATH+=("${sitedir}")
1959 fi 2063 fi
1960 done 2064 done
1961 fi 2065 fi
1962 2066
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 2067 for path in "${SEARCH_PATH[@]}"; do
1975 if [[ -d "${path}" ]]; then 2068 if [[ -d "${path}" ]]; then
1976 find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0 | while read -rd ''; do 2069 find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0 | while read -rd ''; do
1977 if [[ "${REPLY}" == *[co] ]]; then 2070 if [[ "${REPLY}" == *[co] ]]; then
1978 py_file="${REPLY%[co]}" 2071 py_file="${REPLY%[co]}"
1979 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue 2072 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
1980 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}" 2073 echo "${_BLUE}<<< ${py_file}[co]${_NORMAL}"
1981 rm -f "${py_file}"[co] 2074 rm -f "${py_file}"[co]
1982 elif [[ "${REPLY}" == *\$py.class ]]; then 2075 elif [[ "${REPLY}" == *\$py.class ]]; then
1983 py_file="${REPLY%\$py.class}.py" 2076 py_file="${REPLY%\$py.class}.py"
1984 [[ -f "${py_file}" || ! -f "${py_file%.py}\$py.class" ]] && continue 2077 [[ -f "${py_file}" || ! -f "${py_file%.py}\$py.class" ]] && continue
1985 einfo "${BLUE}<<< ${py_file%.py}\$py.class${NORMAL}" 2078 echo "${_BLUE}<<< ${py_file%.py}\$py.class${_NORMAL}"
1986 rm -f "${py_file%.py}\$py.class" 2079 rm -f "${py_file%.py}\$py.class"
1987 fi 2080 fi
1988 done 2081 done
1989 2082
1990 # Attempt to delete directories, which may be empty. 2083 # Attempt to delete directories, which may be empty.
1991 find "${path}" -type d | sort -r | while read -r dir; do 2084 find "${path}" -type d | sort -r | while read -r dir; do
1992 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}" 2085 rmdir "${dir}" 2>/dev/null && echo "${_CYAN}<<< ${dir}${_NORMAL}"
1993 done 2086 done
1994 elif [[ "${path}" == *.py && ! -f "${path}" ]]; then 2087 elif [[ "${path}" == *.py && ! -f "${path}" ]]; then
1995 if [[ (-f "${path}c" || -f "${path}o") ]]; then 2088 if [[ (-f "${path}c" || -f "${path}o") ]]; then
1996 einfo "${BLUE}<<< ${path}[co]${NORMAL}" 2089 echo "${_BLUE}<<< ${path}[co]${_NORMAL}"
1997 rm -f "${path}"[co] 2090 rm -f "${path}"[co]
1998 fi 2091 fi
1999 if [[ -f "${path%.py}\$py.class" ]]; then 2092 if [[ -f "${path%.py}\$py.class" ]]; then
2000 einfo "${BLUE}<<< ${path%.py}\$py.class${NORMAL}" 2093 echo "${_BLUE}<<< ${path%.py}\$py.class${_NORMAL}"
2001 rm -f "${path%.py}\$py.class" 2094 rm -f "${path%.py}\$py.class"
2002 fi 2095 fi
2003 fi 2096 fi
2004 done 2097 done
2005} 2098}
2011# @FUNCTION: python_version 2104# @FUNCTION: python_version
2012# @DESCRIPTION: 2105# @DESCRIPTION:
2013# Run without arguments and it will export the version of python 2106# Run without arguments and it will export the version of python
2014# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 2107# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
2015python_version() { 2108python_version() {
2016 if ! has "${EAPI:-0}" 0 1 2; then 2109 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
2017 eerror "Use PYTHON() and/or python_get_*() instead of ${FUNCNAME}()." 2110 eerror "Use PYTHON() and/or python_get_*() instead of ${FUNCNAME}()."
2018 die "${FUNCNAME}() cannot be used in this EAPI" 2111 die "${FUNCNAME}() cannot be used in this EAPI"
2112 fi
2113
2114 _python_set_color_variables
2115
2116 if [[ "${FUNCNAME[1]}" != "distutils_python_version" ]]; then
2117 echo
2118 echo " ${_RED}*${_NORMAL} ${_RED}Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01.${_NORMAL}"
2119 echo " ${_RED}*${_NORMAL} ${_RED}Use PYTHON() instead of python variable. Use python_get_*() instead of PYVER* variables.${_NORMAL}"
2120 echo " ${_RED}*${_NORMAL} ${_RED}The ebuild needs to be fixed. Please report a bug, if it has not been already reported.${_NORMAL}"
2121 echo
2122
2123 einfo &> /dev/null
2124 einfo "Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01." &> /dev/null
2125 einfo "Use PYTHON() instead of python variable. Use python_get_*() instead of PYVER* variables." &> /dev/null
2126 einfo "The ebuild needs to be fixed. Please report a bug, if it has not been already reported." &> /dev/null
2127 einfo &> /dev/null
2019 fi 2128 fi
2020 2129
2021 [[ -n "${PYVER}" ]] && return 0 2130 [[ -n "${PYVER}" ]] && return 0
2022 local tmpstr 2131 local tmpstr
2023 python="${python:-${EPREFIX}/usr/bin/python}" 2132 python="${python:-${EPREFIX}/usr/bin/python}"
2029 export PYVER_MICRO="${PYVER_ALL:4}" 2138 export PYVER_MICRO="${PYVER_ALL:4}"
2030 fi 2139 fi
2031 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}" 2140 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
2032} 2141}
2033 2142
2143# @FUNCTION: python_mod_exists
2144# @USAGE: <module>
2145# @DESCRIPTION:
2146# Run with the module name as an argument. It will check if a
2147# Python module is installed and loadable. It will return
2148# TRUE(0) if the module exists, and FALSE(1) if the module does
2149# not exist.
2150#
2151# Example:
2152# if python_mod_exists gtk; then
2153# echo "gtk support enabled"
2154# fi
2155python_mod_exists() {
2156 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
2157 eerror "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()."
2158 die "${FUNCNAME}() cannot be used in this EAPI"
2159 fi
2160
2161 echo
2162 echo " ${_RED}*${_NORMAL} ${_RED}Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01.${_NORMAL}"
2163 echo " ${_RED}*${_NORMAL} ${_RED}Use USE dependencies and/or has_version() instead of ${FUNCNAME}().${_NORMAL}"
2164 echo " ${_RED}*${_NORMAL} ${_RED}The ebuild needs to be fixed. Please report a bug, if it has not been already reported.${_NORMAL}"
2165 echo
2166
2167 einfo &> /dev/null
2168 einfo "Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01." &> /dev/null
2169 einfo "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()." &> /dev/null
2170 einfo "The ebuild needs to be fixed. Please report a bug, if it has not been already reported." &> /dev/null
2171 einfo &> /dev/null
2172
2173 if [[ "$#" -ne 1 ]]; then
2174 die "${FUNCNAME}() requires 1 argument"
2175 fi
2176 "$(PYTHON ${PYTHON_ABI})" -c "import $1" &> /dev/null
2177}
2178
2179# @FUNCTION: python_tkinter_exists
2180# @DESCRIPTION:
2181# Run without arguments, checks if Python was compiled with Tkinter
2182# support. If not, prints an error message and dies.
2183python_tkinter_exists() {
2184 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
2185 eerror "Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}()."
2186 die "${FUNCNAME}() cannot be used in this EAPI"
2187 fi
2188
2189 if [[ "${FUNCNAME[1]}" != "distutils_python_tkinter" ]]; then
2190 echo
2191 echo " ${_RED}*${_NORMAL} ${_RED}Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01.${_NORMAL}"
2192 echo " ${_RED}*${_NORMAL} ${_RED}Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}().${_NORMAL}"
2193 echo " ${_RED}*${_NORMAL} ${_RED}The ebuild needs to be fixed. Please report a bug, if it has not been already reported.${_NORMAL}"
2194 echo
2195
2196 einfo &> /dev/null
2197 einfo "Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01." &> /dev/null
2198 einfo "Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}()." &> /dev/null
2199 einfo "The ebuild needs to be fixed. Please report a bug, if it has not been already reported." &> /dev/null
2200 einfo &> /dev/null
2201 fi
2202
2203 if ! "$(PYTHON ${PYTHON_ABI})" -c "from sys import version_info
2204if version_info[0] == 3:
2205 import tkinter
2206else:
2207 import Tkinter" &> /dev/null; then
2208 eerror "Python needs to be rebuilt with tkinter support enabled."
2209 eerror "Add the following line to '${EPREFIX}/etc/portage/package.use' and rebuild Python"
2210 eerror "dev-lang/python tk"
2211 die "Python installed without support for tkinter"
2212 fi
2213}
2214
2034# @FUNCTION: python_mod_compile 2215# @FUNCTION: python_mod_compile
2035# @USAGE: <file> [more files ...] 2216# @USAGE: <file> [more files ...]
2036# @DESCRIPTION: 2217# @DESCRIPTION:
2037# Given filenames, it will pre-compile the module's .pyc and .pyo. 2218# Given filenames, it will pre-compile the module's .pyc and .pyo.
2038# This function should only be run in pkg_postinst() 2219# This function can be used only in pkg_postinst() phase.
2039# 2220#
2040# Example: 2221# Example:
2041# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 2222# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
2042# 2223#
2043python_mod_compile() { 2224python_mod_compile() {
2044 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 2225 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
2045 eerror "Use python_mod_optimize() instead of ${FUNCNAME}()." 2226 eerror "Use python_mod_optimize() instead of ${FUNCNAME}()."
2046 die "${FUNCNAME}() cannot be used in this EAPI" 2227 die "${FUNCNAME}() cannot be used in this EAPI"
2047 fi 2228 fi
2048 2229
2049 _python_initialize_prefix_variables 2230 _python_initialize_prefix_variables
2050 2231
2051 local f myroot myfiles=() 2232 local f myroot myfiles=()
2052 2233
2053 # Check if phase is pkg_postinst() 2234 # Check if phase is pkg_postinst()
2054 [[ ${EBUILD_PHASE} != postinst ]] && die "${FUNCNAME}() should only be run in pkg_postinst()" 2235 [[ "${EBUILD_PHASE}" != "postinst" ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase"
2055 2236
2056 # strip trailing slash 2237 # strip trailing slash
2057 myroot="${EROOT%/}" 2238 myroot="${EROOT%/}"
2058 2239
2059 # respect ROOT 2240 # respect ROOT
2060 for f in "$@"; do 2241 for f in "$@"; do
2061 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") 2242 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
2062 done 2243 done
2063 2244
2064 if ((${#myfiles[@]})); then 2245 if ((${#myfiles[@]})); then
2065 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" 2246 "$(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 2247 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
2067 else 2248 else
2068 ewarn "No files to compile!" 2249 ewarn "No files to compile!"
2069 fi 2250 fi
2070} 2251}

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

  ViewVC Help
Powered by ViewVC 1.1.20