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

Diff of /eclass/python.eclass

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

Revision 1.66 Revision 1.73
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.66 2009/08/28 16:08:51 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.73 2009/09/18 17:50:08 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
66 export PYVER_ALL="${tmpstr#Python }" 64 export PYVER_ALL="${tmpstr#Python }"
67 __python_version_extract $PYVER_ALL 65 __python_version_extract $PYVER_ALL
68} 66}
69 67
70# @FUNCTION: PYTHON 68# @FUNCTION: PYTHON
71# @USAGE: [-a|--absolute-path] <Python_ABI="${PYTHON_ABI}"> 69# @USAGE: [-a|--absolute-path] [--] <Python_ABI="${PYTHON_ABI}">
72# @DESCRIPTION: 70# @DESCRIPTION:
73# Get Python interpreter filename for specified Python ABI. If Python_ABI argument 71# Get Python interpreter filename for specified Python ABI. If Python_ABI argument
74# is ommitted, then PYTHON_ABI environment variable must be set and is used. 72# is ommitted, then PYTHON_ABI environment variable must be set and is used.
75PYTHON() { 73PYTHON() {
76 local absolute_path="0" slot= 74 local absolute_path="0" slot=
77 75
78 while (($#)); do 76 while (($#)); do
79 case "$1" in 77 case "$1" in
80 -a|--absolute-path) 78 -a|--absolute-path)
81 absolute_path="1" 79 absolute_path="1"
80 ;;
81 --)
82 break
82 ;; 83 ;;
83 -*) 84 -*)
84 die "${FUNCNAME}(): Unrecognized option $1" 85 die "${FUNCNAME}(): Unrecognized option $1"
85 ;; 86 ;;
86 *) 87 *)
125 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then 126 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
126 die "'/usr/bin/python-config' isn't valid script" 127 die "'/usr/bin/python-config' isn't valid script"
127 fi 128 fi
128 129
129 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. 130 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4.
130 if [[ -z "${PYTHON_ABIS}" ]] && has "${EAPI:-0}" 0 1 2 3; then 131 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then
131 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI 132 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI
132 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" 133 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
134
135 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
136 if [[ -z "${USE_PYTHON}" ]]; then
137 die "USE_PYTHON variable is empty"
138 fi
139
133 for ABI in ${USE_PYTHON}; do 140 for ABI in ${USE_PYTHON}; do
134 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 141 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
135 ewarn "Ignoring unsupported Python ABI '${ABI}'" 142 die "USE_PYTHON variable contains invalid value '${ABI}'"
136 continue
137 fi 143 fi
138 support_ABI="1" 144 support_ABI="1"
139 if [[ -z "${IGNORE_RESTRICT_PYTHON_ABIS}" ]]; then
140 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 145 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
141 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 146 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then
142 support_ABI="0" 147 support_ABI="0"
143 break 148 break
144 fi 149 fi
145 done 150 done
146 fi
147 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 151 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}"
148 done 152 done
149 export PYTHON_ABIS="${supported_PYTHON_ABIS# }" 153 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
150 fi
151 154
152 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then 155 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
156 die "USE_PYTHON variable doesn't enable any Python version supported by ${CATEGORY}/${PF}"
157 fi
158 else
159 local restricted_ABI
153 python_version 160 python_version
161
162 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
163 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${PYVER}', '${restricted_ABI}'))"; then
164 die "Active Python version isn't supported by ${CATEGORY}/${PF}"
165 fi
166 done
154 export PYTHON_ABIS="${PYVER}" 167 export PYTHON_ABIS="${PYVER}"
155 fi 168 fi
169 fi
156 170
157 # Ensure that EPYTHON variable is respected.
158 local PYTHON_ABI 171 local PYTHON_ABI
159 for PYTHON_ABI in ${PYTHON_ABIS}; do 172 for PYTHON_ABI in ${PYTHON_ABIS}; do
173 # Ensure that appropriate Python version is installed.
174 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
175 die "dev-lang/python:${PYTHON_ABI} isn't installed"
176 fi
177
178 # Ensure that EPYTHON variable is respected.
160 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then 179 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
161 die "'python' doesn't respect EPYTHON variable" 180 die "'python' doesn't respect EPYTHON variable"
162 fi 181 fi
163 done 182 done
164} 183}
165 184
166# @FUNCTION: python_copy_sources 185# @FUNCTION: python_copy_sources
167# @USAGE: [directory] 186# @USAGE: [--no-link] [--] [directory]
168# @DESCRIPTION: 187# @DESCRIPTION:
169# Copy unpacked sources of given package for each Python ABI. 188# Copy unpacked sources of given package for each Python ABI.
170python_copy_sources() { 189python_copy_sources() {
171 local dir dirs=() PYTHON_ABI 190 local dir dirs=() no_link="0" PYTHON_ABI
191
192 while (($#)); do
193 case "$1" in
194 --no-link)
195 no_link="1"
196 ;;
197 --)
198 break
199 ;;
200 -*)
201 die "${FUNCNAME}(): Unrecognized option '$1'"
202 ;;
203 *)
204 break
205 ;;
206 esac
207 shift
208 done
172 209
173 if [[ "$#" -eq "0" ]]; then 210 if [[ "$#" -eq "0" ]]; then
174 if [[ "${WORKDIR}" == "${S}" ]]; then 211 if [[ "${WORKDIR}" == "${S}" ]]; then
175 die "${FUNCNAME}() cannot be used" 212 die "${FUNCNAME}() cannot be used"
176 fi 213 fi
180 fi 217 fi
181 218
182 validate_PYTHON_ABIS 219 validate_PYTHON_ABIS
183 for PYTHON_ABI in ${PYTHON_ABIS}; do 220 for PYTHON_ABI in ${PYTHON_ABIS}; do
184 for dir in "${dirs[@]}"; do 221 for dir in "${dirs[@]}"; do
222 if [[ "${no_link}" == "1" ]]; then
223 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
224 else
185 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed" 225 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
226 fi
186 done 227 done
187 done 228 done
188} 229}
189 230
190# @FUNCTION: python_set_build_dir_symlink 231# @FUNCTION: python_set_build_dir_symlink
201 rm -f "${dir}" || die "Deletion of '${dir}' failed" 242 rm -f "${dir}" || die "Deletion of '${dir}' failed"
202 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed" 243 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
203} 244}
204 245
205# @FUNCTION: python_execute_function 246# @FUNCTION: python_execute_function
206# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] <function> [arguments] 247# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments]
207# @DESCRIPTION: 248# @DESCRIPTION:
208# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 249# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
209# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 250# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
210python_execute_function() { 251python_execute_function() {
211 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" PYTHON_ABI quiet="0" separate_build_dirs="0" 252 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"
212 253
213 while (($#)); do 254 while (($#)); do
214 case "$1" in 255 case "$1" in
215 --action-message) 256 --action-message)
216 action_message_template="$2" 257 action_message_template="$2"
230 quiet="1" 271 quiet="1"
231 ;; 272 ;;
232 -s|--separate-build-dirs) 273 -s|--separate-build-dirs)
233 separate_build_dirs="1" 274 separate_build_dirs="1"
234 ;; 275 ;;
276 --)
277 break
278 ;;
235 -*) 279 -*)
236 die "${FUNCNAME}(): Unrecognized option $1" 280 die "${FUNCNAME}(): Unrecognized option '$1'"
237 ;; 281 ;;
238 *) 282 *)
239 break 283 break
240 ;; 284 ;;
241 esac 285 esac
246 if [[ "$#" -eq "0" ]]; then 290 if [[ "$#" -eq "0" ]]; then
247 die "${FUNCNAME}(): Missing function name" 291 die "${FUNCNAME}(): Missing function name"
248 fi 292 fi
249 function="$1" 293 function="$1"
250 shift 294 shift
295
296 if [[ -z "$(type -t "${function}")" ]]; then
297 die "${FUNCNAME}(): '${function}' function isn't defined"
298 fi
251 else 299 else
252 if [[ "$#" -ne "0" ]]; then 300 if [[ "$#" -ne "0" ]]; then
253 die "${FUNCNAME}(): --default-function option and function name cannot be specified simultaneously" 301 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
254 fi 302 fi
255 if has "${EAPI:-0}" 0 1; then 303 if has "${EAPI:-0}" 0 1; then
256 die "${FUNCNAME}(): --default-function option cannot be used in this EAPI" 304 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
257 fi 305 fi
258 306
259 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 307 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
260 if has "${EAPI}" 2; then 308 if has "${EAPI}" 2; then
261 python_default_function() { 309 python_default_function() {
331 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 379 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
332 else 380 else
333 export BUILDDIR="${S}" 381 export BUILDDIR="${S}"
334 fi 382 fi
335 383
384 previous_directory="$(pwd)"
385 previous_directory_stack="$(dirs -p)"
386 previous_directory_stack_length="$(dirs -p | wc -l)"
387
336 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 388 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
337 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@" 389 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
338 else 390 else
339 EPYTHON="$(PYTHON)" "${function}" "$@" 391 EPYTHON="$(PYTHON)" "${function}" "$@"
340 fi 392 fi
344 failure_message="$(eval echo -n "${failure_message_template}")" 396 failure_message="$(eval echo -n "${failure_message_template}")"
345 else 397 else
346 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function" 398 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
347 fi 399 fi
348 400
349 if [[ "${nonfatal}" == "1" ]] || has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 401 if [[ "${nonfatal}" == "1" ]]; then
350 local ABI enabled_PYTHON_ABIS
351 for ABI in ${PYTHON_ABIS}; do
352 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
353 done
354 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
355 if [[ "${quiet}" == "0" ]]; then 402 if [[ "${quiet}" == "0" ]]; then
356 ewarn "${RED}${failure_message}${NORMAL}" 403 ewarn "${RED}${failure_message}${NORMAL}"
357 fi 404 fi
405 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
406 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
407 local ABI enabled_PYTHON_ABIS=
408 for ABI in ${PYTHON_ABIS}; do
409 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
410 done
411 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
412 fi
413 if [[ "${quiet}" == "0" ]]; then
414 ewarn "${RED}${failure_message}${NORMAL}"
415 fi
416 if [[ -z "${PYTHON_ABIS}" ]]; then
417 die "${function}() function failed with all enabled versions of Python"
418 fi
358 else 419 else
359 die "${failure_message}" 420 die "${failure_message}"
360 fi 421 fi
422 fi
423
424 # Ensure that directory stack hasn't been decreased.
425 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
426 die "Directory stack decreased illegally"
427 fi
428
429 # Avoid side effects of earlier returning from the specified function.
430 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
431 popd > /dev/null || die "popd failed"
432 done
433
434 # Ensure that the bottom part of directory stack hasn't been changed. Restore
435 # previous directory (from before running of the specified function) before
436 # comparison of directory stacks to avoid mismatch of directory stacks after
437 # potential using of 'cd' to change current directory. Restoration of previous
438 # directory allows to safely use 'cd' to change current directory in the
439 # specified function without changing it back to original directory.
440 cd "${previous_directory}"
441 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
442 die "Directory stack changed illegally"
361 fi 443 fi
362 444
363 if [[ "${separate_build_dirs}" == "1" ]]; then 445 if [[ "${separate_build_dirs}" == "1" ]]; then
364 popd > /dev/null || die "popd failed" 446 popd > /dev/null || die "popd failed"
365 fi 447 fi
390 472
391# @FUNCTION: python_pkg_setup 473# @FUNCTION: python_pkg_setup
392# @DESCRIPTION: 474# @DESCRIPTION:
393# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 475# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
394# are respected. Only exported if one of those variables is set. 476# are respected. Only exported if one of those variables is set.
395if ! has ${EAPI:-0} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 477if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
396 python_pkg_setup_fail() { 478 python_pkg_setup_fail() {
397 eerror "${1}" 479 eerror "${1}"
398 die "${1}" 480 die "${1}"
399 } 481 }
400 482
440 if [[ ${PYTHON_USE_WITH_OPT} ]]; then 522 if [[ ${PYTHON_USE_WITH_OPT} ]]; then
441 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 523 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
442 fi 524 fi
443 DEPEND="${PYTHON_USE_WITH_ATOM}" 525 DEPEND="${PYTHON_USE_WITH_ATOM}"
444 RDEPEND="${PYTHON_USE_WITH_ATOM}" 526 RDEPEND="${PYTHON_USE_WITH_ATOM}"
527fi
528
529# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
530# @DESCRIPTION:
531# Set this to define default functions for the following ebuild phases:
532# src_prepare, src_configure, src_compile, src_test, src_install.
533if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
534 python_src_prepare() {
535 python_copy_sources
536 }
537
538 for python_default_function in src_configure src_compile src_test src_install; do
539 eval "python_${python_default_function}() { python_execute_function -d -s; }"
540 done
541 unset python_default_function
542
543 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
445fi 544fi
446 545
447# @FUNCTION: python_disable_pyc 546# @FUNCTION: python_disable_pyc
448# @DESCRIPTION: 547# @DESCRIPTION:
449# Tell Python not to automatically recompile modules to .pyc/.pyo 548# Tell Python not to automatically recompile modules to .pyc/.pyo

Legend:
Removed from v.1.66  
changed lines
  Added in v.1.73

  ViewVC Help
Powered by ViewVC 1.1.20