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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.85 - (hide annotations) (download)
Thu Jan 14 19:23:02 2010 UTC (4 years, 11 months ago) by arfrever
Branch: MAIN
Changes since 1.84: +460 -436 lines
Define functions in more logical order.

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

  ViewVC Help
Powered by ViewVC 1.1.20