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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.94 - (hide annotations) (download)
Sat Mar 13 13:46:20 2010 UTC (4 years, 1 month ago) by arfrever
Branch: MAIN
Changes since 1.93: +90 -49 lines
Improve phase functions.
Simplify and improve validate_PYTHON_ABIS().
Fix handling of ${S} ending in '/' in python_copy_sources() (bug #307943).
Improve python_generate_wrapper_scripts().
Improve python_convert_shebangs().
Add python_clean_sitedirs().
Simplify python_mod_cleanup().
Improve deprecation warnings in python_version(), python_mod_exists() and python_tkinter_exists().

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

  ViewVC Help
Powered by ViewVC 1.1.20