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

Diff of /eclass/python.eclass

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

Revision 1.72 Revision 1.80
1# Copyright 1999-2009 Gentoo Foundation 1# Copyright 1999-2009 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.72 2009/09/11 19:55:05 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.80 2009/11/22 13:48:16 arfrever Exp $
4 4
5# @ECLASS: python.eclass 5# @ECLASS: python.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# python@gentoo.org 7# python@gentoo.org
8#
9# original author: Alastair Tse <liquidx@gentoo.org>
10# @BLURB: A Utility Eclass that should be inherited by anything that deals with Python or Python modules. 8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules.
11# @DESCRIPTION: 9# @DESCRIPTION:
12# Some useful functions for dealing with Python. 10# Some useful functions for dealing with Python.
13 11
14inherit multilib 12inherit multilib
15 13
16if [[ -n "${NEED_PYTHON}" ]] ; then 14if [[ -n "${NEED_PYTHON}" ]]; then
17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" 15 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
18 DEPEND="${PYTHON_ATOM}" 16 DEPEND="${PYTHON_ATOM}"
19 RDEPEND="${DEPEND}" 17 RDEPEND="${DEPEND}"
20else 18else
21 PYTHON_ATOM="dev-lang/python" 19 PYTHON_ATOM="dev-lang/python"
22fi 20fi
23 21
24DEPEND="${DEPEND} >=app-shells/bash-3.2"
25if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
26 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804" 22DEPEND+=" >=app-admin/eselect-python-20090804
27fi 23 >=app-shells/bash-3.2"
28 24
29__python_eclass_test() { 25__python_eclass_test() {
30 __python_version_extract 2.3 26 __python_version_extract 2.3
31 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 27 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
32 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" 28 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
108 if [[ "${absolute_path}" == "1" ]]; then 104 if [[ "${absolute_path}" == "1" ]]; then
109 echo -n "/usr/bin/python${slot}" 105 echo -n "/usr/bin/python${slot}"
110 else 106 else
111 echo -n "python${slot}" 107 echo -n "python${slot}"
112 fi 108 fi
109
110 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
111 echo -n "-${ABI}"
112 fi
113} 113}
114
115unset PYTHON_ABIS
116unset PYTHON_ABIS_SANITY_CHECKS
114 117
115# @FUNCTION: validate_PYTHON_ABIS 118# @FUNCTION: validate_PYTHON_ABIS
116# @DESCRIPTION: 119# @DESCRIPTION:
117# Make sure PYTHON_ABIS variable has valid value. 120# Ensure that PYTHON_ABIS variable has valid value.
118validate_PYTHON_ABIS() { 121validate_PYTHON_ABIS() {
119 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable. 122 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable.
120 if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 123 if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
121 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable" 124 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
122 fi 125 fi
129 die "'/usr/bin/python-config' isn't valid script" 132 die "'/usr/bin/python-config' isn't valid script"
130 fi 133 fi
131 134
132 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. 135 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4.
133 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then 136 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then
134 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI 137 local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS=
135 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" 138 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
139 python2_supported_versions="2.4 2.5 2.6 2.7"
140 python3_supported_versions="3.0 3.1 3.2"
136 141
137 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then 142 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
143 local python2_enabled="0" python3_enabled="0"
144
138 if [[ -z "${USE_PYTHON}" ]]; then 145 if [[ -z "${USE_PYTHON}" ]]; then
139 die "USE_PYTHON variable is empty" 146 die "USE_PYTHON variable is empty"
140 fi 147 fi
141 148
142 for ABI in ${USE_PYTHON}; do 149 for PYTHON_ABI in ${USE_PYTHON}; do
143 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 150 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
144 die "USE_PYTHON variable contains invalid value '${ABI}'" 151 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
145 fi 152 fi
153
154 if has "${PYTHON_ABI}" ${python2_supported_versions}; then
155 python2_enabled="1"
156 fi
157 if has "${PYTHON_ABI}" ${python3_supported_versions}; then
158 python3_enabled="1"
159 fi
160
146 support_ABI="1" 161 support_ABI="1"
147 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 162 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
148 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 163 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
149 support_ABI="0" 164 support_ABI="0"
150 break 165 break
151 fi 166 fi
152 done 167 done
153 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 168 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${PYTHON_ABI}"
154 done 169 done
155 export PYTHON_ABIS="${supported_PYTHON_ABIS# }" 170 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
156 171
157 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then 172 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
158 die "USE_PYTHON variable doesn't enable any Python version supported by ${CATEGORY}/${PF}" 173 die "USE_PYTHON variable doesn't enable any version of Python supported by ${CATEGORY}/${PF}"
174 fi
175
176 if [[ "${python2_enabled}" == "0" ]]; then
177 ewarn "USE_PYTHON variable doesn't enable any version of Python 2. This configuration is unsupported."
178 fi
179 if [[ "${python3_enabled}" == "0" ]]; then
180 ewarn "USE_PYTHON variable doesn't enable any version of Python 3. This configuration is unsupported."
159 fi 181 fi
160 else 182 else
161 local restricted_ABI 183 local python_version python2_version= python3_version= support_python_major_version
162 python_version
163 184
185 python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
186
187 if has_version "=dev-lang/python-2*"; then
188 if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then
189 die "'/usr/bin/python2' isn't valid symlink"
190 fi
191
192 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
193
194 for PYTHON_ABI in ${python2_supported_versions}; do
195 support_python_major_version="1"
164 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 196 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
165 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${PYVER}', '${restricted_ABI}'))"; then 197 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
166 die "Active Python version isn't supported by ${CATEGORY}/${PF}" 198 support_python_major_version="0"
167 fi 199 fi
200 done
201 [[ "${support_python_major_version}" == "1" ]] && break
168 done 202 done
169 export PYTHON_ABIS="${PYVER}" 203 if [[ "${support_python_major_version}" == "1" ]]; then
204 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
205 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
206 die "Active version of Python 2 isn't supported by ${CATEGORY}/${PF}"
207 fi
208 done
209 else
210 python2_version=""
211 fi
170 fi 212 fi
213
214 if has_version "=dev-lang/python-3*"; then
215 if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then
216 die "'/usr/bin/python3' isn't valid symlink"
217 fi
218
219 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
220
221 for PYTHON_ABI in ${python3_supported_versions}; do
222 support_python_major_version="1"
223 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
224 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
225 support_python_major_version="0"
226 fi
227 done
228 [[ "${support_python_major_version}" == "1" ]] && break
229 done
230 if [[ "${support_python_major_version}" == "1" ]]; then
231 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
232 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
233 die "Active version of Python 3 isn't supported by ${CATEGORY}/${PF}"
234 fi
235 done
236 else
237 python3_version=""
238 fi
239 fi
240
241 if ! has "${python_version}" "${python2_version}" "${python3_version}"; then
242 eerror "Python wrapper is configured incorrectly or /usr/bin/python2 or /usr/bin/python3 symlink"
243 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
244 die "Incorrect configuration of Python"
245 fi
246
247 PYTHON_ABIS="${python2_version} ${python3_version}"
248 PYTHON_ABIS="${PYTHON_ABIS# }"
249 export PYTHON_ABIS="${PYTHON_ABIS% }"
171 fi 250 fi
251 fi
172 252
253 if [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
173 local PYTHON_ABI 254 local PYTHON_ABI
174 for PYTHON_ABI in ${PYTHON_ABIS}; do 255 for PYTHON_ABI in ${PYTHON_ABIS}; do
175 # Ensure that appropriate Python version is installed. 256 # Ensure that appropriate Python version is installed.
176 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then 257 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
177 die "dev-lang/python:${PYTHON_ABI} isn't installed" 258 die "dev-lang/python:${PYTHON_ABI} isn't installed"
178 fi 259 fi
179 260
180 # Ensure that EPYTHON variable is respected. 261 # Ensure that EPYTHON variable is respected.
181 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then 262 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
263 eerror "python: '$(type -p python)'"
264 eerror "ABI: '${ABI}'"
265 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
266 eerror "EPYTHON: '$(PYTHON)'"
267 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
268 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')'"
182 die "'python' doesn't respect EPYTHON variable" 269 die "'python' doesn't respect EPYTHON variable"
183 fi 270 fi
184 done 271 done
272 PYTHON_ABIS_SANITY_CHECKS="1"
273 fi
185} 274}
186 275
187# @FUNCTION: python_copy_sources 276# @FUNCTION: python_copy_sources
188# @USAGE: [--no-link] [--] [directory] 277# @USAGE: [--no-link] [--] [directory]
189# @DESCRIPTION: 278# @DESCRIPTION:
249# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments] 338# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments]
250# @DESCRIPTION: 339# @DESCRIPTION:
251# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 340# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
252# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 341# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
253python_execute_function() { 342python_execute_function() {
254 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" 343 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0"
255 344
256 while (($#)); do 345 while (($#)); do
257 case "$1" in 346 case "$1" in
258 --action-message) 347 --action-message)
259 action_message_template="$2" 348 action_message_template="$2"
381 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 470 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
382 else 471 else
383 export BUILDDIR="${S}" 472 export BUILDDIR="${S}"
384 fi 473 fi
385 474
386 previous_directory_stack_length="${#DIRSTACK[@]}" 475 previous_directory="$(pwd)"
476 previous_directory_stack="$(dirs -p)"
477 previous_directory_stack_length="$(dirs -p | wc -l)"
387 478
388 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 479 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
389 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@" 480 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
390 else 481 else
391 EPYTHON="$(PYTHON)" "${function}" "$@" 482 EPYTHON="$(PYTHON)" "${function}" "$@"
402 if [[ "${quiet}" == "0" ]]; then 493 if [[ "${quiet}" == "0" ]]; then
403 ewarn "${RED}${failure_message}${NORMAL}" 494 ewarn "${RED}${failure_message}${NORMAL}"
404 fi 495 fi
405 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 496 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
406 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then 497 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
407 local ABI enabled_PYTHON_ABIS= 498 local enabled_PYTHON_ABIS= other_PYTHON_ABI
408 for ABI in ${PYTHON_ABIS}; do 499 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
409 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}" 500 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${other_PYTHON_ABI}"
410 done 501 done
411 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }" 502 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
412 fi 503 fi
413 if [[ "${quiet}" == "0" ]]; then 504 if [[ "${quiet}" == "0" ]]; then
414 ewarn "${RED}${failure_message}${NORMAL}" 505 ewarn "${RED}${failure_message}${NORMAL}"
419 else 510 else
420 die "${failure_message}" 511 die "${failure_message}"
421 fi 512 fi
422 fi 513 fi
423 514
515 # Ensure that directory stack hasn't been decreased.
424 if [[ "${#DIRSTACK[@]}" -lt "${previous_directory_stack_length}" ]]; then 516 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
425 die "Directory stack decreased illegally" 517 die "Directory stack decreased illegally"
426 fi 518 fi
427 519
520 # Avoid side effects of earlier returning from the specified function.
428 while [[ "${#DIRSTACK[@]}" -gt "${previous_directory_stack_length}" ]]; do 521 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
429 popd > /dev/null || die "popd failed" 522 popd > /dev/null || die "popd failed"
430 done 523 done
431 524
525 # Ensure that the bottom part of directory stack hasn't been changed. Restore
526 # previous directory (from before running of the specified function) before
527 # comparison of directory stacks to avoid mismatch of directory stacks after
528 # potential using of 'cd' to change current directory. Restoration of previous
529 # directory allows to safely use 'cd' to change current directory in the
530 # specified function without changing it back to original directory.
531 cd "${previous_directory}"
532 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
533 die "Directory stack changed illegally"
534 fi
535
432 if [[ "${separate_build_dirs}" == "1" ]]; then 536 if [[ "${separate_build_dirs}" == "1" ]]; then
433 popd > /dev/null || die "popd failed" 537 popd > /dev/null || die "popd failed"
434 fi 538 fi
435 unset BUILDDIR 539 unset BUILDDIR
436 done 540 done
438 if [[ "${default_function}" == "1" ]]; then 542 if [[ "${default_function}" == "1" ]]; then
439 unset -f python_default_function 543 unset -f python_default_function
440 fi 544 fi
441} 545}
442 546
547# @FUNCTION: python_convert_shebangs
548# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
549# @DESCRIPTION:
550# Convert shebangs in specified files. Directories can be specified only with --recursive option.
551python_convert_shebangs() {
552 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
553
554 while (($#)); do
555 case "$1" in
556 -r|--recursive)
557 recursive="1"
558 ;;
559 -q|--quiet)
560 quiet="1"
561 ;;
562 -x|--only-executables)
563 only_executables="1"
564 ;;
565 --)
566 break
567 ;;
568 -*)
569 die "${FUNCNAME}(): Unrecognized option '$1'"
570 ;;
571 *)
572 break
573 ;;
574 esac
575 shift
576 done
577
578 if [[ "$#" -eq 0 ]]; then
579 die "${FUNCNAME}(): Missing Python version and files or directories"
580 elif [[ "$#" -eq 1 ]]; then
581 die "${FUNCNAME}(): Missing files or directories"
582 fi
583
584 python_version="$1"
585 shift
586
587 for argument in "$@"; do
588 if [[ ! -e "${argument}" ]]; then
589 die "${FUNCNAME}(): '${argument}' doesn't exist"
590 elif [[ -f "${argument}" ]]; then
591 files+=("${argument}")
592 elif [[ -d "${argument}" ]]; then
593 if [[ "${recursive}" == "1" ]]; then
594 if [[ "${only_executables}" == "1" ]]; then
595 files+=($(find "${argument}" -perm /111 -type f))
596 else
597 files+=($(find "${argument}" -type f))
598 fi
599 else
600 die "${FUNCNAME}(): '${argument}' isn't a regular file"
601 fi
602 else
603 die "${FUNCNAME}(): '${argument}' isn't a regular file or a directory"
604 fi
605 done
606
607 for file in "${files[@]}"; do
608 file="${file#./}"
609 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
610
611 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
612 [[ "${quiet}" == "0" ]] && einfo "Converting shebang in '${file}'"
613 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
614
615 # Delete potential whitespace after "#!".
616 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
617 fi
618 done
619}
443 620
444# @ECLASS-VARIABLE: PYTHON_USE_WITH 621# @ECLASS-VARIABLE: PYTHON_USE_WITH
445# @DESCRIPTION: 622# @DESCRIPTION:
446# Set this to a space separated list of use flags 623# Set this to a space separated list of use flags
447# the python slot in use must be built with. 624# the python slot in use must be built with.
460# @FUNCTION: python_pkg_setup 637# @FUNCTION: python_pkg_setup
461# @DESCRIPTION: 638# @DESCRIPTION:
462# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 639# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
463# are respected. Only exported if one of those variables is set. 640# are respected. Only exported if one of those variables is set.
464if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 641if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
642 python_pkg_setup() {
465 python_pkg_setup_fail() { 643 python_pkg_setup_fail() {
466 eerror "${1}" 644 eerror "${1}"
467 die "${1}" 645 die "${1}"
646 }
647
648 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
649
650 python_pkg_setup_check_USE_flags() {
651 local pyatom use
652 if [[ -n "${PYTHON_ABI}" ]]; then
653 pyatom="dev-lang/python:${PYTHON_ABI}"
654 else
655 python_version
656 pyatom="dev-lang/python:${PYVER}"
657 fi
658
659 for use in ${PYTHON_USE_WITH}; do
660 if ! has_version "${pyatom}[${use}]"; then
661 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
662 fi
663 done
664
665 for use in ${PYTHON_USE_WITH_OR}; do
666 if has_version "${pyatom}[${use}]"; then
667 return
668 fi
669 done
670
671 if [[ ${PYTHON_USE_WITH_OR} ]]; then
672 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
673 fi
674 }
675
676 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
677 python_execute_function -q python_pkg_setup_check_USE_flags
678 else
679 python_pkg_setup_check_USE_flags
680 fi
468 } 681 }
469 682
470 python_pkg_setup() {
471 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
472
473 python_version
474 local failed
475 local pyatom="dev-lang/python:${PYVER}"
476
477 for use in ${PYTHON_USE_WITH}; do
478 if ! has_version "${pyatom}[${use}]"; then
479 python_pkg_setup_fail \
480 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}"
481 fi
482 done
483
484 for use in ${PYTHON_USE_WITH_OR}; do
485 if has_version "${pyatom}[${use}]"; then
486 return
487 fi
488 done
489
490 if [[ ${PYTHON_USE_WITH_OR} ]]; then
491 python_pkg_setup_fail \
492 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}"
493 fi
494 }
495
496 EXPORT_FUNCTIONS pkg_setup 683 EXPORT_FUNCTIONS pkg_setup
497 684
498 if [[ ${PYTHON_USE_WITH} ]]; then 685 if [[ -n "${PYTHON_USE_WITH}" ]]; then
499 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 686 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
500 elif [[ ${PYTHON_USE_WITH_OR} ]]; then 687 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
501 PYTHON_USE_WITH_ATOM="|| ( " 688 PYTHON_USE_WITH_ATOM="|| ( "
502 for use in ${PYTHON_USE_WITH_OR}; do 689 for use in ${PYTHON_USE_WITH_OR}; do
503 PYTHON_USE_WITH_ATOM=" 690 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
504 ${PYTHON_USE_WITH_ATOM}
505 ${PYTHON_ATOM}[${use}]"
506 done 691 done
507 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )" 692 unset use
693 PYTHON_USE_WITH_ATOM+=" )"
508 fi 694 fi
509 if [[ ${PYTHON_USE_WITH_OPT} ]]; then 695 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
510 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 696 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
511 fi 697 fi
512 DEPEND="${PYTHON_USE_WITH_ATOM}" 698 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
513 RDEPEND="${PYTHON_USE_WITH_ATOM}" 699 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
514fi 700fi
515 701
516# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS 702# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
517# @DESCRIPTION: 703# @DESCRIPTION:
518# Set this to define default functions for the following ebuild phases: 704# Set this to define default functions for the following ebuild phases:
544# Tell Python to automatically recompile modules to .pyc/.pyo if the 730# Tell Python to automatically recompile modules to .pyc/.pyo if the
545# timestamps/version stamps have changed. 731# timestamps/version stamps have changed.
546python_enable_pyc() { 732python_enable_pyc() {
547 unset PYTHONDONTWRITEBYTECODE 733 unset PYTHONDONTWRITEBYTECODE
548} 734}
549
550python_disable_pyc
551 735
552# @FUNCTION: python_need_rebuild 736# @FUNCTION: python_need_rebuild
553# @DESCRIPTION: Run without arguments, specifies that the package should be 737# @DESCRIPTION: Run without arguments, specifies that the package should be
554# rebuilt after a python upgrade. 738# rebuilt after a python upgrade.
555python_need_rebuild() { 739python_need_rebuild() {
761 # Don't use PYTHON_ABI in next calls to python_get_libdir(). 945 # Don't use PYTHON_ABI in next calls to python_get_libdir().
762 unset PYTHON_ABI 946 unset PYTHON_ABI
763 947
764 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 948 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
765 return_code="0" 949 return_code="0"
766 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." 950 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}"
767 if ((${#other_dirs[@]})); then 951 if ((${#other_dirs[@]})); then
768 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 952 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
769 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" 953 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
770 fi 954 fi
771 if ((${#other_files[@]})); then 955 if ((${#other_files[@]})); then
817 fi 1001 fi
818 1002
819 # set additional opts 1003 # set additional opts
820 myopts+=(-q) 1004 myopts+=(-q)
821 1005
822 ebegin "Byte compiling python modules for python-${PYVER} .." 1006 ebegin "Compilation and optimization of Python modules for Python ${PYVER}"
823 if ((${#mydirs[@]})); then 1007 if ((${#mydirs[@]})); then
824 python${PYVER} \ 1008 python${PYVER} \
825 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 1009 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
826 "${myopts[@]}" "${mydirs[@]}" || return_code="1" 1010 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
827 python${PYVER} -O \ 1011 python${PYVER} -O \
836 eend "${return_code}" 1020 eend "${return_code}"
837 fi 1021 fi
838} 1022}
839 1023
840# @FUNCTION: python_mod_cleanup 1024# @FUNCTION: python_mod_cleanup
841# @USAGE: [directory] 1025# @USAGE: [directory|file]
842# @DESCRIPTION: 1026# @DESCRIPTION:
843# Run with optional arguments, where arguments are directories of 1027# Run with optional arguments, where arguments are Python modules. If none given,
844# python modules. If none given, it will look in /usr/lib/python[0-9].[0-9]. 1028# it will look in /usr/lib/python[0-9].[0-9].
845# 1029#
846# It will recursively scan all compiled Python modules in the directories and 1030# It will recursively scan all compiled Python modules in the directories and
847# determine if they are orphaned (i.e. their corresponding .py files are missing.) 1031# determine if they are orphaned (i.e. their corresponding .py files are missing.)
848# If they are, then it will remove their corresponding .pyc and .pyo files. 1032# If they are, then it will remove their corresponding .pyc and .pyo files.
849# 1033#
850# This function should only be run in pkg_postrm(). 1034# This function should only be run in pkg_postrm().
851python_mod_cleanup() { 1035python_mod_cleanup() {
852 local PYTHON_ABI SEARCH_PATH=() root src_py 1036 local path py_file PYTHON_ABI SEARCH_PATH=() root
853 1037
854 # Check if phase is pkg_postrm(). 1038 # Check if phase is pkg_postrm().
855 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" 1039 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
856 1040
857 # Strip trailing slash from ROOT. 1041 # Strip trailing slash from ROOT.
874 else 1058 else
875 SEARCH_PATH=("${@#/}") 1059 SEARCH_PATH=("${@#/}")
876 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 1060 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
877 fi 1061 fi
878 else 1062 else
879 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) 1063 local dir sitedir
1064 for dir in "${root}"/usr/lib*; do
1065 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
1066 for sitedir in "${dir}"/python*/site-packages; do
1067 if [[ -d "${sitedir}" ]]; then
1068 SEARCH_PATH+=("${sitedir}")
1069 fi
1070 done
1071 fi
1072 done
1073 fi
1074
1075 local BLUE CYAN NORMAL
1076 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1077 BLUE=$'\e[1;34m'
1078 CYAN=$'\e[1;36m'
1079 NORMAL=$'\e[0m'
1080 else
1081 BLUE=
1082 CYAN=
1083 NORMAL=
880 fi 1084 fi
881 1085
882 for path in "${SEARCH_PATH[@]}"; do 1086 for path in "${SEARCH_PATH[@]}"; do
883 [[ ! -d "${path}" ]] && continue 1087 if [[ -d "${path}" ]]; then
884 einfo "Cleaning orphaned Python bytecode from ${path} .."
885 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 1088 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
886 src_py="${REPLY%[co]}" 1089 py_file="${REPLY%[co]}"
887 [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue 1090 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
888 einfo "Purging ${src_py}[co]" 1091 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
889 rm -f "${src_py}"[co] 1092 rm -f "${py_file}"[co]
890 done 1093 done
891 1094
892 # Attempt to remove directories that may be empty. 1095 # Attempt to delete directories, which may be empty.
893 find "${path}" -type d | sort -r | while read -r dir; do 1096 find "${path}" -type d | sort -r | while read -r dir; do
894 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}" 1097 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
895 done 1098 done
1099 elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then
1100 einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1101 rm -f "${path}"[co]
1102 fi
896 done 1103 done
897} 1104}

Legend:
Removed from v.1.72  
changed lines
  Added in v.1.80

  ViewVC Help
Powered by ViewVC 1.1.20