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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.87 - (hide annotations) (download)
Tue Feb 2 18:55:00 2010 UTC (4 years, 6 months ago) by arfrever
Branch: MAIN
Changes since 1.86: +546 -92 lines
Support EAPI="3".
Define _PYTHON2_SUPPORTED_VERSIONS and _PYTHON3_SUPPORTED_VERSIONS to avoid code duplication.
Support PYTHON_DEPEND.
Disallow using of NEED_PYTHON in EAPI >=3.
Use prefix variables (bug #302525).
Fix handling of -- option in some functions.
Ensure that python_execute_function() isn't called by a function, which is called by python_execute_function().
Set PYTHON_REQUESTED_ACTIVE_VERSION in python_set_active_version().
Add python_get_library() and python_get_version().
Use PYTHON() in python_mod_exists() and python_tkinter_exists().
Support Python 3 in python_tkinter_exists().
Support PYTHON_TEST_VERBOSITY.
Add python_execute_nosetests(), python_execute_py.test() and python_execute_trial().
Disallow using of python_version() in EAPI >=3.

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.87 # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.86 2010/01/15 14:46:20 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.87 if ! has "${EAPI:-0}" 0 1 2 3; then
15 arfrever 1.83 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
16     fi
17    
18 arfrever 1.87 _PYTHON2_SUPPORTED_VERSIONS=(2.4 2.5 2.6 2.7)
19     _PYTHON3_SUPPORTED_VERSIONS=(3.0 3.1 3.2)
20    
21     # @ECLASS-VARIABLE: PYTHON_DEPEND
22     # @DESCRIPTION:
23     # Specification of dependency on dev-lang/python.
24     # Syntax:
25     # PYTHON_DEPEND: [[!]USE_flag? ]<version_components_group>[ version_components_group]
26     # version_components_group: <major_version[:[minimal_version][:maximal_version]]>
27     # major_version: <2|3|*>
28     # minimal_version: <minimal_major_version.minimal_minor_version>
29     # maximal_version: <maximal_major_version.maximal_minor_version>
30    
31     _parse_PYTHON_DEPEND() {
32     local accepted_version accepted_versions=() major_version maximal_version minimal_version python_all="0" python_atoms= python_maximal_version python_minimal_version python_versions=() python2="0" python2_maximal_version python2_minimal_version python3="0" python3_maximal_version python3_minimal_version USE_flag= version_components_group version_components_group_regex version_components_groups
33    
34     version_components_group_regex="(2|3|\*)(:([[:digit:]]+\.[[:digit:]]+)?(:([[:digit:]]+\.[[:digit:]]+)?)?)?"
35     version_components_groups="${PYTHON_DEPEND}"
36    
37     if [[ "${version_components_groups}" =~ ^((\!)?[[:alnum:]_-]+\?\ )?${version_components_group_regex}(\ ${version_components_group_regex})?$ ]]; then
38     if [[ "${version_components_groups}" =~ ^(\!)?[[:alnum:]_-]+\? ]]; then
39     USE_flag="${version_components_groups%\? *}"
40     version_components_groups="${version_components_groups#* }"
41     fi
42     if [[ "${version_components_groups}" =~ ("*".*" "|" *"|^2.*\ (2|\*)|^3.*\ (3|\*)) ]]; then
43     die "Invalid syntax of PYTHON_DEPEND: Incorrectly specified groups of versions"
44     fi
45    
46     version_components_groups="${version_components_groups// /$'\n'}"
47     while read version_components_group; do
48     major_version="${version_components_group:0:1}"
49     minimal_version="${version_components_group:2}"
50     minimal_version="${minimal_version%:*}"
51     maximal_version="${version_components_group:$((3 + ${#minimal_version}))}"
52    
53     if [[ "${major_version}" =~ ^(2|3)$ ]]; then
54     if [[ -n "${minimal_version}" && "${major_version}" != "${minimal_version:0:1}" ]]; then
55     die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' not in specified group of versions"
56     fi
57     if [[ -n "${maximal_version}" && "${major_version}" != "${maximal_version:0:1}" ]]; then
58     die "Invalid syntax of PYTHON_DEPEND: Maximal version '${maximal_version}' not in specified group of versions"
59     fi
60     fi
61    
62     if [[ "${major_version}" == "2" ]]; then
63     python2="1"
64     python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}")
65     python2_minimal_version="${minimal_version}"
66     python2_maximal_version="${maximal_version}"
67     elif [[ "${major_version}" == "3" ]]; then
68     python3="1"
69     python_versions=("${_PYTHON3_SUPPORTED_VERSIONS[@]}")
70     python3_minimal_version="${minimal_version}"
71     python3_maximal_version="${maximal_version}"
72     else
73     python_all="1"
74     python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}")
75     python_minimal_version="${minimal_version}"
76     python_maximal_version="${maximal_version}"
77     fi
78    
79     if [[ -n "${minimal_version}" ]] && ! has "${minimal_version}" "${python_versions[@]}"; then
80     die "Invalid syntax of PYTHON_DEPEND: Unrecognized minimal version '${minimal_version}'"
81     fi
82     if [[ -n "${maximal_version}" ]] && ! has "${maximal_version}" "${python_versions[@]}"; then
83     die "Invalid syntax of PYTHON_DEPEND: Unrecognized maximal version '${maximal_version}'"
84     fi
85    
86     if [[ -n "${minimal_version}" && -n "${maximal_version}" && "${minimal_version}" > "${maximal_version}" ]]; then
87     die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' greater than maximal version '${maximal_version}'"
88     fi
89     done <<< "${version_components_groups}"
90    
91     _create_accepted_versions_range() {
92     local accepted_version="0" i
93     for ((i = "${#python_versions[@]}"; i >= 0; i--)); do
94     if [[ "${python_versions[${i}]}" == "${python_maximal_version}" ]]; then
95     accepted_version="1"
96     fi
97     if [[ "${accepted_version}" == "1" ]]; then
98     accepted_versions+=("${python_versions[${i}]}")
99     fi
100     if [[ "${python_versions[${i}]}" == "${python_minimal_version}" ]]; then
101     accepted_version="0"
102     fi
103     done
104     }
105    
106     if [[ "${python_all}" == "1" ]]; then
107     python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}")
108     python_minimal_version="${python_minimal_version:-${python_versions[0]}}"
109     python_maximal_version="${python_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
110     _create_accepted_versions_range
111     else
112     if [[ "${python3}" == "1" ]]; then
113     python_versions=("${_PYTHON3_SUPPORTED_VERSIONS[@]}")
114     python_minimal_version="${python3_minimal_version:-${python_versions[0]}}"
115     python_maximal_version="${python3_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
116     _create_accepted_versions_range
117     fi
118     if [[ "${python2}" == "1" ]]; then
119     python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}")
120     python_minimal_version="${python2_minimal_version:-${python_versions[0]}}"
121     python_maximal_version="${python2_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
122     _create_accepted_versions_range
123     fi
124     fi
125    
126     for accepted_version in "${accepted_versions[@]}"; do
127     python_atoms+="${python_atoms:+ }=dev-lang/python-${accepted_version}*"
128     done
129    
130     _PYTHON_ATOMS="${python_atoms// /$'\n'}"
131     DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${python_atoms} )${USE_flag:+ )}"
132     RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${python_atoms} )${USE_flag:+ )}"
133     else
134     die "Invalid syntax of PYTHON_DEPEND"
135     fi
136     }
137    
138     DEPEND=">=app-admin/eselect-python-20090804"
139     RDEPEND="${DEPEND}"
140    
141     if [[ -n "${PYTHON_DEPEND}" && -n "${NEED_PYTHON}" ]]; then
142     die "PYTHON_DEPEND and NEED_PYTHON cannot be set simultaneously"
143     elif [[ -n "${PYTHON_DEPEND}" ]]; then
144     _parse_PYTHON_DEPEND
145     elif [[ -n "${NEED_PYTHON}" ]]; then
146     if ! has "${EAPI:-0}" 0 1 2; then
147     eerror "Use PYTHON_DEPEND instead of NEED_PYTHON."
148     die "NEED_PYTHON cannot be used in this EAPI"
149     fi
150     _PYTHON_ATOMS=">=dev-lang/python-${NEED_PYTHON}"
151     DEPEND+="${DEPEND:+ }${_PYTHON_ATOMS}"
152     RDEPEND+="${RDEPEND:+ }${_PYTHON_ATOMS}"
153 betelgeuse 1.55 else
154 arfrever 1.87 _PYTHON_ATOMS="dev-lang/python"
155 carlo 1.22 fi
156 liquidx 1.18
157 arfrever 1.85 # @ECLASS-VARIABLE: PYTHON_USE_WITH
158     # @DESCRIPTION:
159 arfrever 1.87 # Set this to a space separated list of USE flags the Python slot in use must be built with.
160 liquidx 1.18
161 arfrever 1.85 # @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
162 dev-zero 1.33 # @DESCRIPTION:
163 arfrever 1.87 # Set this to a space separated list of USE flags of which one must be turned on for the slot in use.
164 liquidx 1.18
165 arfrever 1.85 # @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
166     # @DESCRIPTION:
167 arfrever 1.87 # Set this to a name of a USE flag if you need to make either PYTHON_USE_WITH or
168     # PYTHON_USE_WITH_OR atoms conditional under a USE flag.
169 liquidx 1.1
170 arfrever 1.85 # @FUNCTION: python_pkg_setup
171 arfrever 1.56 # @DESCRIPTION:
172 arfrever 1.85 # Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
173     # are respected. Only exported if one of those variables is set.
174     if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
175     python_pkg_setup() {
176     python_pkg_setup_fail() {
177     eerror "${1}"
178     die "${1}"
179     }
180    
181     [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
182 arfrever 1.56
183 arfrever 1.85 python_pkg_setup_check_USE_flags() {
184     local pyatom use
185     if [[ -n "${PYTHON_ABI}" ]]; then
186     pyatom="dev-lang/python:${PYTHON_ABI}"
187     else
188     pyatom="dev-lang/python:$(PYTHON -A --ABI)"
189     fi
190 arfrever 1.56
191 arfrever 1.85 for use in ${PYTHON_USE_WITH}; do
192     if ! has_version "${pyatom}[${use}]"; then
193     python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
194     fi
195     done
196 arfrever 1.82
197 arfrever 1.85 for use in ${PYTHON_USE_WITH_OR}; do
198     if has_version "${pyatom}[${use}]"; then
199     return
200     fi
201     done
202 arfrever 1.82
203 arfrever 1.85 if [[ ${PYTHON_USE_WITH_OR} ]]; then
204     python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
205 arfrever 1.83 fi
206 arfrever 1.85 }
207    
208     if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
209     python_execute_function -q python_pkg_setup_check_USE_flags
210 arfrever 1.56 else
211 arfrever 1.85 python_pkg_setup_check_USE_flags
212 arfrever 1.56 fi
213 arfrever 1.85 }
214    
215     EXPORT_FUNCTIONS pkg_setup
216 arfrever 1.56
217 arfrever 1.87 _PYTHON_USE_WITH_ATOM=""
218 arfrever 1.85 if [[ -n "${PYTHON_USE_WITH}" ]]; then
219 arfrever 1.87 while read _PYTHON_ATOM; do
220     _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
221     done <<< "${_PYTHON_ATOMS}"
222     _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
223 arfrever 1.85 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
224 arfrever 1.87 for _USE_flag in ${PYTHON_USE_WITH_OR}; do
225     while read _PYTHON_ATOM; do
226     _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${_USE_flag}]"
227     done <<< "${_PYTHON_ATOMS}"
228 arfrever 1.85 done
229 arfrever 1.87 unset _USE_flag
230     _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
231 arfrever 1.56 fi
232 arfrever 1.85 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
233 arfrever 1.87 _PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOM} )"
234 arfrever 1.78 fi
235 arfrever 1.87 DEPEND+=" ${_PYTHON_USE_WITH_ATOM}"
236     RDEPEND+=" ${_PYTHON_USE_WITH_ATOM}"
237     unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOM
238 arfrever 1.85 fi
239 arfrever 1.56
240 arfrever 1.87 unset _PYTHON_ATOMS
241    
242 arfrever 1.85 # ================================================================================================
243     # ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ========
244     # ================================================================================================
245 arfrever 1.84
246 arfrever 1.86 # @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
247     # @DESCRIPTION:
248     # Set this in EAPI <= 4 to indicate that given package supports installation for
249     # multiple versions of Python.
250    
251 arfrever 1.85 # @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
252 arfrever 1.82 # @DESCRIPTION:
253 arfrever 1.85 # Set this to define default functions for the following ebuild phases:
254     # src_prepare, src_configure, src_compile, src_test, src_install.
255     if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
256     python_src_prepare() {
257     python_copy_sources
258     }
259 arfrever 1.82
260 arfrever 1.85 for python_default_function in src_configure src_compile src_test src_install; do
261     eval "python_${python_default_function}() { python_execute_function -d -s; }"
262     done
263     unset python_default_function
264 arfrever 1.82
265 arfrever 1.85 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
266     fi
267 arfrever 1.82
268 arfrever 1.77 unset PYTHON_ABIS
269     unset PYTHON_ABIS_SANITY_CHECKS
270    
271 arfrever 1.56 # @FUNCTION: validate_PYTHON_ABIS
272     # @DESCRIPTION:
273 arfrever 1.76 # Ensure that PYTHON_ABIS variable has valid value.
274 arfrever 1.86 # This function usually should not be directly called in ebuilds.
275 arfrever 1.56 validate_PYTHON_ABIS() {
276 arfrever 1.86 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable.
277     if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
278 arfrever 1.66 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
279     fi
280    
281 arfrever 1.59 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
282 arfrever 1.87 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
283     eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
284 arfrever 1.83 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
285 arfrever 1.87 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
286 arfrever 1.57 fi
287 arfrever 1.87 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
288     eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
289 arfrever 1.83 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
290 arfrever 1.87 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
291 arfrever 1.57 fi
292    
293 arfrever 1.82 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
294     if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
295 arfrever 1.87 local PYTHON_ABI restricted_ABI support_ABI supported_PYTHON_ABIS=
296     PYTHON_ABI_SUPPORTED_VALUES="${_PYTHON2_SUPPORTED_VERSIONS[@]} ${_PYTHON3_SUPPORTED_VERSIONS[@]}"
297 arfrever 1.68
298 arfrever 1.70 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
299 arfrever 1.75 local python2_enabled="0" python3_enabled="0"
300    
301 arfrever 1.68 if [[ -z "${USE_PYTHON}" ]]; then
302     die "USE_PYTHON variable is empty"
303 arfrever 1.56 fi
304 arfrever 1.68
305 arfrever 1.78 for PYTHON_ABI in ${USE_PYTHON}; do
306     if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
307     die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
308 arfrever 1.68 fi
309 arfrever 1.75
310 arfrever 1.87 if has "${PYTHON_ABI}" "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; then
311 arfrever 1.75 python2_enabled="1"
312     fi
313 arfrever 1.87 if has "${PYTHON_ABI}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; then
314 arfrever 1.75 python3_enabled="1"
315     fi
316    
317 arfrever 1.68 support_ABI="1"
318 arfrever 1.66 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
319 arfrever 1.78 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
320 arfrever 1.66 support_ABI="0"
321     break
322     fi
323     done
324 arfrever 1.82 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
325 arfrever 1.68 done
326    
327     if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
328 arfrever 1.84 die "USE_PYTHON variable does not enable any version of Python supported by ${CATEGORY}/${PF}"
329 arfrever 1.66 fi
330 arfrever 1.75
331     if [[ "${python2_enabled}" == "0" ]]; then
332 arfrever 1.84 ewarn "USE_PYTHON variable does not enable any version of Python 2. This configuration is unsupported."
333 arfrever 1.75 fi
334     if [[ "${python3_enabled}" == "0" ]]; then
335 arfrever 1.84 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported."
336 arfrever 1.75 fi
337 arfrever 1.68 else
338 arfrever 1.76 local python_version python2_version= python3_version= support_python_major_version
339    
340 arfrever 1.87 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
341 arfrever 1.74
342     if has_version "=dev-lang/python-2*"; then
343 arfrever 1.87 if [[ "$(readlink "${EPREFIX}/usr/bin/python2")" != "python2."* ]]; then
344     die "'${EPREFIX}/usr/bin/python2' is not valid symlink"
345 arfrever 1.74 fi
346    
347 arfrever 1.87 python2_version="$("${EPREFIX}/usr/bin/python2" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
348 arfrever 1.74
349 arfrever 1.87 for PYTHON_ABI in "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; do
350 arfrever 1.74 support_python_major_version="1"
351     for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
352 arfrever 1.78 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
353 arfrever 1.74 support_python_major_version="0"
354     fi
355     done
356     [[ "${support_python_major_version}" == "1" ]] && break
357     done
358     if [[ "${support_python_major_version}" == "1" ]]; then
359     for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
360 arfrever 1.77 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
361 arfrever 1.84 die "Active version of Python 2 is not supported by ${CATEGORY}/${PF}"
362 arfrever 1.74 fi
363     done
364     else
365     python2_version=""
366     fi
367     fi
368 arfrever 1.56
369 arfrever 1.74 if has_version "=dev-lang/python-3*"; then
370 arfrever 1.87 if [[ "$(readlink "${EPREFIX}/usr/bin/python3")" != "python3."* ]]; then
371     die "'${EPREFIX}/usr/bin/python3' is not valid symlink"
372 arfrever 1.68 fi
373 arfrever 1.74
374 arfrever 1.87 python3_version="$("${EPREFIX}/usr/bin/python3" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
375 arfrever 1.74
376 arfrever 1.87 for PYTHON_ABI in "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; do
377 arfrever 1.74 support_python_major_version="1"
378     for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
379 arfrever 1.78 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
380 arfrever 1.74 support_python_major_version="0"
381     fi
382     done
383     [[ "${support_python_major_version}" == "1" ]] && break
384     done
385     if [[ "${support_python_major_version}" == "1" ]]; then
386     for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
387 arfrever 1.77 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
388 arfrever 1.84 die "Active version of Python 3 is not supported by ${CATEGORY}/${PF}"
389 arfrever 1.74 fi
390     done
391     else
392     python3_version=""
393     fi
394     fi
395    
396 arfrever 1.82 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
397 arfrever 1.87 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python2' symlink"
398 arfrever 1.82 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
399     die "Incorrect configuration of Python"
400     fi
401     if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
402 arfrever 1.87 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python3' symlink"
403 arfrever 1.76 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
404     die "Incorrect configuration of Python"
405     fi
406    
407 arfrever 1.74 PYTHON_ABIS="${python2_version} ${python3_version}"
408     PYTHON_ABIS="${PYTHON_ABIS# }"
409     export PYTHON_ABIS="${PYTHON_ABIS% }"
410 arfrever 1.68 fi
411 arfrever 1.56 fi
412 arfrever 1.66
413 arfrever 1.84 if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
414 arfrever 1.77 local PYTHON_ABI
415     for PYTHON_ABI in ${PYTHON_ABIS}; do
416 arfrever 1.82 # Ensure that appropriate version of Python is installed.
417 arfrever 1.77 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
418 arfrever 1.84 die "dev-lang/python:${PYTHON_ABI} is not installed"
419 arfrever 1.77 fi
420 arfrever 1.71
421 arfrever 1.77 # Ensure that EPYTHON variable is respected.
422     if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
423 arfrever 1.79 eerror "python: '$(type -p python)'"
424     eerror "ABI: '${ABI}'"
425     eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
426     eerror "EPYTHON: '$(PYTHON)'"
427     eerror "PYTHON_ABI: '${PYTHON_ABI}'"
428     eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')'"
429 arfrever 1.84 die "'python' does not respect EPYTHON variable"
430 arfrever 1.77 fi
431     done
432     PYTHON_ABIS_SANITY_CHECKS="1"
433     fi
434 arfrever 1.56 }
435    
436     # @FUNCTION: python_execute_function
437 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]
438 arfrever 1.56 # @DESCRIPTION:
439 arfrever 1.60 # Execute specified function for each value of PYTHON_ABIS, optionally passing additional
440     # arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
441 arfrever 1.56 python_execute_function() {
442 arfrever 1.87 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function i nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
443 arfrever 1.56
444     while (($#)); do
445     case "$1" in
446     --action-message)
447     action_message_template="$2"
448     shift
449     ;;
450 arfrever 1.58 -d|--default-function)
451     default_function="1"
452     ;;
453 arfrever 1.56 --failure-message)
454     failure_message_template="$2"
455     shift
456     ;;
457     --nonfatal)
458     nonfatal="1"
459     ;;
460     -q|--quiet)
461     quiet="1"
462     ;;
463     -s|--separate-build-dirs)
464     separate_build_dirs="1"
465     ;;
466 arfrever 1.81 --source-dir)
467     source_dir="$2"
468     shift
469     ;;
470 arfrever 1.71 --)
471 arfrever 1.87 shift
472 arfrever 1.71 break
473     ;;
474 arfrever 1.56 -*)
475 arfrever 1.67 die "${FUNCNAME}(): Unrecognized option '$1'"
476 arfrever 1.56 ;;
477     *)
478     break
479     ;;
480     esac
481     shift
482     done
483    
484 arfrever 1.81 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
485     die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
486     fi
487    
488 arfrever 1.58 if [[ "${default_function}" == "0" ]]; then
489 arfrever 1.83 if [[ "$#" -eq 0 ]]; then
490 arfrever 1.58 die "${FUNCNAME}(): Missing function name"
491     fi
492     function="$1"
493     shift
494 arfrever 1.67
495     if [[ -z "$(type -t "${function}")" ]]; then
496 arfrever 1.84 die "${FUNCNAME}(): '${function}' function is not defined"
497 arfrever 1.67 fi
498 arfrever 1.58 else
499 arfrever 1.87 if [[ "$#" -ne 0 ]]; then
500 arfrever 1.67 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
501 arfrever 1.58 fi
502     if has "${EAPI:-0}" 0 1; then
503 arfrever 1.67 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
504 arfrever 1.58 fi
505    
506     if [[ "${EBUILD_PHASE}" == "configure" ]]; then
507 arfrever 1.82 if has "${EAPI}" 2 3; then
508 arfrever 1.61 python_default_function() {
509     econf
510     }
511     else
512     python_default_function() {
513     nonfatal econf
514     }
515     fi
516 arfrever 1.58 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
517     python_default_function() {
518     emake
519     }
520     elif [[ "${EBUILD_PHASE}" == "test" ]]; then
521     python_default_function() {
522     if emake -j1 -n check &> /dev/null; then
523     emake -j1 check
524     elif emake -j1 -n test &> /dev/null; then
525     emake -j1 test
526     fi
527     }
528     elif [[ "${EBUILD_PHASE}" == "install" ]]; then
529     python_default_function() {
530     emake DESTDIR="${D}" install
531     }
532     else
533 arfrever 1.82 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
534 arfrever 1.58 fi
535     function="python_default_function"
536 arfrever 1.56 fi
537    
538 arfrever 1.87 for ((i = 1; i < "${#FUNCNAME[@]}"; i++)); do
539     if [[ "${FUNCNAME[${i}]}" == "python_execute_function" ]]; then
540     die "${FUNCNAME}(): Invalid call stack"
541     fi
542     done
543    
544 arfrever 1.56 if [[ "${quiet}" == "0" ]]; then
545     [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
546     [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
547     [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
548     [[ "${EBUILD_PHASE}" == "configure" ]] && action="Configuration"
549     [[ "${EBUILD_PHASE}" == "compile" ]] && action="Building"
550     [[ "${EBUILD_PHASE}" == "test" ]] && action="Testing"
551     [[ "${EBUILD_PHASE}" == "install" ]] && action="Installation"
552     [[ "${EBUILD_PHASE}" == "preinst" ]] && action="Preinstallation"
553     [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
554     [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
555     [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
556     fi
557    
558     local RED GREEN BLUE NORMAL
559     if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
560     RED=$'\e[1;31m'
561     GREEN=$'\e[1;32m'
562     BLUE=$'\e[1;34m'
563     NORMAL=$'\e[0m'
564     else
565     RED=
566     GREEN=
567     BLUE=
568     NORMAL=
569     fi
570    
571     validate_PYTHON_ABIS
572     for PYTHON_ABI in ${PYTHON_ABIS}; do
573     if [[ "${quiet}" == "0" ]]; then
574     if [[ -n "${action_message_template}" ]]; then
575     action_message="$(eval echo -n "${action_message_template}")"
576     else
577     action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..."
578     fi
579     echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
580     fi
581 arfrever 1.66
582 arfrever 1.56 if [[ "${separate_build_dirs}" == "1" ]]; then
583 arfrever 1.81 if [[ -n "${source_dir}" ]]; then
584     export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
585     else
586     export BUILDDIR="${S}-${PYTHON_ABI}"
587     fi
588 arfrever 1.60 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
589     else
590     export BUILDDIR="${S}"
591 arfrever 1.56 fi
592 arfrever 1.66
593 arfrever 1.73 previous_directory="$(pwd)"
594     previous_directory_stack="$(dirs -p)"
595     previous_directory_stack_length="$(dirs -p | wc -l)"
596 arfrever 1.72
597 arfrever 1.82 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
598 arfrever 1.66 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
599     else
600     EPYTHON="$(PYTHON)" "${function}" "$@"
601     fi
602    
603     if [[ "$?" != "0" ]]; then
604 arfrever 1.56 if [[ -n "${failure_message_template}" ]]; then
605     failure_message="$(eval echo -n "${failure_message_template}")"
606     else
607     failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
608     fi
609 arfrever 1.66
610 arfrever 1.69 if [[ "${nonfatal}" == "1" ]]; then
611     if [[ "${quiet}" == "0" ]]; then
612     ewarn "${RED}${failure_message}${NORMAL}"
613     fi
614     elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
615 arfrever 1.70 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
616 arfrever 1.78 local enabled_PYTHON_ABIS= other_PYTHON_ABI
617     for other_PYTHON_ABI in ${PYTHON_ABIS}; do
618 arfrever 1.82 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
619 arfrever 1.70 done
620 arfrever 1.82 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
621 arfrever 1.70 fi
622 arfrever 1.56 if [[ "${quiet}" == "0" ]]; then
623     ewarn "${RED}${failure_message}${NORMAL}"
624     fi
625 arfrever 1.69 if [[ -z "${PYTHON_ABIS}" ]]; then
626     die "${function}() function failed with all enabled versions of Python"
627     fi
628 arfrever 1.56 else
629     die "${failure_message}"
630     fi
631     fi
632 arfrever 1.66
633 arfrever 1.84 # Ensure that directory stack has not been decreased.
634 arfrever 1.73 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
635 arfrever 1.72 die "Directory stack decreased illegally"
636     fi
637    
638 arfrever 1.73 # Avoid side effects of earlier returning from the specified function.
639     while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
640 arfrever 1.72 popd > /dev/null || die "popd failed"
641     done
642    
643 arfrever 1.84 # Ensure that the bottom part of directory stack has not been changed. Restore
644 arfrever 1.73 # previous directory (from before running of the specified function) before
645     # comparison of directory stacks to avoid mismatch of directory stacks after
646     # potential using of 'cd' to change current directory. Restoration of previous
647     # directory allows to safely use 'cd' to change current directory in the
648     # specified function without changing it back to original directory.
649     cd "${previous_directory}"
650     if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
651     die "Directory stack changed illegally"
652     fi
653    
654 arfrever 1.56 if [[ "${separate_build_dirs}" == "1" ]]; then
655     popd > /dev/null || die "popd failed"
656     fi
657 arfrever 1.60 unset BUILDDIR
658 arfrever 1.56 done
659 arfrever 1.58
660     if [[ "${default_function}" == "1" ]]; then
661     unset -f python_default_function
662     fi
663 arfrever 1.56 }
664    
665 arfrever 1.85 # @FUNCTION: python_copy_sources
666     # @USAGE: [--no-link] [--] [directory]
667 arfrever 1.77 # @DESCRIPTION:
668 arfrever 1.87 # Copy unpacked sources of given package to separate build directory for each Python ABI.
669 arfrever 1.85 python_copy_sources() {
670     local dir dirs=() no_link="0" PYTHON_ABI
671 arfrever 1.77
672     while (($#)); do
673     case "$1" in
674 arfrever 1.85 --no-link)
675     no_link="1"
676 arfrever 1.77 ;;
677     --)
678 arfrever 1.87 shift
679 arfrever 1.77 break
680     ;;
681     -*)
682     die "${FUNCNAME}(): Unrecognized option '$1'"
683     ;;
684     *)
685     break
686     ;;
687     esac
688     shift
689     done
690    
691     if [[ "$#" -eq 0 ]]; then
692 arfrever 1.85 if [[ "${WORKDIR}" == "${S}" ]]; then
693     die "${FUNCNAME}() cannot be used"
694     fi
695     dirs="${S}"
696     else
697     dirs="$@"
698 arfrever 1.77 fi
699    
700 arfrever 1.85 validate_PYTHON_ABIS
701     for PYTHON_ABI in ${PYTHON_ABIS}; do
702     for dir in "${dirs[@]}"; do
703     if [[ "${no_link}" == "1" ]]; then
704     cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
705 arfrever 1.77 else
706 arfrever 1.85 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
707 arfrever 1.77 fi
708 arfrever 1.85 done
709 arfrever 1.77 done
710 arfrever 1.85 }
711 arfrever 1.77
712 arfrever 1.85 # @FUNCTION: python_set_build_dir_symlink
713     # @USAGE: [directory="build"]
714     # @DESCRIPTION:
715     # Create build directory symlink.
716     python_set_build_dir_symlink() {
717     local dir="$1"
718 arfrever 1.77
719 arfrever 1.85 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
720     [[ -z "${dir}" ]] && dir="build"
721 arfrever 1.77
722 arfrever 1.85 # Do not delete preexistent directories.
723     rm -f "${dir}" || die "Deletion of '${dir}' failed"
724     ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
725 arfrever 1.77 }
726 arfrever 1.56
727 arfrever 1.82 # @FUNCTION: python_generate_wrapper_scripts
728     # @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
729     # @DESCRIPTION:
730     # Generate wrapper scripts. Existing files are overwritten only with --force option.
731     # If --respect-EPYTHON option is specified, then generated wrapper scripts will
732     # respect EPYTHON variable at run time.
733     python_generate_wrapper_scripts() {
734 arfrever 1.87 _python_initialize_prefix_variables
735    
736     local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python3_enabled="0" respect_EPYTHON="0"
737 arfrever 1.82
738     while (($#)); do
739     case "$1" in
740     -E|--respect-EPYTHON)
741     respect_EPYTHON="1"
742     ;;
743     -f|--force)
744     force="1"
745     ;;
746     -q|--quiet)
747     quiet="1"
748     ;;
749     --)
750 arfrever 1.87 shift
751 arfrever 1.82 break
752     ;;
753     -*)
754     die "${FUNCNAME}(): Unrecognized option '$1'"
755     ;;
756     *)
757     break
758     ;;
759     esac
760     shift
761     done
762    
763     if [[ "$#" -eq 0 ]]; then
764     die "${FUNCNAME}(): Missing arguments"
765     fi
766    
767     validate_PYTHON_ABIS
768 arfrever 1.87 for PYTHON_ABI in "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; do
769 arfrever 1.82 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
770     python2_enabled="1"
771     fi
772     done
773 arfrever 1.87 for PYTHON_ABI in "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; do
774 arfrever 1.82 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
775     python3_enabled="1"
776     fi
777     done
778    
779     if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
780     eselect_python_option=
781     elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
782     eselect_python_option="--python2"
783     elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
784     eselect_python_option="--python3"
785     else
786     die "${FUNCNAME}(): Unsupported environment"
787     fi
788    
789     for file in "$@"; do
790     if [[ -f "${file}" && "${force}" == "0" ]]; then
791     die "${FUNCNAME}(): '$1' already exists"
792     fi
793    
794     if [[ "${quiet}" == "0" ]]; then
795     einfo "Generating '${file#${D%/}}' wrapper script"
796     fi
797    
798     cat << EOF > "${file}"
799     #!/usr/bin/env python
800     # Gentoo '${file##*/}' wrapper script
801    
802     import os
803     import re
804     import subprocess
805     import sys
806    
807     EPYTHON_re = re.compile(r"^python(\d+\.\d+)$")
808    
809     EOF
810     if [[ "$?" != "0" ]]; then
811     die "${FUNCNAME}(): Generation of '$1' failed"
812     fi
813     if [[ "${respect_EPYTHON}" == "1" ]]; then
814     cat << EOF >> "${file}"
815     EPYTHON = os.environ.get("EPYTHON")
816     if EPYTHON:
817     EPYTHON_matched = EPYTHON_re.match(EPYTHON)
818     if EPYTHON_matched:
819     PYTHON_ABI = EPYTHON_matched.group(1)
820     else:
821     sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
822     sys.exit(1)
823     else:
824     try:
825 arfrever 1.87 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
826 arfrever 1.82 if eselect_process.wait() != 0:
827     raise ValueError
828     except (OSError, ValueError):
829     sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
830     sys.exit(1)
831    
832     eselect_output = eselect_process.stdout.read()
833     if not isinstance(eselect_output, str):
834     # Python 3
835     eselect_output = eselect_output.decode()
836    
837     EPYTHON_matched = EPYTHON_re.match(eselect_output)
838     if EPYTHON_matched:
839     PYTHON_ABI = EPYTHON_matched.group(1)
840     else:
841     sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
842     sys.exit(1)
843     EOF
844     if [[ "$?" != "0" ]]; then
845     die "${FUNCNAME}(): Generation of '$1' failed"
846     fi
847     else
848     cat << EOF >> "${file}"
849     try:
850 arfrever 1.87 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
851 arfrever 1.82 if eselect_process.wait() != 0:
852     raise ValueError
853     except (OSError, ValueError):
854     sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
855     sys.exit(1)
856    
857     eselect_output = eselect_process.stdout.read()
858     if not isinstance(eselect_output, str):
859     # Python 3
860     eselect_output = eselect_output.decode()
861    
862     EPYTHON_matched = EPYTHON_re.match(eselect_output)
863     if EPYTHON_matched:
864     PYTHON_ABI = EPYTHON_matched.group(1)
865     else:
866     sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
867     sys.exit(1)
868     EOF
869     if [[ "$?" != "0" ]]; then
870     die "${FUNCNAME}(): Generation of '$1' failed"
871     fi
872     fi
873     cat << EOF >> "${file}"
874    
875 arfrever 1.83 os.environ["PYTHON_PROCESS_NAME"] = sys.argv[0]
876     target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI)
877 arfrever 1.82 if not os.path.exists(target_executable):
878     sys.stderr.write("'%s' does not exist\n" % target_executable)
879     sys.exit(1)
880    
881     os.execv(target_executable, sys.argv)
882     EOF
883     if [[ "$?" != "0" ]]; then
884     die "${FUNCNAME}(): Generation of '$1' failed"
885     fi
886 arfrever 1.87 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed"
887 arfrever 1.82 done
888     }
889    
890 arfrever 1.85 # ================================================================================================
891     # ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======
892     # ================================================================================================
893    
894     # @FUNCTION: python_set_active_version
895     # @USAGE: <Python_ABI|2|3>
896 betelgeuse 1.55 # @DESCRIPTION:
897 arfrever 1.85 # Set active version of Python.
898     python_set_active_version() {
899 arfrever 1.87 if [[ "$#" -ne 1 ]]; then
900 arfrever 1.85 die "${FUNCNAME}() requires 1 argument"
901     fi
902    
903     if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
904     if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
905     die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
906     fi
907     export EPYTHON="$(PYTHON "$1")"
908     elif [[ "$1" == "2" ]]; then
909     if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
910     die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
911     fi
912     export EPYTHON="$(PYTHON -2)"
913     elif [[ "$1" == "3" ]]; then
914     if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
915     die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
916     fi
917     export EPYTHON="$(PYTHON -3)"
918     else
919     die "${FUNCNAME}(): Unrecognized argument '$1'"
920     fi
921    
922     # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
923     # so it does not need to be exported to subprocesses.
924     PYTHON_ABI="${EPYTHON#python}"
925     PYTHON_ABI="${PYTHON_ABI%%-*}"
926 arfrever 1.87
927     # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
928     PYTHON_REQUESTED_ACTIVE_VERSION="$1"
929 arfrever 1.85 }
930 betelgeuse 1.55
931 arfrever 1.85 # @FUNCTION: python_need_rebuild
932     # @DESCRIPTION: Run without arguments, specifies that the package should be
933     # rebuilt after a python upgrade.
934     # Do not use this function in ebuilds of packages supporting installation
935     # for multiple versions of Python.
936     python_need_rebuild() {
937     export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
938     }
939 betelgeuse 1.55
940 arfrever 1.85 # ================================================================================================
941     # ======================================= GETTER FUNCTIONS =======================================
942     # ================================================================================================
943 betelgeuse 1.55
944 arfrever 1.85 # @FUNCTION: PYTHON
945     # @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
946 betelgeuse 1.55 # @DESCRIPTION:
947 arfrever 1.87 # Print Python interpreter filename for specified Python ABI. If Python_ABI argument
948 arfrever 1.85 # is ommitted, then PYTHON_ABI environment variable must be set and is used.
949     # If -2 option is specified, then active version of Python 2 is used.
950     # If -3 option is specified, then active version of Python 3 is used.
951     # If --active option is specified, then active version of Python is used.
952     # Active version of Python can be set by python_set_active_version().
953     # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
954     # -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
955     # If --ABI option is specified, then only specified Python ABI is printed instead of
956     # Python interpreter filename.
957     # --ABI and --absolute-path options cannot be specified simultaneously.
958     PYTHON() {
959     local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" python2="0" python3="0" slot=
960 betelgeuse 1.55
961 arfrever 1.85 while (($#)); do
962     case "$1" in
963     -2)
964     python2="1"
965     ;;
966     -3)
967     python3="1"
968     ;;
969     --ABI)
970     ABI_output="1"
971     ;;
972     -A|--active)
973     active="1"
974     ;;
975     -a|--absolute-path)
976     absolute_path_output="1"
977     ;;
978     -f|--final-ABI)
979     final_ABI="1"
980     ;;
981     --)
982 arfrever 1.87 shift
983 arfrever 1.85 break
984     ;;
985     -*)
986     die "${FUNCNAME}(): Unrecognized option '$1'"
987     ;;
988     *)
989     break
990     ;;
991     esac
992     shift
993     done
994 betelgeuse 1.55
995 arfrever 1.85 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
996     die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
997     fi
998 arfrever 1.74
999 arfrever 1.85 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then
1000     die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously"
1001     fi
1002 betelgeuse 1.55
1003 arfrever 1.85 if [[ "$#" -eq 0 ]]; then
1004     if [[ "${active}" == "1" ]]; then
1005     if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1006     die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
1007     fi
1008 arfrever 1.87 slot="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
1009 arfrever 1.85 elif [[ "${final_ABI}" == "1" ]]; then
1010 arfrever 1.86 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
1011     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python"
1012     fi
1013 arfrever 1.85 validate_PYTHON_ABIS
1014     slot="${PYTHON_ABIS##* }"
1015     elif [[ "${python2}" == "1" ]]; then
1016     slot="$(eselect python show --python2)"
1017     if [[ -z "${slot}" ]]; then
1018     die "${FUNCNAME}(): Active Python 2 interpreter not set"
1019     elif [[ "${slot}" != "python2."* ]]; then
1020     die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1021     fi
1022     slot="${slot#python}"
1023     elif [[ "${python3}" == "1" ]]; then
1024     slot="$(eselect python show --python3)"
1025     if [[ -z "${slot}" ]]; then
1026     die "${FUNCNAME}(): Active Python 3 interpreter not set"
1027     elif [[ "${slot}" != "python3."* ]]; then
1028     die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1029 betelgeuse 1.55 fi
1030 arfrever 1.85 slot="${slot#python}"
1031     elif [[ -n "${PYTHON_ABI}" ]]; then
1032     slot="${PYTHON_ABI}"
1033 arfrever 1.74 else
1034 arfrever 1.85 die "${FUNCNAME}(): Invalid usage"
1035     fi
1036     elif [[ "$#" -eq 1 ]]; then
1037     if [[ "${active}" == "1" ]]; then
1038     die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
1039     fi
1040     if [[ "${final_ABI}" == "1" ]]; then
1041     die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
1042     fi
1043     if [[ "${python2}" == "1" ]]; then
1044     die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
1045     fi
1046     if [[ "${python3}" == "1" ]]; then
1047     die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
1048 betelgeuse 1.55 fi
1049 arfrever 1.85 slot="$1"
1050     else
1051     die "${FUNCNAME}(): Invalid usage"
1052 betelgeuse 1.55 fi
1053 hawking 1.46
1054 arfrever 1.85 if [[ "${ABI_output}" == "1" ]]; then
1055     echo -n "${slot}"
1056     return
1057     elif [[ "${absolute_path_output}" == "1" ]]; then
1058 arfrever 1.87 echo -n "${EPREFIX}/usr/bin/python${slot}"
1059 arfrever 1.85 else
1060     echo -n "python${slot}"
1061     fi
1062 hawking 1.46
1063 arfrever 1.85 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
1064     echo -n "-${ABI}"
1065     fi
1066 hawking 1.52 }
1067    
1068 arfrever 1.56 # @FUNCTION: python_get_includedir
1069 arfrever 1.86 # @USAGE: [-f|--final-ABI]
1070 arfrever 1.56 # @DESCRIPTION:
1071 arfrever 1.87 # Print path to Python include directory.
1072 arfrever 1.86 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1073 arfrever 1.56 python_get_includedir() {
1074 arfrever 1.86 local final_ABI="0"
1075    
1076     while (($#)); do
1077     case "$1" in
1078     -f|--final-ABI)
1079     final_ABI="1"
1080     ;;
1081     -*)
1082     die "${FUNCNAME}(): Unrecognized option '$1'"
1083     ;;
1084     *)
1085     die "${FUNCNAME}(): Invalid usage"
1086     ;;
1087     esac
1088     shift
1089     done
1090    
1091     if [[ "${final_ABI}" == "1" ]]; then
1092     echo "/usr/include/python$(PYTHON -f --ABI)"
1093     elif [[ -n "${PYTHON_ABI}" ]]; then
1094 arfrever 1.56 echo "/usr/include/python${PYTHON_ABI}"
1095     else
1096 arfrever 1.83 echo "/usr/include/python$(PYTHON -A --ABI)"
1097 arfrever 1.56 fi
1098     }
1099    
1100 pythonhead 1.43 # @FUNCTION: python_get_libdir
1101 arfrever 1.86 # @USAGE: [-f|--final-ABI]
1102 pythonhead 1.43 # @DESCRIPTION:
1103 arfrever 1.87 # Print path to Python library directory.
1104 arfrever 1.86 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1105 pythonhead 1.43 python_get_libdir() {
1106 arfrever 1.86 local final_ABI="0"
1107    
1108     while (($#)); do
1109     case "$1" in
1110     -f|--final-ABI)
1111     final_ABI="1"
1112     ;;
1113     -*)
1114     die "${FUNCNAME}(): Unrecognized option '$1'"
1115     ;;
1116     *)
1117     die "${FUNCNAME}(): Invalid usage"
1118     ;;
1119     esac
1120     shift
1121     done
1122    
1123     if [[ "${final_ABI}" == "1" ]]; then
1124     echo "/usr/$(get_libdir)/python$(PYTHON -f --ABI)"
1125     elif [[ -n "${PYTHON_ABI}" ]]; then
1126 arfrever 1.56 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
1127     else
1128 arfrever 1.83 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)"
1129 arfrever 1.56 fi
1130 pythonhead 1.42 }
1131    
1132 pythonhead 1.43 # @FUNCTION: python_get_sitedir
1133 arfrever 1.86 # @USAGE: [-f|--final-ABI]
1134 pythonhead 1.43 # @DESCRIPTION:
1135 arfrever 1.87 # Print path to Python site-packages directory.
1136 arfrever 1.86 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1137 pythonhead 1.43 python_get_sitedir() {
1138 arfrever 1.86 local options=()
1139    
1140     while (($#)); do
1141     case "$1" in
1142     -f|--final-ABI)
1143     options+=("$1")
1144     ;;
1145     -*)
1146     die "${FUNCNAME}(): Unrecognized option '$1'"
1147     ;;
1148     *)
1149     die "${FUNCNAME}(): Invalid usage"
1150     ;;
1151     esac
1152     shift
1153     done
1154    
1155     echo "$(python_get_libdir "${options[@]}")/site-packages"
1156 pythonhead 1.42 }
1157    
1158 arfrever 1.87 # @FUNCTION: python_get_library
1159     # @USAGE: [-f|--final-ABI] [-l|--linker-option]
1160     # @DESCRIPTION:
1161     # Print path to Python library.
1162     # If --linker-option is specified, then "-l${library}" linker option is printed.
1163     # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1164     python_get_library() {
1165     local final_ABI="0" linker_option="0" python_version
1166    
1167     while (($#)); do
1168     case "$1" in
1169     -f|--final-ABI)
1170     final_ABI="1"
1171     ;;
1172     -l|--linker-option)
1173     linker_option="1"
1174     ;;
1175     -*)
1176     die "${FUNCNAME}(): Unrecognized option '$1'"
1177     ;;
1178     *)
1179     die "${FUNCNAME}(): Invalid usage"
1180     ;;
1181     esac
1182     shift
1183     done
1184    
1185     if [[ "${final_ABI}" == "1" ]]; then
1186     python_version="$(PYTHON -f --ABI)"
1187     elif [[ -n "${PYTHON_ABI}" ]]; then
1188     python_version="${PYTHON_ABI}"
1189     else
1190     python_version="$(PYTHON -A --ABI)"
1191     fi
1192    
1193     if [[ "${linker_option}" == "1" ]]; then
1194     echo "-lpython${python_version}"
1195     else
1196     echo "/usr/$(get_libdir)/libpython${python_version}$(get_libname)"
1197     fi
1198     }
1199    
1200     # @FUNCTION: python_get_version
1201     # @USAGE: [-f|--final-ABI] [--major] [--minor] [--micro]
1202     # @DESCRIPTION:
1203     # Print Python version.
1204     # --major, --minor and --micro options cannot be specified simultaneously.
1205     # If --major, --minor and --micro options are not specified, then "${major_version}.${minor_version}" is printed.
1206     # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1207     python_get_version() {
1208     local final_ABI="0" major="0" minor="0" micro="0" python_command
1209    
1210     while (($#)); do
1211     case "$1" in
1212     -f|--final-ABI)
1213     final_ABI="1"
1214     ;;
1215     --major)
1216     major="1"
1217     ;;
1218     --minor)
1219     minor="1"
1220     ;;
1221     --micro)
1222     micro="1"
1223     ;;
1224     -*)
1225     die "${FUNCNAME}(): Unrecognized option '$1'"
1226     ;;
1227     *)
1228     die "${FUNCNAME}(): Invalid usage"
1229     ;;
1230     esac
1231     shift
1232     done
1233    
1234     if [[ "$((${major} + ${minor} + ${micro}))" -gt 1 ]]; then
1235     die "${FUNCNAME}(): '--major', '--minor' or '--micro' options cannot be specified simultaneously"
1236     fi
1237    
1238     if [[ "${major}" == "1" ]]; then
1239     python_command="from sys import version_info; print(version_info[0])"
1240     elif [[ "${minor}" == "1" ]]; then
1241     python_command="from sys import version_info; print(version_info[1])"
1242     elif [[ "${micro}" == "1" ]]; then
1243     python_command="from sys import version_info; print(version_info[2])"
1244     else
1245     python_command="from sys import version_info; print('.'.join([str(x) for x in version_info[:2]]))"
1246     fi
1247    
1248     if [[ "${final_ABI}" == "1" ]]; then
1249     "$(PYTHON -f)" -c "${python_command}"
1250     else
1251     "$(PYTHON "${PYTHON_ABI--A}")" -c "${python_command}"
1252     fi
1253     }
1254    
1255 arfrever 1.85 # ================================================================================================
1256     # =================================== MISCELLANEOUS FUNCTIONS ====================================
1257     # ================================================================================================
1258    
1259     _python_implementation() {
1260     if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1261     return 0
1262     else
1263     return 1
1264     fi
1265     }
1266    
1267 arfrever 1.87 _python_initialize_prefix_variables() {
1268     if has "${EAPI:-0}" 0 1 2; then
1269     if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
1270     EROOT="${ROOT%/}${EPREFIX}/"
1271     fi
1272     if [[ -n "${D}" && -z "${ED}" ]]; then
1273     ED="${D%/}${EPREFIX}/"
1274     fi
1275     fi
1276     }
1277    
1278 arfrever 1.85 # @FUNCTION: python_convert_shebangs
1279     # @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1280 dev-zero 1.33 # @DESCRIPTION:
1281 arfrever 1.85 # Convert shebangs in specified files. Directories can be specified only with --recursive option.
1282     python_convert_shebangs() {
1283     local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
1284    
1285     while (($#)); do
1286     case "$1" in
1287     -r|--recursive)
1288     recursive="1"
1289     ;;
1290     -q|--quiet)
1291     quiet="1"
1292     ;;
1293     -x|--only-executables)
1294     only_executables="1"
1295     ;;
1296     --)
1297 arfrever 1.87 shift
1298 arfrever 1.85 break
1299     ;;
1300     -*)
1301     die "${FUNCNAME}(): Unrecognized option '$1'"
1302     ;;
1303     *)
1304     break
1305     ;;
1306     esac
1307     shift
1308     done
1309    
1310     if [[ "$#" -eq 0 ]]; then
1311     die "${FUNCNAME}(): Missing Python version and files or directories"
1312     elif [[ "$#" -eq 1 ]]; then
1313     die "${FUNCNAME}(): Missing files or directories"
1314 liquidx 1.1 fi
1315 arfrever 1.85
1316     python_version="$1"
1317     shift
1318    
1319     for argument in "$@"; do
1320     if [[ ! -e "${argument}" ]]; then
1321     die "${FUNCNAME}(): '${argument}' does not exist"
1322     elif [[ -f "${argument}" ]]; then
1323     files+=("${argument}")
1324     elif [[ -d "${argument}" ]]; then
1325     if [[ "${recursive}" == "1" ]]; then
1326     if [[ "${only_executables}" == "1" ]]; then
1327     files+=($(find "${argument}" -perm /111 -type f))
1328     else
1329     files+=($(find "${argument}" -type f))
1330     fi
1331     else
1332     die "${FUNCNAME}(): '${argument}' is not a regular file"
1333     fi
1334     else
1335     die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
1336     fi
1337     done
1338    
1339     for file in "${files[@]}"; do
1340     file="${file#./}"
1341     [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1342    
1343     if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1344     if [[ "${quiet}" == "0" ]]; then
1345     einfo "Converting shebang in '${file}'"
1346     fi
1347     sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1348    
1349     # Delete potential whitespace after "#!".
1350     sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1351     fi
1352     done
1353 liquidx 1.1 }
1354    
1355 dev-zero 1.33 # @FUNCTION: python_mod_exists
1356 arfrever 1.56 # @USAGE: <module>
1357 dev-zero 1.33 # @DESCRIPTION:
1358 arfrever 1.87 # Run with the module name as an argument. It will check if a
1359     # Python module is installed and loadable. It will return
1360 dev-zero 1.33 # TRUE(0) if the module exists, and FALSE(1) if the module does
1361     # not exist.
1362 liquidx 1.1 #
1363 dev-zero 1.33 # Example:
1364 liquidx 1.1 # if python_mod_exists gtk; then
1365 liquidx 1.28 # echo "gtk support enabled"
1366 liquidx 1.1 # fi
1367     python_mod_exists() {
1368 arfrever 1.87 if [[ "$#" -ne 1 ]]; then
1369     die "${FUNCNAME}() requires 1 argument"
1370     fi
1371     "$(PYTHON "${PYTHON_ABI--A}")" -c "import $1" &> /dev/null
1372 liquidx 1.1 }
1373    
1374 arfrever 1.85 # @FUNCTION: python_tkinter_exists
1375 dev-zero 1.33 # @DESCRIPTION:
1376 arfrever 1.87 # Run without arguments, checks if Python was compiled with Tkinter
1377 arfrever 1.85 # support. If not, prints an error message and dies.
1378     python_tkinter_exists() {
1379 arfrever 1.87 if ! "$(PYTHON "${PYTHON_ABI--A}")" -c "from sys import version_info
1380     if version_info[0] == 3:
1381     import tkinter
1382     else:
1383     import Tkinter" &> /dev/null; then
1384     eerror "Python needs to be rebuilt with tkinter support enabled."
1385     eerror "Add the following line to '${EPREFIX}/etc/portage/package.use' and rebuild Python"
1386     eerror "dev-lang/python tk"
1387     die "Python installed without support for tkinter"
1388     fi
1389     }
1390    
1391     # ================================================================================================
1392     # ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1393     # ================================================================================================
1394    
1395     # @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
1396     # @DESCRIPTION:
1397     # User-configurable verbosity of tests of Python modules.
1398     # Supported values: 0, 1, 2, 3, 4.
1399     PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
1400    
1401     # @FUNCTION: python_execute_nosetests
1402     # @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1403     # @DESCRIPTION:
1404     # Execute nosetests for all enabled versions of Python.
1405     python_execute_nosetests() {
1406     local PYTHONPATH_template= separate_build_dirs=
1407    
1408     while (($#)); do
1409     case "$1" in
1410     -P|--PYTHONPATH)
1411     PYTHONPATH_template="$2"
1412     shift
1413     ;;
1414     -s|--separate-build-dirs)
1415     separate_build_dirs="1"
1416     ;;
1417     --)
1418     shift
1419     break
1420     ;;
1421     -*)
1422     die "${FUNCNAME}(): Unrecognized option '$1'"
1423     ;;
1424     *)
1425     break
1426     ;;
1427     esac
1428     shift
1429     done
1430    
1431     python_test_function() {
1432     local evaluated_PYTHONPATH=
1433    
1434     if [[ -n "${PYTHONPATH_template}" ]]; then
1435     evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1436     if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1437     unset evaluated_PYTHONPATH
1438     fi
1439     fi
1440    
1441     if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1442     echo PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1443     PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1444     else
1445     echo nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1446     nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1447     fi
1448     }
1449     if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1450     python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1451     else
1452     if [[ -n "${separate_build_dirs}" ]]; then
1453     die "${FUNCNAME}(): Invalid usage"
1454     fi
1455     python_test_function "$@"
1456     fi
1457     }
1458    
1459     # @FUNCTION: python_execute_py.test
1460     # @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1461     # @DESCRIPTION:
1462     # Execute py.test for all enabled versions of Python.
1463     python_execute_py.test() {
1464     local PYTHONPATH_template= separate_build_dirs=
1465    
1466     while (($#)); do
1467     case "$1" in
1468     -P|--PYTHONPATH)
1469     PYTHONPATH_template="$2"
1470     shift
1471     ;;
1472     -s|--separate-build-dirs)
1473     separate_build_dirs="1"
1474     ;;
1475     --)
1476     shift
1477     break
1478     ;;
1479     -*)
1480     die "${FUNCNAME}(): Unrecognized option '$1'"
1481     ;;
1482     *)
1483     break
1484     ;;
1485     esac
1486     shift
1487     done
1488    
1489     python_test_function() {
1490     local evaluated_PYTHONPATH=
1491    
1492     if [[ -n "${PYTHONPATH_template}" ]]; then
1493     evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1494     if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1495     unset evaluated_PYTHONPATH
1496     fi
1497     fi
1498    
1499     if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1500     echo PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"
1501     PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"
1502     else
1503     echo py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"
1504     py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"
1505     fi
1506     }
1507     if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1508     python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1509     else
1510     if [[ -n "${separate_build_dirs}" ]]; then
1511     die "${FUNCNAME}(): Invalid usage"
1512     fi
1513     python_test_function "$@"
1514     fi
1515     }
1516    
1517     # @FUNCTION: python_execute_trial
1518     # @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1519     # @DESCRIPTION:
1520     # Execute trial for all enabled versions of Python.
1521     python_execute_trial() {
1522     local PYTHONPATH_template= separate_build_dirs=
1523    
1524     while (($#)); do
1525     case "$1" in
1526     -P|--PYTHONPATH)
1527     PYTHONPATH_template="$2"
1528     shift
1529     ;;
1530     -s|--separate-build-dirs)
1531     separate_build_dirs="1"
1532     ;;
1533     --)
1534     shift
1535     break
1536     ;;
1537     -*)
1538     die "${FUNCNAME}(): Unrecognized option '$1'"
1539     ;;
1540     *)
1541     break
1542     ;;
1543     esac
1544     shift
1545     done
1546    
1547     python_test_function() {
1548     local evaluated_PYTHONPATH=
1549    
1550     if [[ -n "${PYTHONPATH_template}" ]]; then
1551     evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1552     if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1553     unset evaluated_PYTHONPATH
1554     fi
1555     fi
1556    
1557     if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1558     echo PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1559     PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1560     else
1561     echo trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1562     trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1563     fi
1564     }
1565     if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1566     python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1567     else
1568     if [[ -n "${separate_build_dirs}" ]]; then
1569     die "${FUNCNAME}(): Invalid usage"
1570     fi
1571     python_test_function "$@"
1572 arfrever 1.56 fi
1573 arfrever 1.85 }
1574 arfrever 1.56
1575 arfrever 1.85 # ================================================================================================
1576     # ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
1577     # ================================================================================================
1578 hawking 1.37
1579 arfrever 1.85 # @FUNCTION: python_enable_pyc
1580     # @DESCRIPTION:
1581     # Tell Python to automatically recompile modules to .pyc/.pyo if the
1582     # timestamps/version stamps have changed.
1583     python_enable_pyc() {
1584     unset PYTHONDONTWRITEBYTECODE
1585     }
1586 hawking 1.37
1587 arfrever 1.85 # @FUNCTION: python_disable_pyc
1588     # @DESCRIPTION:
1589     # Tell Python not to automatically recompile modules to .pyc/.pyo
1590     # even if the timestamps/version stamps do not match. This is done
1591     # to protect sandbox.
1592     python_disable_pyc() {
1593     export PYTHONDONTWRITEBYTECODE="1"
1594 liquidx 1.1 }
1595    
1596 dev-zero 1.33 # @FUNCTION: python_mod_optimize
1597 arfrever 1.56 # @USAGE: [options] [directory|file]
1598 dev-zero 1.33 # @DESCRIPTION:
1599 arfrever 1.56 # If no arguments supplied, it will recompile not recursively all modules
1600     # under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages).
1601 liquidx 1.1 #
1602 dev-zero 1.33 # If supplied with arguments, it will recompile all modules recursively
1603 arfrever 1.56 # in the supplied directory.
1604     # This function should only be run in pkg_postinst().
1605 dev-zero 1.33 #
1606 arfrever 1.56 # Options passed to this function are passed to compileall.py.
1607 hawking 1.39 #
1608 dev-zero 1.33 # Example:
1609 arfrever 1.56 # python_mod_optimize ctypesgencore
1610 liquidx 1.1 python_mod_optimize() {
1611 arfrever 1.87 _python_initialize_prefix_variables
1612    
1613 arfrever 1.56 # Check if phase is pkg_postinst().
1614 arfrever 1.87 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME}() should only be run in pkg_postinst()"
1615 arfrever 1.56
1616     if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1617 arfrever 1.86 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
1618 hawking 1.37
1619 arfrever 1.56 # Strip trailing slash from ROOT.
1620 arfrever 1.87 root="${EROOT%/}"
1621 arfrever 1.56
1622     # Respect ROOT and options passed to compileall.py.
1623     while (($#)); do
1624     case "$1" in
1625     -l|-f|-q)
1626     options+=("$1")
1627     ;;
1628     -d|-x)
1629     options+=("$1" "$2")
1630     shift
1631     ;;
1632     -*)
1633 arfrever 1.87 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1634 arfrever 1.56 ;;
1635     *)
1636 arfrever 1.87 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1637     die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1638 arfrever 1.56 elif [[ "$1" =~ ^/ ]]; then
1639     if [[ -d "${root}/$1" ]]; then
1640     other_dirs+=("${root}/$1")
1641     elif [[ -f "${root}/$1" ]]; then
1642     other_files+=("${root}/$1")
1643     elif [[ -e "${root}/$1" ]]; then
1644     ewarn "'${root}/$1' is not a file or a directory!"
1645     else
1646 arfrever 1.84 ewarn "'${root}/$1' does not exist!"
1647 arfrever 1.56 fi
1648     else
1649 arfrever 1.83 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1650 arfrever 1.62 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
1651 arfrever 1.56 site_packages_dirs+=("$1")
1652     break
1653 arfrever 1.62 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
1654 arfrever 1.56 site_packages_files+=("$1")
1655     break
1656 arfrever 1.62 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
1657 arfrever 1.56 ewarn "'$1' is not a file or a directory!"
1658     else
1659 arfrever 1.84 ewarn "'$1' does not exist!"
1660 arfrever 1.56 fi
1661     done
1662     fi
1663     ;;
1664     esac
1665     shift
1666     done
1667 hawking 1.37
1668 arfrever 1.56 # Set additional options.
1669     options+=("-q")
1670 liquidx 1.13
1671 arfrever 1.83 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1672 arfrever 1.56 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
1673     return_code="0"
1674     ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
1675     if ((${#site_packages_dirs[@]})); then
1676     for dir in "${site_packages_dirs[@]}"; do
1677 arfrever 1.62 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
1678 arfrever 1.56 done
1679 arfrever 1.62 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
1680 arfrever 1.63 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
1681 arfrever 1.56 fi
1682     if ((${#site_packages_files[@]})); then
1683     for file in "${site_packages_files[@]}"; do
1684 arfrever 1.62 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
1685 arfrever 1.56 done
1686 arfrever 1.62 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
1687 arfrever 1.63 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
1688 hawking 1.49 fi
1689 arfrever 1.56 eend "${return_code}"
1690     fi
1691     unset site_packages_absolute_dirs site_packages_absolute_files
1692     done
1693    
1694 arfrever 1.86 # Restore previous value of PYTHON_ABI.
1695     if [[ -n "${previous_PYTHON_ABI}" ]]; then
1696     PYTHON_ABI="${previous_PYTHON_ABI}"
1697     else
1698     unset PYTHON_ABI
1699     fi
1700 hawking 1.36
1701 arfrever 1.56 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1702     return_code="0"
1703 arfrever 1.83 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)"
1704 arfrever 1.56 if ((${#other_dirs[@]})); then
1705 arfrever 1.86 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
1706     "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
1707 arfrever 1.56 fi
1708     if ((${#other_files[@]})); then
1709 arfrever 1.86 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
1710     "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
1711 arfrever 1.56 fi
1712     eend "${return_code}"
1713     fi
1714 pythonhead 1.7 else
1715 arfrever 1.63 local myroot mydirs=() myfiles=() myopts=() return_code="0"
1716 arfrever 1.56
1717     # strip trailing slash
1718 arfrever 1.87 myroot="${EROOT%/}"
1719 arfrever 1.56
1720     # respect ROOT and options passed to compileall.py
1721     while (($#)); do
1722     case "$1" in
1723     -l|-f|-q)
1724     myopts+=("$1")
1725     ;;
1726     -d|-x)
1727     myopts+=("$1" "$2")
1728     shift
1729     ;;
1730     -*)
1731 arfrever 1.87 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1732 arfrever 1.56 ;;
1733     *)
1734     if [[ -d "${myroot}"/$1 ]]; then
1735     mydirs+=("${myroot}/$1")
1736     elif [[ -f "${myroot}"/$1 ]]; then
1737     # Files are passed to python_mod_compile which is ROOT-aware
1738     myfiles+=("$1")
1739     elif [[ -e "${myroot}/$1" ]]; then
1740     ewarn "${myroot}/$1 is not a file or directory!"
1741     else
1742 arfrever 1.84 ewarn "${myroot}/$1 does not exist!"
1743 arfrever 1.56 fi
1744     ;;
1745     esac
1746     shift
1747     done
1748    
1749     # set additional opts
1750     myopts+=(-q)
1751 pythonhead 1.7
1752 arfrever 1.83 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)"
1753 arfrever 1.56 if ((${#mydirs[@]})); then
1754 arfrever 1.86 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
1755     "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
1756 arfrever 1.56 fi
1757 pythonhead 1.7
1758 arfrever 1.56 if ((${#myfiles[@]})); then
1759     python_mod_compile "${myfiles[@]}"
1760     fi
1761 hawking 1.36
1762 arfrever 1.63 eend "${return_code}"
1763 hawking 1.36 fi
1764 liquidx 1.1 }
1765    
1766 dev-zero 1.33 # @FUNCTION: python_mod_cleanup
1767 arfrever 1.79 # @USAGE: [directory|file]
1768 dev-zero 1.33 # @DESCRIPTION:
1769 arfrever 1.79 # Run with optional arguments, where arguments are Python modules. If none given,
1770     # it will look in /usr/lib/python[0-9].[0-9].
1771 dev-zero 1.33 #
1772 arfrever 1.56 # It will recursively scan all compiled Python modules in the directories and
1773     # determine if they are orphaned (i.e. their corresponding .py files are missing.)
1774     # If they are, then it will remove their corresponding .pyc and .pyo files.
1775 hawking 1.37 #
1776 arfrever 1.56 # This function should only be run in pkg_postrm().
1777 liquidx 1.1 python_mod_cleanup() {
1778 arfrever 1.87 _python_initialize_prefix_variables
1779    
1780 arfrever 1.78 local path py_file PYTHON_ABI SEARCH_PATH=() root
1781 liquidx 1.16
1782 arfrever 1.56 # Check if phase is pkg_postrm().
1783 arfrever 1.87 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME}() should only be run in pkg_postrm()"
1784 hawking 1.37
1785 arfrever 1.56 # Strip trailing slash from ROOT.
1786 arfrever 1.87 root="${EROOT%/}"
1787 liquidx 1.1
1788 hawking 1.50 if (($#)); then
1789 arfrever 1.56 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1790     while (($#)); do
1791 arfrever 1.87 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1792     die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1793 arfrever 1.56 elif [[ "$1" =~ ^/ ]]; then
1794     SEARCH_PATH+=("${root}/${1#/}")
1795     else
1796 arfrever 1.83 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1797 arfrever 1.62 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
1798 arfrever 1.56 done
1799     fi
1800     shift
1801     done
1802     else
1803     SEARCH_PATH=("${@#/}")
1804     SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
1805     fi
1806 liquidx 1.5 else
1807 arfrever 1.78 local dir sitedir
1808     for dir in "${root}"/usr/lib*; do
1809     if [[ -d "${dir}" && ! -L "${dir}" ]]; then
1810     for sitedir in "${dir}"/python*/site-packages; do
1811     if [[ -d "${sitedir}" ]]; then
1812     SEARCH_PATH+=("${sitedir}")
1813     fi
1814     done
1815     fi
1816     done
1817     fi
1818    
1819     local BLUE CYAN NORMAL
1820     if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1821 arfrever 1.80 BLUE=$'\e[1;34m'
1822     CYAN=$'\e[1;36m'
1823 arfrever 1.78 NORMAL=$'\e[0m'
1824     else
1825     BLUE=
1826     CYAN=
1827     NORMAL=
1828 liquidx 1.5 fi
1829 pythonhead 1.7
1830 hawking 1.50 for path in "${SEARCH_PATH[@]}"; do
1831 arfrever 1.78 if [[ -d "${path}" ]]; then
1832     find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
1833     py_file="${REPLY%[co]}"
1834     [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
1835     einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
1836     rm -f "${py_file}"[co]
1837     done
1838 hawking 1.50
1839 arfrever 1.78 # Attempt to delete directories, which may be empty.
1840     find "${path}" -type d | sort -r | while read -r dir; do
1841     rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
1842     done
1843     elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then
1844     einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1845     rm -f "${path}"[co]
1846     fi
1847 pythonhead 1.7 done
1848 liquidx 1.1 }
1849 arfrever 1.85
1850     # ================================================================================================
1851     # ===================================== DEPRECATED FUNCTIONS =====================================
1852     # ================================================================================================
1853    
1854     # @FUNCTION: python_version
1855     # @DESCRIPTION:
1856     # Run without arguments and it will export the version of python
1857     # currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
1858     python_version() {
1859 arfrever 1.87 if ! has "${EAPI:-0}" 0 1 2; then
1860     eerror "Use PYTHON() and/or python_get_*() instead of python_version()."
1861     die "${FUNCNAME}() cannot be used in this EAPI"
1862     fi
1863    
1864 arfrever 1.85 [[ -n "${PYVER}" ]] && return 0
1865     local tmpstr
1866 arfrever 1.87 python="${python:-${EPREFIX}/usr/bin/python}"
1867 arfrever 1.85 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
1868     export PYVER_ALL="${tmpstr#Python }"
1869 arfrever 1.86 export PYVER_MAJOR="${PYVER_ALL:0:1}"
1870     export PYVER_MINOR="${PYVER_ALL:2:1}"
1871     if [[ "${PYVER_ALL:3:1}" == "." ]]; then
1872     export PYVER_MICRO="${PYVER_ALL:4}"
1873     fi
1874     export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
1875 arfrever 1.85 }
1876    
1877     # @FUNCTION: python_mod_compile
1878     # @USAGE: <file> [more files ...]
1879     # @DESCRIPTION:
1880     # Given filenames, it will pre-compile the module's .pyc and .pyo.
1881     # This function should only be run in pkg_postinst()
1882     #
1883     # Example:
1884     # python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
1885     #
1886     python_mod_compile() {
1887     if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1888 arfrever 1.87 eerror "Use python_mod_optimize() instead of python_mod_compile()."
1889 arfrever 1.85 die "${FUNCNAME}() cannot be used in this EAPI"
1890     fi
1891    
1892 arfrever 1.87 _python_initialize_prefix_variables
1893    
1894 arfrever 1.85 local f myroot myfiles=()
1895    
1896     # Check if phase is pkg_postinst()
1897 arfrever 1.87 [[ ${EBUILD_PHASE} != postinst ]] && die "${FUNCNAME}() should only be run in pkg_postinst()"
1898 arfrever 1.85
1899     # strip trailing slash
1900 arfrever 1.87 myroot="${EROOT%/}"
1901 arfrever 1.85
1902     # respect ROOT
1903     for f in "$@"; do
1904     [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
1905     done
1906    
1907     if ((${#myfiles[@]})); then
1908 arfrever 1.86 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
1909     "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
1910 arfrever 1.85 else
1911     ewarn "No files to compile!"
1912     fi
1913     }

  ViewVC Help
Powered by ViewVC 1.1.20