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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.82 - (hide annotations) (download)
Wed Dec 23 23:43:42 2009 UTC (4 years, 9 months ago) by arfrever
Branch: MAIN
Changes since 1.81: +283 -26 lines
Update EAPI checks. Add support for -2, -3, --only-ABI and --final-ABI options of PYTHON(). Add python_set_active_version(). Add python_generate_wrapper_scripts().

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.82 # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.81 2009/11/22 16:45:54 arfrever Exp $
4 liquidx 1.1
5 dev-zero 1.33 # @ECLASS: python.eclass
6     # @MAINTAINER:
7     # python@gentoo.org
8 arfrever 1.74 # @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules.
9 dev-zero 1.33 # @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 arfrever 1.77 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.74 DEPEND+=" >=app-admin/eselect-python-20090804
23     >=app-shells/bash-3.2"
24 arfrever 1.56
25 liquidx 1.18 __python_eclass_test() {
26     __python_version_extract 2.3
27     echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
28     echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
29     __python_version_extract 2.3.4
30     echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
31     echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
32     __python_version_extract 2.3.5
33     echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
34     echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
35     __python_version_extract 2.4
36     echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
37     echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
38 liquidx 1.28 __python_version_extract 2.5b3
39     echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
40     echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
41 liquidx 1.18 }
42    
43 dev-zero 1.33 # @FUNCTION: python_version
44     # @DESCRIPTION:
45     # Run without arguments and it will export the version of python
46     # currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
47 liquidx 1.18 __python_version_extract() {
48 hawking 1.45 local verstr=$1
49 liquidx 1.18 export PYVER_MAJOR=${verstr:0:1}
50     export PYVER_MINOR=${verstr:2:1}
51 hawking 1.47 if [[ ${verstr:3:1} == . ]]; then
52 swegener 1.29 export PYVER_MICRO=${verstr:4}
53 liquidx 1.28 fi
54 swegener 1.21 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
55 liquidx 1.18 }
56    
57 liquidx 1.1 python_version() {
58 hawking 1.44 [[ -n "${PYVER}" ]] && return 0
59 liquidx 1.1 local tmpstr
60     python=${python:-/usr/bin/python}
61 arfrever 1.82 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
62 liquidx 1.1 export PYVER_ALL="${tmpstr#Python }"
63 liquidx 1.18 __python_version_extract $PYVER_ALL
64 liquidx 1.1 }
65    
66 arfrever 1.60 # @FUNCTION: PYTHON
67 arfrever 1.82 # @USAGE: [-2] [-3] [-A|--only-ABI] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
68 arfrever 1.56 # @DESCRIPTION:
69     # Get Python interpreter filename for specified Python ABI. If Python_ABI argument
70     # is ommitted, then PYTHON_ABI environment variable must be set and is used.
71 arfrever 1.82 # If -2 option is specified, then active version of Python 2 is used.
72     # If -3 option is specified, then active version of Python 3 is used.
73     # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
74     # -2, -3 and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
75     # If --only-ABI option is specified, then only specified Python ABI is printed instead of
76     # Python interpreter filename.
77     # --only-ABI and --absolute-path options cannot be specified simultaneously.
78 arfrever 1.60 PYTHON() {
79 arfrever 1.82 local absolute_path="0" final_ABI="0" only_ABI="0" python2="0" python3="0" slot=
80 arfrever 1.56
81     while (($#)); do
82     case "$1" in
83 arfrever 1.82 -2)
84     python2="1"
85     ;;
86     -3)
87     python3="1"
88     ;;
89     -A|--only-ABI)
90     only_ABI="1"
91     ;;
92 arfrever 1.56 -a|--absolute-path)
93     absolute_path="1"
94     ;;
95 arfrever 1.82 -f|--final-ABI)
96     final_ABI="1"
97     ;;
98 arfrever 1.71 --)
99     break
100     ;;
101 arfrever 1.56 -*)
102 arfrever 1.82 die "${FUNCNAME}(): Unrecognized option '$1'"
103 arfrever 1.56 ;;
104     *)
105     break
106     ;;
107     esac
108     shift
109     done
110    
111 arfrever 1.82 if [[ "${only_ABI}" == "1" && "${absolute_path}" == "1" ]]; then
112     die "${FUNCNAME}(): '--only-ABI and '--absolute-path' options cannot be specified simultaneously"
113     fi
114    
115     if [[ "$((${python2} + ${python3} + ${final_ABI}))" -gt "1" ]]; then
116     die "${FUNCNAME}(): '-2', '-3' or '--final-ABI' options cannot be specified simultaneously"
117     fi
118    
119 arfrever 1.56 if [[ "$#" -eq "0" ]]; then
120 arfrever 1.82 if [[ "${final_ABI}" == "1" ]]; then
121     validate_PYTHON_ABIS
122     slot="${PYTHON_ABIS##* }"
123     elif [[ "${python2}" == "1" ]]; then
124     slot="$(eselect python show --python2)"
125     if [[ -z "${slot}" ]]; then
126     die "${FUNCNAME}(): Active Python 2 interpreter not set"
127     elif [[ "${slot}" != "python2."* ]]; then
128     die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
129     fi
130     slot="${slot#python}"
131     elif [[ "${python3}" == "1" ]]; then
132     slot="$(eselect python show --python3)"
133     if [[ -z "${slot}" ]]; then
134     die "${FUNCNAME}(): Active Python 3 interpreter not set"
135     elif [[ "${slot}" != "python3."* ]]; then
136     die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
137     fi
138     slot="${slot#python}"
139     elif [[ -n "${PYTHON_ABI}" ]]; then
140 arfrever 1.56 slot="${PYTHON_ABI}"
141     else
142     die "${FUNCNAME}(): Invalid usage"
143     fi
144     elif [[ "$#" -eq "1" ]]; then
145 arfrever 1.82 if [[ "${final_ABI}" == "1" ]]; then
146     die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
147     fi
148     if [[ "${python2}" == "1" ]]; then
149     die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
150     fi
151     if [[ "${python3}" == "1" ]]; then
152     die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
153     fi
154 arfrever 1.56 slot="$1"
155     else
156     die "${FUNCNAME}(): Invalid usage"
157     fi
158    
159 arfrever 1.82 if [[ "${only_ABI}" == "1" ]]; then
160     echo -n "${slot}"
161     return
162     elif [[ "${absolute_path}" == "1" ]]; then
163 arfrever 1.56 echo -n "/usr/bin/python${slot}"
164     else
165     echo -n "python${slot}"
166     fi
167 arfrever 1.78
168 arfrever 1.79 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
169 arfrever 1.78 echo -n "-${ABI}"
170     fi
171 arfrever 1.56 }
172    
173 arfrever 1.82 # @FUNCTION: python_set_active_version
174     # @USAGE: <Python_ABI|2|3>
175     # @DESCRIPTION:
176     # Set active version of Python.
177     python_set_active_version() {
178     if [[ "$#" -ne "1" ]]; then
179     die "${FUNCNAME}() requires 1 argument"
180     fi
181    
182     if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
183     if ! has_version "dev-lang/python:$1"; then
184     die "${FUNCNAME}(): 'dev-lang/python:$1' isn't installed"
185     fi
186     export EPYTHON="$(PYTHON "$1")"
187     elif [[ "$1" == "2" ]]; then
188     if ! has_version "=dev-lang/python-2*"; then
189     die "${FUNCNAME}(): '=dev-lang/python-2*' isn't installed"
190     fi
191     export EPYTHON="$(PYTHON -2)"
192     elif [[ "$1" == "3" ]]; then
193     if ! has_version "=dev-lang/python-3*"; then
194     die "${FUNCNAME}(): '=dev-lang/python-3*' isn't installed"
195     fi
196     export EPYTHON="$(PYTHON -3)"
197     else
198     die "${FUNCNAME}(): Unrecognized argument '$1'"
199     fi
200    
201     # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
202     # so it doesn't need to be exported to subprocesses.
203     PYTHON_ABI="${EPYTHON#python}"
204     PYTHON_ABI="${PYTHON_ABI%%-*}"
205     }
206    
207 arfrever 1.77 unset PYTHON_ABIS
208     unset PYTHON_ABIS_SANITY_CHECKS
209    
210 arfrever 1.56 # @FUNCTION: validate_PYTHON_ABIS
211     # @DESCRIPTION:
212 arfrever 1.76 # Ensure that PYTHON_ABIS variable has valid value.
213 arfrever 1.56 validate_PYTHON_ABIS() {
214 arfrever 1.66 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable.
215     if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
216     die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
217     fi
218    
219 arfrever 1.59 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
220 arfrever 1.63 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then
221 arfrever 1.66 die "'/usr/bin/python' isn't valid symlink"
222 arfrever 1.57 fi
223     if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
224 arfrever 1.66 die "'/usr/bin/python-config' isn't valid script"
225 arfrever 1.57 fi
226    
227 arfrever 1.82 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
228     if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
229 arfrever 1.78 local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS=
230 arfrever 1.56 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
231 arfrever 1.75 python2_supported_versions="2.4 2.5 2.6 2.7"
232     python3_supported_versions="3.0 3.1 3.2"
233 arfrever 1.68
234 arfrever 1.70 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
235 arfrever 1.75 local python2_enabled="0" python3_enabled="0"
236    
237 arfrever 1.68 if [[ -z "${USE_PYTHON}" ]]; then
238     die "USE_PYTHON variable is empty"
239 arfrever 1.56 fi
240 arfrever 1.68
241 arfrever 1.78 for PYTHON_ABI in ${USE_PYTHON}; do
242     if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
243     die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
244 arfrever 1.68 fi
245 arfrever 1.75
246 arfrever 1.78 if has "${PYTHON_ABI}" ${python2_supported_versions}; then
247 arfrever 1.75 python2_enabled="1"
248     fi
249 arfrever 1.78 if has "${PYTHON_ABI}" ${python3_supported_versions}; then
250 arfrever 1.75 python3_enabled="1"
251     fi
252    
253 arfrever 1.68 support_ABI="1"
254 arfrever 1.66 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
255 arfrever 1.78 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
256 arfrever 1.66 support_ABI="0"
257     break
258     fi
259     done
260 arfrever 1.82 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
261 arfrever 1.68 done
262    
263     if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
264 arfrever 1.74 die "USE_PYTHON variable doesn't enable any version of Python supported by ${CATEGORY}/${PF}"
265 arfrever 1.66 fi
266 arfrever 1.75
267     if [[ "${python2_enabled}" == "0" ]]; then
268     ewarn "USE_PYTHON variable doesn't enable any version of Python 2. This configuration is unsupported."
269     fi
270     if [[ "${python3_enabled}" == "0" ]]; then
271     ewarn "USE_PYTHON variable doesn't enable any version of Python 3. This configuration is unsupported."
272     fi
273 arfrever 1.68 else
274 arfrever 1.76 local python_version python2_version= python3_version= support_python_major_version
275    
276     python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
277 arfrever 1.74
278     if has_version "=dev-lang/python-2*"; then
279     if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then
280     die "'/usr/bin/python2' isn't valid symlink"
281     fi
282    
283     python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
284    
285 arfrever 1.78 for PYTHON_ABI in ${python2_supported_versions}; do
286 arfrever 1.74 support_python_major_version="1"
287     for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
288 arfrever 1.78 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
289 arfrever 1.74 support_python_major_version="0"
290     fi
291     done
292     [[ "${support_python_major_version}" == "1" ]] && break
293     done
294     if [[ "${support_python_major_version}" == "1" ]]; then
295     for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
296 arfrever 1.77 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
297 arfrever 1.74 die "Active version of Python 2 isn't supported by ${CATEGORY}/${PF}"
298     fi
299     done
300     else
301     python2_version=""
302     fi
303     fi
304 arfrever 1.56
305 arfrever 1.74 if has_version "=dev-lang/python-3*"; then
306     if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then
307     die "'/usr/bin/python3' isn't valid symlink"
308 arfrever 1.68 fi
309 arfrever 1.74
310     python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
311    
312 arfrever 1.78 for PYTHON_ABI in ${python3_supported_versions}; do
313 arfrever 1.74 support_python_major_version="1"
314     for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
315 arfrever 1.78 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
316 arfrever 1.74 support_python_major_version="0"
317     fi
318     done
319     [[ "${support_python_major_version}" == "1" ]] && break
320     done
321     if [[ "${support_python_major_version}" == "1" ]]; then
322     for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
323 arfrever 1.77 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
324 arfrever 1.74 die "Active version of Python 3 isn't supported by ${CATEGORY}/${PF}"
325     fi
326     done
327     else
328     python3_version=""
329     fi
330     fi
331    
332 arfrever 1.82 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
333     eerror "Python wrapper is configured incorrectly or /usr/bin/python2 symlink"
334     eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
335     die "Incorrect configuration of Python"
336     fi
337     if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
338     eerror "Python wrapper is configured incorrectly or /usr/bin/python3 symlink"
339 arfrever 1.76 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
340     die "Incorrect configuration of Python"
341     fi
342    
343 arfrever 1.74 PYTHON_ABIS="${python2_version} ${python3_version}"
344     PYTHON_ABIS="${PYTHON_ABIS# }"
345     export PYTHON_ABIS="${PYTHON_ABIS% }"
346 arfrever 1.68 fi
347 arfrever 1.56 fi
348 arfrever 1.66
349 arfrever 1.77 if [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
350     local PYTHON_ABI
351     for PYTHON_ABI in ${PYTHON_ABIS}; do
352 arfrever 1.82 # Ensure that appropriate version of Python is installed.
353 arfrever 1.77 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
354     die "dev-lang/python:${PYTHON_ABI} isn't installed"
355     fi
356 arfrever 1.71
357 arfrever 1.77 # Ensure that EPYTHON variable is respected.
358     if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
359 arfrever 1.79 eerror "python: '$(type -p python)'"
360     eerror "ABI: '${ABI}'"
361     eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
362     eerror "EPYTHON: '$(PYTHON)'"
363     eerror "PYTHON_ABI: '${PYTHON_ABI}'"
364     eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')'"
365 arfrever 1.77 die "'python' doesn't respect EPYTHON variable"
366     fi
367     done
368     PYTHON_ABIS_SANITY_CHECKS="1"
369     fi
370 arfrever 1.56 }
371    
372     # @FUNCTION: python_copy_sources
373 arfrever 1.71 # @USAGE: [--no-link] [--] [directory]
374 arfrever 1.56 # @DESCRIPTION:
375     # Copy unpacked sources of given package for each Python ABI.
376     python_copy_sources() {
377 arfrever 1.71 local dir dirs=() no_link="0" PYTHON_ABI
378    
379     while (($#)); do
380     case "$1" in
381     --no-link)
382     no_link="1"
383     ;;
384     --)
385     break
386     ;;
387     -*)
388     die "${FUNCNAME}(): Unrecognized option '$1'"
389     ;;
390     *)
391     break
392     ;;
393     esac
394     shift
395     done
396 arfrever 1.56
397     if [[ "$#" -eq "0" ]]; then
398     if [[ "${WORKDIR}" == "${S}" ]]; then
399     die "${FUNCNAME}() cannot be used"
400     fi
401     dirs="${S}"
402     else
403     dirs="$@"
404     fi
405    
406     validate_PYTHON_ABIS
407     for PYTHON_ABI in ${PYTHON_ABIS}; do
408     for dir in "${dirs[@]}"; do
409 arfrever 1.71 if [[ "${no_link}" == "1" ]]; then
410     cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
411     else
412     cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
413     fi
414 arfrever 1.56 done
415     done
416     }
417    
418     # @FUNCTION: python_set_build_dir_symlink
419     # @USAGE: [directory="build"]
420     # @DESCRIPTION:
421     # Create build directory symlink.
422     python_set_build_dir_symlink() {
423     local dir="$1"
424    
425 arfrever 1.66 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
426 arfrever 1.56 [[ -z "${dir}" ]] && dir="build"
427    
428     # Don't delete preexistent directories.
429     rm -f "${dir}" || die "Deletion of '${dir}' failed"
430     ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
431     }
432    
433     # @FUNCTION: python_execute_function
434 arfrever 1.81 # @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
435 arfrever 1.56 # @DESCRIPTION:
436 arfrever 1.60 # Execute specified function for each value of PYTHON_ABIS, optionally passing additional
437     # arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
438 arfrever 1.56 python_execute_function() {
439 arfrever 1.81 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
440 arfrever 1.56
441     while (($#)); do
442     case "$1" in
443     --action-message)
444     action_message_template="$2"
445     shift
446     ;;
447 arfrever 1.58 -d|--default-function)
448     default_function="1"
449     ;;
450 arfrever 1.56 --failure-message)
451     failure_message_template="$2"
452     shift
453     ;;
454     --nonfatal)
455     nonfatal="1"
456     ;;
457     -q|--quiet)
458     quiet="1"
459     ;;
460     -s|--separate-build-dirs)
461     separate_build_dirs="1"
462     ;;
463 arfrever 1.81 --source-dir)
464     source_dir="$2"
465     shift
466     ;;
467 arfrever 1.71 --)
468     break
469     ;;
470 arfrever 1.56 -*)
471 arfrever 1.67 die "${FUNCNAME}(): Unrecognized option '$1'"
472 arfrever 1.56 ;;
473     *)
474     break
475     ;;
476     esac
477     shift
478     done
479    
480 arfrever 1.81 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
481     die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
482     fi
483    
484 arfrever 1.58 if [[ "${default_function}" == "0" ]]; then
485     if [[ "$#" -eq "0" ]]; then
486     die "${FUNCNAME}(): Missing function name"
487     fi
488     function="$1"
489     shift
490 arfrever 1.67
491     if [[ -z "$(type -t "${function}")" ]]; then
492     die "${FUNCNAME}(): '${function}' function isn't defined"
493     fi
494 arfrever 1.58 else
495     if [[ "$#" -ne "0" ]]; then
496 arfrever 1.67 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
497 arfrever 1.58 fi
498     if has "${EAPI:-0}" 0 1; then
499 arfrever 1.67 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
500 arfrever 1.58 fi
501    
502     if [[ "${EBUILD_PHASE}" == "configure" ]]; then
503 arfrever 1.82 if has "${EAPI}" 2 3; then
504 arfrever 1.61 python_default_function() {
505     econf
506     }
507     else
508     python_default_function() {
509     nonfatal econf
510     }
511     fi
512 arfrever 1.58 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
513     python_default_function() {
514     emake
515     }
516     elif [[ "${EBUILD_PHASE}" == "test" ]]; then
517     python_default_function() {
518     if emake -j1 -n check &> /dev/null; then
519     emake -j1 check
520     elif emake -j1 -n test &> /dev/null; then
521     emake -j1 test
522     fi
523     }
524     elif [[ "${EBUILD_PHASE}" == "install" ]]; then
525     python_default_function() {
526     emake DESTDIR="${D}" install
527     }
528     else
529 arfrever 1.82 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
530 arfrever 1.58 fi
531     function="python_default_function"
532 arfrever 1.56 fi
533    
534     if [[ "${quiet}" == "0" ]]; then
535     [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
536     [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
537     [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
538     [[ "${EBUILD_PHASE}" == "configure" ]] && action="Configuration"
539     [[ "${EBUILD_PHASE}" == "compile" ]] && action="Building"
540     [[ "${EBUILD_PHASE}" == "test" ]] && action="Testing"
541     [[ "${EBUILD_PHASE}" == "install" ]] && action="Installation"
542     [[ "${EBUILD_PHASE}" == "preinst" ]] && action="Preinstallation"
543     [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
544     [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
545     [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
546     fi
547    
548     local RED GREEN BLUE NORMAL
549     if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
550     RED=$'\e[1;31m'
551     GREEN=$'\e[1;32m'
552     BLUE=$'\e[1;34m'
553     NORMAL=$'\e[0m'
554     else
555     RED=
556     GREEN=
557     BLUE=
558     NORMAL=
559     fi
560    
561     validate_PYTHON_ABIS
562     for PYTHON_ABI in ${PYTHON_ABIS}; do
563     if [[ "${quiet}" == "0" ]]; then
564     if [[ -n "${action_message_template}" ]]; then
565     action_message="$(eval echo -n "${action_message_template}")"
566     else
567     action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..."
568     fi
569     echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
570     fi
571 arfrever 1.66
572 arfrever 1.56 if [[ "${separate_build_dirs}" == "1" ]]; then
573 arfrever 1.81 if [[ -n "${source_dir}" ]]; then
574     export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
575     else
576     export BUILDDIR="${S}-${PYTHON_ABI}"
577     fi
578 arfrever 1.60 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
579     else
580     export BUILDDIR="${S}"
581 arfrever 1.56 fi
582 arfrever 1.66
583 arfrever 1.73 previous_directory="$(pwd)"
584     previous_directory_stack="$(dirs -p)"
585     previous_directory_stack_length="$(dirs -p | wc -l)"
586 arfrever 1.72
587 arfrever 1.82 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
588 arfrever 1.66 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
589     else
590     EPYTHON="$(PYTHON)" "${function}" "$@"
591     fi
592    
593     if [[ "$?" != "0" ]]; then
594 arfrever 1.56 if [[ -n "${failure_message_template}" ]]; then
595     failure_message="$(eval echo -n "${failure_message_template}")"
596     else
597     failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
598     fi
599 arfrever 1.66
600 arfrever 1.69 if [[ "${nonfatal}" == "1" ]]; then
601     if [[ "${quiet}" == "0" ]]; then
602     ewarn "${RED}${failure_message}${NORMAL}"
603     fi
604     elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
605 arfrever 1.70 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
606 arfrever 1.78 local enabled_PYTHON_ABIS= other_PYTHON_ABI
607     for other_PYTHON_ABI in ${PYTHON_ABIS}; do
608 arfrever 1.82 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
609 arfrever 1.70 done
610 arfrever 1.82 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
611 arfrever 1.70 fi
612 arfrever 1.56 if [[ "${quiet}" == "0" ]]; then
613     ewarn "${RED}${failure_message}${NORMAL}"
614     fi
615 arfrever 1.69 if [[ -z "${PYTHON_ABIS}" ]]; then
616     die "${function}() function failed with all enabled versions of Python"
617     fi
618 arfrever 1.56 else
619     die "${failure_message}"
620     fi
621     fi
622 arfrever 1.66
623 arfrever 1.73 # Ensure that directory stack hasn't been decreased.
624     if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
625 arfrever 1.72 die "Directory stack decreased illegally"
626     fi
627    
628 arfrever 1.73 # Avoid side effects of earlier returning from the specified function.
629     while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
630 arfrever 1.72 popd > /dev/null || die "popd failed"
631     done
632    
633 arfrever 1.73 # Ensure that the bottom part of directory stack hasn't been changed. Restore
634     # previous directory (from before running of the specified function) before
635     # comparison of directory stacks to avoid mismatch of directory stacks after
636     # potential using of 'cd' to change current directory. Restoration of previous
637     # directory allows to safely use 'cd' to change current directory in the
638     # specified function without changing it back to original directory.
639     cd "${previous_directory}"
640     if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
641     die "Directory stack changed illegally"
642     fi
643    
644 arfrever 1.56 if [[ "${separate_build_dirs}" == "1" ]]; then
645     popd > /dev/null || die "popd failed"
646     fi
647 arfrever 1.60 unset BUILDDIR
648 arfrever 1.56 done
649 arfrever 1.58
650     if [[ "${default_function}" == "1" ]]; then
651     unset -f python_default_function
652     fi
653 arfrever 1.56 }
654    
655 arfrever 1.77 # @FUNCTION: python_convert_shebangs
656     # @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
657     # @DESCRIPTION:
658     # Convert shebangs in specified files. Directories can be specified only with --recursive option.
659     python_convert_shebangs() {
660     local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
661    
662     while (($#)); do
663     case "$1" in
664     -r|--recursive)
665     recursive="1"
666     ;;
667     -q|--quiet)
668     quiet="1"
669     ;;
670     -x|--only-executables)
671     only_executables="1"
672     ;;
673     --)
674     break
675     ;;
676     -*)
677     die "${FUNCNAME}(): Unrecognized option '$1'"
678     ;;
679     *)
680     break
681     ;;
682     esac
683     shift
684     done
685    
686     if [[ "$#" -eq 0 ]]; then
687     die "${FUNCNAME}(): Missing Python version and files or directories"
688     elif [[ "$#" -eq 1 ]]; then
689     die "${FUNCNAME}(): Missing files or directories"
690     fi
691    
692     python_version="$1"
693     shift
694    
695     for argument in "$@"; do
696     if [[ ! -e "${argument}" ]]; then
697     die "${FUNCNAME}(): '${argument}' doesn't exist"
698     elif [[ -f "${argument}" ]]; then
699     files+=("${argument}")
700     elif [[ -d "${argument}" ]]; then
701     if [[ "${recursive}" == "1" ]]; then
702     if [[ "${only_executables}" == "1" ]]; then
703     files+=($(find "${argument}" -perm /111 -type f))
704     else
705     files+=($(find "${argument}" -type f))
706     fi
707     else
708     die "${FUNCNAME}(): '${argument}' isn't a regular file"
709     fi
710     else
711     die "${FUNCNAME}(): '${argument}' isn't a regular file or a directory"
712     fi
713     done
714    
715     for file in "${files[@]}"; do
716 arfrever 1.78 file="${file#./}"
717 arfrever 1.77 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
718    
719     if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
720 arfrever 1.82 if [[ "${quiet}" == "0" ]]; then
721     einfo "Converting shebang in '${file}'"
722     fi
723 arfrever 1.77 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
724    
725     # Delete potential whitespace after "#!".
726     sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
727     fi
728     done
729     }
730 arfrever 1.56
731 arfrever 1.82 # @FUNCTION: python_generate_wrapper_scripts
732     # @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
733     # @DESCRIPTION:
734     # Generate wrapper scripts. Existing files are overwritten only with --force option.
735     # If --respect-EPYTHON option is specified, then generated wrapper scripts will
736     # respect EPYTHON variable at run time.
737     python_generate_wrapper_scripts() {
738     local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python2_supported_versions python3_enabled="0" python3_supported_versions respect_EPYTHON="0"
739     python2_supported_versions="2.4 2.5 2.6 2.7"
740     python3_supported_versions="3.0 3.1 3.2"
741    
742     while (($#)); do
743     case "$1" in
744     -E|--respect-EPYTHON)
745     respect_EPYTHON="1"
746     ;;
747     -f|--force)
748     force="1"
749     ;;
750     -q|--quiet)
751     quiet="1"
752     ;;
753     --)
754     break
755     ;;
756     -*)
757     die "${FUNCNAME}(): Unrecognized option '$1'"
758     ;;
759     *)
760     break
761     ;;
762     esac
763     shift
764     done
765    
766     if [[ "$#" -eq 0 ]]; then
767     die "${FUNCNAME}(): Missing arguments"
768     fi
769    
770     validate_PYTHON_ABIS
771     for PYTHON_ABI in ${python2_supported_versions}; do
772     if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
773     python2_enabled="1"
774     fi
775     done
776     for PYTHON_ABI in ${python3_supported_versions}; do
777     if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
778     python3_enabled="1"
779     fi
780     done
781    
782     if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
783     eselect_python_option=
784     elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
785     eselect_python_option="--python2"
786     elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
787     eselect_python_option="--python3"
788     else
789     die "${FUNCNAME}(): Unsupported environment"
790     fi
791    
792     for file in "$@"; do
793     if [[ -f "${file}" && "${force}" == "0" ]]; then
794     die "${FUNCNAME}(): '$1' already exists"
795     fi
796    
797     if [[ "${quiet}" == "0" ]]; then
798     einfo "Generating '${file#${D%/}}' wrapper script"
799     fi
800    
801     cat << EOF > "${file}"
802     #!/usr/bin/env python
803     # Gentoo '${file##*/}' wrapper script
804    
805     import os
806     import re
807     import subprocess
808     import sys
809    
810     EPYTHON_re = re.compile(r"^python(\d+\.\d+)$")
811    
812     EOF
813     if [[ "$?" != "0" ]]; then
814     die "${FUNCNAME}(): Generation of '$1' failed"
815     fi
816     if [[ "${respect_EPYTHON}" == "1" ]]; then
817     cat << EOF >> "${file}"
818     EPYTHON = os.environ.get("EPYTHON")
819     if EPYTHON:
820     EPYTHON_matched = EPYTHON_re.match(EPYTHON)
821     if EPYTHON_matched:
822     PYTHON_ABI = EPYTHON_matched.group(1)
823     else:
824     sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
825     sys.exit(1)
826     else:
827     try:
828     eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
829     if eselect_process.wait() != 0:
830     raise ValueError
831     except (OSError, ValueError):
832     sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
833     sys.exit(1)
834    
835     eselect_output = eselect_process.stdout.read()
836     if not isinstance(eselect_output, str):
837     # Python 3
838     eselect_output = eselect_output.decode()
839    
840     EPYTHON_matched = EPYTHON_re.match(eselect_output)
841     if EPYTHON_matched:
842     PYTHON_ABI = EPYTHON_matched.group(1)
843     else:
844     sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
845     sys.exit(1)
846     EOF
847     if [[ "$?" != "0" ]]; then
848     die "${FUNCNAME}(): Generation of '$1' failed"
849     fi
850     else
851     cat << EOF >> "${file}"
852     try:
853     eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
854     if eselect_process.wait() != 0:
855     raise ValueError
856     except (OSError, ValueError):
857     sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
858     sys.exit(1)
859    
860     eselect_output = eselect_process.stdout.read()
861     if not isinstance(eselect_output, str):
862     # Python 3
863     eselect_output = eselect_output.decode()
864    
865     EPYTHON_matched = EPYTHON_re.match(eselect_output)
866     if EPYTHON_matched:
867     PYTHON_ABI = EPYTHON_matched.group(1)
868     else:
869     sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
870     sys.exit(1)
871     EOF
872     if [[ "$?" != "0" ]]; then
873     die "${FUNCNAME}(): Generation of '$1' failed"
874     fi
875     fi
876     cat << EOF >> "${file}"
877    
878     target_executable = "%s-%s" % (sys.argv[0], PYTHON_ABI)
879     if not os.path.exists(target_executable):
880     sys.stderr.write("'%s' does not exist\n" % target_executable)
881     sys.exit(1)
882    
883     os.execv(target_executable, sys.argv)
884     EOF
885     if [[ "$?" != "0" ]]; then
886     die "${FUNCNAME}(): Generation of '$1' failed"
887     fi
888     fperms +x "${file#${D%/}}" || die "fperms '${file}' failed"
889     done
890     }
891    
892 betelgeuse 1.55 # @ECLASS-VARIABLE: PYTHON_USE_WITH
893     # @DESCRIPTION:
894     # Set this to a space separated list of use flags
895     # the python slot in use must be built with.
896    
897     # @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
898     # @DESCRIPTION:
899     # Set this to a space separated list of use flags
900     # of which one must be turned on for the slot of
901     # in use.
902    
903     # @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
904     # @DESCRIPTION:
905     # Set this if you need to make either PYTHON_USE_WITH or
906     # PYTHON_USE_WITH_OR atoms conditional under a use flag.
907    
908     # @FUNCTION: python_pkg_setup
909     # @DESCRIPTION:
910     # Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
911     # are respected. Only exported if one of those variables is set.
912 arfrever 1.67 if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
913 arfrever 1.74 python_pkg_setup() {
914     python_pkg_setup_fail() {
915     eerror "${1}"
916     die "${1}"
917     }
918 betelgeuse 1.55
919     [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
920    
921 arfrever 1.74 python_pkg_setup_check_USE_flags() {
922     local pyatom use
923     if [[ -n "${PYTHON_ABI}" ]]; then
924     pyatom="dev-lang/python:${PYTHON_ABI}"
925     else
926     python_version
927     pyatom="dev-lang/python:${PYVER}"
928     fi
929    
930     for use in ${PYTHON_USE_WITH}; do
931     if ! has_version "${pyatom}[${use}]"; then
932     python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
933     fi
934     done
935 betelgeuse 1.55
936 arfrever 1.74 for use in ${PYTHON_USE_WITH_OR}; do
937     if has_version "${pyatom}[${use}]"; then
938     return
939     fi
940     done
941 betelgeuse 1.55
942 arfrever 1.74 if [[ ${PYTHON_USE_WITH_OR} ]]; then
943     python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
944 betelgeuse 1.55 fi
945 arfrever 1.74 }
946 betelgeuse 1.55
947 arfrever 1.74 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
948     python_execute_function -q python_pkg_setup_check_USE_flags
949     else
950     python_pkg_setup_check_USE_flags
951 betelgeuse 1.55 fi
952     }
953    
954     EXPORT_FUNCTIONS pkg_setup
955    
956 arfrever 1.74 if [[ -n "${PYTHON_USE_WITH}" ]]; then
957 betelgeuse 1.55 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
958 arfrever 1.74 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
959 betelgeuse 1.55 PYTHON_USE_WITH_ATOM="|| ( "
960     for use in ${PYTHON_USE_WITH_OR}; do
961 arfrever 1.74 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
962 betelgeuse 1.55 done
963 arfrever 1.74 unset use
964     PYTHON_USE_WITH_ATOM+=" )"
965 betelgeuse 1.55 fi
966 arfrever 1.74 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
967 betelgeuse 1.55 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
968     fi
969 arfrever 1.74 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
970     RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
971 betelgeuse 1.55 fi
972    
973 arfrever 1.67 # @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
974     # @DESCRIPTION:
975     # Set this to define default functions for the following ebuild phases:
976     # src_prepare, src_configure, src_compile, src_test, src_install.
977     if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
978     python_src_prepare() {
979     python_copy_sources
980     }
981    
982     for python_default_function in src_configure src_compile src_test src_install; do
983     eval "python_${python_default_function}() { python_execute_function -d -s; }"
984     done
985     unset python_default_function
986    
987     EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
988     fi
989    
990 hawking 1.46 # @FUNCTION: python_disable_pyc
991     # @DESCRIPTION:
992 arfrever 1.66 # Tell Python not to automatically recompile modules to .pyc/.pyo
993 hawking 1.46 # even if the timestamps/version stamps don't match. This is done
994     # to protect sandbox.
995     python_disable_pyc() {
996 arfrever 1.66 export PYTHONDONTWRITEBYTECODE="1"
997 hawking 1.46 }
998    
999     # @FUNCTION: python_enable_pyc
1000     # @DESCRIPTION:
1001 arfrever 1.66 # Tell Python to automatically recompile modules to .pyc/.pyo if the
1002     # timestamps/version stamps have changed.
1003 hawking 1.46 python_enable_pyc() {
1004 hawking 1.53 unset PYTHONDONTWRITEBYTECODE
1005 hawking 1.46 }
1006    
1007 hawking 1.52 # @FUNCTION: python_need_rebuild
1008     # @DESCRIPTION: Run without arguments, specifies that the package should be
1009     # rebuilt after a python upgrade.
1010     python_need_rebuild() {
1011     python_version
1012     export PYTHON_NEED_REBUILD=${PYVER}
1013     }
1014    
1015 arfrever 1.56 # @FUNCTION: python_get_includedir
1016     # @DESCRIPTION:
1017     # Run without arguments, returns the Python include directory.
1018     python_get_includedir() {
1019     if [[ -n "${PYTHON_ABI}" ]]; then
1020     echo "/usr/include/python${PYTHON_ABI}"
1021     else
1022     python_version
1023     echo "/usr/include/python${PYVER}"
1024     fi
1025     }
1026    
1027 pythonhead 1.43 # @FUNCTION: python_get_libdir
1028     # @DESCRIPTION:
1029 arfrever 1.56 # Run without arguments, returns the Python library directory.
1030 pythonhead 1.43 python_get_libdir() {
1031 arfrever 1.56 if [[ -n "${PYTHON_ABI}" ]]; then
1032     echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
1033     else
1034     python_version
1035     echo "/usr/$(get_libdir)/python${PYVER}"
1036     fi
1037 pythonhead 1.42 }
1038    
1039 pythonhead 1.43 # @FUNCTION: python_get_sitedir
1040     # @DESCRIPTION:
1041 arfrever 1.56 # Run without arguments, returns the Python site-packages directory.
1042 pythonhead 1.43 python_get_sitedir() {
1043     echo "$(python_get_libdir)/site-packages"
1044 pythonhead 1.42 }
1045    
1046 dev-zero 1.33 # @FUNCTION: python_tkinter_exists
1047     # @DESCRIPTION:
1048     # Run without arguments, checks if python was compiled with Tkinter
1049     # support. If not, prints an error message and dies.
1050 liquidx 1.1 python_tkinter_exists() {
1051     if ! python -c "import Tkinter" >/dev/null 2>&1; then
1052     eerror "You need to recompile python with Tkinter support."
1053 kloeri 1.30 eerror "Try adding: 'dev-lang/python tk'"
1054 liquidx 1.27 eerror "in to /etc/portage/package.use"
1055 liquidx 1.1 echo
1056     die "missing tkinter support with installed python"
1057     fi
1058     }
1059    
1060 dev-zero 1.33 # @FUNCTION: python_mod_exists
1061 arfrever 1.56 # @USAGE: <module>
1062 dev-zero 1.33 # @DESCRIPTION:
1063     # Run with the module name as an argument. it will check if a
1064     # python module is installed and loadable. it will return
1065     # TRUE(0) if the module exists, and FALSE(1) if the module does
1066     # not exist.
1067 liquidx 1.1 #
1068 dev-zero 1.33 # Example:
1069 liquidx 1.1 # if python_mod_exists gtk; then
1070 liquidx 1.28 # echo "gtk support enabled"
1071 liquidx 1.1 # fi
1072     python_mod_exists() {
1073 zmedico 1.54 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
1074 arfrever 1.56 python -c "import $1" &>/dev/null
1075 liquidx 1.1 }
1076    
1077 dev-zero 1.33 # @FUNCTION: python_mod_compile
1078 arfrever 1.56 # @USAGE: <file> [more files ...]
1079 dev-zero 1.33 # @DESCRIPTION:
1080 hawking 1.35 # Given filenames, it will pre-compile the module's .pyc and .pyo.
1081 hawking 1.37 # This function should only be run in pkg_postinst()
1082 liquidx 1.1 #
1083 dev-zero 1.33 # Example:
1084 hawking 1.35 # python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
1085 liquidx 1.1 #
1086     python_mod_compile() {
1087 arfrever 1.56 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1088     die "${FUNCNAME}() cannot be used in this EAPI"
1089     fi
1090    
1091 hawking 1.48 local f myroot myfiles=()
1092 hawking 1.37
1093     # Check if phase is pkg_postinst()
1094     [[ ${EBUILD_PHASE} != postinst ]] &&\
1095 hawking 1.39 die "${FUNCNAME} should only be run in pkg_postinst()"
1096 hawking 1.37
1097 arfrever 1.82 if [[ -n "${PYTHON_ABI}" ]]; then
1098     PYVER="${PYTHON_ABI}"
1099 pythonhead 1.7 else
1100 liquidx 1.5 python_version
1101     fi
1102 pythonhead 1.7
1103 hawking 1.35 # strip trailing slash
1104     myroot="${ROOT%/}"
1105    
1106     # respect ROOT
1107 hawking 1.51 for f in "$@"; do
1108 hawking 1.48 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
1109 hawking 1.35 done
1110    
1111 hawking 1.48 if ((${#myfiles[@]})); then
1112     python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
1113 arfrever 1.63 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" &> /dev/null
1114 liquidx 1.1 else
1115 hawking 1.35 ewarn "No files to compile!"
1116 pythonhead 1.7 fi
1117 liquidx 1.1 }
1118    
1119 dev-zero 1.33 # @FUNCTION: python_mod_optimize
1120 arfrever 1.56 # @USAGE: [options] [directory|file]
1121 dev-zero 1.33 # @DESCRIPTION:
1122 arfrever 1.56 # If no arguments supplied, it will recompile not recursively all modules
1123     # under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages).
1124 liquidx 1.1 #
1125 dev-zero 1.33 # If supplied with arguments, it will recompile all modules recursively
1126 arfrever 1.56 # in the supplied directory.
1127     # This function should only be run in pkg_postinst().
1128 dev-zero 1.33 #
1129 arfrever 1.56 # Options passed to this function are passed to compileall.py.
1130 hawking 1.39 #
1131 dev-zero 1.33 # Example:
1132 arfrever 1.56 # python_mod_optimize ctypesgencore
1133 liquidx 1.1 python_mod_optimize() {
1134 arfrever 1.56 # Check if phase is pkg_postinst().
1135     [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()"
1136    
1137     if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1138     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=()
1139 hawking 1.37
1140 arfrever 1.56 # Strip trailing slash from ROOT.
1141     root="${ROOT%/}"
1142    
1143     # Respect ROOT and options passed to compileall.py.
1144     while (($#)); do
1145     case "$1" in
1146     -l|-f|-q)
1147     options+=("$1")
1148     ;;
1149     -d|-x)
1150     options+=("$1" "$2")
1151     shift
1152     ;;
1153     -*)
1154     ewarn "${FUNCNAME}: Ignoring compile option $1"
1155     ;;
1156     *)
1157     if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1158     die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories"
1159     elif [[ "$1" =~ ^/ ]]; then
1160     if [[ -d "${root}/$1" ]]; then
1161     other_dirs+=("${root}/$1")
1162     elif [[ -f "${root}/$1" ]]; then
1163     other_files+=("${root}/$1")
1164     elif [[ -e "${root}/$1" ]]; then
1165     ewarn "'${root}/$1' is not a file or a directory!"
1166     else
1167     ewarn "'${root}/$1' doesn't exist!"
1168     fi
1169     else
1170     for PYTHON_ABI in ${PYTHON_ABIS}; do
1171 arfrever 1.62 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
1172 arfrever 1.56 site_packages_dirs+=("$1")
1173     break
1174 arfrever 1.62 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
1175 arfrever 1.56 site_packages_files+=("$1")
1176     break
1177 arfrever 1.62 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
1178 arfrever 1.56 ewarn "'$1' is not a file or a directory!"
1179     else
1180     ewarn "'$1' doesn't exist!"
1181     fi
1182     done
1183     fi
1184     ;;
1185     esac
1186     shift
1187     done
1188 hawking 1.37
1189 arfrever 1.56 # Set additional options.
1190     options+=("-q")
1191 liquidx 1.13
1192 arfrever 1.56 for PYTHON_ABI in ${PYTHON_ABIS}; do
1193     if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
1194     return_code="0"
1195     ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
1196     if ((${#site_packages_dirs[@]})); then
1197     for dir in "${site_packages_dirs[@]}"; do
1198 arfrever 1.62 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
1199 arfrever 1.56 done
1200 arfrever 1.62 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
1201 arfrever 1.63 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
1202 arfrever 1.56 fi
1203     if ((${#site_packages_files[@]})); then
1204     for file in "${site_packages_files[@]}"; do
1205 arfrever 1.62 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
1206 arfrever 1.56 done
1207 arfrever 1.62 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
1208 arfrever 1.63 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
1209 hawking 1.49 fi
1210 arfrever 1.56 eend "${return_code}"
1211     fi
1212     unset site_packages_absolute_dirs site_packages_absolute_files
1213     done
1214    
1215     # Don't use PYTHON_ABI in next calls to python_get_libdir().
1216     unset PYTHON_ABI
1217 hawking 1.36
1218 arfrever 1.56 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1219     return_code="0"
1220 arfrever 1.78 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}"
1221 arfrever 1.56 if ((${#other_dirs[@]})); then
1222 arfrever 1.62 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
1223 arfrever 1.63 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
1224 arfrever 1.56 fi
1225     if ((${#other_files[@]})); then
1226 arfrever 1.62 python${PYVER} "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
1227 arfrever 1.63 python${PYVER} -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
1228 arfrever 1.56 fi
1229     eend "${return_code}"
1230     fi
1231 pythonhead 1.7 else
1232 arfrever 1.63 local myroot mydirs=() myfiles=() myopts=() return_code="0"
1233 arfrever 1.56
1234     # strip trailing slash
1235     myroot="${ROOT%/}"
1236    
1237     # respect ROOT and options passed to compileall.py
1238     while (($#)); do
1239     case "$1" in
1240     -l|-f|-q)
1241     myopts+=("$1")
1242     ;;
1243     -d|-x)
1244     myopts+=("$1" "$2")
1245     shift
1246     ;;
1247     -*)
1248     ewarn "${FUNCNAME}: Ignoring compile option $1"
1249     ;;
1250     *)
1251     if [[ -d "${myroot}"/$1 ]]; then
1252     mydirs+=("${myroot}/$1")
1253     elif [[ -f "${myroot}"/$1 ]]; then
1254     # Files are passed to python_mod_compile which is ROOT-aware
1255     myfiles+=("$1")
1256     elif [[ -e "${myroot}/$1" ]]; then
1257     ewarn "${myroot}/$1 is not a file or directory!"
1258     else
1259     ewarn "${myroot}/$1 doesn't exist!"
1260     fi
1261     ;;
1262     esac
1263     shift
1264     done
1265    
1266 arfrever 1.82 if [[ -n "${PYTHON_ABI}" ]]; then
1267     PYVER="${PYTHON_ABI}"
1268 arfrever 1.56 else
1269     python_version
1270     fi
1271    
1272     # set additional opts
1273     myopts+=(-q)
1274 pythonhead 1.7
1275 arfrever 1.78 ebegin "Compilation and optimization of Python modules for Python ${PYVER}"
1276 arfrever 1.56 if ((${#mydirs[@]})); then
1277     python${PYVER} \
1278     "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
1279 arfrever 1.63 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
1280 arfrever 1.56 python${PYVER} -O \
1281     "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
1282 arfrever 1.63 "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
1283 arfrever 1.56 fi
1284 pythonhead 1.7
1285 arfrever 1.56 if ((${#myfiles[@]})); then
1286     python_mod_compile "${myfiles[@]}"
1287     fi
1288 hawking 1.36
1289 arfrever 1.63 eend "${return_code}"
1290 hawking 1.36 fi
1291 liquidx 1.1 }
1292    
1293 dev-zero 1.33 # @FUNCTION: python_mod_cleanup
1294 arfrever 1.79 # @USAGE: [directory|file]
1295 dev-zero 1.33 # @DESCRIPTION:
1296 arfrever 1.79 # Run with optional arguments, where arguments are Python modules. If none given,
1297     # it will look in /usr/lib/python[0-9].[0-9].
1298 dev-zero 1.33 #
1299 arfrever 1.56 # It will recursively scan all compiled Python modules in the directories and
1300     # determine if they are orphaned (i.e. their corresponding .py files are missing.)
1301     # If they are, then it will remove their corresponding .pyc and .pyo files.
1302 hawking 1.37 #
1303 arfrever 1.56 # This function should only be run in pkg_postrm().
1304 liquidx 1.1 python_mod_cleanup() {
1305 arfrever 1.78 local path py_file PYTHON_ABI SEARCH_PATH=() root
1306 liquidx 1.16
1307 arfrever 1.56 # Check if phase is pkg_postrm().
1308     [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
1309 hawking 1.37
1310 arfrever 1.56 # Strip trailing slash from ROOT.
1311     root="${ROOT%/}"
1312 liquidx 1.1
1313 hawking 1.50 if (($#)); then
1314 arfrever 1.56 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1315     while (($#)); do
1316     if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1317     die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories"
1318     elif [[ "$1" =~ ^/ ]]; then
1319     SEARCH_PATH+=("${root}/${1#/}")
1320     else
1321     for PYTHON_ABI in ${PYTHON_ABIS}; do
1322 arfrever 1.62 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
1323 arfrever 1.56 done
1324     fi
1325     shift
1326     done
1327     else
1328     SEARCH_PATH=("${@#/}")
1329     SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
1330     fi
1331 liquidx 1.5 else
1332 arfrever 1.78 local dir sitedir
1333     for dir in "${root}"/usr/lib*; do
1334     if [[ -d "${dir}" && ! -L "${dir}" ]]; then
1335     for sitedir in "${dir}"/python*/site-packages; do
1336     if [[ -d "${sitedir}" ]]; then
1337     SEARCH_PATH+=("${sitedir}")
1338     fi
1339     done
1340     fi
1341     done
1342     fi
1343    
1344     local BLUE CYAN NORMAL
1345     if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1346 arfrever 1.80 BLUE=$'\e[1;34m'
1347     CYAN=$'\e[1;36m'
1348 arfrever 1.78 NORMAL=$'\e[0m'
1349     else
1350     BLUE=
1351     CYAN=
1352     NORMAL=
1353 liquidx 1.5 fi
1354 pythonhead 1.7
1355 hawking 1.50 for path in "${SEARCH_PATH[@]}"; do
1356 arfrever 1.78 if [[ -d "${path}" ]]; then
1357     find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
1358     py_file="${REPLY%[co]}"
1359     [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
1360     einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
1361     rm -f "${py_file}"[co]
1362     done
1363 hawking 1.50
1364 arfrever 1.78 # Attempt to delete directories, which may be empty.
1365     find "${path}" -type d | sort -r | while read -r dir; do
1366     rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
1367     done
1368     elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then
1369     einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1370     rm -f "${path}"[co]
1371     fi
1372 pythonhead 1.7 done
1373 liquidx 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.20