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

Diff of /eclass/python.eclass

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

Revision 1.76 Revision 1.77
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.76 2009/10/02 23:09:08 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.77 2009/10/11 13:34:23 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# @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.
9# @DESCRIPTION: 9# @DESCRIPTION:
10# Some useful functions for dealing with Python. 10# Some useful functions for dealing with Python.
11 11
12inherit multilib 12inherit multilib
13 13
14if [[ -n "${NEED_PYTHON}" ]] ; then 14if [[ -n "${NEED_PYTHON}" ]]; then
15 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" 15 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
16 DEPEND="${PYTHON_ATOM}" 16 DEPEND="${PYTHON_ATOM}"
17 RDEPEND="${DEPEND}" 17 RDEPEND="${DEPEND}"
18else 18else
19 PYTHON_ATOM="dev-lang/python" 19 PYTHON_ATOM="dev-lang/python"
106 else 106 else
107 echo -n "python${slot}" 107 echo -n "python${slot}"
108 fi 108 fi
109} 109}
110 110
111unset PYTHON_ABIS
112unset PYTHON_ABIS_SANITY_CHECKS
113
111# @FUNCTION: validate_PYTHON_ABIS 114# @FUNCTION: validate_PYTHON_ABIS
112# @DESCRIPTION: 115# @DESCRIPTION:
113# Ensure that PYTHON_ABIS variable has valid value. 116# Ensure that PYTHON_ABIS variable has valid value.
114validate_PYTHON_ABIS() { 117validate_PYTHON_ABIS() {
115 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable. 118 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable.
151 python3_enabled="1" 154 python3_enabled="1"
152 fi 155 fi
153 156
154 support_ABI="1" 157 support_ABI="1"
155 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 158 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
156 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 159 if [[ "${ABI}" == ${restricted_ABI} ]]; then
157 support_ABI="0" 160 support_ABI="0"
158 break 161 break
159 fi 162 fi
160 done 163 done
161 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 164 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}"
185 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" 188 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
186 189
187 for ABI in ${python2_supported_versions}; do 190 for ABI in ${python2_supported_versions}; do
188 support_python_major_version="1" 191 support_python_major_version="1"
189 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 192 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
190 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 193 if [[ "${ABI}" == ${restricted_ABI} ]]; then
191 support_python_major_version="0" 194 support_python_major_version="0"
192 fi 195 fi
193 done 196 done
194 [[ "${support_python_major_version}" == "1" ]] && break 197 [[ "${support_python_major_version}" == "1" ]] && break
195 done 198 done
196 if [[ "${support_python_major_version}" == "1" ]]; then 199 if [[ "${support_python_major_version}" == "1" ]]; then
197 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 200 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
198 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${python2_version}', '${restricted_ABI}'))"; then 201 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
199 die "Active version of Python 2 isn't supported by ${CATEGORY}/${PF}" 202 die "Active version of Python 2 isn't supported by ${CATEGORY}/${PF}"
200 fi 203 fi
201 done 204 done
202 else 205 else
203 python2_version="" 206 python2_version=""
212 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" 215 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
213 216
214 for ABI in ${python3_supported_versions}; do 217 for ABI in ${python3_supported_versions}; do
215 support_python_major_version="1" 218 support_python_major_version="1"
216 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 219 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
217 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 220 if [[ "${ABI}" == ${restricted_ABI} ]]; then
218 support_python_major_version="0" 221 support_python_major_version="0"
219 fi 222 fi
220 done 223 done
221 [[ "${support_python_major_version}" == "1" ]] && break 224 [[ "${support_python_major_version}" == "1" ]] && break
222 done 225 done
223 if [[ "${support_python_major_version}" == "1" ]]; then 226 if [[ "${support_python_major_version}" == "1" ]]; then
224 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 227 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
225 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${python3_version}', '${restricted_ABI}'))"; then 228 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
226 die "Active version of Python 3 isn't supported by ${CATEGORY}/${PF}" 229 die "Active version of Python 3 isn't supported by ${CATEGORY}/${PF}"
227 fi 230 fi
228 done 231 done
229 else 232 else
230 python3_version="" 233 python3_version=""
241 PYTHON_ABIS="${PYTHON_ABIS# }" 244 PYTHON_ABIS="${PYTHON_ABIS# }"
242 export PYTHON_ABIS="${PYTHON_ABIS% }" 245 export PYTHON_ABIS="${PYTHON_ABIS% }"
243 fi 246 fi
244 fi 247 fi
245 248
249 if [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
246 local PYTHON_ABI 250 local PYTHON_ABI
247 for PYTHON_ABI in ${PYTHON_ABIS}; do 251 for PYTHON_ABI in ${PYTHON_ABIS}; do
248 # Ensure that appropriate Python version is installed. 252 # Ensure that appropriate Python version is installed.
249 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then 253 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
250 die "dev-lang/python:${PYTHON_ABI} isn't installed" 254 die "dev-lang/python:${PYTHON_ABI} isn't installed"
251 fi 255 fi
252 256
253 # Ensure that EPYTHON variable is respected. 257 # Ensure that EPYTHON variable is respected.
254 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then 258 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
255 die "'python' doesn't respect EPYTHON variable" 259 die "'python' doesn't respect EPYTHON variable"
256 fi 260 fi
257 done 261 done
262 PYTHON_ABIS_SANITY_CHECKS="1"
263 fi
258} 264}
259 265
260# @FUNCTION: python_copy_sources 266# @FUNCTION: python_copy_sources
261# @USAGE: [--no-link] [--] [directory] 267# @USAGE: [--no-link] [--] [directory]
262# @DESCRIPTION: 268# @DESCRIPTION:
526 if [[ "${default_function}" == "1" ]]; then 532 if [[ "${default_function}" == "1" ]]; then
527 unset -f python_default_function 533 unset -f python_default_function
528 fi 534 fi
529} 535}
530 536
537# @FUNCTION: python_convert_shebangs
538# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
539# @DESCRIPTION:
540# Convert shebangs in specified files. Directories can be specified only with --recursive option.
541python_convert_shebangs() {
542 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
543
544 while (($#)); do
545 case "$1" in
546 -r|--recursive)
547 recursive="1"
548 ;;
549 -q|--quiet)
550 quiet="1"
551 ;;
552 -x|--only-executables)
553 only_executables="1"
554 ;;
555 --)
556 break
557 ;;
558 -*)
559 die "${FUNCNAME}(): Unrecognized option '$1'"
560 ;;
561 *)
562 break
563 ;;
564 esac
565 shift
566 done
567
568 if [[ "$#" -eq 0 ]]; then
569 die "${FUNCNAME}(): Missing Python version and files or directories"
570 elif [[ "$#" -eq 1 ]]; then
571 die "${FUNCNAME}(): Missing files or directories"
572 fi
573
574 python_version="$1"
575 shift
576
577 for argument in "$@"; do
578 if [[ ! -e "${argument}" ]]; then
579 die "${FUNCNAME}(): '${argument}' doesn't exist"
580 elif [[ -f "${argument}" ]]; then
581 files+=("${argument}")
582 elif [[ -d "${argument}" ]]; then
583 if [[ "${recursive}" == "1" ]]; then
584 if [[ "${only_executables}" == "1" ]]; then
585 files+=($(find "${argument}" -perm /111 -type f))
586 else
587 files+=($(find "${argument}" -type f))
588 fi
589 else
590 die "${FUNCNAME}(): '${argument}' isn't a regular file"
591 fi
592 else
593 die "${FUNCNAME}(): '${argument}' isn't a regular file or a directory"
594 fi
595 done
596
597 for file in "${files[@]}"; do
598 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
599
600 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
601 [[ "${quiet}" == "0" ]] && einfo "Converting shebang in '${file}'"
602 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
603
604 # Delete potential whitespace after "#!".
605 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
606 fi
607 done
608}
531 609
532# @ECLASS-VARIABLE: PYTHON_USE_WITH 610# @ECLASS-VARIABLE: PYTHON_USE_WITH
533# @DESCRIPTION: 611# @DESCRIPTION:
534# Set this to a space separated list of use flags 612# Set this to a space separated list of use flags
535# the python slot in use must be built with. 613# the python slot in use must be built with.
563 if [[ -n "${PYTHON_ABI}" ]]; then 641 if [[ -n "${PYTHON_ABI}" ]]; then
564 pyatom="dev-lang/python:${PYTHON_ABI}" 642 pyatom="dev-lang/python:${PYTHON_ABI}"
565 else 643 else
566 python_version 644 python_version
567 pyatom="dev-lang/python:${PYVER}" 645 pyatom="dev-lang/python:${PYVER}"
568 fi
569
570 # Workaround for older versions of Portage.
571 # has_version() calls portageq which is implemented in Python.
572 if has_version "=dev-lang/python-2*"; then
573 local EPYTHON
574 export EPYTHON="$(readlink /usr/bin/python2)"
575 fi 646 fi
576 647
577 for use in ${PYTHON_USE_WITH}; do 648 for use in ${PYTHON_USE_WITH}; do
578 if ! has_version "${pyatom}[${use}]"; then 649 if ! has_version "${pyatom}[${use}]"; then
579 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}" 650 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"

Legend:
Removed from v.1.76  
changed lines
  Added in v.1.77

  ViewVC Help
Powered by ViewVC 1.1.20