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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.73 - (hide annotations) (download)
Fri Sep 18 17:50:08 2009 UTC (5 years, 10 months ago) by arfrever
Branch: MAIN
Changes since 1.72: +20 -7 lines
Improve handling of directory stack in python_execute_function().

1 arfrever 1.56 # Copyright 1999-2009 Gentoo Foundation
2 liquidx 1.1 # Distributed under the terms of the GNU General Public License v2
3 arfrever 1.73 # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.72 2009/09/11 19:55:05 arfrever Exp $
4 liquidx 1.1
5 dev-zero 1.33 # @ECLASS: python.eclass
6     # @MAINTAINER:
7     # python@gentoo.org
8     # @BLURB: A Utility Eclass that should be inherited by anything that deals with Python or Python modules.
9     # @DESCRIPTION:
10 arfrever 1.66 # Some useful functions for dealing with Python.
11 liquidx 1.1
12 arfrever 1.65 inherit multilib
13 liquidx 1.2
14 carlo 1.23 if [[ -n "${NEED_PYTHON}" ]] ; then
15 betelgeuse 1.55 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
16     DEPEND="${PYTHON_ATOM}"
17 swegener 1.24 RDEPEND="${DEPEND}"
18 betelgeuse 1.55 else
19     PYTHON_ATOM="dev-lang/python"
20 carlo 1.22 fi
21 liquidx 1.18
22 arfrever 1.66 DEPEND="${DEPEND} >=app-shells/bash-3.2"
23 arfrever 1.56 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
24 arfrever 1.59 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804"
25 arfrever 1.56 fi
26    
27 liquidx 1.18 __python_eclass_test() {
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 liquidx 1.28 __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"
43 liquidx 1.18 }
44    
45 dev-zero 1.33 # @FUNCTION: python_version
46     # @DESCRIPTION:
47     # Run without arguments and it will export the version of python
48     # currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
49 liquidx 1.18 __python_version_extract() {
50 hawking 1.45 local verstr=$1
51 liquidx 1.18 export PYVER_MAJOR=${verstr:0:1}
52     export PYVER_MINOR=${verstr:2:1}
53 hawking 1.47 if [[ ${verstr:3:1} == . ]]; then
54 swegener 1.29 export PYVER_MICRO=${verstr:4}
55 liquidx 1.28 fi
56 swegener 1.21 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
57 liquidx 1.18 }
58    
59 liquidx 1.1 python_version() {
60 hawking 1.44 [[ -n "${PYVER}" ]] && return 0
61 liquidx 1.1 local tmpstr
62     python=${python:-/usr/bin/python}
63     tmpstr="$(${python} -V 2>&1 )"
64     export PYVER_ALL="${tmpstr#Python }"
65 liquidx 1.18 __python_version_extract $PYVER_ALL
66 liquidx 1.1 }
67    
68 arfrever 1.60 # @FUNCTION: PYTHON
69 arfrever 1.71 # @USAGE: [-a|--absolute-path] [--] <Python_ABI="${PYTHON_ABI}">
70 arfrever 1.56 # @DESCRIPTION:
71     # Get Python interpreter filename for specified Python ABI. If Python_ABI argument
72     # is ommitted, then PYTHON_ABI environment variable must be set and is used.
73 arfrever 1.60 PYTHON() {
74 arfrever 1.56 local absolute_path="0" slot=
75    
76     while (($#)); do
77     case "$1" in
78     -a|--absolute-path)
79     absolute_path="1"
80     ;;
81 arfrever 1.71 --)
82     break
83     ;;
84 arfrever 1.56 -*)
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.
116     validate_PYTHON_ABIS() {
117 arfrever 1.66 # 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 arfrever 1.59 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
123 arfrever 1.63 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then
124 arfrever 1.66 die "'/usr/bin/python' isn't valid symlink"
125 arfrever 1.57 fi
126     if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
127 arfrever 1.66 die "'/usr/bin/python-config' isn't valid script"
128 arfrever 1.57 fi
129    
130 arfrever 1.56 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4.
131 arfrever 1.70 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then
132 arfrever 1.56 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 arfrever 1.68
135 arfrever 1.70 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
136 arfrever 1.68 if [[ -z "${USE_PYTHON}" ]]; then
137     die "USE_PYTHON variable is empty"
138 arfrever 1.56 fi
139 arfrever 1.68
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 arfrever 1.66 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 arfrever 1.68 [[ "${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 arfrever 1.66 fi
158 arfrever 1.68 else
159     local restricted_ABI
160     python_version
161 arfrever 1.56
162 arfrever 1.68 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 arfrever 1.56 fi
170 arfrever 1.66
171     local PYTHON_ABI
172     for PYTHON_ABI in ${PYTHON_ABIS}; do
173 arfrever 1.71 # 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 arfrever 1.66 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 arfrever 1.56 }
184    
185     # @FUNCTION: python_copy_sources
186 arfrever 1.71 # @USAGE: [--no-link] [--] [directory]
187 arfrever 1.56 # @DESCRIPTION:
188     # Copy unpacked sources of given package for each Python ABI.
189     python_copy_sources() {
190 arfrever 1.71 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 arfrever 1.56
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 arfrever 1.71 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 arfrever 1.56 done
228     done
229     }
230    
231     # @FUNCTION: python_set_build_dir_symlink
232     # @USAGE: [directory="build"]
233     # @DESCRIPTION:
234     # Create build directory symlink.
235     python_set_build_dir_symlink() {
236     local dir="$1"
237    
238 arfrever 1.66 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
239 arfrever 1.56 [[ -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 arfrever 1.71 # @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments]
248 arfrever 1.56 # @DESCRIPTION:
249 arfrever 1.60 # Execute specified function for each value of PYTHON_ABIS, optionally passing additional
250     # arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
251 arfrever 1.56 python_execute_function() {
252 arfrever 1.73 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 arfrever 1.56
254     while (($#)); do
255     case "$1" in
256     --action-message)
257     action_message_template="$2"
258     shift
259     ;;
260 arfrever 1.58 -d|--default-function)
261     default_function="1"
262     ;;
263 arfrever 1.56 --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 arfrever 1.71 --)
277     break
278     ;;
279 arfrever 1.56 -*)
280 arfrever 1.67 die "${FUNCNAME}(): Unrecognized option '$1'"
281 arfrever 1.56 ;;
282     *)
283     break
284     ;;
285     esac
286     shift
287     done
288    
289 arfrever 1.58 if [[ "${default_function}" == "0" ]]; then
290     if [[ "$#" -eq "0" ]]; then
291     die "${FUNCNAME}(): Missing function name"
292     fi
293     function="$1"
294     shift
295 arfrever 1.67
296     if [[ -z "$(type -t "${function}")" ]]; then
297     die "${FUNCNAME}(): '${function}' function isn't defined"
298     fi
299 arfrever 1.58 else
300     if [[ "$#" -ne "0" ]]; then
301 arfrever 1.67 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
302 arfrever 1.58 fi
303     if has "${EAPI:-0}" 0 1; then
304 arfrever 1.67 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
305 arfrever 1.58 fi
306    
307     if [[ "${EBUILD_PHASE}" == "configure" ]]; then
308 arfrever 1.61 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 arfrever 1.58 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 arfrever 1.56 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 arfrever 1.66
377 arfrever 1.56 if [[ "${separate_build_dirs}" == "1" ]]; then
378 arfrever 1.60 export BUILDDIR="${S}-${PYTHON_ABI}"
379     pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
380     else
381     export BUILDDIR="${S}"
382 arfrever 1.56 fi
383 arfrever 1.66
384 arfrever 1.73 previous_directory="$(pwd)"
385     previous_directory_stack="$(dirs -p)"
386     previous_directory_stack_length="$(dirs -p | wc -l)"
387 arfrever 1.72
388 arfrever 1.66 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 arfrever 1.56 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 arfrever 1.66
401 arfrever 1.69 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 arfrever 1.70 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 arfrever 1.56 if [[ "${quiet}" == "0" ]]; then
414     ewarn "${RED}${failure_message}${NORMAL}"
415     fi
416 arfrever 1.69 if [[ -z "${PYTHON_ABIS}" ]]; then
417     die "${function}() function failed with all enabled versions of Python"
418     fi
419 arfrever 1.56 else
420     die "${failure_message}"
421     fi
422     fi
423 arfrever 1.66
424 arfrever 1.73 # Ensure that directory stack hasn't been decreased.
425     if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
426 arfrever 1.72 die "Directory stack decreased illegally"
427     fi
428    
429 arfrever 1.73 # Avoid side effects of earlier returning from the specified function.
430     while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
431 arfrever 1.72 popd > /dev/null || die "popd failed"
432     done
433    
434 arfrever 1.73 # 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 arfrever 1.56 if [[ "${separate_build_dirs}" == "1" ]]; then
446     popd > /dev/null || die "popd failed"
447     fi
448 arfrever 1.60 unset BUILDDIR
449 arfrever 1.56 done
450 arfrever 1.58
451     if [[ "${default_function}" == "1" ]]; then
452     unset -f python_default_function
453     fi
454 arfrever 1.56 }
455    
456    
457 betelgeuse 1.55 # @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.
477 arfrever 1.67 if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
478 betelgeuse 1.55 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}"
527     fi
528    
529 arfrever 1.67 # @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.
533     if ! 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
544     fi
545    
546 hawking 1.46 # @FUNCTION: python_disable_pyc
547     # @DESCRIPTION:
548 arfrever 1.66 # Tell Python not to automatically recompile modules to .pyc/.pyo
549 hawking 1.46 # even if the timestamps/version stamps don't match. This is done
550     # to protect sandbox.
551     python_disable_pyc() {
552 arfrever 1.66 export PYTHONDONTWRITEBYTECODE="1"
553 hawking 1.46 }
554    
555     # @FUNCTION: python_enable_pyc
556     # @DESCRIPTION:
557 arfrever 1.66 # Tell Python to automatically recompile modules to .pyc/.pyo if the
558     # timestamps/version stamps have changed.
559 hawking 1.46 python_enable_pyc() {
560 hawking 1.53 unset PYTHONDONTWRITEBYTECODE
561 hawking 1.46 }
562    
563     python_disable_pyc
564    
565 hawking 1.52 # @FUNCTION: python_need_rebuild
566     # @DESCRIPTION: Run without arguments, specifies that the package should be
567     # rebuilt after a python upgrade.
568     python_need_rebuild() {
569     python_version
570     export PYTHON_NEED_REBUILD=${PYVER}
571     }
572    
573 arfrever 1.56 # @FUNCTION: python_get_includedir
574     # @DESCRIPTION:
575     # Run without arguments, returns the Python include directory.
576     python_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 pythonhead 1.43 # @FUNCTION: python_get_libdir
586     # @DESCRIPTION:
587 arfrever 1.56 # Run without arguments, returns the Python library directory.
588 pythonhead 1.43 python_get_libdir() {
589 arfrever 1.56 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 pythonhead 1.42 }
596    
597 pythonhead 1.43 # @FUNCTION: python_get_sitedir
598     # @DESCRIPTION:
599 arfrever 1.56 # Run without arguments, returns the Python site-packages directory.
600 pythonhead 1.43 python_get_sitedir() {
601     echo "$(python_get_libdir)/site-packages"
602 pythonhead 1.42 }
603    
604 dev-zero 1.33 # @FUNCTION: python_tkinter_exists
605     # @DESCRIPTION:
606     # Run without arguments, checks if python was compiled with Tkinter
607     # support. If not, prints an error message and dies.
608 liquidx 1.1 python_tkinter_exists() {
609     if ! python -c "import Tkinter" >/dev/null 2>&1; then
610     eerror "You need to recompile python with Tkinter support."
611 kloeri 1.30 eerror "Try adding: 'dev-lang/python tk'"
612 liquidx 1.27 eerror "in to /etc/portage/package.use"
613 liquidx 1.1 echo
614     die "missing tkinter support with installed python"
615     fi
616     }
617    
618 dev-zero 1.33 # @FUNCTION: python_mod_exists
619 arfrever 1.56 # @USAGE: <module>
620 dev-zero 1.33 # @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.
625 liquidx 1.1 #
626 dev-zero 1.33 # Example:
627 liquidx 1.1 # if python_mod_exists gtk; then
628 liquidx 1.28 # echo "gtk support enabled"
629 liquidx 1.1 # fi
630     python_mod_exists() {
631 zmedico 1.54 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
632 arfrever 1.56 python -c "import $1" &>/dev/null
633 liquidx 1.1 }
634    
635 dev-zero 1.33 # @FUNCTION: python_mod_compile
636 arfrever 1.56 # @USAGE: <file> [more files ...]
637 dev-zero 1.33 # @DESCRIPTION:
638 hawking 1.35 # Given filenames, it will pre-compile the module's .pyc and .pyo.
639 hawking 1.37 # This function should only be run in pkg_postinst()
640 liquidx 1.1 #
641 dev-zero 1.33 # Example:
642 hawking 1.35 # python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
643 liquidx 1.1 #
644     python_mod_compile() {
645 arfrever 1.56 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 hawking 1.48 local f myroot myfiles=()
650 hawking 1.37
651     # Check if phase is pkg_postinst()
652     [[ ${EBUILD_PHASE} != postinst ]] &&\
653 hawking 1.39 die "${FUNCNAME} should only be run in pkg_postinst()"
654 hawking 1.37
655 liquidx 1.5 # allow compiling for older python versions
656 hawking 1.48 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
657 liquidx 1.5 PYVER=${PYTHON_OVERRIDE_PYVER}
658 pythonhead 1.7 else
659 liquidx 1.5 python_version
660     fi
661 pythonhead 1.7
662 hawking 1.35 # strip trailing slash
663     myroot="${ROOT%/}"
664    
665     # respect ROOT
666 hawking 1.51 for f in "$@"; do
667 hawking 1.48 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
668 hawking 1.35 done
669    
670 hawking 1.48 if ((${#myfiles[@]})); then
671     python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
672 arfrever 1.63 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" &> /dev/null
673 liquidx 1.1 else
674 hawking 1.35 ewarn "No files to compile!"
675 pythonhead 1.7 fi
676 liquidx 1.1 }
677    
678 dev-zero 1.33 # @FUNCTION: python_mod_optimize
679 arfrever 1.56 # @USAGE: [options] [directory|file]
680 dev-zero 1.33 # @DESCRIPTION:
681 arfrever 1.56 # 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).
683 liquidx 1.1 #
684 dev-zero 1.33 # If supplied with arguments, it will recompile all modules recursively
685 arfrever 1.56 # in the supplied directory.
686     # This function should only be run in pkg_postinst().
687 dev-zero 1.33 #
688 arfrever 1.56 # Options passed to this function are passed to compileall.py.
689 hawking 1.39 #
690 dev-zero 1.33 # Example:
691 arfrever 1.56 # python_mod_optimize ctypesgencore
692 liquidx 1.1 python_mod_optimize() {
693 arfrever 1.56 # 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 hawking 1.37
699 arfrever 1.56 # 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 arfrever 1.62 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
731 arfrever 1.56 site_packages_dirs+=("$1")
732     break
733 arfrever 1.62 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
734 arfrever 1.56 site_packages_files+=("$1")
735     break
736 arfrever 1.62 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
737 arfrever 1.56 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 hawking 1.37
748 arfrever 1.56 # Set additional options.
749     options+=("-q")
750 liquidx 1.13
751 arfrever 1.56 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 arfrever 1.62 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
758 arfrever 1.56 done
759 arfrever 1.62 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
760 arfrever 1.63 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
761 arfrever 1.56 fi
762     if ((${#site_packages_files[@]})); then
763     for file in "${site_packages_files[@]}"; do
764 arfrever 1.62 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
765 arfrever 1.56 done
766 arfrever 1.62 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
767 arfrever 1.63 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
768 hawking 1.49 fi
769 arfrever 1.56 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 hawking 1.36
777 arfrever 1.56 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 arfrever 1.62 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
782 arfrever 1.63 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
783 arfrever 1.56 fi
784     if ((${#other_files[@]})); then
785 arfrever 1.62 python${PYVER} "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
786 arfrever 1.63 python${PYVER} -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
787 arfrever 1.56 fi
788     eend "${return_code}"
789     fi
790 pythonhead 1.7 else
791 arfrever 1.63 local myroot mydirs=() myfiles=() myopts=() return_code="0"
792 arfrever 1.56
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    
825     # allow compiling for older python versions
826     if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
827     PYVER=${PYTHON_OVERRIDE_PYVER}
828     else
829     python_version
830     fi
831    
832     # set additional opts
833     myopts+=(-q)
834 pythonhead 1.7
835 arfrever 1.56 ebegin "Byte compiling python modules for python-${PYVER} .."
836     if ((${#mydirs[@]})); then
837     python${PYVER} \
838     "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
839 arfrever 1.63 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
840 arfrever 1.56 python${PYVER} -O \
841     "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
842 arfrever 1.63 "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
843 arfrever 1.56 fi
844 pythonhead 1.7
845 arfrever 1.56 if ((${#myfiles[@]})); then
846     python_mod_compile "${myfiles[@]}"
847     fi
848 hawking 1.36
849 arfrever 1.63 eend "${return_code}"
850 hawking 1.36 fi
851 liquidx 1.1 }
852    
853 dev-zero 1.33 # @FUNCTION: python_mod_cleanup
854 arfrever 1.56 # @USAGE: [directory]
855 dev-zero 1.33 # @DESCRIPTION:
856     # Run with optional arguments, where arguments are directories of
857 arfrever 1.56 # python modules. If none given, it will look in /usr/lib/python[0-9].[0-9].
858 dev-zero 1.33 #
859 arfrever 1.56 # It will recursively scan all compiled Python modules in the directories and
860     # determine if they are orphaned (i.e. their corresponding .py files are missing.)
861     # If they are, then it will remove their corresponding .pyc and .pyo files.
862 hawking 1.37 #
863 arfrever 1.56 # This function should only be run in pkg_postrm().
864 liquidx 1.1 python_mod_cleanup() {
865 arfrever 1.56 local PYTHON_ABI SEARCH_PATH=() root src_py
866 liquidx 1.16
867 arfrever 1.56 # Check if phase is pkg_postrm().
868     [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
869 hawking 1.37
870 arfrever 1.56 # Strip trailing slash from ROOT.
871     root="${ROOT%/}"
872 liquidx 1.1
873 hawking 1.50 if (($#)); then
874 arfrever 1.56 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 arfrever 1.62 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
883 arfrever 1.56 done
884     fi
885     shift
886     done
887     else
888     SEARCH_PATH=("${@#/}")
889     SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
890     fi
891 liquidx 1.5 else
892 arfrever 1.56 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages)
893 liquidx 1.5 fi
894 pythonhead 1.7
895 hawking 1.50 for path in "${SEARCH_PATH[@]}"; do
896 arfrever 1.62 [[ ! -d "${path}" ]] && continue
897 liquidx 1.11 einfo "Cleaning orphaned Python bytecode from ${path} .."
898 zmedico 1.54 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
899 hawking 1.50 src_py="${REPLY%[co]}"
900 arfrever 1.62 [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue
901 hawking 1.50 einfo "Purging ${src_py}[co]"
902     rm -f "${src_py}"[co]
903 zmedico 1.54 done
904 hawking 1.50
905 arfrever 1.56 # Attempt to remove directories that may be empty.
906 zmedico 1.54 find "${path}" -type d | sort -r | while read -r dir; do
907 arfrever 1.62 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}"
908 zmedico 1.54 done
909 pythonhead 1.7 done
910 liquidx 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.20