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

Diff of /eclass/python.eclass

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

Revision 1.7 Revision 1.73
1# Copyright 1999-2003 Gentoo Technologies, Inc. 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.7 2003/10/24 07:12:42 pythonhead Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.73 2009/09/18 17:50:08 arfrever Exp $
4# 4
5# Author: Alastair Tse <liquidx@gentoo.org> 5# @ECLASS: python.eclass
6# 6# @MAINTAINER:
7# python@gentoo.org
7# A Utility Eclass that should be inherited by anything that deals with 8# @BLURB: A Utility Eclass that should be inherited by anything that deals with Python or Python modules.
8# Python or Python modules. 9# @DESCRIPTION:
9# 10# Some useful functions for dealing with Python.
10# - Features:
11# python_version() - sets PYVER/PYVER_MAJOR/PYVER_MINOR
12# python_tkinter_exists() - Checks for tkinter support in python
13# python_mod_exists() - Checks if a python module exists
14# python_mod_compile() - Compiles a .py file to a .pyc/.pyo
15# python_mod_optimize() - Generates .pyc/.pyo precompiled scripts
16# python_mod_cleanup() - Goes through /usr/lib/python* to remove
17# orphaned *.pyc *.pyo
18# python_makesym() - Makes /usr/bin/python symlinks
19 11
20inherit alternatives 12inherit multilib
21 13
22ECLASS="python" 14if [[ -n "${NEED_PYTHON}" ]] ; then
23INHERITED="$INHERITED $ECLASS" 15 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
16 DEPEND="${PYTHON_ATOM}"
17 RDEPEND="${DEPEND}"
18else
19 PYTHON_ATOM="dev-lang/python"
20fi
24 21
25# 22DEPEND="${DEPEND} >=app-shells/bash-3.2"
26# name: python_disable/enable_pyc 23if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
27# desc: tells python not to automatically recompile modules to .pyc/.pyo 24 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804"
28# even if the timestamps/version stamps don't match. this is 25fi
29# done to protect sandbox.
30#
31# note: supported by >=dev-lang/python-2.2.3-r3 only.
32#
33python_disable_pyc() {
34 export PYTHON_DONTCOMPILE=1
35}
36 26
37python_enable_pyc() { 27__python_eclass_test() {
38 unset PYTHON_DONTCOMPILE 28 __python_version_extract 2.3
29 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
30 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
31 __python_version_extract 2.3.4
32 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
33 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
34 __python_version_extract 2.3.5
35 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
36 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
37 __python_version_extract 2.4
38 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
39 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
40 __python_version_extract 2.5b3
41 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
42 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
39} 43}
40 44
41python_disable_pyc 45# @FUNCTION: python_version
42 46# @DESCRIPTION:
43#
44# name: python_version
45# desc: run without arguments and it will export the version of python 47# Run without arguments and it will export the version of python
46# currently in use as $PYVER 48# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
47# 49__python_version_extract() {
50 local verstr=$1
51 export PYVER_MAJOR=${verstr:0:1}
52 export PYVER_MINOR=${verstr:2:1}
53 if [[ ${verstr:3:1} == . ]]; then
54 export PYVER_MICRO=${verstr:4}
55 fi
56 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
57}
58
48python_version() { 59python_version() {
60 [[ -n "${PYVER}" ]] && return 0
49 local tmpstr 61 local tmpstr
50 python=${python:-/usr/bin/python} 62 python=${python:-/usr/bin/python}
51 tmpstr="$(${python} -V 2>&1 )" 63 tmpstr="$(${python} -V 2>&1 )"
52 export PYVER_ALL="${tmpstr#Python }" 64 export PYVER_ALL="${tmpstr#Python }"
53 65 __python_version_extract $PYVER_ALL
54 export PYVER_MAJOR=$(echo ${PYVER_ALL} | cut -d. -f1)
55 export PYVER_MINOR=$(echo ${PYVER_ALL} | cut -d. -f2)
56 export PYVER_MICRO=$(echo ${PYVER_ALL} | cut -d. -f3-)
57 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
58} 66}
59 67
60# 68# @FUNCTION: PYTHON
61# name: python_makesym 69# @USAGE: [-a|--absolute-path] [--] <Python_ABI="${PYTHON_ABI}">
62# desc: run without arguments, it will create the /usr/bin/python symlinks 70# @DESCRIPTION:
63# to the latest installed version 71# Get Python interpreter filename for specified Python ABI. If Python_ABI argument
64# 72# is ommitted, then PYTHON_ABI environment variable must be set and is used.
65python_makesym() { 73PYTHON() {
66 alternatives_auto_makesym "/usr/bin/python" "/usr/bin/python[0-9].[0-9]" 74 local absolute_path="0" slot=
67 alternatives_auto_makesym "/usr/bin/python2" "/usr/bin/python[0-9].[0-9]"
68}
69 75
70# 76 while (($#)); do
77 case "$1" in
78 -a|--absolute-path)
79 absolute_path="1"
80 ;;
81 --)
82 break
83 ;;
84 -*)
85 die "${FUNCNAME}(): Unrecognized option $1"
86 ;;
87 *)
88 break
89 ;;
90 esac
91 shift
92 done
93
94 if [[ "$#" -eq "0" ]]; then
95 if [[ -n "${PYTHON_ABI}" ]]; then
96 slot="${PYTHON_ABI}"
97 else
98 die "${FUNCNAME}(): Invalid usage"
99 fi
100 elif [[ "$#" -eq "1" ]]; then
101 slot="$1"
102 else
103 die "${FUNCNAME}(): Invalid usage"
104 fi
105
106 if [[ "${absolute_path}" == "1" ]]; then
107 echo -n "/usr/bin/python${slot}"
108 else
109 echo -n "python${slot}"
110 fi
111}
112
113# @FUNCTION: validate_PYTHON_ABIS
114# @DESCRIPTION:
115# Make sure PYTHON_ABIS variable has valid value.
116validate_PYTHON_ABIS() {
117 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable.
118 if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
119 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
120 fi
121
122 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
123 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then
124 die "'/usr/bin/python' isn't valid symlink"
125 fi
126 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
127 die "'/usr/bin/python-config' isn't valid script"
128 fi
129
130 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4.
131 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then
132 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI
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
140 for ABI in ${USE_PYTHON}; do
141 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
142 die "USE_PYTHON variable contains invalid value '${ABI}'"
143 fi
144 support_ABI="1"
145 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
146 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then
147 support_ABI="0"
148 break
149 fi
150 done
151 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}"
152 done
153 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
154
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
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
167 export PYTHON_ABIS="${PYVER}"
168 fi
169 fi
170
171 local PYTHON_ABI
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.
179 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
180 die "'python' doesn't respect EPYTHON variable"
181 fi
182 done
183}
184
185# @FUNCTION: python_copy_sources
186# @USAGE: [--no-link] [--] [directory]
187# @DESCRIPTION:
188# Copy unpacked sources of given package for each Python ABI.
189python_copy_sources() {
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
209
210 if [[ "$#" -eq "0" ]]; then
211 if [[ "${WORKDIR}" == "${S}" ]]; then
212 die "${FUNCNAME}() cannot be used"
213 fi
214 dirs="${S}"
215 else
216 dirs="$@"
217 fi
218
219 validate_PYTHON_ABIS
220 for PYTHON_ABI in ${PYTHON_ABIS}; 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
225 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
226 fi
227 done
228 done
229}
230
231# @FUNCTION: python_set_build_dir_symlink
232# @USAGE: [directory="build"]
233# @DESCRIPTION:
234# Create build directory symlink.
235python_set_build_dir_symlink() {
236 local dir="$1"
237
238 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
239 [[ -z "${dir}" ]] && dir="build"
240
241 # Don't delete preexistent directories.
242 rm -f "${dir}" || die "Deletion of '${dir}' failed"
243 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
244}
245
246# @FUNCTION: python_execute_function
247# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments]
248# @DESCRIPTION:
249# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
250# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
251python_execute_function() {
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"
253
254 while (($#)); do
255 case "$1" in
256 --action-message)
257 action_message_template="$2"
258 shift
259 ;;
260 -d|--default-function)
261 default_function="1"
262 ;;
263 --failure-message)
264 failure_message_template="$2"
265 shift
266 ;;
267 --nonfatal)
268 nonfatal="1"
269 ;;
270 -q|--quiet)
271 quiet="1"
272 ;;
273 -s|--separate-build-dirs)
274 separate_build_dirs="1"
275 ;;
276 --)
277 break
278 ;;
279 -*)
280 die "${FUNCNAME}(): Unrecognized option '$1'"
281 ;;
282 *)
283 break
284 ;;
285 esac
286 shift
287 done
288
289 if [[ "${default_function}" == "0" ]]; then
290 if [[ "$#" -eq "0" ]]; then
291 die "${FUNCNAME}(): Missing function name"
292 fi
293 function="$1"
294 shift
295
296 if [[ -z "$(type -t "${function}")" ]]; then
297 die "${FUNCNAME}(): '${function}' function isn't defined"
298 fi
299 else
300 if [[ "$#" -ne "0" ]]; then
301 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
302 fi
303 if has "${EAPI:-0}" 0 1; then
304 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
305 fi
306
307 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
308 if has "${EAPI}" 2; then
309 python_default_function() {
310 econf
311 }
312 else
313 python_default_function() {
314 nonfatal econf
315 }
316 fi
317 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
318 python_default_function() {
319 emake
320 }
321 elif [[ "${EBUILD_PHASE}" == "test" ]]; then
322 python_default_function() {
323 if emake -j1 -n check &> /dev/null; then
324 emake -j1 check
325 elif emake -j1 -n test &> /dev/null; then
326 emake -j1 test
327 fi
328 }
329 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
330 python_default_function() {
331 emake DESTDIR="${D}" install
332 }
333 else
334 die "${FUNCNAME}(): --default-function option cannot be used in this ebuild phase"
335 fi
336 function="python_default_function"
337 fi
338
339 if [[ "${quiet}" == "0" ]]; then
340 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
341 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
342 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
343 [[ "${EBUILD_PHASE}" == "configure" ]] && action="Configuration"
344 [[ "${EBUILD_PHASE}" == "compile" ]] && action="Building"
345 [[ "${EBUILD_PHASE}" == "test" ]] && action="Testing"
346 [[ "${EBUILD_PHASE}" == "install" ]] && action="Installation"
347 [[ "${EBUILD_PHASE}" == "preinst" ]] && action="Preinstallation"
348 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
349 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
350 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
351 fi
352
353 local RED GREEN BLUE NORMAL
354 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
355 RED=$'\e[1;31m'
356 GREEN=$'\e[1;32m'
357 BLUE=$'\e[1;34m'
358 NORMAL=$'\e[0m'
359 else
360 RED=
361 GREEN=
362 BLUE=
363 NORMAL=
364 fi
365
366 validate_PYTHON_ABIS
367 for PYTHON_ABI in ${PYTHON_ABIS}; do
368 if [[ "${quiet}" == "0" ]]; then
369 if [[ -n "${action_message_template}" ]]; then
370 action_message="$(eval echo -n "${action_message_template}")"
371 else
372 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..."
373 fi
374 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
375 fi
376
377 if [[ "${separate_build_dirs}" == "1" ]]; then
378 export BUILDDIR="${S}-${PYTHON_ABI}"
379 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
380 else
381 export BUILDDIR="${S}"
382 fi
383
384 previous_directory="$(pwd)"
385 previous_directory_stack="$(dirs -p)"
386 previous_directory_stack_length="$(dirs -p | wc -l)"
387
388 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
389 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
390 else
391 EPYTHON="$(PYTHON)" "${function}" "$@"
392 fi
393
394 if [[ "$?" != "0" ]]; then
395 if [[ -n "${failure_message_template}" ]]; then
396 failure_message="$(eval echo -n "${failure_message_template}")"
397 else
398 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
399 fi
400
401 if [[ "${nonfatal}" == "1" ]]; then
402 if [[ "${quiet}" == "0" ]]; then
403 ewarn "${RED}${failure_message}${NORMAL}"
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
419 else
420 die "${failure_message}"
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"
443 fi
444
445 if [[ "${separate_build_dirs}" == "1" ]]; then
446 popd > /dev/null || die "popd failed"
447 fi
448 unset BUILDDIR
449 done
450
451 if [[ "${default_function}" == "1" ]]; then
452 unset -f python_default_function
453 fi
454}
455
456
457# @ECLASS-VARIABLE: PYTHON_USE_WITH
458# @DESCRIPTION:
459# Set this to a space separated list of use flags
460# the python slot in use must be built with.
461
462# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
463# @DESCRIPTION:
464# Set this to a space separated list of use flags
465# of which one must be turned on for the slot of
466# in use.
467
468# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
469# @DESCRIPTION:
470# Set this if you need to make either PYTHON_USE_WITH or
471# PYTHON_USE_WITH_OR atoms conditional under a use flag.
472
473# @FUNCTION: python_pkg_setup
474# @DESCRIPTION:
475# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
476# are respected. Only exported if one of those variables is set.
477if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
478 python_pkg_setup_fail() {
479 eerror "${1}"
480 die "${1}"
481 }
482
483 python_pkg_setup() {
484 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
485
486 python_version
487 local failed
488 local pyatom="dev-lang/python:${PYVER}"
489
490 for use in ${PYTHON_USE_WITH}; do
491 if ! has_version "${pyatom}[${use}]"; then
492 python_pkg_setup_fail \
493 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}"
494 fi
495 done
496
497 for use in ${PYTHON_USE_WITH_OR}; do
498 if has_version "${pyatom}[${use}]"; then
499 return
500 fi
501 done
502
503 if [[ ${PYTHON_USE_WITH_OR} ]]; then
504 python_pkg_setup_fail \
505 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}"
506 fi
507 }
508
509 EXPORT_FUNCTIONS pkg_setup
510
511 if [[ ${PYTHON_USE_WITH} ]]; then
512 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
513 elif [[ ${PYTHON_USE_WITH_OR} ]]; then
514 PYTHON_USE_WITH_ATOM="|| ( "
515 for use in ${PYTHON_USE_WITH_OR}; do
516 PYTHON_USE_WITH_ATOM="
517 ${PYTHON_USE_WITH_ATOM}
518 ${PYTHON_ATOM}[${use}]"
519 done
520 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )"
521 fi
522 if [[ ${PYTHON_USE_WITH_OPT} ]]; then
523 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
524 fi
525 DEPEND="${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
544fi
545
546# @FUNCTION: python_disable_pyc
547# @DESCRIPTION:
548# Tell Python not to automatically recompile modules to .pyc/.pyo
549# even if the timestamps/version stamps don't match. This is done
550# to protect sandbox.
551python_disable_pyc() {
552 export PYTHONDONTWRITEBYTECODE="1"
553}
554
555# @FUNCTION: python_enable_pyc
556# @DESCRIPTION:
557# Tell Python to automatically recompile modules to .pyc/.pyo if the
558# timestamps/version stamps have changed.
559python_enable_pyc() {
560 unset PYTHONDONTWRITEBYTECODE
561}
562
563python_disable_pyc
564
565# @FUNCTION: python_need_rebuild
566# @DESCRIPTION: Run without arguments, specifies that the package should be
567# rebuilt after a python upgrade.
568python_need_rebuild() {
569 python_version
570 export PYTHON_NEED_REBUILD=${PYVER}
571}
572
573# @FUNCTION: python_get_includedir
574# @DESCRIPTION:
575# Run without arguments, returns the Python include directory.
576python_get_includedir() {
577 if [[ -n "${PYTHON_ABI}" ]]; then
578 echo "/usr/include/python${PYTHON_ABI}"
579 else
580 python_version
581 echo "/usr/include/python${PYVER}"
582 fi
583}
584
585# @FUNCTION: python_get_libdir
586# @DESCRIPTION:
587# Run without arguments, returns the Python library directory.
588python_get_libdir() {
589 if [[ -n "${PYTHON_ABI}" ]]; then
590 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
591 else
592 python_version
593 echo "/usr/$(get_libdir)/python${PYVER}"
594 fi
595}
596
597# @FUNCTION: python_get_sitedir
598# @DESCRIPTION:
599# Run without arguments, returns the Python site-packages directory.
600python_get_sitedir() {
601 echo "$(python_get_libdir)/site-packages"
602}
603
71# name: python_tkinter_exists 604# @FUNCTION: python_tkinter_exists
72# desc: run without arguments, it will return TRUE(0) if python is compiled 605# @DESCRIPTION:
73# with tkinter or FALSE(1) if python is compiled without tkinter. 606# Run without arguments, checks if python was compiled with Tkinter
74# 607# support. If not, prints an error message and dies.
75python_tkinter_exists() { 608python_tkinter_exists() {
76 if ! python -c "import Tkinter" >/dev/null 2>&1; then 609 if ! python -c "import Tkinter" >/dev/null 2>&1; then
77 eerror "You need to recompile python with Tkinter support." 610 eerror "You need to recompile python with Tkinter support."
78 eerror "That means: USE='tcltk' emerge python" 611 eerror "Try adding: 'dev-lang/python tk'"
612 eerror "in to /etc/portage/package.use"
79 echo 613 echo
80 die "missing tkinter support with installed python" 614 die "missing tkinter support with installed python"
81 fi 615 fi
82} 616}
83 617
618# @FUNCTION: python_mod_exists
619# @USAGE: <module>
620# @DESCRIPTION:
621# Run with the module name as an argument. it will check if a
622# python module is installed and loadable. it will return
623# TRUE(0) if the module exists, and FALSE(1) if the module does
624# not exist.
84# 625#
85# name: python_mod_exists 626# Example:
86# desc: run with the module name as an argument. it will check if a
87# python module is installed and loadable. it will return
88# TRUE(0) if the module exists, and FALSE(1) if the module does
89# not exist.
90# exam:
91# if python_mod_exists gtk; then 627# if python_mod_exists gtk; then
92# echo "gtk support enabled 628# echo "gtk support enabled"
93# fi 629# fi
630python_mod_exists() {
631 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
632 python -c "import $1" &>/dev/null
633}
634
635# @FUNCTION: python_mod_compile
636# @USAGE: <file> [more files ...]
637# @DESCRIPTION:
638# Given filenames, it will pre-compile the module's .pyc and .pyo.
639# This function should only be run in pkg_postinst()
94# 640#
95python_mod_exists() { 641# Example:
96 if ! python -c "import $1" >/dev/null 2>&1; then
97 return 1
98 fi
99 return 0
100}
101
102#
103# name: python_mod_compile
104# desc: given a filename, it will pre-compile the module's .pyc and .pyo.
105# should only be run in pkg_postinst()
106# exam:
107# python_mod_compile ${ROOT}usr/lib/python2.3/site-packages/pygoogle.py 642# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
108# 643#
109python_mod_compile() { 644python_mod_compile() {
645 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
646 die "${FUNCNAME}() cannot be used in this EAPI"
647 fi
648
649 local f myroot myfiles=()
650
651 # Check if phase is pkg_postinst()
652 [[ ${EBUILD_PHASE} != postinst ]] &&\
653 die "${FUNCNAME} should only be run in pkg_postinst()"
654
110 # allow compiling for older python versions 655 # allow compiling for older python versions
111 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 656 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
112 PYVER=${PYTHON_OVERRIDE_PYVER} 657 PYVER=${PYTHON_OVERRIDE_PYVER}
113 else 658 else
114 python_version 659 python_version
115 fi 660 fi
116 661
117 if [ -f "$1" ]; then 662 # strip trailing slash
118 python${PYVER} -c "import py_compile; py_compile.compile('${1}')" || \ 663 myroot="${ROOT%/}"
664
665 # respect ROOT
666 for f in "$@"; do
667 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
668 done
669
670 if ((${#myfiles[@]})); then
671 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
672 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" &> /dev/null
673 else
119 ewarn "Failed to compile ${1}" 674 ewarn "No files to compile!"
120 python${PYVER} -O -c "import py_compile; py_compile.compile('${1}')" || \
121 ewarn "Failed to compile ${1}"
122 else
123 ewarn "Unable to find ${1}"
124 fi 675 fi
125} 676}
126 677
678# @FUNCTION: python_mod_optimize
679# @USAGE: [options] [directory|file]
680# @DESCRIPTION:
681# If no arguments supplied, it will recompile not recursively all modules
682# under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages).
127# 683#
128# name: python_mod_optimize 684# If supplied with arguments, it will recompile all modules recursively
129# desc: if no arguments supplied, it will recompile all modules under 685# in the supplied directory.
130# sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..) 686# This function should only be run in pkg_postinst().
131# no recursively
132# 687#
133# if supplied with arguments, it will recompile all modules recursively 688# Options passed to this function are passed to compileall.py.
134# in the supplied directory
135# exam:
136# python_mod_optimize ${ROOT}usr/share/codegen
137# 689#
690# Example:
691# python_mod_optimize ctypesgencore
138python_mod_optimize() { 692python_mod_optimize() {
693 # Check if phase is pkg_postinst().
694 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()"
695
696 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
697 local dir file options=() other_dirs=() other_files=() PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
698
699 # Strip trailing slash from ROOT.
700 root="${ROOT%/}"
701
702 # Respect ROOT and options passed to compileall.py.
703 while (($#)); do
704 case "$1" in
705 -l|-f|-q)
706 options+=("$1")
707 ;;
708 -d|-x)
709 options+=("$1" "$2")
710 shift
711 ;;
712 -*)
713 ewarn "${FUNCNAME}: Ignoring compile option $1"
714 ;;
715 *)
716 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
717 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories"
718 elif [[ "$1" =~ ^/ ]]; then
719 if [[ -d "${root}/$1" ]]; then
720 other_dirs+=("${root}/$1")
721 elif [[ -f "${root}/$1" ]]; then
722 other_files+=("${root}/$1")
723 elif [[ -e "${root}/$1" ]]; then
724 ewarn "'${root}/$1' is not a file or a directory!"
725 else
726 ewarn "'${root}/$1' doesn't exist!"
727 fi
728 else
729 for PYTHON_ABI in ${PYTHON_ABIS}; do
730 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
731 site_packages_dirs+=("$1")
732 break
733 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
734 site_packages_files+=("$1")
735 break
736 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
737 ewarn "'$1' is not a file or a directory!"
738 else
739 ewarn "'$1' doesn't exist!"
740 fi
741 done
742 fi
743 ;;
744 esac
745 shift
746 done
747
748 # Set additional options.
749 options+=("-q")
750
751 for PYTHON_ABI in ${PYTHON_ABIS}; do
752 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
753 return_code="0"
754 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
755 if ((${#site_packages_dirs[@]})); then
756 for dir in "${site_packages_dirs[@]}"; do
757 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
758 done
759 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
760 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
761 fi
762 if ((${#site_packages_files[@]})); then
763 for file in "${site_packages_files[@]}"; do
764 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
765 done
766 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
767 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
768 fi
769 eend "${return_code}"
770 fi
771 unset site_packages_absolute_dirs site_packages_absolute_files
772 done
773
774 # Don't use PYTHON_ABI in next calls to python_get_libdir().
775 unset PYTHON_ABI
776
777 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
778 return_code="0"
779 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..."
780 if ((${#other_dirs[@]})); then
781 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
782 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
783 fi
784 if ((${#other_files[@]})); then
785 python${PYVER} "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
786 python${PYVER} -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
787 fi
788 eend "${return_code}"
789 fi
790 else
791 local myroot mydirs=() myfiles=() myopts=() return_code="0"
792
793 # strip trailing slash
794 myroot="${ROOT%/}"
795
796 # respect ROOT and options passed to compileall.py
797 while (($#)); do
798 case "$1" in
799 -l|-f|-q)
800 myopts+=("$1")
801 ;;
802 -d|-x)
803 myopts+=("$1" "$2")
804 shift
805 ;;
806 -*)
807 ewarn "${FUNCNAME}: Ignoring compile option $1"
808 ;;
809 *)
810 if [[ -d "${myroot}"/$1 ]]; then
811 mydirs+=("${myroot}/$1")
812 elif [[ -f "${myroot}"/$1 ]]; then
813 # Files are passed to python_mod_compile which is ROOT-aware
814 myfiles+=("$1")
815 elif [[ -e "${myroot}/$1" ]]; then
816 ewarn "${myroot}/$1 is not a file or directory!"
817 else
818 ewarn "${myroot}/$1 doesn't exist!"
819 fi
820 ;;
821 esac
822 shift
823 done
824
139 # allow compiling for older python versions 825 # allow compiling for older python versions
140 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 826 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
141 PYVER=${PYTHON_OVERRIDE_PYVER} 827 PYVER=${PYTHON_OVERRIDE_PYVER}
142 else 828 else
143 python_version 829 python_version
144 fi 830 fi
145 831
146 # set opts 832 # set additional opts
147 if [ "${PYVER}" = "2.2" ]; then 833 myopts+=(-q)
148 compileopts=""
149 else
150 compileopts="-q"
151 fi
152 834
153 ebegin "Byte Compiling Python modules for ${PYVER} .." 835 ebegin "Byte compiling python modules for python-${PYVER} .."
154 python${PYVER} ${ROOT}usr/lib/python${PYVER}/compileall.py ${compileopts} $@ 836 if ((${#mydirs[@]})); then
155 python${PYVER} -O ${ROOT}usr/lib/python${PYVER}/compileall.py ${compileopts} $@ 837 python${PYVER} \
156 eend $? 838 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
157} 839 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
840 python${PYVER} -O \
841 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
842 "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
843 fi
158 844
845 if ((${#myfiles[@]})); then
846 python_mod_compile "${myfiles[@]}"
847 fi
848
849 eend "${return_code}"
850 fi
851}
852
853# @FUNCTION: python_mod_cleanup
854# @USAGE: [directory]
855# @DESCRIPTION:
856# Run with optional arguments, where arguments are directories of
857# python modules. If none given, it will look in /usr/lib/python[0-9].[0-9].
159# 858#
160# name: python_mod_cleanup 859# It will recursively scan all compiled Python modules in the directories and
161# desc: run with optional arguments, where arguments are directories of 860# determine if they are orphaned (i.e. their corresponding .py files are missing.)
162# python modules. if none given, it will look in /usr/lib/python[0-9].[0-9] 861# If they are, then it will remove their corresponding .pyc and .pyo files.
163# 862#
164# it will recursively scan all compiled python modules in the directories 863# This function should only be run in pkg_postrm().
165# and determine if they are orphaned (eg. their corresponding .py is missing.)
166# if they are, then it will remove their corresponding .pyc and .pyo
167#
168python_mod_cleanup() { 864python_mod_cleanup() {
169 local SEARCH_PATH 865 local PYTHON_ABI SEARCH_PATH=() root src_py
170 866
171 if [ $# -gt 0 ]; then 867 # Check if phase is pkg_postrm().
172 for path in $@; do 868 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
173 SEARCH_PATH="${SEARCH_PATH} ${ROOT}${path#/}" 869
870 # Strip trailing slash from ROOT.
871 root="${ROOT%/}"
872
873 if (($#)); then
874 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
875 while (($#)); do
876 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
877 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories"
878 elif [[ "$1" =~ ^/ ]]; then
879 SEARCH_PATH+=("${root}/${1#/}")
880 else
881 for PYTHON_ABI in ${PYTHON_ABIS}; do
882 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
883 done
884 fi
885 shift
886 done
887 else
888 SEARCH_PATH=("${@#/}")
889 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
890 fi
891 else
892 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages)
893 fi
894
895 for path in "${SEARCH_PATH[@]}"; do
896 [[ ! -d "${path}" ]] && continue
897 einfo "Cleaning orphaned Python bytecode from ${path} .."
898 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
899 src_py="${REPLY%[co]}"
900 [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue
901 einfo "Purging ${src_py}[co]"
902 rm -f "${src_py}"[co]
174 done 903 done
175 else
176 for path in ${ROOT}usr/lib/python*/site-packages; do
177 SEARCH_PATH="${SEARCH_PATH} ${path}"
178 done
179 fi
180 904
181 for path in ${SEARCH_PATH}; do 905 # Attempt to remove directories that may be empty.
182 einfo "Searching ${path} .." 906 find "${path}" -type d | sort -r | while read -r dir; do
183 for obj in $(find ${path} -name *.pyc); do 907 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}"
184 src_py="$(echo $obj | sed 's:c$::')"
185 if [ ! -f "${src_py}" ]; then
186 einfo "Purging ${src_py}[co]"
187 rm -f ${src_py}[co]
188 fi
189 done 908 done
190 done 909 done
191} 910}
192
193

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

  ViewVC Help
Powered by ViewVC 1.1.20