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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20