/[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.81
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.81 2009/11/22 16:45:54 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"
66 export PYVER_ALL="${tmpstr#Python }" 62 export PYVER_ALL="${tmpstr#Python }"
67 __python_version_extract $PYVER_ALL 63 __python_version_extract $PYVER_ALL
68} 64}
69 65
70# @FUNCTION: PYTHON 66# @FUNCTION: PYTHON
71# @USAGE: [-a|--absolute-path] <Python_ABI="${PYTHON_ABI}"> 67# @USAGE: [-a|--absolute-path] [--] <Python_ABI="${PYTHON_ABI}">
72# @DESCRIPTION: 68# @DESCRIPTION:
73# Get Python interpreter filename for specified Python ABI. If Python_ABI argument 69# 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. 70# is ommitted, then PYTHON_ABI environment variable must be set and is used.
75PYTHON() { 71PYTHON() {
76 local absolute_path="0" slot= 72 local absolute_path="0" slot=
77 73
78 while (($#)); do 74 while (($#)); do
79 case "$1" in 75 case "$1" in
80 -a|--absolute-path) 76 -a|--absolute-path)
81 absolute_path="1" 77 absolute_path="1"
78 ;;
79 --)
80 break
82 ;; 81 ;;
83 -*) 82 -*)
84 die "${FUNCNAME}(): Unrecognized option $1" 83 die "${FUNCNAME}(): Unrecognized option $1"
85 ;; 84 ;;
86 *) 85 *)
105 if [[ "${absolute_path}" == "1" ]]; then 104 if [[ "${absolute_path}" == "1" ]]; then
106 echo -n "/usr/bin/python${slot}" 105 echo -n "/usr/bin/python${slot}"
107 else 106 else
108 echo -n "python${slot}" 107 echo -n "python${slot}"
109 fi 108 fi
109
110 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
111 echo -n "-${ABI}"
112 fi
110} 113}
114
115unset PYTHON_ABIS
116unset PYTHON_ABIS_SANITY_CHECKS
111 117
112# @FUNCTION: validate_PYTHON_ABIS 118# @FUNCTION: validate_PYTHON_ABIS
113# @DESCRIPTION: 119# @DESCRIPTION:
114# Make sure PYTHON_ABIS variable has valid value. 120# Ensure that PYTHON_ABIS variable has valid value.
115validate_PYTHON_ABIS() { 121validate_PYTHON_ABIS() {
116 # 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.
117 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
118 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"
119 fi 125 fi
125 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then 131 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
126 die "'/usr/bin/python-config' isn't valid script" 132 die "'/usr/bin/python-config' isn't valid script"
127 fi 133 fi
128 134
129 # 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.
130 if [[ -z "${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
131 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=
132 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"
141
142 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
143 local python2_enabled="0" python3_enabled="0"
144
145 if [[ -z "${USE_PYTHON}" ]]; then
146 die "USE_PYTHON variable is empty"
147 fi
148
133 for ABI in ${USE_PYTHON}; do 149 for PYTHON_ABI in ${USE_PYTHON}; do
134 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 150 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
135 ewarn "Ignoring unsupported Python ABI '${ABI}'" 151 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
136 continue
137 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
138 support_ABI="1" 161 support_ABI="1"
139 if [[ -z "${IGNORE_RESTRICT_PYTHON_ABIS}" ]]; then
140 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 162 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
141 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 163 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
142 support_ABI="0" 164 support_ABI="0"
143 break 165 break
144 fi 166 fi
145 done 167 done
146 fi
147 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 168 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${PYTHON_ABI}"
169 done
170 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
171
172 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
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."
181 fi
182 else
183 local python_version python2_version= python3_version= support_python_major_version
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"
196 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
197 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
198 support_python_major_version="0"
199 fi
200 done
201 [[ "${support_python_major_version}" == "1" ]] && break
202 done
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
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% }"
250 fi
251 fi
252
253 if [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
254 local PYTHON_ABI
255 for PYTHON_ABI in ${PYTHON_ABIS}; do
256 # Ensure that appropriate Python version is installed.
257 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
258 die "dev-lang/python:${PYTHON_ABI} isn't installed"
259 fi
260
261 # Ensure that EPYTHON variable is respected.
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]]))')'"
269 die "'python' doesn't respect EPYTHON variable"
270 fi
148 done 271 done
149 export PYTHON_ABIS="${supported_PYTHON_ABIS# }" 272 PYTHON_ABIS_SANITY_CHECKS="1"
150 fi
151
152 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
153 python_version
154 export PYTHON_ABIS="${PYVER}"
155 fi
156
157 # Ensure that EPYTHON variable is respected.
158 local PYTHON_ABI
159 for PYTHON_ABI in ${PYTHON_ABIS}; do
160 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"
162 fi 273 fi
163 done
164} 274}
165 275
166# @FUNCTION: python_copy_sources 276# @FUNCTION: python_copy_sources
167# @USAGE: [directory] 277# @USAGE: [--no-link] [--] [directory]
168# @DESCRIPTION: 278# @DESCRIPTION:
169# Copy unpacked sources of given package for each Python ABI. 279# Copy unpacked sources of given package for each Python ABI.
170python_copy_sources() { 280python_copy_sources() {
171 local dir dirs=() PYTHON_ABI 281 local dir dirs=() no_link="0" PYTHON_ABI
282
283 while (($#)); do
284 case "$1" in
285 --no-link)
286 no_link="1"
287 ;;
288 --)
289 break
290 ;;
291 -*)
292 die "${FUNCNAME}(): Unrecognized option '$1'"
293 ;;
294 *)
295 break
296 ;;
297 esac
298 shift
299 done
172 300
173 if [[ "$#" -eq "0" ]]; then 301 if [[ "$#" -eq "0" ]]; then
174 if [[ "${WORKDIR}" == "${S}" ]]; then 302 if [[ "${WORKDIR}" == "${S}" ]]; then
175 die "${FUNCNAME}() cannot be used" 303 die "${FUNCNAME}() cannot be used"
176 fi 304 fi
180 fi 308 fi
181 309
182 validate_PYTHON_ABIS 310 validate_PYTHON_ABIS
183 for PYTHON_ABI in ${PYTHON_ABIS}; do 311 for PYTHON_ABI in ${PYTHON_ABIS}; do
184 for dir in "${dirs[@]}"; do 312 for dir in "${dirs[@]}"; do
313 if [[ "${no_link}" == "1" ]]; then
314 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
315 else
185 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed" 316 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
317 fi
186 done 318 done
187 done 319 done
188} 320}
189 321
190# @FUNCTION: python_set_build_dir_symlink 322# @FUNCTION: python_set_build_dir_symlink
201 rm -f "${dir}" || die "Deletion of '${dir}' failed" 333 rm -f "${dir}" || die "Deletion of '${dir}' failed"
202 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed" 334 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
203} 335}
204 336
205# @FUNCTION: python_execute_function 337# @FUNCTION: python_execute_function
206# @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] [--source-dir source_directory] [--] <function> [arguments]
207# @DESCRIPTION: 339# @DESCRIPTION:
208# 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
209# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 341# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
210python_execute_function() { 342python_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" 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" source_dir=
212 344
213 while (($#)); do 345 while (($#)); do
214 case "$1" in 346 case "$1" in
215 --action-message) 347 --action-message)
216 action_message_template="$2" 348 action_message_template="$2"
230 quiet="1" 362 quiet="1"
231 ;; 363 ;;
232 -s|--separate-build-dirs) 364 -s|--separate-build-dirs)
233 separate_build_dirs="1" 365 separate_build_dirs="1"
234 ;; 366 ;;
367 --source-dir)
368 source_dir="$2"
369 shift
370 ;;
371 --)
372 break
373 ;;
235 -*) 374 -*)
236 die "${FUNCNAME}(): Unrecognized option $1" 375 die "${FUNCNAME}(): Unrecognized option '$1'"
237 ;; 376 ;;
238 *) 377 *)
239 break 378 break
240 ;; 379 ;;
241 esac 380 esac
242 shift 381 shift
243 done 382 done
244 383
384 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
385 die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
386 fi
387
245 if [[ "${default_function}" == "0" ]]; then 388 if [[ "${default_function}" == "0" ]]; then
246 if [[ "$#" -eq "0" ]]; then 389 if [[ "$#" -eq "0" ]]; then
247 die "${FUNCNAME}(): Missing function name" 390 die "${FUNCNAME}(): Missing function name"
248 fi 391 fi
249 function="$1" 392 function="$1"
250 shift 393 shift
394
395 if [[ -z "$(type -t "${function}")" ]]; then
396 die "${FUNCNAME}(): '${function}' function isn't defined"
397 fi
251 else 398 else
252 if [[ "$#" -ne "0" ]]; then 399 if [[ "$#" -ne "0" ]]; then
253 die "${FUNCNAME}(): --default-function option and function name cannot be specified simultaneously" 400 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
254 fi 401 fi
255 if has "${EAPI:-0}" 0 1; then 402 if has "${EAPI:-0}" 0 1; then
256 die "${FUNCNAME}(): --default-function option cannot be used in this EAPI" 403 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
257 fi 404 fi
258 405
259 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 406 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
260 if has "${EAPI}" 2; then 407 if has "${EAPI}" 2; then
261 python_default_function() { 408 python_default_function() {
325 fi 472 fi
326 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 473 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
327 fi 474 fi
328 475
329 if [[ "${separate_build_dirs}" == "1" ]]; then 476 if [[ "${separate_build_dirs}" == "1" ]]; then
477 if [[ -n "${source_dir}" ]]; then
478 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
479 else
330 export BUILDDIR="${S}-${PYTHON_ABI}" 480 export BUILDDIR="${S}-${PYTHON_ABI}"
481 fi
331 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 482 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
332 else 483 else
333 export BUILDDIR="${S}" 484 export BUILDDIR="${S}"
334 fi 485 fi
486
487 previous_directory="$(pwd)"
488 previous_directory_stack="$(dirs -p)"
489 previous_directory_stack_length="$(dirs -p | wc -l)"
335 490
336 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 491 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
337 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@" 492 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
338 else 493 else
339 EPYTHON="$(PYTHON)" "${function}" "$@" 494 EPYTHON="$(PYTHON)" "${function}" "$@"
344 failure_message="$(eval echo -n "${failure_message_template}")" 499 failure_message="$(eval echo -n "${failure_message_template}")"
345 else 500 else
346 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function" 501 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
347 fi 502 fi
348 503
349 if [[ "${nonfatal}" == "1" ]] || has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 504 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 505 if [[ "${quiet}" == "0" ]]; then
356 ewarn "${RED}${failure_message}${NORMAL}" 506 ewarn "${RED}${failure_message}${NORMAL}"
357 fi 507 fi
508 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
509 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
510 local enabled_PYTHON_ABIS= other_PYTHON_ABI
511 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
512 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${other_PYTHON_ABI}"
513 done
514 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
515 fi
516 if [[ "${quiet}" == "0" ]]; then
517 ewarn "${RED}${failure_message}${NORMAL}"
518 fi
519 if [[ -z "${PYTHON_ABIS}" ]]; then
520 die "${function}() function failed with all enabled versions of Python"
521 fi
358 else 522 else
359 die "${failure_message}" 523 die "${failure_message}"
360 fi 524 fi
361 fi 525 fi
362 526
527 # Ensure that directory stack hasn't been decreased.
528 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
529 die "Directory stack decreased illegally"
530 fi
531
532 # Avoid side effects of earlier returning from the specified function.
533 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
534 popd > /dev/null || die "popd failed"
535 done
536
537 # Ensure that the bottom part of directory stack hasn't been changed. Restore
538 # previous directory (from before running of the specified function) before
539 # comparison of directory stacks to avoid mismatch of directory stacks after
540 # potential using of 'cd' to change current directory. Restoration of previous
541 # directory allows to safely use 'cd' to change current directory in the
542 # specified function without changing it back to original directory.
543 cd "${previous_directory}"
544 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
545 die "Directory stack changed illegally"
546 fi
547
363 if [[ "${separate_build_dirs}" == "1" ]]; then 548 if [[ "${separate_build_dirs}" == "1" ]]; then
364 popd > /dev/null || die "popd failed" 549 popd > /dev/null || die "popd failed"
365 fi 550 fi
366 unset BUILDDIR 551 unset BUILDDIR
367 done 552 done
369 if [[ "${default_function}" == "1" ]]; then 554 if [[ "${default_function}" == "1" ]]; then
370 unset -f python_default_function 555 unset -f python_default_function
371 fi 556 fi
372} 557}
373 558
559# @FUNCTION: python_convert_shebangs
560# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
561# @DESCRIPTION:
562# Convert shebangs in specified files. Directories can be specified only with --recursive option.
563python_convert_shebangs() {
564 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
565
566 while (($#)); do
567 case "$1" in
568 -r|--recursive)
569 recursive="1"
570 ;;
571 -q|--quiet)
572 quiet="1"
573 ;;
574 -x|--only-executables)
575 only_executables="1"
576 ;;
577 --)
578 break
579 ;;
580 -*)
581 die "${FUNCNAME}(): Unrecognized option '$1'"
582 ;;
583 *)
584 break
585 ;;
586 esac
587 shift
588 done
589
590 if [[ "$#" -eq 0 ]]; then
591 die "${FUNCNAME}(): Missing Python version and files or directories"
592 elif [[ "$#" -eq 1 ]]; then
593 die "${FUNCNAME}(): Missing files or directories"
594 fi
595
596 python_version="$1"
597 shift
598
599 for argument in "$@"; do
600 if [[ ! -e "${argument}" ]]; then
601 die "${FUNCNAME}(): '${argument}' doesn't exist"
602 elif [[ -f "${argument}" ]]; then
603 files+=("${argument}")
604 elif [[ -d "${argument}" ]]; then
605 if [[ "${recursive}" == "1" ]]; then
606 if [[ "${only_executables}" == "1" ]]; then
607 files+=($(find "${argument}" -perm /111 -type f))
608 else
609 files+=($(find "${argument}" -type f))
610 fi
611 else
612 die "${FUNCNAME}(): '${argument}' isn't a regular file"
613 fi
614 else
615 die "${FUNCNAME}(): '${argument}' isn't a regular file or a directory"
616 fi
617 done
618
619 for file in "${files[@]}"; do
620 file="${file#./}"
621 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
622
623 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
624 [[ "${quiet}" == "0" ]] && einfo "Converting shebang in '${file}'"
625 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
626
627 # Delete potential whitespace after "#!".
628 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
629 fi
630 done
631}
374 632
375# @ECLASS-VARIABLE: PYTHON_USE_WITH 633# @ECLASS-VARIABLE: PYTHON_USE_WITH
376# @DESCRIPTION: 634# @DESCRIPTION:
377# Set this to a space separated list of use flags 635# Set this to a space separated list of use flags
378# the python slot in use must be built with. 636# the python slot in use must be built with.
390 648
391# @FUNCTION: python_pkg_setup 649# @FUNCTION: python_pkg_setup
392# @DESCRIPTION: 650# @DESCRIPTION:
393# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 651# 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. 652# 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 653if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
654 python_pkg_setup() {
396 python_pkg_setup_fail() { 655 python_pkg_setup_fail() {
397 eerror "${1}" 656 eerror "${1}"
398 die "${1}" 657 die "${1}"
658 }
659
660 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
661
662 python_pkg_setup_check_USE_flags() {
663 local pyatom use
664 if [[ -n "${PYTHON_ABI}" ]]; then
665 pyatom="dev-lang/python:${PYTHON_ABI}"
666 else
667 python_version
668 pyatom="dev-lang/python:${PYVER}"
669 fi
670
671 for use in ${PYTHON_USE_WITH}; do
672 if ! has_version "${pyatom}[${use}]"; then
673 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
674 fi
675 done
676
677 for use in ${PYTHON_USE_WITH_OR}; do
678 if has_version "${pyatom}[${use}]"; then
679 return
680 fi
681 done
682
683 if [[ ${PYTHON_USE_WITH_OR} ]]; then
684 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
685 fi
686 }
687
688 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
689 python_execute_function -q python_pkg_setup_check_USE_flags
690 else
691 python_pkg_setup_check_USE_flags
692 fi
399 } 693 }
400 694
401 python_pkg_setup() {
402 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
403
404 python_version
405 local failed
406 local pyatom="dev-lang/python:${PYVER}"
407
408 for use in ${PYTHON_USE_WITH}; do
409 if ! has_version "${pyatom}[${use}]"; then
410 python_pkg_setup_fail \
411 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}"
412 fi
413 done
414
415 for use in ${PYTHON_USE_WITH_OR}; do
416 if has_version "${pyatom}[${use}]"; then
417 return
418 fi
419 done
420
421 if [[ ${PYTHON_USE_WITH_OR} ]]; then
422 python_pkg_setup_fail \
423 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}"
424 fi
425 }
426
427 EXPORT_FUNCTIONS pkg_setup 695 EXPORT_FUNCTIONS pkg_setup
428 696
429 if [[ ${PYTHON_USE_WITH} ]]; then 697 if [[ -n "${PYTHON_USE_WITH}" ]]; then
430 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 698 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
431 elif [[ ${PYTHON_USE_WITH_OR} ]]; then 699 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
432 PYTHON_USE_WITH_ATOM="|| ( " 700 PYTHON_USE_WITH_ATOM="|| ( "
433 for use in ${PYTHON_USE_WITH_OR}; do 701 for use in ${PYTHON_USE_WITH_OR}; do
434 PYTHON_USE_WITH_ATOM=" 702 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
435 ${PYTHON_USE_WITH_ATOM}
436 ${PYTHON_ATOM}[${use}]"
437 done 703 done
438 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )" 704 unset use
705 PYTHON_USE_WITH_ATOM+=" )"
439 fi 706 fi
440 if [[ ${PYTHON_USE_WITH_OPT} ]]; then 707 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
441 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 708 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
442 fi 709 fi
443 DEPEND="${PYTHON_USE_WITH_ATOM}" 710 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
444 RDEPEND="${PYTHON_USE_WITH_ATOM}" 711 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
712fi
713
714# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
715# @DESCRIPTION:
716# Set this to define default functions for the following ebuild phases:
717# src_prepare, src_configure, src_compile, src_test, src_install.
718if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
719 python_src_prepare() {
720 python_copy_sources
721 }
722
723 for python_default_function in src_configure src_compile src_test src_install; do
724 eval "python_${python_default_function}() { python_execute_function -d -s; }"
725 done
726 unset python_default_function
727
728 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
445fi 729fi
446 730
447# @FUNCTION: python_disable_pyc 731# @FUNCTION: python_disable_pyc
448# @DESCRIPTION: 732# @DESCRIPTION:
449# Tell Python not to automatically recompile modules to .pyc/.pyo 733# Tell Python not to automatically recompile modules to .pyc/.pyo
458# Tell Python to automatically recompile modules to .pyc/.pyo if the 742# Tell Python to automatically recompile modules to .pyc/.pyo if the
459# timestamps/version stamps have changed. 743# timestamps/version stamps have changed.
460python_enable_pyc() { 744python_enable_pyc() {
461 unset PYTHONDONTWRITEBYTECODE 745 unset PYTHONDONTWRITEBYTECODE
462} 746}
463
464python_disable_pyc
465 747
466# @FUNCTION: python_need_rebuild 748# @FUNCTION: python_need_rebuild
467# @DESCRIPTION: Run without arguments, specifies that the package should be 749# @DESCRIPTION: Run without arguments, specifies that the package should be
468# rebuilt after a python upgrade. 750# rebuilt after a python upgrade.
469python_need_rebuild() { 751python_need_rebuild() {
675 # Don't use PYTHON_ABI in next calls to python_get_libdir(). 957 # Don't use PYTHON_ABI in next calls to python_get_libdir().
676 unset PYTHON_ABI 958 unset PYTHON_ABI
677 959
678 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 960 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
679 return_code="0" 961 return_code="0"
680 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." 962 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}"
681 if ((${#other_dirs[@]})); then 963 if ((${#other_dirs[@]})); then
682 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 964 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
683 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" 965 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
684 fi 966 fi
685 if ((${#other_files[@]})); then 967 if ((${#other_files[@]})); then
731 fi 1013 fi
732 1014
733 # set additional opts 1015 # set additional opts
734 myopts+=(-q) 1016 myopts+=(-q)
735 1017
736 ebegin "Byte compiling python modules for python-${PYVER} .." 1018 ebegin "Compilation and optimization of Python modules for Python ${PYVER}"
737 if ((${#mydirs[@]})); then 1019 if ((${#mydirs[@]})); then
738 python${PYVER} \ 1020 python${PYVER} \
739 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 1021 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
740 "${myopts[@]}" "${mydirs[@]}" || return_code="1" 1022 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
741 python${PYVER} -O \ 1023 python${PYVER} -O \
750 eend "${return_code}" 1032 eend "${return_code}"
751 fi 1033 fi
752} 1034}
753 1035
754# @FUNCTION: python_mod_cleanup 1036# @FUNCTION: python_mod_cleanup
755# @USAGE: [directory] 1037# @USAGE: [directory|file]
756# @DESCRIPTION: 1038# @DESCRIPTION:
757# Run with optional arguments, where arguments are directories of 1039# Run with optional arguments, where arguments are Python modules. If none given,
758# python modules. If none given, it will look in /usr/lib/python[0-9].[0-9]. 1040# it will look in /usr/lib/python[0-9].[0-9].
759# 1041#
760# It will recursively scan all compiled Python modules in the directories and 1042# It will recursively scan all compiled Python modules in the directories and
761# determine if they are orphaned (i.e. their corresponding .py files are missing.) 1043# determine if they are orphaned (i.e. their corresponding .py files are missing.)
762# If they are, then it will remove their corresponding .pyc and .pyo files. 1044# If they are, then it will remove their corresponding .pyc and .pyo files.
763# 1045#
764# This function should only be run in pkg_postrm(). 1046# This function should only be run in pkg_postrm().
765python_mod_cleanup() { 1047python_mod_cleanup() {
766 local PYTHON_ABI SEARCH_PATH=() root src_py 1048 local path py_file PYTHON_ABI SEARCH_PATH=() root
767 1049
768 # Check if phase is pkg_postrm(). 1050 # Check if phase is pkg_postrm().
769 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" 1051 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
770 1052
771 # Strip trailing slash from ROOT. 1053 # Strip trailing slash from ROOT.
788 else 1070 else
789 SEARCH_PATH=("${@#/}") 1071 SEARCH_PATH=("${@#/}")
790 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 1072 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
791 fi 1073 fi
792 else 1074 else
793 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) 1075 local dir sitedir
1076 for dir in "${root}"/usr/lib*; do
1077 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
1078 for sitedir in "${dir}"/python*/site-packages; do
1079 if [[ -d "${sitedir}" ]]; then
1080 SEARCH_PATH+=("${sitedir}")
1081 fi
1082 done
1083 fi
1084 done
1085 fi
1086
1087 local BLUE CYAN NORMAL
1088 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1089 BLUE=$'\e[1;34m'
1090 CYAN=$'\e[1;36m'
1091 NORMAL=$'\e[0m'
1092 else
1093 BLUE=
1094 CYAN=
1095 NORMAL=
794 fi 1096 fi
795 1097
796 for path in "${SEARCH_PATH[@]}"; do 1098 for path in "${SEARCH_PATH[@]}"; do
797 [[ ! -d "${path}" ]] && continue 1099 if [[ -d "${path}" ]]; then
798 einfo "Cleaning orphaned Python bytecode from ${path} .."
799 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 1100 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
800 src_py="${REPLY%[co]}" 1101 py_file="${REPLY%[co]}"
801 [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue 1102 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
802 einfo "Purging ${src_py}[co]" 1103 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
803 rm -f "${src_py}"[co] 1104 rm -f "${py_file}"[co]
804 done 1105 done
805 1106
806 # Attempt to remove directories that may be empty. 1107 # Attempt to delete directories, which may be empty.
807 find "${path}" -type d | sort -r | while read -r dir; do 1108 find "${path}" -type d | sort -r | while read -r dir; do
808 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}" 1109 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
809 done 1110 done
1111 elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then
1112 einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1113 rm -f "${path}"[co]
1114 fi
810 done 1115 done
811} 1116}

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

  ViewVC Help
Powered by ViewVC 1.1.20