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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.144 - (hide annotations) (download)
Sun Jan 1 05:02:27 2012 UTC (3 years, 6 months ago) by floppym
Branch: MAIN
Changes since 1.143: +17 -7 lines
Avoid including python.eclass more than once.

1 floppym 1.144 # Copyright 1999-2012 Gentoo Foundation
2 liquidx 1.1 # Distributed under the terms of the GNU General Public License v2
3 floppym 1.144 # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.143 2011/12/19 01:29:57 robbat2 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 floppym 1.144 # Must call inherit before EXPORT_FUNCTIONS to avoid QA warning.
13     if [[ -z "${_PYTHON_ECLASS_INHERITED}" ]]; then
14     inherit multilib
15     fi
16    
17     # Export pkg_setup every time to avoid issues with eclass inheritance order.
18     if ! has "${EAPI:-0}" 0 1 2 3 || { has "${EAPI:-0}" 2 3 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; }; then
19     EXPORT_FUNCTIONS pkg_setup
20     fi
21    
22     # Avoid processing this eclass more than once.
23     if [[ -z "${_PYTHON_ECLASS_INHERITED}" ]]; then
24     _PYTHON_ECLASS_INHERITED="1"
25 liquidx 1.2
26 phajdan.jr 1.139 if ! has "${EAPI:-0}" 0 1 2 3 4; then
27 arfrever 1.83 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
28     fi
29    
30 arfrever 1.106 _CPYTHON2_GLOBALLY_SUPPORTED_ABIS=(2.4 2.5 2.6 2.7)
31 djc 1.132 _CPYTHON3_GLOBALLY_SUPPORTED_ABIS=(3.1 3.2)
32 arfrever 1.106 _JYTHON_GLOBALLY_SUPPORTED_ABIS=(2.5-jython)
33 djc 1.141 _PYPY_GLOBALLY_SUPPORTED_ABIS=(2.7-pypy-1.7)
34 djc 1.126 _PYTHON_GLOBALLY_SUPPORTED_ABIS=(${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]} ${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]} ${_JYTHON_GLOBALLY_SUPPORTED_ABIS[@]} ${_PYPY_GLOBALLY_SUPPORTED_ABIS[@]})
35 arfrever 1.104
36     # ================================================================================================
37     # ===================================== HANDLING OF METADATA =====================================
38     # ================================================================================================
39    
40 djc 1.120 _PYTHON_ABI_PATTERN_REGEX="([[:alnum:]]|\.|-|\*|\[|\])+"
41    
42 arfrever 1.106 _python_check_python_abi_matching() {
43 djc 1.115 local pattern patterns patterns_list="0" PYTHON_ABI
44    
45     while (($#)); do
46     case "$1" in
47     --patterns-list)
48     patterns_list="1"
49     ;;
50     --)
51     shift
52     break
53     ;;
54     -*)
55     die "${FUNCNAME}(): Unrecognized option '$1'"
56     ;;
57     *)
58     break
59     ;;
60     esac
61     shift
62     done
63    
64 arfrever 1.106 if [[ "$#" -ne 2 ]]; then
65     die "${FUNCNAME}() requires 2 arguments"
66     fi
67    
68 djc 1.115 PYTHON_ABI="$1"
69    
70     if [[ "${patterns_list}" == "0" ]]; then
71     pattern="$2"
72    
73     if [[ "${pattern}" == *"-cpython" ]]; then
74     [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+$ && "${PYTHON_ABI}" == ${pattern%-cpython} ]]
75     elif [[ "${pattern}" == *"-jython" ]]; then
76     [[ "${PYTHON_ABI}" == ${pattern} ]]
77 djc 1.126 elif [[ "${pattern}" == *"-pypy-"* ]]; then
78     [[ "${PYTHON_ABI}" == ${pattern} ]]
79 djc 1.115 else
80     if [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
81     [[ "${PYTHON_ABI}" == ${pattern} ]]
82     elif [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+-jython$ ]]; then
83     [[ "${PYTHON_ABI%-jython}" == ${pattern} ]]
84 djc 1.126 elif [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+-pypy-[[:digit:]]+\.[[:digit:]]+$ ]]; then
85     [[ "${PYTHON_ABI%-pypy-*}" == ${pattern} ]]
86 djc 1.115 else
87     die "${FUNCNAME}(): Unrecognized Python ABI '${PYTHON_ABI}'"
88     fi
89 arfrever 1.106 fi
90 djc 1.115 else
91     patterns="${2// /$'\n'}"
92    
93     while read pattern; do
94     if _python_check_python_abi_matching "${PYTHON_ABI}" "${pattern}"; then
95     return 0
96     fi
97     done <<< "${patterns}"
98    
99     return 1
100 arfrever 1.106 fi
101     }
102    
103 djc 1.121 _python_package_supporting_installation_for_multiple_python_abis() {
104     if has "${EAPI:-0}" 0 1 2 3 4; then
105     if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
106     return 0
107     else
108     return 1
109     fi
110     else
111     die "${FUNCNAME}(): Support for EAPI=\"${EAPI}\" not implemented"
112     fi
113     }
114    
115 arfrever 1.87 # @ECLASS-VARIABLE: PYTHON_DEPEND
116     # @DESCRIPTION:
117     # Specification of dependency on dev-lang/python.
118     # Syntax:
119     # PYTHON_DEPEND: [[!]USE_flag? ]<version_components_group>[ version_components_group]
120     # version_components_group: <major_version[:[minimal_version][:maximal_version]]>
121     # major_version: <2|3|*>
122     # minimal_version: <minimal_major_version.minimal_minor_version>
123     # maximal_version: <maximal_major_version.maximal_minor_version>
124    
125 arfrever 1.104 _python_parse_PYTHON_DEPEND() {
126 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
127 arfrever 1.87
128     version_components_group_regex="(2|3|\*)(:([[:digit:]]+\.[[:digit:]]+)?(:([[:digit:]]+\.[[:digit:]]+)?)?)?"
129     version_components_groups="${PYTHON_DEPEND}"
130    
131     if [[ "${version_components_groups}" =~ ^((\!)?[[:alnum:]_-]+\?\ )?${version_components_group_regex}(\ ${version_components_group_regex})?$ ]]; then
132     if [[ "${version_components_groups}" =~ ^(\!)?[[:alnum:]_-]+\? ]]; then
133     USE_flag="${version_components_groups%\? *}"
134     version_components_groups="${version_components_groups#* }"
135     fi
136     if [[ "${version_components_groups}" =~ ("*".*" "|" *"|^2.*\ (2|\*)|^3.*\ (3|\*)) ]]; then
137     die "Invalid syntax of PYTHON_DEPEND: Incorrectly specified groups of versions"
138     fi
139    
140     version_components_groups="${version_components_groups// /$'\n'}"
141     while read version_components_group; do
142     major_version="${version_components_group:0:1}"
143     minimal_version="${version_components_group:2}"
144     minimal_version="${minimal_version%:*}"
145     maximal_version="${version_components_group:$((3 + ${#minimal_version}))}"
146    
147     if [[ "${major_version}" =~ ^(2|3)$ ]]; then
148     if [[ -n "${minimal_version}" && "${major_version}" != "${minimal_version:0:1}" ]]; then
149     die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' not in specified group of versions"
150     fi
151     if [[ -n "${maximal_version}" && "${major_version}" != "${maximal_version:0:1}" ]]; then
152     die "Invalid syntax of PYTHON_DEPEND: Maximal version '${maximal_version}' not in specified group of versions"
153     fi
154     fi
155    
156     if [[ "${major_version}" == "2" ]]; then
157     python2="1"
158 arfrever 1.106 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}")
159 arfrever 1.87 python2_minimal_version="${minimal_version}"
160     python2_maximal_version="${maximal_version}"
161     elif [[ "${major_version}" == "3" ]]; then
162     python3="1"
163 arfrever 1.106 python_versions=("${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
164 arfrever 1.87 python3_minimal_version="${minimal_version}"
165     python3_maximal_version="${maximal_version}"
166     else
167     python_all="1"
168 arfrever 1.106 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
169 arfrever 1.87 python_minimal_version="${minimal_version}"
170     python_maximal_version="${maximal_version}"
171     fi
172    
173     if [[ -n "${minimal_version}" ]] && ! has "${minimal_version}" "${python_versions[@]}"; then
174     die "Invalid syntax of PYTHON_DEPEND: Unrecognized minimal version '${minimal_version}'"
175     fi
176     if [[ -n "${maximal_version}" ]] && ! has "${maximal_version}" "${python_versions[@]}"; then
177     die "Invalid syntax of PYTHON_DEPEND: Unrecognized maximal version '${maximal_version}'"
178     fi
179    
180     if [[ -n "${minimal_version}" && -n "${maximal_version}" && "${minimal_version}" > "${maximal_version}" ]]; then
181     die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' greater than maximal version '${maximal_version}'"
182     fi
183     done <<< "${version_components_groups}"
184    
185 arfrever 1.88 _PYTHON_ATOMS=()
186    
187     _append_accepted_versions_range() {
188 arfrever 1.87 local accepted_version="0" i
189     for ((i = "${#python_versions[@]}"; i >= 0; i--)); do
190     if [[ "${python_versions[${i}]}" == "${python_maximal_version}" ]]; then
191     accepted_version="1"
192     fi
193     if [[ "${accepted_version}" == "1" ]]; then
194 arfrever 1.88 _PYTHON_ATOMS+=("=dev-lang/python-${python_versions[${i}]}*")
195 arfrever 1.87 fi
196     if [[ "${python_versions[${i}]}" == "${python_minimal_version}" ]]; then
197     accepted_version="0"
198     fi
199     done
200     }
201    
202     if [[ "${python_all}" == "1" ]]; then
203 arfrever 1.88 if [[ -z "${python_minimal_version}" && -z "${python_maximal_version}" ]]; then
204     _PYTHON_ATOMS+=("dev-lang/python")
205     else
206 arfrever 1.106 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
207 arfrever 1.88 python_minimal_version="${python_minimal_version:-${python_versions[0]}}"
208     python_maximal_version="${python_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
209     _append_accepted_versions_range
210     fi
211 arfrever 1.87 else
212     if [[ "${python3}" == "1" ]]; then
213 arfrever 1.88 if [[ -z "${python3_minimal_version}" && -z "${python3_maximal_version}" ]]; then
214     _PYTHON_ATOMS+=("=dev-lang/python-3*")
215     else
216 arfrever 1.106 python_versions=("${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}")
217 arfrever 1.88 python_minimal_version="${python3_minimal_version:-${python_versions[0]}}"
218     python_maximal_version="${python3_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
219     _append_accepted_versions_range
220     fi
221 arfrever 1.87 fi
222     if [[ "${python2}" == "1" ]]; then
223 arfrever 1.88 if [[ -z "${python2_minimal_version}" && -z "${python2_maximal_version}" ]]; then
224     _PYTHON_ATOMS+=("=dev-lang/python-2*")
225     else
226 arfrever 1.106 python_versions=("${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}")
227 arfrever 1.88 python_minimal_version="${python2_minimal_version:-${python_versions[0]}}"
228     python_maximal_version="${python2_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
229     _append_accepted_versions_range
230     fi
231 arfrever 1.87 fi
232     fi
233    
234 arfrever 1.88 unset -f _append_accepted_versions_range
235 arfrever 1.87
236 arfrever 1.88 if [[ "${#_PYTHON_ATOMS[@]}" -gt 1 ]]; then
237     DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${_PYTHON_ATOMS[@]} )${USE_flag:+ )}"
238     RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${_PYTHON_ATOMS[@]} )${USE_flag:+ )}"
239     else
240     DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }${_PYTHON_ATOMS[@]}${USE_flag:+ )}"
241     RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }${_PYTHON_ATOMS[@]}${USE_flag:+ )}"
242     fi
243 arfrever 1.87 else
244     die "Invalid syntax of PYTHON_DEPEND"
245     fi
246     }
247    
248 arfrever 1.89 DEPEND=">=app-admin/eselect-python-20091230"
249 arfrever 1.87 RDEPEND="${DEPEND}"
250    
251 arfrever 1.104 if [[ -n "${PYTHON_DEPEND}" ]]; then
252     _python_parse_PYTHON_DEPEND
253 betelgeuse 1.55 else
254 arfrever 1.88 _PYTHON_ATOMS=("dev-lang/python")
255 carlo 1.22 fi
256 arfrever 1.104 unset -f _python_parse_PYTHON_DEPEND
257    
258     if [[ -n "${NEED_PYTHON}" ]]; then
259     eerror "Use PYTHON_DEPEND variable instead of NEED_PYTHON variable."
260     die "NEED_PYTHON variable is banned"
261     fi
262 liquidx 1.18
263 arfrever 1.85 # @ECLASS-VARIABLE: PYTHON_USE_WITH
264     # @DESCRIPTION:
265 arfrever 1.87 # Set this to a space separated list of USE flags the Python slot in use must be built with.
266 liquidx 1.18
267 arfrever 1.85 # @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
268 dev-zero 1.33 # @DESCRIPTION:
269 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.
270 liquidx 1.18
271 arfrever 1.85 # @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
272     # @DESCRIPTION:
273 arfrever 1.87 # Set this to a name of a USE flag if you need to make either PYTHON_USE_WITH or
274     # PYTHON_USE_WITH_OR atoms conditional under a USE flag.
275 liquidx 1.1
276 arfrever 1.97 if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
277     _PYTHON_USE_WITH_ATOMS_ARRAY=()
278     if [[ -n "${PYTHON_USE_WITH}" ]]; then
279     for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
280     _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${PYTHON_USE_WITH// /,}]")
281     done
282     elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
283     for _USE_flag in ${PYTHON_USE_WITH_OR}; do
284     for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
285     _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${_USE_flag}]")
286     done
287     done
288     unset _USE_flag
289     fi
290     if [[ "${#_PYTHON_USE_WITH_ATOMS_ARRAY[@]}" -gt 1 ]]; then
291     _PYTHON_USE_WITH_ATOMS="|| ( ${_PYTHON_USE_WITH_ATOMS_ARRAY[@]} )"
292     else
293     _PYTHON_USE_WITH_ATOMS="${_PYTHON_USE_WITH_ATOMS_ARRAY[@]}"
294     fi
295     if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
296     _PYTHON_USE_WITH_ATOMS="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOMS} )"
297     fi
298     DEPEND+=" ${_PYTHON_USE_WITH_ATOMS}"
299     RDEPEND+=" ${_PYTHON_USE_WITH_ATOMS}"
300     unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOMS _PYTHON_USE_WITH_ATOMS_ARRAY
301     fi
302    
303     unset _PYTHON_ATOMS
304    
305     # ================================================================================================
306     # =================================== MISCELLANEOUS FUNCTIONS ====================================
307     # ================================================================================================
308    
309     _python_implementation() {
310     if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
311     return 0
312     elif [[ "${CATEGORY}/${PN}" == "dev-java/jython" ]]; then
313     return 0
314 djc 1.126 elif [[ "${CATEGORY}/${PN}" == "dev-python/pypy" ]]; then
315     return 0
316 arfrever 1.97 else
317     return 1
318     fi
319     }
320    
321 arfrever 1.101 _python_abi-specific_local_scope() {
322     [[ " ${FUNCNAME[@]:2} " =~ " "(_python_final_sanity_checks|python_execute_function|python_mod_optimize|python_mod_cleanup)" " ]]
323     }
324    
325 arfrever 1.97 _python_initialize_prefix_variables() {
326     if has "${EAPI:-0}" 0 1 2; then
327     if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
328     EROOT="${ROOT%/}${EPREFIX}/"
329     fi
330     if [[ -n "${D}" && -z "${ED}" ]]; then
331     ED="${D%/}${EPREFIX}/"
332     fi
333     fi
334     }
335    
336 arfrever 1.101 unset PYTHON_SANITY_CHECKS_EXECUTED PYTHON_SKIP_SANITY_CHECKS
337 arfrever 1.97
338     _python_initial_sanity_checks() {
339 arfrever 1.101 if [[ "$(declare -p PYTHON_SANITY_CHECKS_EXECUTED 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS_EXECUTED="* || " ${FUNCNAME[@]:1} " =~ " "(python_set_active_version|python_pkg_setup)" " && -z "${PYTHON_SKIP_SANITY_CHECKS}" ]]; then
340 arfrever 1.97 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
341     if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
342     eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
343     eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
344     die "'${EPREFIX}/usr/bin/python' is not valid symlink"
345     fi
346     if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
347     eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
348     eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
349     die "'${EPREFIX}/usr/bin/python-config' is not valid script"
350     fi
351     fi
352     }
353    
354     _python_final_sanity_checks() {
355 arfrever 1.101 if ! _python_implementation && [[ "$(declare -p PYTHON_SANITY_CHECKS_EXECUTED 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS_EXECUTED="* || " ${FUNCNAME[@]:1} " =~ " "(python_set_active_version|python_pkg_setup)" " && -z "${PYTHON_SKIP_SANITY_CHECKS}" ]]; then
356 arfrever 1.97 local PYTHON_ABI="${PYTHON_ABI}"
357     for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI}}; do
358     # Ensure that appropriate version of Python is installed.
359     if ! has_version "$(python_get_implementational_package)"; then
360     die "$(python_get_implementational_package) is not installed"
361     fi
362    
363     # Ensure that EPYTHON variable is respected.
364     if [[ "$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" != "${PYTHON_ABI}" ]]; then
365     eerror "Path to 'python': '$(type -p python)'"
366     eerror "ABI: '${ABI}'"
367     eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
368     eerror "EPYTHON: '$(PYTHON)'"
369     eerror "PYTHON_ABI: '${PYTHON_ABI}'"
370     eerror "Locally active version of Python: '$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")'"
371     die "'python' does not respect EPYTHON variable"
372     fi
373     done
374     fi
375 arfrever 1.101 PYTHON_SANITY_CHECKS_EXECUTED="1"
376 arfrever 1.97 }
377    
378 arfrever 1.102 # @ECLASS-VARIABLE: PYTHON_COLORS
379     # @DESCRIPTION:
380     # User-configurable colored output.
381     PYTHON_COLORS="${PYTHON_COLORS:-0}"
382    
383 arfrever 1.97 _python_set_color_variables() {
384 arfrever 1.102 if [[ "${PYTHON_COLORS}" != "0" && "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
385 arfrever 1.97 _BOLD=$'\e[1m'
386     _RED=$'\e[1;31m'
387     _GREEN=$'\e[1;32m'
388     _BLUE=$'\e[1;34m'
389     _CYAN=$'\e[1;36m'
390     _NORMAL=$'\e[0m'
391     else
392     _BOLD=
393     _RED=
394     _GREEN=
395     _BLUE=
396     _CYAN=
397     _NORMAL=
398     fi
399     }
400    
401 arfrever 1.101 _python_check_python_pkg_setup_execution() {
402     [[ " ${FUNCNAME[@]:1} " =~ " "(python_set_active_version|python_pkg_setup)" " ]] && return
403    
404     if ! has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_PKG_SETUP_EXECUTED}" ]]; then
405     die "python_pkg_setup() not called"
406     fi
407     }
408    
409 arfrever 1.85 # @FUNCTION: python_pkg_setup
410 arfrever 1.56 # @DESCRIPTION:
411 arfrever 1.97 # Perform sanity checks and initialize environment.
412     #
413 lxnay 1.129 # This function is exported in EAPI 2 and 3 when PYTHON_USE_WITH or PYTHON_USE_WITH_OR variable
414     # is set and always in EAPI >=4. Calling of this function is mandatory in EAPI >=4.
415 arfrever 1.97 python_pkg_setup() {
416 djc 1.114 if [[ "${EBUILD_PHASE}" != "setup" ]]; then
417     die "${FUNCNAME}() can be used only in pkg_setup() phase"
418     fi
419 arfrever 1.97
420 arfrever 1.101 if [[ "$#" -ne 0 ]]; then
421     die "${FUNCNAME}() does not accept arguments"
422     fi
423    
424 arfrever 1.106 export JYTHON_SYSTEM_CACHEDIR="1"
425     addwrite "${EPREFIX}/var/cache/jython"
426    
427 arfrever 1.97 if _python_package_supporting_installation_for_multiple_python_abis; then
428 arfrever 1.98 _python_calculate_PYTHON_ABIS
429 arfrever 1.97 export EPYTHON="$(PYTHON -f)"
430     else
431 arfrever 1.98 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
432 arfrever 1.97 fi
433 arfrever 1.85
434 lxnay 1.130 if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; then
435 lxnay 1.129 if [[ "${PYTHON_USE_WITH_OPT}" ]]; then
436     if [[ "${PYTHON_USE_WITH_OPT}" == !* ]]; then
437     use ${PYTHON_USE_WITH_OPT#!} && return
438     else
439     use !${PYTHON_USE_WITH_OPT} && return
440     fi
441     fi
442    
443     python_pkg_setup_check_USE_flags() {
444     local python_atom USE_flag
445     python_atom="$(python_get_implementational_package)"
446    
447     for USE_flag in ${PYTHON_USE_WITH}; do
448     if ! has_version "${python_atom}[${USE_flag}]"; then
449     eerror "Please rebuild ${python_atom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
450     die "Please rebuild ${python_atom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
451     fi
452     done
453    
454     for USE_flag in ${PYTHON_USE_WITH_OR}; do
455     if has_version "${python_atom}[${USE_flag}]"; then
456     return
457     fi
458     done
459    
460     if [[ ${PYTHON_USE_WITH_OR} ]]; then
461     eerror "Please rebuild ${python_atom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
462     die "Please rebuild ${python_atom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
463     fi
464     }
465    
466     if _python_package_supporting_installation_for_multiple_python_abis; then
467     PYTHON_SKIP_SANITY_CHECKS="1" python_execute_function -q python_pkg_setup_check_USE_flags
468     else
469     python_pkg_setup_check_USE_flags
470     fi
471    
472     unset -f python_pkg_setup_check_USE_flags
473     fi
474    
475 arfrever 1.97 PYTHON_PKG_SETUP_EXECUTED="1"
476     }
477 arfrever 1.85
478 djc 1.126 _PYTHON_SHEBANG_BASE_PART_REGEX='^#![[:space:]]*([^[:space:]]*/usr/bin/env[[:space:]]+)?([^[:space:]]*/)?(jython|pypy-c|python)'
479 arfrever 1.106
480 arfrever 1.97 # @FUNCTION: python_convert_shebangs
481 arfrever 1.106 # @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_ABI|Python_version> <file|directory> [files|directories]
482 arfrever 1.97 # @DESCRIPTION:
483     # Convert shebangs in specified files. Directories can be specified only with --recursive option.
484     python_convert_shebangs() {
485 arfrever 1.101 _python_check_python_pkg_setup_execution
486    
487 arfrever 1.106 local argument file files=() only_executables="0" python_interpreter quiet="0" recursive="0"
488 arfrever 1.97
489     while (($#)); do
490     case "$1" in
491     -r|--recursive)
492     recursive="1"
493     ;;
494     -q|--quiet)
495     quiet="1"
496     ;;
497     -x|--only-executables)
498     only_executables="1"
499     ;;
500     --)
501     shift
502     break
503     ;;
504     -*)
505     die "${FUNCNAME}(): Unrecognized option '$1'"
506     ;;
507     *)
508     break
509     ;;
510     esac
511     shift
512     done
513    
514     if [[ "$#" -eq 0 ]]; then
515     die "${FUNCNAME}(): Missing Python version and files or directories"
516     elif [[ "$#" -eq 1 ]]; then
517     die "${FUNCNAME}(): Missing files or directories"
518     fi
519    
520 arfrever 1.106 if [[ -n "$(_python_get_implementation --ignore-invalid "$1")" ]]; then
521     python_interpreter="$(PYTHON "$1")"
522     else
523     python_interpreter="python$1"
524     fi
525 arfrever 1.97 shift
526    
527     for argument in "$@"; do
528     if [[ ! -e "${argument}" ]]; then
529     die "${FUNCNAME}(): '${argument}' does not exist"
530     elif [[ -f "${argument}" ]]; then
531     files+=("${argument}")
532     elif [[ -d "${argument}" ]]; then
533     if [[ "${recursive}" == "1" ]]; then
534     while read -d $'\0' -r file; do
535     files+=("${file}")
536     done < <(find "${argument}" $([[ "${only_executables}" == "1" ]] && echo -perm /111) -type f -print0)
537     else
538     die "${FUNCNAME}(): '${argument}' is not a regular file"
539     fi
540     else
541     die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
542     fi
543     done
544    
545     for file in "${files[@]}"; do
546     file="${file#./}"
547     [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
548    
549 arfrever 1.106 if [[ "$(head -n1 "${file}")" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX} ]]; then
550 arfrever 1.97 [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue
551    
552     if [[ "${quiet}" == "0" ]]; then
553     einfo "Converting shebang in '${file}'"
554     fi
555    
556 djc 1.126 sed -e "1s:^#![[:space:]]*\([^[:space:]]*/usr/bin/env[[:space:]]\)\?[[:space:]]*\([^[:space:]]*/\)\?\(jython\|pypy-c\|python\)\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?\(\$\|[[:space:]].*\):#!\1\2${python_interpreter}\6:" -i "${file}" || die "Conversion of shebang in '${file}' failed"
557 arfrever 1.97 fi
558     done
559     }
560    
561     # @FUNCTION: python_clean_installation_image
562     # @USAGE: [-q|--quiet]
563     # @DESCRIPTION:
564     # Delete needless files in installation image.
565 djc 1.114 #
566     # This function can be used only in src_install() phase.
567 arfrever 1.97 python_clean_installation_image() {
568 djc 1.114 if [[ "${EBUILD_PHASE}" != "install" ]]; then
569     die "${FUNCNAME}() can be used only in src_install() phase"
570     fi
571    
572 arfrever 1.101 _python_check_python_pkg_setup_execution
573 arfrever 1.97 _python_initialize_prefix_variables
574    
575     local file files=() quiet="0"
576    
577     while (($#)); do
578     case "$1" in
579     -q|--quiet)
580     quiet="1"
581     ;;
582     -*)
583     die "${FUNCNAME}(): Unrecognized option '$1'"
584     ;;
585     *)
586     die "${FUNCNAME}(): Invalid usage"
587     ;;
588     esac
589     shift
590     done
591 arfrever 1.56
592 arfrever 1.97 while read -d $'\0' -r file; do
593     files+=("${file}")
594     done < <(find "${ED}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -type f -print0)
595    
596     if [[ "${#files[@]}" -gt 0 ]]; then
597     if [[ "${quiet}" == "0" ]]; then
598     ewarn "Deleting byte-compiled Python modules needlessly generated by build system:"
599     fi
600     for file in "${files[@]}"; do
601     if [[ "${quiet}" == "0" ]]; then
602     ewarn " ${file}"
603     fi
604     rm -f "${file}"
605 arfrever 1.98
606     # Delete empty __pycache__ directories.
607     if [[ "${file%/*}" == *"/__pycache__" ]]; then
608     rmdir "${file%/*}" 2> /dev/null
609     fi
610 arfrever 1.85 done
611 arfrever 1.92 fi
612 arfrever 1.97
613     python_clean_sitedirs() {
614 arfrever 1.98 if [[ -d "${ED}$(python_get_sitedir)" ]]; then
615     find "${ED}$(python_get_sitedir)" "(" -name "*.c" -o -name "*.h" -o -name "*.la" ")" -type f -print0 | xargs -0 rm -f
616     fi
617 arfrever 1.97 }
618     if _python_package_supporting_installation_for_multiple_python_abis; then
619     python_execute_function -q python_clean_sitedirs
620 arfrever 1.92 else
621 arfrever 1.97 python_clean_sitedirs
622 arfrever 1.78 fi
623 arfrever 1.56
624 arfrever 1.97 unset -f python_clean_sitedirs
625     }
626 arfrever 1.87
627 arfrever 1.85 # ================================================================================================
628 arfrever 1.95 # =========== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ============
629 arfrever 1.85 # ================================================================================================
630 arfrever 1.84
631 arfrever 1.86 # @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
632     # @DESCRIPTION:
633 arfrever 1.88 # Set this in EAPI <= 4 to indicate that current package supports installation for
634 arfrever 1.95 # multiple Python ABIs.
635 arfrever 1.86
636 djc 1.116 # @ECLASS-VARIABLE: PYTHON_TESTS_RESTRICTED_ABIS
637     # @DESCRIPTION:
638     # Space-separated list of Python ABI patterns. Testing in Python ABIs matching any Python ABI
639     # patterns specified in this list is skipped.
640    
641 arfrever 1.90 # @ECLASS-VARIABLE: PYTHON_EXPORT_PHASE_FUNCTIONS
642 arfrever 1.82 # @DESCRIPTION:
643 arfrever 1.90 # Set this to export phase functions for the following ebuild phases:
644 djc 1.114 # src_prepare(), src_configure(), src_compile(), src_test(), src_install().
645 arfrever 1.94 if ! has "${EAPI:-0}" 0 1; then
646 arfrever 1.85 python_src_prepare() {
647 djc 1.114 if [[ "${EBUILD_PHASE}" != "prepare" ]]; then
648     die "${FUNCNAME}() can be used only in src_prepare() phase"
649     fi
650 arfrever 1.101
651 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
652     die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
653     fi
654    
655 djc 1.114 _python_check_python_pkg_setup_execution
656    
657 arfrever 1.101 if [[ "$#" -ne 0 ]]; then
658     die "${FUNCNAME}() does not accept arguments"
659     fi
660    
661 arfrever 1.85 python_copy_sources
662     }
663 arfrever 1.82
664 djc 1.113 for python_default_function in src_configure src_compile src_test; do
665 arfrever 1.94 eval "python_${python_default_function}() {
666 djc 1.114 if [[ \"\${EBUILD_PHASE}\" != \"${python_default_function#src_}\" ]]; then
667     die \"\${FUNCNAME}() can be used only in ${python_default_function}() phase\"
668     fi
669 arfrever 1.101
670 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
671     die \"\${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs\"
672     fi
673    
674 djc 1.114 _python_check_python_pkg_setup_execution
675    
676 arfrever 1.97 python_execute_function -d -s -- \"\$@\"
677 arfrever 1.94 }"
678 arfrever 1.85 done
679     unset python_default_function
680 arfrever 1.82
681 djc 1.113 python_src_install() {
682 djc 1.114 if [[ "${EBUILD_PHASE}" != "install" ]]; then
683     die "${FUNCNAME}() can be used only in src_install() phase"
684     fi
685    
686 djc 1.113 if ! _python_package_supporting_installation_for_multiple_python_abis; then
687     die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
688     fi
689    
690     _python_check_python_pkg_setup_execution
691    
692     if has "${EAPI:-0}" 0 1 2 3; then
693     python_execute_function -d -s -- "$@"
694     else
695     python_installation() {
696     emake DESTDIR="${T}/images/${PYTHON_ABI}" install "$@"
697     }
698     python_execute_function -s python_installation "$@"
699     unset python_installation
700    
701     python_merge_intermediate_installation_images "${T}/images"
702     fi
703     }
704    
705 arfrever 1.90 if [[ -n "${PYTHON_EXPORT_PHASE_FUNCTIONS}" ]]; then
706     EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
707     fi
708 arfrever 1.85 fi
709 arfrever 1.82
710 arfrever 1.110 if has "${EAPI:-0}" 0 1 2 3 4; then
711 arfrever 1.104 unset PYTHON_ABIS
712     fi
713 arfrever 1.77
714 arfrever 1.98 _python_calculate_PYTHON_ABIS() {
715 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
716     die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
717 arfrever 1.66 fi
718    
719 arfrever 1.90 _python_initial_sanity_checks
720 arfrever 1.57
721 arfrever 1.110 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
722 djc 1.117 local PYTHON_ABI
723 arfrever 1.68
724 arfrever 1.70 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
725 arfrever 1.94 local cpython_enabled="0"
726 arfrever 1.75
727 arfrever 1.68 if [[ -z "${USE_PYTHON}" ]]; then
728     die "USE_PYTHON variable is empty"
729 arfrever 1.56 fi
730 arfrever 1.68
731 arfrever 1.78 for PYTHON_ABI in ${USE_PYTHON}; do
732 arfrever 1.106 if ! has "${PYTHON_ABI}" "${_PYTHON_GLOBALLY_SUPPORTED_ABIS[@]}"; then
733 arfrever 1.78 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
734 arfrever 1.68 fi
735 arfrever 1.75
736 arfrever 1.106 if has "${PYTHON_ABI}" "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}" "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; then
737 arfrever 1.94 cpython_enabled="1"
738 arfrever 1.75 fi
739    
740 djc 1.117 if ! _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${RESTRICT_PYTHON_ABIS}"; then
741     export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
742     fi
743 arfrever 1.68 done
744    
745     if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
746 arfrever 1.95 die "USE_PYTHON variable does not enable any Python ABI supported by ${CATEGORY}/${PF}"
747 arfrever 1.66 fi
748 arfrever 1.75
749 arfrever 1.94 if [[ "${cpython_enabled}" == "0" ]]; then
750 arfrever 1.95 die "USE_PYTHON variable does not enable any CPython ABI"
751 arfrever 1.88 fi
752 arfrever 1.68 else
753 djc 1.114 local python_version python2_version python3_version support_python_major_version
754 arfrever 1.76
755 arfrever 1.97 if ! has_version "dev-lang/python"; then
756     die "${FUNCNAME}(): 'dev-lang/python' is not installed"
757     fi
758    
759 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]))')"
760 arfrever 1.74
761     if has_version "=dev-lang/python-2*"; then
762 arfrever 1.87 if [[ "$(readlink "${EPREFIX}/usr/bin/python2")" != "python2."* ]]; then
763     die "'${EPREFIX}/usr/bin/python2' is not valid symlink"
764 arfrever 1.74 fi
765    
766 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]))')"
767 arfrever 1.74
768 djc 1.117 support_python_major_version="0"
769 arfrever 1.106 for PYTHON_ABI in "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}"; do
770 djc 1.117 if ! _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${RESTRICT_PYTHON_ABIS}"; then
771     support_python_major_version="1"
772     break
773     fi
774 arfrever 1.74 done
775     if [[ "${support_python_major_version}" == "1" ]]; then
776 djc 1.117 if _python_check_python_abi_matching --patterns-list "${python2_version}" "${RESTRICT_PYTHON_ABIS}"; then
777     die "Active version of CPython 2 is not supported by ${CATEGORY}/${PF}"
778     fi
779 arfrever 1.74 else
780     python2_version=""
781     fi
782     fi
783 arfrever 1.56
784 arfrever 1.74 if has_version "=dev-lang/python-3*"; then
785 arfrever 1.87 if [[ "$(readlink "${EPREFIX}/usr/bin/python3")" != "python3."* ]]; then
786     die "'${EPREFIX}/usr/bin/python3' is not valid symlink"
787 arfrever 1.68 fi
788 arfrever 1.74
789 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]))')"
790 arfrever 1.74
791 djc 1.117 support_python_major_version="0"
792 arfrever 1.106 for PYTHON_ABI in "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; do
793 djc 1.117 if ! _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${RESTRICT_PYTHON_ABIS}"; then
794     support_python_major_version="1"
795     break
796     fi
797 arfrever 1.74 done
798     if [[ "${support_python_major_version}" == "1" ]]; then
799 djc 1.117 if _python_check_python_abi_matching --patterns-list "${python3_version}" "${RESTRICT_PYTHON_ABIS}"; then
800     die "Active version of CPython 3 is not supported by ${CATEGORY}/${PF}"
801     fi
802 arfrever 1.74 else
803     python3_version=""
804     fi
805     fi
806    
807 arfrever 1.82 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
808 arfrever 1.87 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python2' symlink"
809 arfrever 1.82 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
810     die "Incorrect configuration of Python"
811     fi
812     if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
813 arfrever 1.87 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python3' symlink"
814 arfrever 1.76 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
815     die "Incorrect configuration of Python"
816     fi
817    
818 arfrever 1.74 PYTHON_ABIS="${python2_version} ${python3_version}"
819     PYTHON_ABIS="${PYTHON_ABIS# }"
820     export PYTHON_ABIS="${PYTHON_ABIS% }"
821 arfrever 1.68 fi
822 arfrever 1.56 fi
823 arfrever 1.66
824 arfrever 1.90 _python_final_sanity_checks
825 arfrever 1.56 }
826    
827 arfrever 1.101 _python_prepare_flags() {
828     local array=() deleted_flag element flags new_value old_flag old_value operator pattern prefix variable
829    
830     for variable in CPPFLAGS CFLAGS CXXFLAGS LDFLAGS; do
831     eval "_PYTHON_SAVED_${variable}=\"\${!variable}\""
832     for prefix in PYTHON_USER_ PYTHON_; do
833     if [[ "$(declare -p ${prefix}${variable} 2> /dev/null)" == "declare -a ${prefix}${variable}="* ]]; then
834     eval "array=(\"\${${prefix}${variable}[@]}\")"
835     for element in "${array[@]}"; do
836 djc 1.120 if [[ "${element}" =~ ^${_PYTHON_ABI_PATTERN_REGEX}\ (\+|-)\ .+ ]]; then
837 arfrever 1.101 pattern="${element%% *}"
838     element="${element#* }"
839     operator="${element%% *}"
840     flags="${element#* }"
841 arfrever 1.106 if _python_check_python_abi_matching "${PYTHON_ABI}" "${pattern}"; then
842 arfrever 1.101 if [[ "${operator}" == "+" ]]; then
843     eval "export ${variable}+=\"\${variable:+ }${flags}\""
844     elif [[ "${operator}" == "-" ]]; then
845     flags="${flags// /$'\n'}"
846     old_value="${!variable// /$'\n'}"
847     new_value=""
848     while read old_flag; do
849     while read deleted_flag; do
850     if [[ "${old_flag}" == ${deleted_flag} ]]; then
851     continue 2
852     fi
853     done <<< "${flags}"
854     new_value+="${new_value:+ }${old_flag}"
855     done <<< "${old_value}"
856     eval "export ${variable}=\"\${new_value}\""
857     fi
858     fi
859     else
860     die "Element '${element}' of ${prefix}${variable} array has invalid syntax"
861     fi
862     done
863     elif [[ -n "$(declare -p ${prefix}${variable} 2> /dev/null)" ]]; then
864     die "${prefix}${variable} should be indexed array"
865     fi
866     done
867     done
868     }
869    
870     _python_restore_flags() {
871     local variable
872    
873     for variable in CPPFLAGS CFLAGS CXXFLAGS LDFLAGS; do
874     eval "${variable}=\"\${_PYTHON_SAVED_${variable}}\""
875     unset _PYTHON_SAVED_${variable}
876     done
877     }
878    
879 arfrever 1.56 # @FUNCTION: python_execute_function
880 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]
881 arfrever 1.56 # @DESCRIPTION:
882 arfrever 1.60 # Execute specified function for each value of PYTHON_ABIS, optionally passing additional
883     # arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
884 arfrever 1.56 python_execute_function() {
885 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
886     die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
887     fi
888    
889 djc 1.114 _python_check_python_pkg_setup_execution
890 arfrever 1.90 _python_set_color_variables
891    
892 djc 1.114 local action action_message action_message_template default_function="0" failure_message failure_message_template final_ABI="0" function iterated_PYTHON_ABIS nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" return_code separate_build_dirs="0" source_dir
893 arfrever 1.56
894     while (($#)); do
895     case "$1" in
896     --action-message)
897     action_message_template="$2"
898     shift
899     ;;
900 arfrever 1.58 -d|--default-function)
901     default_function="1"
902     ;;
903 arfrever 1.56 --failure-message)
904     failure_message_template="$2"
905     shift
906     ;;
907 arfrever 1.90 -f|--final-ABI)
908     final_ABI="1"
909     ;;
910 arfrever 1.56 --nonfatal)
911     nonfatal="1"
912     ;;
913     -q|--quiet)
914     quiet="1"
915     ;;
916     -s|--separate-build-dirs)
917     separate_build_dirs="1"
918     ;;
919 arfrever 1.81 --source-dir)
920     source_dir="$2"
921     shift
922     ;;
923 arfrever 1.71 --)
924 arfrever 1.87 shift
925 arfrever 1.71 break
926     ;;
927 arfrever 1.56 -*)
928 arfrever 1.67 die "${FUNCNAME}(): Unrecognized option '$1'"
929 arfrever 1.56 ;;
930     *)
931     break
932     ;;
933     esac
934     shift
935     done
936    
937 arfrever 1.81 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
938     die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
939     fi
940    
941 arfrever 1.58 if [[ "${default_function}" == "0" ]]; then
942 arfrever 1.83 if [[ "$#" -eq 0 ]]; then
943 arfrever 1.58 die "${FUNCNAME}(): Missing function name"
944     fi
945     function="$1"
946     shift
947 arfrever 1.67
948     if [[ -z "$(type -t "${function}")" ]]; then
949 arfrever 1.84 die "${FUNCNAME}(): '${function}' function is not defined"
950 arfrever 1.67 fi
951 arfrever 1.58 else
952     if has "${EAPI:-0}" 0 1; then
953 arfrever 1.67 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
954 arfrever 1.58 fi
955    
956     if [[ "${EBUILD_PHASE}" == "configure" ]]; then
957 arfrever 1.82 if has "${EAPI}" 2 3; then
958 arfrever 1.61 python_default_function() {
959 arfrever 1.94 econf "$@"
960 arfrever 1.61 }
961     else
962     python_default_function() {
963 arfrever 1.94 nonfatal econf "$@"
964 arfrever 1.61 }
965     fi
966 arfrever 1.58 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
967     python_default_function() {
968 arfrever 1.94 emake "$@"
969 arfrever 1.58 }
970     elif [[ "${EBUILD_PHASE}" == "test" ]]; then
971     python_default_function() {
972     if emake -j1 -n check &> /dev/null; then
973 arfrever 1.94 emake -j1 check "$@"
974 arfrever 1.58 elif emake -j1 -n test &> /dev/null; then
975 arfrever 1.94 emake -j1 test "$@"
976 arfrever 1.58 fi
977     }
978     elif [[ "${EBUILD_PHASE}" == "install" ]]; then
979     python_default_function() {
980 arfrever 1.94 emake DESTDIR="${D}" install "$@"
981 arfrever 1.58 }
982     else
983 arfrever 1.82 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
984 arfrever 1.58 fi
985     function="python_default_function"
986 arfrever 1.56 fi
987    
988 arfrever 1.95 # Ensure that python_execute_function() cannot be directly or indirectly called by python_execute_function().
989 arfrever 1.101 if _python_abi-specific_local_scope; then
990     die "${FUNCNAME}(): Invalid call stack"
991     fi
992 arfrever 1.87
993 arfrever 1.56 if [[ "${quiet}" == "0" ]]; then
994     [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
995     [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
996     [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
997     [[ "${EBUILD_PHASE}" == "configure" ]] && action="Configuration"
998     [[ "${EBUILD_PHASE}" == "compile" ]] && action="Building"
999     [[ "${EBUILD_PHASE}" == "test" ]] && action="Testing"
1000     [[ "${EBUILD_PHASE}" == "install" ]] && action="Installation"
1001     [[ "${EBUILD_PHASE}" == "preinst" ]] && action="Preinstallation"
1002     [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
1003     [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
1004     [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
1005     fi
1006    
1007 arfrever 1.98 _python_calculate_PYTHON_ABIS
1008 arfrever 1.90 if [[ "${final_ABI}" == "1" ]]; then
1009     iterated_PYTHON_ABIS="$(PYTHON -f --ABI)"
1010 arfrever 1.56 else
1011 arfrever 1.90 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
1012 arfrever 1.56 fi
1013 arfrever 1.90 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
1014 djc 1.116 if [[ "${EBUILD_PHASE}" == "test" ]] && _python_check_python_abi_matching --patterns-list "${PYTHON_ABI}" "${PYTHON_TESTS_RESTRICTED_ABIS}"; then
1015     if [[ "${quiet}" == "0" ]]; then
1016 djc 1.124 echo " ${_GREEN}*${_NORMAL} ${_BLUE}Testing of ${CATEGORY}/${PF} with $(python_get_implementation_and_version) skipped${_NORMAL}"
1017 djc 1.116 fi
1018     continue
1019     fi
1020    
1021 arfrever 1.101 _python_prepare_flags
1022    
1023 arfrever 1.56 if [[ "${quiet}" == "0" ]]; then
1024     if [[ -n "${action_message_template}" ]]; then
1025 arfrever 1.103 eval "action_message=\"${action_message_template}\""
1026 arfrever 1.56 else
1027 djc 1.124 action_message="${action} of ${CATEGORY}/${PF} with $(python_get_implementation_and_version)..."
1028 arfrever 1.56 fi
1029 arfrever 1.90 echo " ${_GREEN}*${_NORMAL} ${_BLUE}${action_message}${_NORMAL}"
1030 arfrever 1.56 fi
1031 arfrever 1.66
1032 arfrever 1.56 if [[ "${separate_build_dirs}" == "1" ]]; then
1033 arfrever 1.81 if [[ -n "${source_dir}" ]]; then
1034     export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
1035     else
1036     export BUILDDIR="${S}-${PYTHON_ABI}"
1037     fi
1038 arfrever 1.60 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
1039     else
1040     export BUILDDIR="${S}"
1041 arfrever 1.56 fi
1042 arfrever 1.66
1043 arfrever 1.73 previous_directory="$(pwd)"
1044     previous_directory_stack="$(dirs -p)"
1045     previous_directory_stack_length="$(dirs -p | wc -l)"
1046 arfrever 1.72
1047 arfrever 1.82 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
1048 arfrever 1.66 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
1049     else
1050     EPYTHON="$(PYTHON)" "${function}" "$@"
1051     fi
1052    
1053 arfrever 1.101 return_code="$?"
1054    
1055     _python_restore_flags
1056    
1057     if [[ "${return_code}" -ne 0 ]]; then
1058 arfrever 1.56 if [[ -n "${failure_message_template}" ]]; then
1059 arfrever 1.103 eval "failure_message=\"${failure_message_template}\""
1060 arfrever 1.56 else
1061 djc 1.124 failure_message="${action} failed with $(python_get_implementation_and_version) in ${function}() function"
1062 arfrever 1.56 fi
1063 arfrever 1.66
1064 arfrever 1.69 if [[ "${nonfatal}" == "1" ]]; then
1065     if [[ "${quiet}" == "0" ]]; then
1066 arfrever 1.102 ewarn "${failure_message}"
1067 arfrever 1.69 fi
1068 arfrever 1.90 elif [[ "${final_ABI}" == "0" ]] && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
1069 arfrever 1.70 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
1070 arfrever 1.78 local enabled_PYTHON_ABIS= other_PYTHON_ABI
1071     for other_PYTHON_ABI in ${PYTHON_ABIS}; do
1072 arfrever 1.82 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
1073 arfrever 1.70 done
1074 arfrever 1.82 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
1075 arfrever 1.70 fi
1076 arfrever 1.56 if [[ "${quiet}" == "0" ]]; then
1077 arfrever 1.102 ewarn "${failure_message}"
1078 arfrever 1.56 fi
1079 arfrever 1.69 if [[ -z "${PYTHON_ABIS}" ]]; then
1080 arfrever 1.95 die "${function}() function failed with all enabled Python ABIs"
1081 arfrever 1.69 fi
1082 arfrever 1.56 else
1083     die "${failure_message}"
1084     fi
1085     fi
1086 arfrever 1.66
1087 arfrever 1.84 # Ensure that directory stack has not been decreased.
1088 arfrever 1.73 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
1089 arfrever 1.72 die "Directory stack decreased illegally"
1090     fi
1091    
1092 arfrever 1.73 # Avoid side effects of earlier returning from the specified function.
1093     while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
1094 arfrever 1.72 popd > /dev/null || die "popd failed"
1095     done
1096    
1097 arfrever 1.84 # Ensure that the bottom part of directory stack has not been changed. Restore
1098 arfrever 1.73 # previous directory (from before running of the specified function) before
1099     # comparison of directory stacks to avoid mismatch of directory stacks after
1100     # potential using of 'cd' to change current directory. Restoration of previous
1101     # directory allows to safely use 'cd' to change current directory in the
1102     # specified function without changing it back to original directory.
1103     cd "${previous_directory}"
1104     if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
1105     die "Directory stack changed illegally"
1106     fi
1107    
1108 arfrever 1.56 if [[ "${separate_build_dirs}" == "1" ]]; then
1109     popd > /dev/null || die "popd failed"
1110     fi
1111 arfrever 1.60 unset BUILDDIR
1112 arfrever 1.56 done
1113 arfrever 1.58
1114     if [[ "${default_function}" == "1" ]]; then
1115     unset -f python_default_function
1116     fi
1117 arfrever 1.56 }
1118    
1119 arfrever 1.85 # @FUNCTION: python_copy_sources
1120 arfrever 1.90 # @USAGE: <directory="${S}"> [directory]
1121 arfrever 1.77 # @DESCRIPTION:
1122 arfrever 1.88 # Copy unpacked sources of current package to separate build directory for each Python ABI.
1123 arfrever 1.85 python_copy_sources() {
1124 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1125     die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1126     fi
1127    
1128 djc 1.114 _python_check_python_pkg_setup_execution
1129    
1130 arfrever 1.90 local dir dirs=() PYTHON_ABI
1131 arfrever 1.77
1132     if [[ "$#" -eq 0 ]]; then
1133 arfrever 1.85 if [[ "${WORKDIR}" == "${S}" ]]; then
1134 arfrever 1.97 die "${FUNCNAME}() cannot be used with current value of S variable"
1135 arfrever 1.85 fi
1136 arfrever 1.94 dirs=("${S%/}")
1137 arfrever 1.85 else
1138 arfrever 1.90 dirs=("$@")
1139 arfrever 1.77 fi
1140    
1141 arfrever 1.98 _python_calculate_PYTHON_ABIS
1142 arfrever 1.85 for PYTHON_ABI in ${PYTHON_ABIS}; do
1143     for dir in "${dirs[@]}"; do
1144 arfrever 1.90 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
1145 arfrever 1.85 done
1146 arfrever 1.77 done
1147 arfrever 1.85 }
1148 arfrever 1.77
1149 arfrever 1.82 # @FUNCTION: python_generate_wrapper_scripts
1150     # @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
1151     # @DESCRIPTION:
1152     # Generate wrapper scripts. Existing files are overwritten only with --force option.
1153     # If --respect-EPYTHON option is specified, then generated wrapper scripts will
1154     # respect EPYTHON variable at run time.
1155 djc 1.114 #
1156     # This function can be used only in src_install() phase.
1157 arfrever 1.82 python_generate_wrapper_scripts() {
1158 djc 1.114 if [[ "${EBUILD_PHASE}" != "install" ]]; then
1159     die "${FUNCNAME}() can be used only in src_install() phase"
1160     fi
1161 arfrever 1.101
1162 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1163     die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1164     fi
1165    
1166 djc 1.114 _python_check_python_pkg_setup_execution
1167 arfrever 1.87 _python_initialize_prefix_variables
1168    
1169 arfrever 1.106 local eselect_python_option file force="0" quiet="0" PYTHON_ABI PYTHON_ABIS_list python2_enabled="0" python3_enabled="0" respect_EPYTHON="0"
1170 arfrever 1.82
1171     while (($#)); do
1172     case "$1" in
1173     -E|--respect-EPYTHON)
1174     respect_EPYTHON="1"
1175     ;;
1176     -f|--force)
1177     force="1"
1178     ;;
1179     -q|--quiet)
1180     quiet="1"
1181     ;;
1182     --)
1183 arfrever 1.87 shift
1184 arfrever 1.82 break
1185     ;;
1186     -*)
1187     die "${FUNCNAME}(): Unrecognized option '$1'"
1188     ;;
1189     *)
1190     break
1191     ;;
1192     esac
1193     shift
1194     done
1195    
1196     if [[ "$#" -eq 0 ]]; then
1197     die "${FUNCNAME}(): Missing arguments"
1198     fi
1199    
1200 arfrever 1.98 _python_calculate_PYTHON_ABIS
1201 arfrever 1.106 for PYTHON_ABI in "${_CPYTHON2_GLOBALLY_SUPPORTED_ABIS[@]}"; do
1202 arfrever 1.82 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
1203     python2_enabled="1"
1204     fi
1205     done
1206 arfrever 1.106 for PYTHON_ABI in "${_CPYTHON3_GLOBALLY_SUPPORTED_ABIS[@]}"; do
1207 arfrever 1.82 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
1208     python3_enabled="1"
1209     fi
1210     done
1211    
1212     if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
1213     eselect_python_option=
1214     elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
1215     eselect_python_option="--python2"
1216     elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
1217     eselect_python_option="--python3"
1218     else
1219     die "${FUNCNAME}(): Unsupported environment"
1220     fi
1221    
1222 arfrever 1.106 PYTHON_ABIS_list="$("$(PYTHON -f)" -c "print(', '.join('\"%s\"' % x for x in reversed('${PYTHON_ABIS}'.split())))")"
1223    
1224 arfrever 1.82 for file in "$@"; do
1225     if [[ -f "${file}" && "${force}" == "0" ]]; then
1226 arfrever 1.106 die "${FUNCNAME}(): '${file}' already exists"
1227 arfrever 1.82 fi
1228    
1229     if [[ "${quiet}" == "0" ]]; then
1230 arfrever 1.94 einfo "Generating '${file#${ED%/}}' wrapper script"
1231 arfrever 1.82 fi
1232    
1233     cat << EOF > "${file}"
1234     #!/usr/bin/env python
1235 arfrever 1.94 # Gentoo '${file##*/}' wrapper script generated by python_generate_wrapper_scripts()
1236 arfrever 1.82
1237     import os
1238     import re
1239     import subprocess
1240     import sys
1241    
1242 djc 1.135 cpython_ABI_re = re.compile(r"^(\d+\.\d+)$")
1243     jython_ABI_re = re.compile(r"^(\d+\.\d+)-jython$")
1244     pypy_ABI_re = re.compile(r"^\d+\.\d+-pypy-(\d+\.\d+)$")
1245     cpython_interpreter_re = re.compile(r"^python(\d+\.\d+)$")
1246     jython_interpreter_re = re.compile(r"^jython(\d+\.\d+)$")
1247     pypy_interpreter_re = re.compile(r"^pypy-c(\d+\.\d+)$")
1248 djc 1.133 cpython_shebang_re = re.compile(r"^#![ \t]*(?:${EPREFIX}/usr/bin/python|(?:${EPREFIX})?/usr/bin/env[ \t]+(?:${EPREFIX}/usr/bin/)?python)")
1249     python_shebang_options_re = re.compile(r"^#![ \t]*${EPREFIX}/usr/bin/(?:jython|pypy-c|python)(?:\d+(?:\.\d+)?)?[ \t]+(-\S)")
1250 arfrever 1.94 python_verification_output_re = re.compile("^GENTOO_PYTHON_TARGET_SCRIPT_PATH supported\n$")
1251 arfrever 1.82
1252 djc 1.126 pypy_versions_mapping = {
1253     "1.5": "2.7"
1254     }
1255    
1256 djc 1.135 def get_PYTHON_ABI(python_interpreter):
1257     cpython_matched = cpython_interpreter_re.match(python_interpreter)
1258     jython_matched = jython_interpreter_re.match(python_interpreter)
1259     pypy_matched = pypy_interpreter_re.match(python_interpreter)
1260 arfrever 1.106 if cpython_matched is not None:
1261     PYTHON_ABI = cpython_matched.group(1)
1262     elif jython_matched is not None:
1263     PYTHON_ABI = jython_matched.group(1) + "-jython"
1264 djc 1.126 elif pypy_matched is not None:
1265     PYTHON_ABI = pypy_versions_mapping[pypy_matched.group(1)] + "-pypy-" + pypy_matched.group(1)
1266 arfrever 1.106 else:
1267     PYTHON_ABI = None
1268     return PYTHON_ABI
1269    
1270 djc 1.135 def get_python_interpreter(PYTHON_ABI):
1271     cpython_matched = cpython_ABI_re.match(PYTHON_ABI)
1272     jython_matched = jython_ABI_re.match(PYTHON_ABI)
1273     pypy_matched = pypy_ABI_re.match(PYTHON_ABI)
1274     if cpython_matched is not None:
1275     python_interpreter = "python" + cpython_matched.group(1)
1276     elif jython_matched is not None:
1277     python_interpreter = "jython" + jython_matched.group(1)
1278     elif pypy_matched is not None:
1279     python_interpreter = "pypy-c" + pypy_matched.group(1)
1280     else:
1281     python_interpreter = None
1282     return python_interpreter
1283    
1284 arfrever 1.82 EOF
1285     if [[ "$?" != "0" ]]; then
1286     die "${FUNCNAME}(): Generation of '$1' failed"
1287     fi
1288     if [[ "${respect_EPYTHON}" == "1" ]]; then
1289     cat << EOF >> "${file}"
1290 djc 1.135 python_interpreter = os.environ.get("EPYTHON")
1291     if python_interpreter:
1292     PYTHON_ABI = get_PYTHON_ABI(python_interpreter)
1293 arfrever 1.106 if PYTHON_ABI is None:
1294 djc 1.135 sys.stderr.write("%s: EPYTHON variable has unrecognized value '%s'\n" % (sys.argv[0], python_interpreter))
1295 arfrever 1.82 sys.exit(1)
1296     else:
1297     try:
1298 djc 1.122 environment = os.environ.copy()
1299     environment["ROOT"] = "/"
1300     eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], env=environment, stdout=subprocess.PIPE)
1301 arfrever 1.82 if eselect_process.wait() != 0:
1302     raise ValueError
1303     except (OSError, ValueError):
1304 djc 1.123 sys.stderr.write("%s: Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n" % sys.argv[0])
1305 arfrever 1.82 sys.exit(1)
1306    
1307 djc 1.135 python_interpreter = eselect_process.stdout.read()
1308     if not isinstance(python_interpreter, str):
1309 arfrever 1.82 # Python 3
1310 djc 1.135 python_interpreter = python_interpreter.decode()
1311     python_interpreter = python_interpreter.rstrip("\n")
1312 arfrever 1.82
1313 djc 1.135 PYTHON_ABI = get_PYTHON_ABI(python_interpreter)
1314 arfrever 1.106 if PYTHON_ABI is None:
1315 djc 1.135 sys.stderr.write("%s: 'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % (sys.argv[0], python_interpreter))
1316 arfrever 1.82 sys.exit(1)
1317 arfrever 1.106
1318     wrapper_script_path = os.path.realpath(sys.argv[0])
1319     target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
1320     if not os.path.exists(target_executable_path):
1321 djc 1.123 sys.stderr.write("%s: '%s' does not exist\n" % (sys.argv[0], target_executable_path))
1322 arfrever 1.106 sys.exit(1)
1323 arfrever 1.82 EOF
1324     if [[ "$?" != "0" ]]; then
1325     die "${FUNCNAME}(): Generation of '$1' failed"
1326     fi
1327     else
1328     cat << EOF >> "${file}"
1329     try:
1330 djc 1.122 environment = os.environ.copy()
1331     environment["ROOT"] = "/"
1332     eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], env=environment, stdout=subprocess.PIPE)
1333 arfrever 1.82 if eselect_process.wait() != 0:
1334     raise ValueError
1335     except (OSError, ValueError):
1336 djc 1.123 sys.stderr.write("%s: Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n" % sys.argv[0])
1337 arfrever 1.82 sys.exit(1)
1338    
1339 djc 1.135 python_interpreter = eselect_process.stdout.read()
1340     if not isinstance(python_interpreter, str):
1341 arfrever 1.82 # Python 3
1342 djc 1.135 python_interpreter = python_interpreter.decode()
1343     python_interpreter = python_interpreter.rstrip("\n")
1344 arfrever 1.82
1345 djc 1.135 PYTHON_ABI = get_PYTHON_ABI(python_interpreter)
1346 arfrever 1.106 if PYTHON_ABI is None:
1347 djc 1.135 sys.stderr.write("%s: 'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s'\n" % (sys.argv[0], python_interpreter))
1348 arfrever 1.106 sys.exit(1)
1349    
1350     wrapper_script_path = os.path.realpath(sys.argv[0])
1351     for PYTHON_ABI in [PYTHON_ABI, ${PYTHON_ABIS_list}]:
1352     target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
1353     if os.path.exists(target_executable_path):
1354     break
1355 arfrever 1.82 else:
1356 djc 1.123 sys.stderr.write("%s: No target script exists for '%s'\n" % (sys.argv[0], wrapper_script_path))
1357 arfrever 1.82 sys.exit(1)
1358 djc 1.135
1359     python_interpreter = get_python_interpreter(PYTHON_ABI)
1360     if python_interpreter is None:
1361     sys.stderr.write("%s: Unrecognized Python ABI '%s'\n" % (sys.argv[0], PYTHON_ABI))
1362     sys.exit(1)
1363 arfrever 1.82 EOF
1364     if [[ "$?" != "0" ]]; then
1365     die "${FUNCNAME}(): Generation of '$1' failed"
1366     fi
1367     fi
1368     cat << EOF >> "${file}"
1369    
1370 arfrever 1.94 target_executable = open(target_executable_path, "rb")
1371     target_executable_first_line = target_executable.readline()
1372 djc 1.133 target_executable.close()
1373 arfrever 1.94 if not isinstance(target_executable_first_line, str):
1374     # Python 3
1375     target_executable_first_line = target_executable_first_line.decode("utf_8", "replace")
1376    
1377 djc 1.133 options = []
1378     python_shebang_options_matched = python_shebang_options_re.match(target_executable_first_line)
1379     if python_shebang_options_matched is not None:
1380     options = [python_shebang_options_matched.group(1)]
1381    
1382     cpython_shebang_matched = cpython_shebang_re.match(target_executable_first_line)
1383 arfrever 1.94
1384 djc 1.133 if cpython_shebang_matched is not None:
1385 arfrever 1.94 try:
1386 djc 1.135 python_interpreter_path = "${EPREFIX}/usr/bin/%s" % python_interpreter
1387 arfrever 1.94 os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"] = "1"
1388     python_verification_process = subprocess.Popen([python_interpreter_path, "-c", "pass"], stdout=subprocess.PIPE)
1389     del os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"]
1390     if python_verification_process.wait() != 0:
1391     raise ValueError
1392    
1393     python_verification_output = python_verification_process.stdout.read()
1394     if not isinstance(python_verification_output, str):
1395     # Python 3
1396     python_verification_output = python_verification_output.decode()
1397    
1398     if not python_verification_output_re.match(python_verification_output):
1399     raise ValueError
1400    
1401 djc 1.135 if cpython_interpreter_re.match(python_interpreter) is not None:
1402 arfrever 1.106 os.environ["GENTOO_PYTHON_PROCESS_NAME"] = os.path.basename(sys.argv[0])
1403     os.environ["GENTOO_PYTHON_WRAPPER_SCRIPT_PATH"] = sys.argv[0]
1404     os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH"] = target_executable_path
1405    
1406     if hasattr(os, "execv"):
1407 djc 1.133 os.execv(python_interpreter_path, [python_interpreter_path] + options + sys.argv)
1408 arfrever 1.106 else:
1409 djc 1.133 sys.exit(subprocess.Popen([python_interpreter_path] + options + sys.argv).wait())
1410 arfrever 1.106 except (KeyboardInterrupt, SystemExit):
1411     raise
1412 arfrever 1.94 except:
1413     pass
1414 arfrever 1.106 for variable in ("GENTOO_PYTHON_PROCESS_NAME", "GENTOO_PYTHON_WRAPPER_SCRIPT_PATH", "GENTOO_PYTHON_TARGET_SCRIPT_PATH", "GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"):
1415     if variable in os.environ:
1416     del os.environ[variable]
1417 arfrever 1.94
1418 arfrever 1.106 if hasattr(os, "execv"):
1419     os.execv(target_executable_path, sys.argv)
1420     else:
1421     sys.exit(subprocess.Popen([target_executable_path] + sys.argv[1:]).wait())
1422 arfrever 1.82 EOF
1423     if [[ "$?" != "0" ]]; then
1424     die "${FUNCNAME}(): Generation of '$1' failed"
1425     fi
1426 arfrever 1.87 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed"
1427 arfrever 1.82 done
1428     }
1429    
1430 arfrever 1.106 # @ECLASS-VARIABLE: PYTHON_VERSIONED_SCRIPTS
1431     # @DESCRIPTION:
1432     # Array of regular expressions of paths to versioned Python scripts.
1433     # Python scripts in /usr/bin and /usr/sbin are versioned by default.
1434    
1435     # @ECLASS-VARIABLE: PYTHON_VERSIONED_EXECUTABLES
1436     # @DESCRIPTION:
1437     # Array of regular expressions of paths to versioned executables (including Python scripts).
1438    
1439     # @ECLASS-VARIABLE: PYTHON_NONVERSIONED_EXECUTABLES
1440     # @DESCRIPTION:
1441     # Array of regular expressions of paths to nonversioned executables (including Python scripts).
1442    
1443     # @FUNCTION: python_merge_intermediate_installation_images
1444     # @USAGE: [-q|--quiet] [--] <intermediate_installation_images_directory>
1445     # @DESCRIPTION:
1446     # Merge intermediate installation images into installation image.
1447 djc 1.114 #
1448     # This function can be used only in src_install() phase.
1449 arfrever 1.106 python_merge_intermediate_installation_images() {
1450 djc 1.114 if [[ "${EBUILD_PHASE}" != "install" ]]; then
1451     die "${FUNCNAME}() can be used only in src_install() phase"
1452     fi
1453    
1454     if ! _python_package_supporting_installation_for_multiple_python_abis; then
1455     die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1456     fi
1457    
1458 arfrever 1.106 _python_check_python_pkg_setup_execution
1459     _python_initialize_prefix_variables
1460    
1461 djc 1.127 local absolute_file b file files=() intermediate_installation_images_directory PYTHON_ABI quiet="0" regex shebang version_executable wrapper_scripts=() wrapper_scripts_set=()
1462 arfrever 1.106
1463     while (($#)); do
1464     case "$1" in
1465     -q|--quiet)
1466     quiet="1"
1467     ;;
1468     --)
1469     shift
1470     break
1471     ;;
1472     -*)
1473     die "${FUNCNAME}(): Unrecognized option '$1'"
1474     ;;
1475     *)
1476     break
1477     ;;
1478     esac
1479     shift
1480     done
1481    
1482     if [[ "$#" -ne 1 ]]; then
1483     die "${FUNCNAME}() requires 1 argument"
1484     fi
1485    
1486     intermediate_installation_images_directory="$1"
1487    
1488     if [[ ! -d "${intermediate_installation_images_directory}" ]]; then
1489     die "${FUNCNAME}(): Intermediate installation images directory '${intermediate_installation_images_directory}' does not exist"
1490     fi
1491    
1492     _python_calculate_PYTHON_ABIS
1493     if [[ "$(PYTHON -f --ABI)" == 3.* ]]; then
1494     b="b"
1495     fi
1496    
1497     while read -d $'\0' -r file; do
1498     files+=("${file}")
1499     done < <("$(PYTHON -f)" -c \
1500     "import os
1501     import sys
1502    
1503     if hasattr(sys.stdout, 'buffer'):
1504     # Python 3
1505     stdout = sys.stdout.buffer
1506     else:
1507     # Python 2
1508     stdout = sys.stdout
1509    
1510     files_set = set()
1511    
1512     os.chdir(${b}'${intermediate_installation_images_directory}')
1513    
1514     for PYTHON_ABI in ${b}'${PYTHON_ABIS}'.split():
1515     for root, dirs, files in os.walk(PYTHON_ABI + ${b}'${EPREFIX}'):
1516     root = root[len(PYTHON_ABI + ${b}'${EPREFIX}')+1:]
1517     files_set.update(root + ${b}'/' + file for file in files)
1518    
1519     for file in sorted(files_set):
1520     stdout.write(file)
1521     stdout.write(${b}'\x00')" || die "${FUNCNAME}(): Failure of extraction of files in intermediate installation images")
1522    
1523     for PYTHON_ABI in ${PYTHON_ABIS}; do
1524     if [[ ! -d "${intermediate_installation_images_directory}/${PYTHON_ABI}" ]]; then
1525     die "${FUNCNAME}(): Intermediate installation image for Python ABI '${PYTHON_ABI}' does not exist"
1526     fi
1527    
1528     pushd "${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}" > /dev/null || die "pushd failed"
1529    
1530     for file in "${files[@]}"; do
1531     version_executable="0"
1532     for regex in "/usr/bin/.*" "/usr/sbin/.*" "${PYTHON_VERSIONED_SCRIPTS[@]}"; do
1533     if [[ "/${file}" =~ ^${regex}$ ]]; then
1534     version_executable="1"
1535     break
1536     fi
1537     done
1538     for regex in "${PYTHON_VERSIONED_EXECUTABLES[@]}"; do
1539     if [[ "/${file}" =~ ^${regex}$ ]]; then
1540     version_executable="2"
1541     break
1542     fi
1543     done
1544     if [[ "${version_executable}" != "0" ]]; then
1545     for regex in "${PYTHON_NONVERSIONED_EXECUTABLES[@]}"; do
1546     if [[ "/${file}" =~ ^${regex}$ ]]; then
1547     version_executable="0"
1548     break
1549     fi
1550     done
1551     fi
1552    
1553 djc 1.127 [[ "${version_executable}" == "0" ]] && continue
1554 arfrever 1.106
1555 djc 1.127 if [[ -L "${file}" ]]; then
1556     absolute_file="$(readlink "${file}")"
1557     if [[ "${absolute_file}" == /* ]]; then
1558     absolute_file="${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}/${absolute_file##/}"
1559     else
1560     if [[ "${file}" == */* ]]; then
1561     absolute_file="${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}/${file%/*}/${absolute_file}"
1562     else
1563     absolute_file="${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}/${absolute_file}"
1564     fi
1565     fi
1566     else
1567     absolute_file="${intermediate_installation_images_directory}/${PYTHON_ABI}${EPREFIX}/${file}"
1568     fi
1569    
1570     [[ ! -x "${absolute_file}" ]] && continue
1571    
1572     shebang="$(head -n1 "${absolute_file}")" || die "Extraction of shebang from '${absolute_file}' failed"
1573 arfrever 1.106
1574     if [[ "${version_executable}" == "2" ]]; then
1575     wrapper_scripts+=("${ED}${file}")
1576     elif [[ "${version_executable}" == "1" ]]; then
1577     if [[ "${shebang}" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX}([[:digit:]]+(\.[[:digit:]]+)?)?($|[[:space:]]+) ]]; then
1578     wrapper_scripts+=("${ED}${file}")
1579     else
1580     version_executable="0"
1581     fi
1582     fi
1583    
1584     [[ "${version_executable}" == "0" ]] && continue
1585    
1586     if [[ -e "${file}-${PYTHON_ABI}" ]]; then
1587     die "${FUNCNAME}(): '${EPREFIX}/${file}-${PYTHON_ABI}' already exists"
1588     fi
1589    
1590     mv "${file}" "${file}-${PYTHON_ABI}" || die "Renaming of '${file}' failed"
1591    
1592     if [[ "${shebang}" =~ ${_PYTHON_SHEBANG_BASE_PART_REGEX}[[:digit:]]*($|[[:space:]]+) ]]; then
1593 djc 1.127 if [[ -L "${file}-${PYTHON_ABI}" ]]; then
1594     python_convert_shebangs $([[ "${quiet}" == "1" ]] && echo --quiet) "${PYTHON_ABI}" "${absolute_file}"
1595     else
1596     python_convert_shebangs $([[ "${quiet}" == "1" ]] && echo --quiet) "${PYTHON_ABI}" "${file}-${PYTHON_ABI}"
1597     fi
1598 arfrever 1.106 fi
1599     done
1600    
1601     popd > /dev/null || die "popd failed"
1602    
1603 robbat2 1.142 # This is per bug #390691, without the duplication refactor, and with
1604     # the 3-way structure per comment #6. This enable users with old
1605     # coreutils to upgrade a lot easier (you need to upgrade python+portage
1606     # before coreutils can be upgraded).
1607 robbat2 1.143 if ROOT="/" has_version '>=sys-apps/coreutils-6.9.90'; then
1608 djc 1.134 cp -fr --preserve=all --no-preserve=context "${intermediate_installation_images_directory}/${PYTHON_ABI}/"* "${D}" || die "Merging of intermediate installation image for Python ABI '${PYTHON_ABI} into installation image failed"
1609 robbat2 1.142 elif ROOT="/" has_version sys-apps/coreutils; then
1610     cp -fr --preserve=all "${intermediate_installation_images_directory}/${PYTHON_ABI}/"* "${D}" || die "Merging of intermediate installation image for Python ABI '${PYTHON_ABI} into installation image failed"
1611 arfrever 1.109 else
1612     cp -fpr "${intermediate_installation_images_directory}/${PYTHON_ABI}/"* "${D}" || die "Merging of intermediate installation image for Python ABI '${PYTHON_ABI} into installation image failed"
1613     fi
1614 arfrever 1.106 done
1615    
1616 arfrever 1.107 rm -fr "${intermediate_installation_images_directory}"
1617    
1618 arfrever 1.106 if [[ "${#wrapper_scripts[@]}" -ge 1 ]]; then
1619     rm -f "${T}/python_wrapper_scripts"
1620    
1621     for file in "${wrapper_scripts[@]}"; do
1622     echo -n "${file}" >> "${T}/python_wrapper_scripts"
1623     echo -en "\x00" >> "${T}/python_wrapper_scripts"
1624     done
1625    
1626     while read -d $'\0' -r file; do
1627     wrapper_scripts_set+=("${file}")
1628     done < <("$(PYTHON -f)" -c \
1629     "import sys
1630    
1631     if hasattr(sys.stdout, 'buffer'):
1632     # Python 3
1633     stdout = sys.stdout.buffer
1634     else:
1635     # Python 2
1636     stdout = sys.stdout
1637    
1638 djc 1.137 python_wrapper_scripts_file = open('${T}/python_wrapper_scripts', 'rb')
1639     files = set(python_wrapper_scripts_file.read().rstrip(${b}'\x00').split(${b}'\x00'))
1640     python_wrapper_scripts_file.close()
1641 arfrever 1.106
1642     for file in sorted(files):
1643     stdout.write(file)
1644     stdout.write(${b}'\x00')" || die "${FUNCNAME}(): Failure of extraction of set of wrapper scripts")
1645    
1646     python_generate_wrapper_scripts $([[ "${quiet}" == "1" ]] && echo --quiet) "${wrapper_scripts_set[@]}"
1647     fi
1648     }
1649    
1650 arfrever 1.85 # ================================================================================================
1651 arfrever 1.95 # ========= FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ==========
1652 arfrever 1.85 # ================================================================================================
1653    
1654 arfrever 1.101 unset EPYTHON PYTHON_ABI
1655    
1656 arfrever 1.85 # @FUNCTION: python_set_active_version
1657 arfrever 1.106 # @USAGE: <Python_ABI|2|3>
1658 betelgeuse 1.55 # @DESCRIPTION:
1659 arfrever 1.106 # Set locally active version of Python.
1660     # If Python_ABI argument is specified, then version of Python corresponding to Python_ABI is used.
1661     # If 2 argument is specified, then active version of CPython 2 is used.
1662     # If 3 argument is specified, then active version of CPython 3 is used.
1663 arfrever 1.101 #
1664     # This function can be used only in pkg_setup() phase.
1665 arfrever 1.85 python_set_active_version() {
1666 djc 1.114 if [[ "${EBUILD_PHASE}" != "setup" ]]; then
1667     die "${FUNCNAME}() can be used only in pkg_setup() phase"
1668     fi
1669 arfrever 1.101
1670 arfrever 1.95 if _python_package_supporting_installation_for_multiple_python_abis; then
1671     die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
1672 arfrever 1.88 fi
1673    
1674 arfrever 1.87 if [[ "$#" -ne 1 ]]; then
1675 arfrever 1.85 die "${FUNCNAME}() requires 1 argument"
1676     fi
1677    
1678 arfrever 1.90 _python_initial_sanity_checks
1679    
1680 arfrever 1.101 if [[ -z "${PYTHON_ABI}" ]]; then
1681 arfrever 1.106 if [[ -n "$(_python_get_implementation --ignore-invalid "$1")" ]]; then
1682     # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
1683     # so it does not need to be exported to subprocesses.
1684     PYTHON_ABI="$1"
1685     if ! _python_implementation && ! has_version "$(python_get_implementational_package)"; then
1686     die "${FUNCNAME}(): '$(python_get_implementational_package)' is not installed"
1687 arfrever 1.101 fi
1688     export EPYTHON="$(PYTHON "$1")"
1689     elif [[ "$1" == "2" ]]; then
1690     if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
1691     die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
1692     fi
1693     export EPYTHON="$(PYTHON -2)"
1694 arfrever 1.106 PYTHON_ABI="${EPYTHON#python}"
1695     PYTHON_ABI="${PYTHON_ABI%%-*}"
1696 arfrever 1.101 elif [[ "$1" == "3" ]]; then
1697     if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
1698     die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
1699     fi
1700     export EPYTHON="$(PYTHON -3)"
1701 arfrever 1.106 PYTHON_ABI="${EPYTHON#python}"
1702     PYTHON_ABI="${PYTHON_ABI%%-*}"
1703 arfrever 1.101 else
1704     die "${FUNCNAME}(): Unrecognized argument '$1'"
1705 arfrever 1.85 fi
1706     fi
1707    
1708 arfrever 1.90 _python_final_sanity_checks
1709    
1710 arfrever 1.87 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
1711     PYTHON_REQUESTED_ACTIVE_VERSION="$1"
1712 arfrever 1.85 }
1713 betelgeuse 1.55
1714 arfrever 1.85 # @FUNCTION: python_need_rebuild
1715 vapier 1.131 # @DESCRIPTION:
1716     # Mark current package for rebuilding by python-updater after
1717 arfrever 1.88 # switching of active version of Python.
1718 arfrever 1.85 python_need_rebuild() {
1719 arfrever 1.95 if _python_package_supporting_installation_for_multiple_python_abis; then
1720     die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
1721 arfrever 1.90 fi
1722    
1723 djc 1.114 _python_check_python_pkg_setup_execution
1724    
1725 arfrever 1.101 if [[ "$#" -ne 0 ]]; then
1726     die "${FUNCNAME}() does not accept arguments"
1727     fi
1728    
1729 arfrever 1.90 export PYTHON_NEED_REBUILD="$(PYTHON --ABI)"
1730 arfrever 1.85 }
1731 betelgeuse 1.55
1732 arfrever 1.85 # ================================================================================================
1733     # ======================================= GETTER FUNCTIONS =======================================
1734     # ================================================================================================
1735 betelgeuse 1.55
1736 djc 1.126 _PYTHON_ABI_EXTRACTION_COMMAND=\
1737     'import platform
1738 arfrever 1.88 import sys
1739     sys.stdout.write(".".join(str(x) for x in sys.version_info[:2]))
1740     if platform.system()[:4] == "Java":
1741 djc 1.126 sys.stdout.write("-jython")
1742     elif hasattr(platform, "python_implementation") and platform.python_implementation() == "PyPy":
1743     sys.stdout.write("-pypy-" + ".".join(str(x) for x in sys.pypy_version_info[:2]))'
1744 arfrever 1.88
1745     _python_get_implementation() {
1746 arfrever 1.106 local ignore_invalid="0"
1747    
1748     while (($#)); do
1749     case "$1" in
1750     --ignore-invalid)
1751     ignore_invalid="1"
1752     ;;
1753     --)
1754     shift
1755     break
1756     ;;
1757     -*)
1758     die "${FUNCNAME}(): Unrecognized option '$1'"
1759     ;;
1760     *)
1761     break
1762     ;;
1763     esac
1764     shift
1765     done
1766    
1767 arfrever 1.88 if [[ "$#" -ne 1 ]]; then
1768     die "${FUNCNAME}() requires 1 argument"
1769     fi
1770    
1771     if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
1772     echo "CPython"
1773     elif [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+-jython$ ]]; then
1774     echo "Jython"
1775 djc 1.126 elif [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+-pypy-[[:digit:]]+\.[[:digit:]]+$ ]]; then
1776     echo "PyPy"
1777 arfrever 1.88 else
1778 arfrever 1.106 if [[ "${ignore_invalid}" == "0" ]]; then
1779     die "${FUNCNAME}(): Unrecognized Python ABI '$1'"
1780     fi
1781 arfrever 1.88 fi
1782     }
1783    
1784 arfrever 1.85 # @FUNCTION: PYTHON
1785 arfrever 1.90 # @USAGE: [-2] [-3] [--ABI] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
1786 betelgeuse 1.55 # @DESCRIPTION:
1787 arfrever 1.88 # Print filename of Python interpreter for specified Python ABI. If Python_ABI argument
1788 arfrever 1.85 # is ommitted, then PYTHON_ABI environment variable must be set and is used.
1789 arfrever 1.106 # If -2 option is specified, then active version of CPython 2 is used.
1790     # If -3 option is specified, then active version of CPython 3 is used.
1791 arfrever 1.85 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1792 arfrever 1.90 # -2, -3 and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
1793 arfrever 1.85 # If --ABI option is specified, then only specified Python ABI is printed instead of
1794 arfrever 1.88 # filename of Python interpreter.
1795     # If --absolute-path option is specified, then absolute path to Python interpreter is printed.
1796 arfrever 1.85 # --ABI and --absolute-path options cannot be specified simultaneously.
1797     PYTHON() {
1798 arfrever 1.101 _python_check_python_pkg_setup_execution
1799    
1800 arfrever 1.90 local ABI_output="0" absolute_path_output="0" final_ABI="0" PYTHON_ABI="${PYTHON_ABI}" python_interpreter python2="0" python3="0"
1801 betelgeuse 1.55
1802 arfrever 1.85 while (($#)); do
1803     case "$1" in
1804     -2)
1805     python2="1"
1806     ;;
1807     -3)
1808     python3="1"
1809     ;;
1810     --ABI)
1811     ABI_output="1"
1812     ;;
1813     -a|--absolute-path)
1814     absolute_path_output="1"
1815     ;;
1816     -f|--final-ABI)
1817     final_ABI="1"
1818     ;;
1819     --)
1820 arfrever 1.87 shift
1821 arfrever 1.85 break
1822     ;;
1823     -*)
1824     die "${FUNCNAME}(): Unrecognized option '$1'"
1825     ;;
1826     *)
1827     break
1828     ;;
1829     esac
1830     shift
1831     done
1832 betelgeuse 1.55
1833 arfrever 1.85 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
1834 arfrever 1.101 die "${FUNCNAME}(): '--ABI' and '--absolute-path' options cannot be specified simultaneously"
1835 arfrever 1.85 fi
1836 arfrever 1.74
1837 arfrever 1.90 if [[ "$((${python2} + ${python3} + ${final_ABI}))" -gt 1 ]]; then
1838     die "${FUNCNAME}(): '-2', '-3' or '--final-ABI' options cannot be specified simultaneously"
1839 arfrever 1.85 fi
1840 betelgeuse 1.55
1841 arfrever 1.85 if [[ "$#" -eq 0 ]]; then
1842 arfrever 1.90 if [[ "${final_ABI}" == "1" ]]; then
1843 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1844     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1845 arfrever 1.86 fi
1846 arfrever 1.98 _python_calculate_PYTHON_ABIS
1847 arfrever 1.88 PYTHON_ABI="${PYTHON_ABIS##* }"
1848 arfrever 1.85 elif [[ "${python2}" == "1" ]]; then
1849 djc 1.122 PYTHON_ABI="$(ROOT="/" eselect python show --python2 --ABI)"
1850 arfrever 1.88 if [[ -z "${PYTHON_ABI}" ]]; then
1851 arfrever 1.106 die "${FUNCNAME}(): Active version of CPython 2 not set"
1852 arfrever 1.89 elif [[ "${PYTHON_ABI}" != "2."* ]]; then
1853 arfrever 1.85 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1854     fi
1855     elif [[ "${python3}" == "1" ]]; then
1856 djc 1.122 PYTHON_ABI="$(ROOT="/" eselect python show --python3 --ABI)"
1857 arfrever 1.88 if [[ -z "${PYTHON_ABI}" ]]; then
1858 arfrever 1.106 die "${FUNCNAME}(): Active version of CPython 3 not set"
1859 arfrever 1.89 elif [[ "${PYTHON_ABI}" != "3."* ]]; then
1860 arfrever 1.85 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1861 betelgeuse 1.55 fi
1862 arfrever 1.101 elif _python_package_supporting_installation_for_multiple_python_abis; then
1863     if ! _python_abi-specific_local_scope; then
1864     die "${FUNCNAME}() should be used in ABI-specific local scope"
1865     fi
1866     else
1867 arfrever 1.90 PYTHON_ABI="$("${EPREFIX}/usr/bin/python" -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")"
1868 arfrever 1.97 if [[ -z "${PYTHON_ABI}" ]]; then
1869 arfrever 1.98 die "${FUNCNAME}(): Failure of extraction of locally active version of Python"
1870 arfrever 1.97 fi
1871 arfrever 1.85 fi
1872     elif [[ "$#" -eq 1 ]]; then
1873     if [[ "${final_ABI}" == "1" ]]; then
1874     die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
1875     fi
1876     if [[ "${python2}" == "1" ]]; then
1877     die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
1878     fi
1879     if [[ "${python3}" == "1" ]]; then
1880     die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
1881 betelgeuse 1.55 fi
1882 arfrever 1.88 PYTHON_ABI="$1"
1883 arfrever 1.85 else
1884     die "${FUNCNAME}(): Invalid usage"
1885 betelgeuse 1.55 fi
1886 hawking 1.46
1887 arfrever 1.85 if [[ "${ABI_output}" == "1" ]]; then
1888 arfrever 1.88 echo -n "${PYTHON_ABI}"
1889 arfrever 1.85 return
1890     else
1891 arfrever 1.88 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1892     python_interpreter="python${PYTHON_ABI}"
1893     elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1894 arfrever 1.106 python_interpreter="jython${PYTHON_ABI%-jython}"
1895 djc 1.126 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
1896     python_interpreter="pypy-c${PYTHON_ABI#*-pypy-}"
1897 arfrever 1.88 fi
1898    
1899     if [[ "${absolute_path_output}" == "1" ]]; then
1900     echo -n "${EPREFIX}/usr/bin/${python_interpreter}"
1901     else
1902     echo -n "${python_interpreter}"
1903     fi
1904 arfrever 1.85 fi
1905 hawking 1.46
1906 arfrever 1.85 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
1907     echo -n "-${ABI}"
1908     fi
1909 hawking 1.52 }
1910    
1911 arfrever 1.88 # @FUNCTION: python_get_implementation
1912     # @USAGE: [-f|--final-ABI]
1913     # @DESCRIPTION:
1914     # Print name of Python implementation.
1915     # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1916     python_get_implementation() {
1917 arfrever 1.101 _python_check_python_pkg_setup_execution
1918    
1919 arfrever 1.88 local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
1920    
1921     while (($#)); do
1922     case "$1" in
1923     -f|--final-ABI)
1924     final_ABI="1"
1925     ;;
1926     -*)
1927     die "${FUNCNAME}(): Unrecognized option '$1'"
1928     ;;
1929     *)
1930     die "${FUNCNAME}(): Invalid usage"
1931     ;;
1932     esac
1933     shift
1934     done
1935    
1936     if [[ "${final_ABI}" == "1" ]]; then
1937 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1938     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1939     fi
1940 arfrever 1.88 PYTHON_ABI="$(PYTHON -f --ABI)"
1941 arfrever 1.101 else
1942     if _python_package_supporting_installation_for_multiple_python_abis; then
1943     if ! _python_abi-specific_local_scope; then
1944     die "${FUNCNAME}() should be used in ABI-specific local scope"
1945     fi
1946     else
1947     PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
1948     fi
1949 arfrever 1.88 fi
1950    
1951     echo "$(_python_get_implementation "${PYTHON_ABI}")"
1952     }
1953    
1954     # @FUNCTION: python_get_implementational_package
1955     # @USAGE: [-f|--final-ABI]
1956     # @DESCRIPTION:
1957     # Print category, name and slot of package providing Python implementation.
1958     # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1959     python_get_implementational_package() {
1960 arfrever 1.101 _python_check_python_pkg_setup_execution
1961    
1962 arfrever 1.88 local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
1963    
1964     while (($#)); do
1965     case "$1" in
1966     -f|--final-ABI)
1967     final_ABI="1"
1968     ;;
1969     -*)
1970     die "${FUNCNAME}(): Unrecognized option '$1'"
1971     ;;
1972     *)
1973     die "${FUNCNAME}(): Invalid usage"
1974     ;;
1975     esac
1976     shift
1977     done
1978    
1979     if [[ "${final_ABI}" == "1" ]]; then
1980 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
1981     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
1982     fi
1983 arfrever 1.88 PYTHON_ABI="$(PYTHON -f --ABI)"
1984 arfrever 1.101 else
1985     if _python_package_supporting_installation_for_multiple_python_abis; then
1986     if ! _python_abi-specific_local_scope; then
1987     die "${FUNCNAME}() should be used in ABI-specific local scope"
1988     fi
1989     else
1990     PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
1991     fi
1992 arfrever 1.88 fi
1993    
1994 arfrever 1.104 if [[ "${EAPI:-0}" == "0" ]]; then
1995     if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1996     echo "=dev-lang/python-${PYTHON_ABI}*"
1997     elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1998     echo "=dev-java/jython-${PYTHON_ABI%-jython}*"
1999 djc 1.126 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2000     echo "=dev-python/pypy-${PYTHON_ABI#*-pypy-}*"
2001 arfrever 1.104 fi
2002     else
2003     if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2004     echo "dev-lang/python:${PYTHON_ABI}"
2005     elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2006     echo "dev-java/jython:${PYTHON_ABI%-jython}"
2007 djc 1.126 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2008     echo "dev-python/pypy:${PYTHON_ABI#*-pypy-}"
2009 arfrever 1.104 fi
2010 arfrever 1.88 fi
2011     }
2012    
2013 arfrever 1.56 # @FUNCTION: python_get_includedir
2014 arfrever 1.101 # @USAGE: [-b|--base-path] [-f|--final-ABI]
2015 arfrever 1.56 # @DESCRIPTION:
2016 arfrever 1.87 # Print path to Python include directory.
2017 arfrever 1.101 # If --base-path option is specified, then path not prefixed with "/" is printed.
2018 arfrever 1.86 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2019 arfrever 1.56 python_get_includedir() {
2020 arfrever 1.101 _python_check_python_pkg_setup_execution
2021    
2022     local base_path="0" final_ABI="0" prefix PYTHON_ABI="${PYTHON_ABI}"
2023 arfrever 1.86
2024     while (($#)); do
2025     case "$1" in
2026 arfrever 1.101 -b|--base-path)
2027     base_path="1"
2028     ;;
2029 arfrever 1.86 -f|--final-ABI)
2030     final_ABI="1"
2031     ;;
2032     -*)
2033     die "${FUNCNAME}(): Unrecognized option '$1'"
2034     ;;
2035     *)
2036     die "${FUNCNAME}(): Invalid usage"
2037     ;;
2038     esac
2039     shift
2040     done
2041    
2042 arfrever 1.101 if [[ "${base_path}" == "0" ]]; then
2043     prefix="/"
2044     fi
2045    
2046 arfrever 1.86 if [[ "${final_ABI}" == "1" ]]; then
2047 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2048     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2049     fi
2050 arfrever 1.88 PYTHON_ABI="$(PYTHON -f --ABI)"
2051 arfrever 1.101 else
2052     if _python_package_supporting_installation_for_multiple_python_abis; then
2053     if ! _python_abi-specific_local_scope; then
2054     die "${FUNCNAME}() should be used in ABI-specific local scope"
2055     fi
2056     else
2057     PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2058     fi
2059 arfrever 1.88 fi
2060    
2061     if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2062 arfrever 1.101 echo "${prefix}usr/include/python${PYTHON_ABI}"
2063 arfrever 1.88 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2064 arfrever 1.101 echo "${prefix}usr/share/jython-${PYTHON_ABI%-jython}/Include"
2065 djc 1.126 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2066     echo "${prefix}usr/$(get_libdir)/pypy${PYTHON_ABI#*-pypy-}/include"
2067 arfrever 1.56 fi
2068     }
2069    
2070 pythonhead 1.43 # @FUNCTION: python_get_libdir
2071 arfrever 1.101 # @USAGE: [-b|--base-path] [-f|--final-ABI]
2072 pythonhead 1.43 # @DESCRIPTION:
2073 djc 1.126 # Print path to Python standard library directory.
2074 arfrever 1.101 # If --base-path option is specified, then path not prefixed with "/" is printed.
2075 arfrever 1.86 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2076 pythonhead 1.43 python_get_libdir() {
2077 arfrever 1.101 _python_check_python_pkg_setup_execution
2078    
2079     local base_path="0" final_ABI="0" prefix PYTHON_ABI="${PYTHON_ABI}"
2080 arfrever 1.86
2081     while (($#)); do
2082     case "$1" in
2083 arfrever 1.101 -b|--base-path)
2084     base_path="1"
2085     ;;
2086 arfrever 1.86 -f|--final-ABI)
2087     final_ABI="1"
2088     ;;
2089     -*)
2090     die "${FUNCNAME}(): Unrecognized option '$1'"
2091     ;;
2092     *)
2093     die "${FUNCNAME}(): Invalid usage"
2094     ;;
2095     esac
2096     shift
2097     done
2098    
2099 arfrever 1.101 if [[ "${base_path}" == "0" ]]; then
2100     prefix="/"
2101     fi
2102    
2103 arfrever 1.86 if [[ "${final_ABI}" == "1" ]]; then
2104 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2105     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2106     fi
2107 arfrever 1.88 PYTHON_ABI="$(PYTHON -f --ABI)"
2108 arfrever 1.101 else
2109     if _python_package_supporting_installation_for_multiple_python_abis; then
2110     if ! _python_abi-specific_local_scope; then
2111     die "${FUNCNAME}() should be used in ABI-specific local scope"
2112     fi
2113     else
2114     PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2115     fi
2116 arfrever 1.88 fi
2117    
2118     if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2119 arfrever 1.101 echo "${prefix}usr/$(get_libdir)/python${PYTHON_ABI}"
2120 arfrever 1.88 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2121 arfrever 1.101 echo "${prefix}usr/share/jython-${PYTHON_ABI%-jython}/Lib"
2122 djc 1.126 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2123     die "${FUNCNAME}(): PyPy has multiple standard library directories"
2124 arfrever 1.56 fi
2125 pythonhead 1.42 }
2126    
2127 pythonhead 1.43 # @FUNCTION: python_get_sitedir
2128 arfrever 1.101 # @USAGE: [-b|--base-path] [-f|--final-ABI]
2129 pythonhead 1.43 # @DESCRIPTION:
2130 arfrever 1.87 # Print path to Python site-packages directory.
2131 arfrever 1.101 # If --base-path option is specified, then path not prefixed with "/" is printed.
2132 arfrever 1.86 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2133 pythonhead 1.43 python_get_sitedir() {
2134 arfrever 1.101 _python_check_python_pkg_setup_execution
2135    
2136 djc 1.126 local base_path="0" final_ABI="0" prefix PYTHON_ABI="${PYTHON_ABI}"
2137 arfrever 1.86
2138     while (($#)); do
2139     case "$1" in
2140 arfrever 1.101 -b|--base-path)
2141 djc 1.126 base_path="1"
2142 arfrever 1.101 ;;
2143 arfrever 1.86 -f|--final-ABI)
2144 arfrever 1.101 final_ABI="1"
2145 arfrever 1.86 ;;
2146     -*)
2147     die "${FUNCNAME}(): Unrecognized option '$1'"
2148     ;;
2149     *)
2150     die "${FUNCNAME}(): Invalid usage"
2151     ;;
2152     esac
2153     shift
2154     done
2155    
2156 djc 1.126 if [[ "${base_path}" == "0" ]]; then
2157     prefix="/"
2158     fi
2159    
2160 arfrever 1.101 if [[ "${final_ABI}" == "1" ]]; then
2161     if ! _python_package_supporting_installation_for_multiple_python_abis; then
2162     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2163     fi
2164 djc 1.126 PYTHON_ABI="$(PYTHON -f --ABI)"
2165 arfrever 1.101 else
2166 djc 1.126 if _python_package_supporting_installation_for_multiple_python_abis; then
2167     if ! _python_abi-specific_local_scope; then
2168     die "${FUNCNAME}() should be used in ABI-specific local scope"
2169     fi
2170     else
2171     PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2172 arfrever 1.101 fi
2173     fi
2174    
2175 djc 1.126 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2176     echo "${prefix}usr/$(get_libdir)/python${PYTHON_ABI}/site-packages"
2177     elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2178     echo "${prefix}usr/share/jython-${PYTHON_ABI%-jython}/Lib/site-packages"
2179     elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2180     echo "${prefix}usr/$(get_libdir)/pypy${PYTHON_ABI#*-pypy-}/site-packages"
2181     fi
2182 pythonhead 1.42 }
2183    
2184 arfrever 1.87 # @FUNCTION: python_get_library
2185 arfrever 1.101 # @USAGE: [-b|--base-path] [-f|--final-ABI] [-l|--linker-option]
2186 arfrever 1.87 # @DESCRIPTION:
2187     # Print path to Python library.
2188 arfrever 1.101 # If --base-path option is specified, then path not prefixed with "/" is printed.
2189 arfrever 1.87 # If --linker-option is specified, then "-l${library}" linker option is printed.
2190     # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2191     python_get_library() {
2192 arfrever 1.101 _python_check_python_pkg_setup_execution
2193    
2194     local base_path="0" final_ABI="0" linker_option="0" prefix PYTHON_ABI="${PYTHON_ABI}"
2195 arfrever 1.87
2196     while (($#)); do
2197     case "$1" in
2198 arfrever 1.101 -b|--base-path)
2199     base_path="1"
2200     ;;
2201 arfrever 1.87 -f|--final-ABI)
2202     final_ABI="1"
2203     ;;
2204     -l|--linker-option)
2205     linker_option="1"
2206     ;;
2207     -*)
2208     die "${FUNCNAME}(): Unrecognized option '$1'"
2209     ;;
2210     *)
2211     die "${FUNCNAME}(): Invalid usage"
2212     ;;
2213     esac
2214     shift
2215     done
2216    
2217 arfrever 1.101 if [[ "${base_path}" == "0" ]]; then
2218     prefix="/"
2219     fi
2220    
2221     if [[ "${base_path}" == "1" && "${linker_option}" == "1" ]]; then
2222     die "${FUNCNAME}(): '--base-path' and '--linker-option' options cannot be specified simultaneously"
2223     fi
2224    
2225 arfrever 1.87 if [[ "${final_ABI}" == "1" ]]; then
2226 arfrever 1.95 if ! _python_package_supporting_installation_for_multiple_python_abis; then
2227     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2228     fi
2229 arfrever 1.88 PYTHON_ABI="$(PYTHON -f --ABI)"
2230 arfrever 1.101 else
2231     if _python_package_supporting_installation_for_multiple_python_abis; then
2232     if ! _python_abi-specific_local_scope; then
2233     die "${FUNCNAME}() should be used in ABI-specific local scope"
2234     fi
2235     else
2236     PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2237     fi
2238 arfrever 1.87 fi
2239    
2240 arfrever 1.88 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2241     if [[ "${linker_option}" == "1" ]]; then
2242     echo "-lpython${PYTHON_ABI}"
2243     else
2244 arfrever 1.101 echo "${prefix}usr/$(get_libdir)/libpython${PYTHON_ABI}$(get_libname)"
2245 arfrever 1.88 fi
2246     elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2247     die "${FUNCNAME}(): Jython does not have shared library"
2248 djc 1.126 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2249     die "${FUNCNAME}(): PyPy does not have shared library"
2250 arfrever 1.87 fi
2251     }
2252    
2253     # @FUNCTION: python_get_version
2254 djc 1.118 # @USAGE: [-f|--final-ABI] [-l|--language] [--full] [--major] [--minor] [--micro]
2255 arfrever 1.87 # @DESCRIPTION:
2256 djc 1.124 # Print version of Python implementation.
2257 arfrever 1.92 # --full, --major, --minor and --micro options cannot be specified simultaneously.
2258     # If --full, --major, --minor and --micro options are not specified, then "${major_version}.${minor_version}" is printed.
2259 djc 1.124 # If --language option is specified, then version of Python language is printed.
2260 djc 1.118 # --language and --full options cannot be specified simultaneously.
2261     # --language and --micro options cannot be specified simultaneously.
2262 arfrever 1.87 # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2263     python_get_version() {
2264 arfrever 1.101 _python_check_python_pkg_setup_execution
2265    
2266 djc 1.118 local final_ABI="0" language="0" language_version full="0" major="0" minor="0" micro="0" PYTHON_ABI="${PYTHON_ABI}" python_command
2267 arfrever 1.87
2268     while (($#)); do
2269     case "$1" in
2270     -f|--final-ABI)
2271     final_ABI="1"
2272     ;;
2273 djc 1.118 -l|--language)
2274     language="1"
2275     ;;
2276 arfrever 1.92 --full)
2277     full="1"
2278     ;;
2279 arfrever 1.87 --major)
2280     major="1"
2281     ;;
2282     --minor)
2283     minor="1"
2284     ;;
2285     --micro)
2286     micro="1"
2287     ;;
2288     -*)
2289     die "${FUNCNAME}(): Unrecognized option '$1'"
2290     ;;
2291     *)
2292     die "${FUNCNAME}(): Invalid usage"
2293     ;;
2294     esac
2295     shift
2296     done
2297    
2298 djc 1.118 if [[ "${final_ABI}" == "1" ]]; then
2299     if ! _python_package_supporting_installation_for_multiple_python_abis; then
2300     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2301     fi
2302     else
2303     if _python_package_supporting_installation_for_multiple_python_abis && ! _python_abi-specific_local_scope; then
2304     die "${FUNCNAME}() should be used in ABI-specific local scope"
2305     fi
2306     fi
2307    
2308 arfrever 1.92 if [[ "$((${full} + ${major} + ${minor} + ${micro}))" -gt 1 ]]; then
2309     die "${FUNCNAME}(): '--full', '--major', '--minor' or '--micro' options cannot be specified simultaneously"
2310 arfrever 1.87 fi
2311    
2312 djc 1.118 if [[ "${language}" == "1" ]]; then
2313     if [[ "${final_ABI}" == "1" ]]; then
2314     PYTHON_ABI="$(PYTHON -f --ABI)"
2315     elif [[ -z "${PYTHON_ABI}" ]]; then
2316     PYTHON_ABI="$(PYTHON --ABI)"
2317     fi
2318     language_version="${PYTHON_ABI%%-*}"
2319     if [[ "${full}" == "1" ]]; then
2320     die "${FUNCNAME}(): '--language' and '--full' options cannot be specified simultaneously"
2321     elif [[ "${major}" == "1" ]]; then
2322     echo "${language_version%.*}"
2323     elif [[ "${minor}" == "1" ]]; then
2324     echo "${language_version#*.}"
2325     elif [[ "${micro}" == "1" ]]; then
2326     die "${FUNCNAME}(): '--language' and '--micro' options cannot be specified simultaneously"
2327     else
2328     echo "${language_version}"
2329     fi
2330     else
2331     if [[ "${full}" == "1" ]]; then
2332 djc 1.126 python_command="import sys; print('.'.join(str(x) for x in getattr(sys, 'pypy_version_info', sys.version_info)[:3]))"
2333 djc 1.118 elif [[ "${major}" == "1" ]]; then
2334 djc 1.126 python_command="import sys; print(getattr(sys, 'pypy_version_info', sys.version_info)[0])"
2335 djc 1.118 elif [[ "${minor}" == "1" ]]; then
2336 djc 1.126 python_command="import sys; print(getattr(sys, 'pypy_version_info', sys.version_info)[1])"
2337 djc 1.118 elif [[ "${micro}" == "1" ]]; then
2338 djc 1.126 python_command="import sys; print(getattr(sys, 'pypy_version_info', sys.version_info)[2])"
2339 djc 1.118 else
2340     if [[ -n "${PYTHON_ABI}" && "${final_ABI}" == "0" ]]; then
2341     if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
2342     echo "${PYTHON_ABI}"
2343     elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
2344     echo "${PYTHON_ABI%-jython}"
2345 djc 1.126 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "PyPy" ]]; then
2346     echo "${PYTHON_ABI#*-pypy-}"
2347 djc 1.118 fi
2348     return
2349 arfrever 1.92 fi
2350 djc 1.118 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:2]))"
2351 arfrever 1.92 fi
2352 arfrever 1.87
2353 djc 1.118 if [[ "${final_ABI}" == "1" ]]; then
2354     "$(PYTHON -f)" -c "${python_command}"
2355     else
2356     "$(PYTHON ${PYTHON_ABI})" -c "${python_command}"
2357 arfrever 1.101 fi
2358 arfrever 1.87 fi
2359     }
2360    
2361 djc 1.124 # @FUNCTION: python_get_implementation_and_version
2362     # @USAGE: [-f|--final-ABI]
2363     # @DESCRIPTION:
2364     # Print name and version of Python implementation.
2365     # If version of Python implementation is not bound to version of Python language, then
2366     # version of Python language is additionally printed.
2367     # If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
2368     python_get_implementation_and_version() {
2369     _python_check_python_pkg_setup_execution
2370    
2371     local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
2372    
2373     while (($#)); do
2374     case "$1" in
2375     -f|--final-ABI)
2376     final_ABI="1"
2377     ;;
2378     -*)
2379     die "${FUNCNAME}(): Unrecognized option '$1'"
2380     ;;
2381     *)
2382     die "${FUNCNAME}(): Invalid usage"
2383     ;;
2384     esac
2385     shift
2386     done
2387    
2388     if [[ "${final_ABI}" == "1" ]]; then
2389     if ! _python_package_supporting_installation_for_multiple_python_abis; then
2390     die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2391     fi
2392     PYTHON_ABI="$(PYTHON -f --ABI)"
2393     else
2394     if _python_package_supporting_installation_for_multiple_python_abis; then
2395     if ! _python_abi-specific_local_scope; then
2396     die "${FUNCNAME}() should be used in ABI-specific local scope"
2397     fi
2398     else
2399     PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
2400     fi
2401     fi
2402    
2403     if [[ "${PYTHON_ABI}" =~ ^[[:digit:]]+\.[[:digit:]]+-[[:alnum:]]+-[[:digit:]]+\.[[:digit:]]+$ ]]; then
2404     echo "$(_python_get_implementation "${PYTHON_ABI}") ${PYTHON_ABI##*-} (Python ${PYTHON_ABI%%-*})"
2405     else
2406     echo "$(_python_get_implementation "${PYTHON_ABI}") ${PYTHON_ABI%%-*}"
2407     fi
2408     }
2409    
2410 arfrever 1.85 # ================================================================================================
2411 arfrever 1.87 # ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
2412     # ================================================================================================
2413    
2414     # @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
2415     # @DESCRIPTION:
2416     # User-configurable verbosity of tests of Python modules.
2417     # Supported values: 0, 1, 2, 3, 4.
2418     PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
2419    
2420 arfrever 1.92 _python_test_hook() {
2421     if [[ "$#" -ne 1 ]]; then
2422     die "${FUNCNAME}() requires 1 argument"
2423     fi
2424    
2425 djc 1.136 if _python_package_supporting_installation_for_multiple_python_abis && [[ "$(type -t "${_PYTHON_TEST_FUNCTION}_$1_hook")" == "function" ]]; then
2426     "${_PYTHON_TEST_FUNCTION}_$1_hook"
2427 arfrever 1.92 fi
2428     }
2429    
2430 arfrever 1.87 # @FUNCTION: python_execute_nosetests
2431     # @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
2432     # @DESCRIPTION:
2433 arfrever 1.95 # Execute nosetests for all enabled Python ABIs.
2434     # In ebuilds of packages supporting installation for multiple Python ABIs, this function calls
2435     # python_execute_nosetests_pre_hook() and python_execute_nosetests_post_hook(), if they are defined.
2436 arfrever 1.87 python_execute_nosetests() {
2437 arfrever 1.101 _python_check_python_pkg_setup_execution
2438 arfrever 1.90 _python_set_color_variables
2439    
2440 djc 1.114 local PYTHONPATH_template separate_build_dirs
2441 arfrever 1.87
2442     while (($#)); do
2443     case "$1" in
2444     -P|--PYTHONPATH)
2445     PYTHONPATH_template="$2"
2446     shift
2447     ;;
2448     -s|--separate-build-dirs)
2449     separate_build_dirs="1"
2450     ;;
2451     --)
2452     shift
2453     break
2454     ;;
2455     -*)
2456     die "${FUNCNAME}(): Unrecognized option '$1'"
2457     ;;
2458     *)
2459     break
2460     ;;
2461     esac
2462     shift
2463     done
2464    
2465     python_test_function() {
2466 arfrever 1.101 local evaluated_PYTHONPATH
2467 arfrever 1.87
2468 arfrever 1.103 eval "evaluated_PYTHONPATH=\"${PYTHONPATH_template}\""
2469 arfrever 1.87
2470 djc 1.136 _PYTHON_TEST_FUNCTION="python_execute_nosetests" _python_test_hook pre
2471 arfrever 1.92
2472 arfrever 1.87 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
2473 arfrever 1.90 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
2474 arfrever 1.92 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
2475 arfrever 1.87 else
2476 arfrever 1.90 echo ${_BOLD}nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
2477 arfrever 1.92 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
2478 arfrever 1.87 fi
2479 arfrever 1.92
2480 djc 1.136 _PYTHON_TEST_FUNCTION="python_execute_nosetests" _python_test_hook post
2481 arfrever 1.87 }
2482 arfrever 1.95 if _python_package_supporting_installation_for_multiple_python_abis; then
2483 arfrever 1.87 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
2484     else
2485     if [[ -n "${separate_build_dirs}" ]]; then
2486     die "${FUNCNAME}(): Invalid usage"
2487     fi
2488 arfrever 1.88 python_test_function "$@" || die "Testing failed"
2489 arfrever 1.87 fi
2490 arfrever 1.88
2491     unset -f python_test_function
2492 arfrever 1.87 }
2493    
2494     # @FUNCTION: python_execute_py.test
2495     # @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
2496     # @DESCRIPTION:
2497 arfrever 1.95 # Execute py.test for all enabled Python ABIs.
2498     # In ebuilds of packages supporting installation for multiple Python ABIs, this function calls
2499     # python_execute_py.test_pre_hook() and python_execute_py.test_post_hook(), if they are defined.
2500 arfrever 1.87 python_execute_py.test() {
2501 arfrever 1.101 _python_check_python_pkg_setup_execution
2502 arfrever 1.90 _python_set_color_variables
2503    
2504 djc 1.114 local PYTHONPATH_template separate_build_dirs
2505 arfrever 1.87
2506     while (($#)); do
2507     case "$1" in
2508     -P|--PYTHONPATH)
2509     PYTHONPATH_template="$2"
2510     shift
2511     ;;
2512     -s|--separate-build-dirs)
2513     separate_build_dirs="1"
2514     ;;
2515     --)
2516     shift
2517     break
2518     ;;
2519     -*)
2520     die "${FUNCNAME}(): Unrecognized option '$1'"
2521     ;;
2522     *)
2523     break
2524     ;;
2525     esac
2526     shift
2527     done
2528    
2529     python_test_function() {
2530 arfrever 1.101 local evaluated_PYTHONPATH
2531 arfrever 1.87
2532 arfrever 1.103 eval "evaluated_PYTHONPATH=\"${PYTHONPATH_template}\""
2533 arfrever 1.87
2534 djc 1.136 _PYTHON_TEST_FUNCTION="python_execute_py.test" _python_test_hook pre
2535 arfrever 1.92
2536 arfrever 1.87 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
2537 arfrever 1.90 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"${_NORMAL}
2538 arfrever 1.92 PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@" || return "$?"
2539 arfrever 1.87 else
2540 arfrever 1.90 echo ${_BOLD}py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"${_NORMAL}
2541 arfrever 1.92 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" || return "$?"
2542 arfrever 1.87 fi
2543 arfrever 1.92
2544 djc 1.136 _PYTHON_TEST_FUNCTION="python_execute_py.test" _python_test_hook post
2545 arfrever 1.87 }
2546 arfrever 1.95 if _python_package_supporting_installation_for_multiple_python_abis; then
2547 arfrever 1.87 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
2548     else
2549     if [[ -n "${separate_build_dirs}" ]]; then
2550     die "${FUNCNAME}(): Invalid usage"
2551     fi
2552 arfrever 1.88 python_test_function "$@" || die "Testing failed"
2553 arfrever 1.87 fi
2554 arfrever 1.88
2555     unset -f python_test_function
2556 arfrever 1.87 }
2557    
2558     # @FUNCTION: python_execute_trial
2559     # @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
2560     # @DESCRIPTION:
2561 arfrever 1.95 # Execute trial for all enabled Python ABIs.
2562     # In ebuilds of packages supporting installation for multiple Python ABIs, this function
2563 arfrever 1.92 # calls python_execute_trial_pre_hook() and python_execute_trial_post_hook(), if they are defined.
2564 arfrever 1.87 python_execute_trial() {
2565 arfrever 1.101 _python_check_python_pkg_setup_execution
2566 arfrever 1.90 _python_set_color_variables
2567    
2568 djc 1.114 local PYTHONPATH_template separate_build_dirs
2569 arfrever 1.87
2570     while (($#)); do
2571     case "$1" in
2572     -P|--PYTHONPATH)
2573     PYTHONPATH_template="$2"
2574     shift
2575     ;;
2576     -s|--separate-build-dirs)
2577     separate_build_dirs="1"
2578     ;;
2579     --)
2580     shift
2581     break
2582     ;;
2583     -*)
2584     die "${FUNCNAME}(): Unrecognized option '$1'"
2585     ;;
2586     *)
2587     break
2588     ;;
2589     esac
2590     shift
2591     done
2592    
2593     python_test_function() {
2594 arfrever 1.101 local evaluated_PYTHONPATH
2595 arfrever 1.87
2596 arfrever 1.103 eval "evaluated_PYTHONPATH=\"${PYTHONPATH_template}\""
2597 arfrever 1.87
2598 djc 1.136 _PYTHON_TEST_FUNCTION="python_execute_trial" _python_test_hook pre
2599 arfrever 1.92
2600 arfrever 1.87 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
2601 arfrever 1.90 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
2602 arfrever 1.92 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
2603 arfrever 1.87 else
2604 arfrever 1.90 echo ${_BOLD}trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
2605 arfrever 1.92 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
2606 arfrever 1.87 fi
2607 arfrever 1.92
2608 djc 1.136 _PYTHON_TEST_FUNCTION="python_execute_trial" _python_test_hook post
2609 arfrever 1.87 }
2610 arfrever 1.95 if _python_package_supporting_installation_for_multiple_python_abis; then
2611 arfrever 1.87 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
2612     else
2613     if [[ -n "${separate_build_dirs}" ]]; then
2614     die "${FUNCNAME}(): Invalid usage"
2615     fi
2616 arfrever 1.88 python_test_function "$@" || die "Testing failed"
2617 arfrever 1.56 fi
2618 arfrever 1.88
2619     unset -f python_test_function
2620 arfrever 1.85 }
2621 arfrever 1.56
2622 arfrever 1.85 # ================================================================================================
2623     # ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
2624     # ================================================================================================
2625 hawking 1.37
2626 arfrever 1.85 # @FUNCTION: python_enable_pyc
2627     # @DESCRIPTION:
2628     # Tell Python to automatically recompile modules to .pyc/.pyo if the
2629     # timestamps/version stamps have changed.
2630     python_enable_pyc() {
2631 arfrever 1.101 _python_check_python_pkg_setup_execution
2632    
2633     if [[ "$#" -ne 0 ]]; then
2634     die "${FUNCNAME}() does not accept arguments"
2635     fi
2636    
2637 arfrever 1.85 unset PYTHONDONTWRITEBYTECODE
2638     }
2639 hawking 1.37
2640 arfrever 1.85 # @FUNCTION: python_disable_pyc
2641     # @DESCRIPTION:
2642     # Tell Python not to automatically recompile modules to .pyc/.pyo
2643     # even if the timestamps/version stamps do not match. This is done
2644     # to protect sandbox.
2645     python_disable_pyc() {
2646 arfrever 1.101 _python_check_python_pkg_setup_execution
2647    
2648     if [[ "$#" -ne 0 ]]; then
2649     die "${FUNCNAME}() does not accept arguments"
2650     fi
2651    
2652 arfrever 1.85 export PYTHONDONTWRITEBYTECODE="1"
2653 liquidx 1.1 }
2654    
2655 arfrever 1.97 _python_clean_compiled_modules() {
2656     _python_initialize_prefix_variables
2657     _python_set_color_variables
2658    
2659 arfrever 1.104 [[ "${FUNCNAME[1]}" =~ ^(python_mod_optimize|python_mod_cleanup)$ ]] || die "${FUNCNAME}(): Invalid usage"
2660 arfrever 1.97
2661     local base_module_name compiled_file compiled_files=() dir path py_file root
2662    
2663     # Strip trailing slash from EROOT.
2664     root="${EROOT%/}"
2665    
2666     for path in "$@"; do
2667     compiled_files=()
2668     if [[ -d "${path}" ]]; then
2669     while read -d $'\0' -r compiled_file; do
2670     compiled_files+=("${compiled_file}")
2671     done < <(find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0)
2672    
2673     if [[ "${EBUILD_PHASE}" == "postrm" ]]; then
2674     # Delete empty child directories.
2675     find "${path}" -type d | sort -r | while read -r dir; do
2676 arfrever 1.100 if rmdir "${dir}" 2> /dev/null; then
2677     echo "${_CYAN}<<< ${dir}${_NORMAL}"
2678     fi
2679 arfrever 1.97 done
2680     fi
2681     elif [[ "${path}" == *.py ]]; then
2682     base_module_name="${path##*/}"
2683     base_module_name="${base_module_name%.py}"
2684     if [[ -d "${path%/*}/__pycache__" ]]; then
2685     while read -d $'\0' -r compiled_file; do
2686     compiled_files+=("${compiled_file}")
2687     done < <(find "${path%/*}/__pycache__" "(" -name "${base_module_name}.*.py[co]" -o -name "${base_module_name}\$py.class" ")" -print0)
2688     fi
2689     compiled_files+=("${path}c" "${path}o" "${path%.py}\$py.class")
2690     fi
2691    
2692     for compiled_file in "${compiled_files[@]}"; do
2693     [[ ! -f "${compiled_file}" ]] && continue
2694     dir="${compiled_file%/*}"
2695     dir="${dir##*/}"
2696     if [[ "${compiled_file}" == *.py[co] ]]; then
2697     if [[ "${dir}" == "__pycache__" ]]; then
2698     base_module_name="${compiled_file##*/}"
2699 arfrever 1.98 base_module_name="${base_module_name%.*py[co]}"
2700     base_module_name="${base_module_name%.*}"
2701 arfrever 1.97 py_file="${compiled_file%__pycache__/*}${base_module_name}.py"
2702     else
2703     py_file="${compiled_file%[co]}"
2704     fi
2705     if [[ "${EBUILD_PHASE}" == "postinst" ]]; then
2706     [[ -f "${py_file}" && "${compiled_file}" -nt "${py_file}" ]] && continue
2707     else
2708     [[ -f "${py_file}" ]] && continue
2709     fi
2710     echo "${_BLUE}<<< ${compiled_file%[co]}[co]${_NORMAL}"
2711     rm -f "${compiled_file%[co]}"[co]
2712     elif [[ "${compiled_file}" == *\$py.class ]]; then
2713     if [[ "${dir}" == "__pycache__" ]]; then
2714     base_module_name="${compiled_file##*/}"
2715     base_module_name="${base_module_name%\$py.class}"
2716     py_file="${compiled_file%__pycache__/*}${base_module_name}.py"
2717     else
2718 arfrever 1.106 py_file="${compiled_file%\$py.class}.py"
2719 arfrever 1.97 fi
2720     if [[ "${EBUILD_PHASE}" == "postinst" ]]; then
2721     [[ -f "${py_file}" && "${compiled_file}" -nt "${py_file}" ]] && continue
2722     else
2723     [[ -f "${py_file}" ]] && continue
2724     fi
2725     echo "${_BLUE}<<< ${compiled_file}${_NORMAL}"
2726     rm -f "${compiled_file}"
2727     else
2728     die "${FUNCNAME}(): Unrecognized file type: '${compiled_file}'"
2729     fi
2730    
2731     # Delete empty parent directories.
2732     dir="${compiled_file%/*}"
2733     while [[ "${dir}" != "${root}" ]]; do
2734 arfrever 1.100 if rmdir "${dir}" 2> /dev/null; then
2735     echo "${_CYAN}<<< ${dir}${_NORMAL}"
2736 arfrever 1.97 else
2737     break
2738     fi
2739     dir="${dir%/*}"
2740     done
2741     done
2742     done
2743     }
2744    
2745 dev-zero 1.33 # @FUNCTION: python_mod_optimize
2746 arfrever 1.104 # @USAGE: [--allow-evaluated-non-sitedir-paths] [-d directory] [-f] [-l] [-q] [-x regular_expression] [--] <file|directory> [files|directories]
2747 dev-zero 1.33 # @DESCRIPTION:
2748 arfrever 1.104 # Byte-compile specified Python modules.
2749     # -d, -f, -l, -q and -x options passed to this function are passed to compileall.py.
2750 hawking 1.39 #
2751 arfrever 1.92 # This function can be used only in pkg_postinst() phase.
2752 liquidx 1.1 python_mod_optimize() {
2753 djc 1.114 if [[ "${EBUILD_PHASE}" != "postinst" ]]; then
2754     die "${FUNCNAME}() can be used only in pkg_postinst() phase"
2755     fi
2756    
2757 arfrever 1.101 _python_check_python_pkg_setup_execution
2758 arfrever 1.87 _python_initialize_prefix_variables
2759    
2760 arfrever 1.104 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis || _python_implementation || [[ "${CATEGORY}/${PN}" == "sys-apps/portage" ]]; then
2761 arfrever 1.97 # PYTHON_ABI variable cannot be local in packages not supporting installation for multiple Python ABIs.
2762 djc 1.119 local allow_evaluated_non_sitedir_paths="0" dir dirs=() evaluated_dirs=() evaluated_files=() file files=() iterated_PYTHON_ABIS options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" return_code root site_packages_dirs=() site_packages_files=() stderr stderr_line
2763 hawking 1.37
2764 arfrever 1.97 if _python_package_supporting_installation_for_multiple_python_abis; then
2765 arfrever 1.101 if has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_ABIS}" ]]; then
2766     die "${FUNCNAME}(): python_pkg_setup() or python_execute_function() not called"
2767 arfrever 1.97 fi
2768     iterated_PYTHON_ABIS="${PYTHON_ABIS}"
2769     else
2770 arfrever 1.101 if has "${EAPI:-0}" 0 1 2 3; then
2771     iterated_PYTHON_ABIS="${PYTHON_ABI:=$(PYTHON --ABI)}"
2772     else
2773     iterated_PYTHON_ABIS="${PYTHON_ABI}"
2774     fi
2775 arfrever 1.97 fi
2776    
2777     # Strip trailing slash from EROOT.
2778 arfrever 1.87 root="${EROOT%/}"
2779 arfrever 1.56
2780     while (($#)); do
2781     case "$1" in
2782 arfrever 1.103 --allow-evaluated-non-sitedir-paths)
2783     allow_evaluated_non_sitedir_paths="1"
2784     ;;
2785 arfrever 1.56 -l|-f|-q)
2786     options+=("$1")
2787     ;;
2788     -d|-x)
2789     options+=("$1" "$2")
2790     shift
2791     ;;
2792 arfrever 1.101 --)
2793     shift
2794     break
2795     ;;
2796 arfrever 1.56 -*)
2797 arfrever 1.101 die "${FUNCNAME}(): Unrecognized option '$1'"
2798 arfrever 1.56 ;;
2799     *)
2800 arfrever 1.98 break
2801     ;;
2802     esac
2803     shift
2804     done
2805    
2806 arfrever 1.103 if [[ "${allow_evaluated_non_sitedir_paths}" == "1" ]] && ! _python_package_supporting_installation_for_multiple_python_abis; then
2807     die "${FUNCNAME}(): '--allow-evaluated-non-sitedir-paths' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
2808     fi
2809    
2810 arfrever 1.98 if [[ "$#" -eq 0 ]]; then
2811 arfrever 1.104 die "${FUNCNAME}(): Missing files or directories"
2812 arfrever 1.98 fi
2813    
2814     while (($#)); do
2815 arfrever 1.101 if [[ "$1" =~ ^($|(\.|\.\.|/)($|/)) ]]; then
2816     die "${FUNCNAME}(): Invalid argument '$1'"
2817     elif ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
2818 arfrever 1.98 die "${FUNCNAME}(): Paths of directories / files in site-packages directories must be relative to site-packages directories"
2819     elif [[ "$1" =~ ^/ ]]; then
2820     if _python_package_supporting_installation_for_multiple_python_abis; then
2821 arfrever 1.103 if [[ "${allow_evaluated_non_sitedir_paths}" != "1" ]]; then
2822     die "${FUNCNAME}(): Absolute paths cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
2823     fi
2824     if [[ "$1" != *\$* ]]; then
2825     die "${FUNCNAME}(): '$1' has invalid syntax"
2826     fi
2827     if [[ "$1" == *.py ]]; then
2828     evaluated_files+=("$1")
2829     else
2830     evaluated_dirs+=("$1")
2831     fi
2832 arfrever 1.98 else
2833 arfrever 1.103 if [[ -d "${root}$1" ]]; then
2834     other_dirs+=("${root}$1")
2835     elif [[ -f "${root}$1" ]]; then
2836     other_files+=("${root}$1")
2837     elif [[ -e "${root}$1" ]]; then
2838     eerror "${FUNCNAME}(): '${root}$1' is not a regular file or a directory"
2839     else
2840     eerror "${FUNCNAME}(): '${root}$1' does not exist"
2841     fi
2842 arfrever 1.98 fi
2843     else
2844     for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
2845     if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
2846     site_packages_dirs+=("$1")
2847     break
2848     elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
2849     site_packages_files+=("$1")
2850     break
2851     elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
2852 arfrever 1.101 eerror "${FUNCNAME}(): '$1' is not a regular file or a directory"
2853 arfrever 1.56 else
2854 arfrever 1.101 eerror "${FUNCNAME}(): '$1' does not exist"
2855 arfrever 1.56 fi
2856 arfrever 1.98 done
2857     fi
2858 arfrever 1.56 shift
2859     done
2860 hawking 1.37
2861 arfrever 1.56 # Set additional options.
2862     options+=("-q")
2863 liquidx 1.13
2864 arfrever 1.97 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
2865 arfrever 1.103 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})) || ((${#evaluated_dirs[@]})) || ((${#evaluated_files[@]})); then
2866 arfrever 1.56 return_code="0"
2867 djc 1.119 stderr=""
2868 djc 1.124 ebegin "Compilation and optimization of Python modules for $(python_get_implementation_and_version)"
2869 arfrever 1.103 if ((${#site_packages_dirs[@]})) || ((${#evaluated_dirs[@]})); then
2870 arfrever 1.56 for dir in "${site_packages_dirs[@]}"; do
2871 arfrever 1.103 dirs+=("${root}$(python_get_sitedir)/${dir}")
2872     done
2873     for dir in "${evaluated_dirs[@]}"; do
2874     eval "dirs+=(\"\${root}${dir}\")"
2875 arfrever 1.56 done
2876 djc 1.126 stderr+="${stderr:+$'\n'}$("$(PYTHON)" -m compileall "${options[@]}" "${dirs[@]}" 2>&1)" || return_code="1"
2877 djc 1.138 if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then
2878 djc 1.126 "$(PYTHON)" -O -m compileall "${options[@]}" "${dirs[@]}" &> /dev/null || return_code="1"
2879 arfrever 1.88 fi
2880 arfrever 1.103 _python_clean_compiled_modules "${dirs[@]}"
2881 arfrever 1.56 fi
2882 arfrever 1.103 if ((${#site_packages_files[@]})) || ((${#evaluated_files[@]})); then
2883 arfrever 1.56 for file in "${site_packages_files[@]}"; do
2884 arfrever 1.103 files+=("${root}$(python_get_sitedir)/${file}")
2885 arfrever 1.56 done
2886 arfrever 1.103 for file in "${evaluated_files[@]}"; do
2887     eval "files+=(\"\${root}${file}\")"
2888     done
2889 djc 1.126 stderr+="${stderr:+$'\n'}$("$(PYTHON)" -m py_compile "${files[@]}" 2>&1)" || return_code="1"
2890 djc 1.138 if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then
2891 djc 1.126 "$(PYTHON)" -O -m py_compile "${files[@]}" &> /dev/null || return_code="1"
2892 arfrever 1.88 fi
2893 arfrever 1.103 _python_clean_compiled_modules "${files[@]}"
2894 hawking 1.49 fi
2895 arfrever 1.56 eend "${return_code}"
2896 djc 1.119 if [[ -n "${stderr}" ]]; then
2897 djc 1.124 eerror "Syntax errors / warnings in Python modules for $(python_get_implementation_and_version):" &> /dev/null
2898 djc 1.119 while read stderr_line; do
2899     eerror " ${stderr_line}"
2900     done <<< "${stderr}"
2901     fi
2902 arfrever 1.56 fi
2903 arfrever 1.103 unset dirs files
2904 arfrever 1.56 done
2905    
2906 arfrever 1.97 if _python_package_supporting_installation_for_multiple_python_abis; then
2907     # Restore previous value of PYTHON_ABI.
2908     if [[ -n "${previous_PYTHON_ABI}" ]]; then
2909     PYTHON_ABI="${previous_PYTHON_ABI}"
2910     else
2911     unset PYTHON_ABI
2912     fi
2913 arfrever 1.86 fi
2914 hawking 1.36
2915 arfrever 1.56 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
2916     return_code="0"
2917 djc 1.119 stderr=""
2918 djc 1.124 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation_and_version)"
2919 arfrever 1.56 if ((${#other_dirs[@]})); then
2920 djc 1.126 stderr+="${stderr:+$'\n'}$("$(PYTHON ${PYTHON_ABI})" -m compileall "${options[@]}" "${other_dirs[@]}" 2>&1)" || return_code="1"
2921 djc 1.138 if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then
2922 djc 1.126 "$(PYTHON ${PYTHON_ABI})" -O -m compileall "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
2923 arfrever 1.88 fi
2924 arfrever 1.97 _python_clean_compiled_modules "${other_dirs[@]}"
2925 arfrever 1.56 fi
2926     if ((${#other_files[@]})); then
2927 djc 1.126 stderr+="${stderr:+$'\n'}$("$(PYTHON ${PYTHON_ABI})" -m py_compile "${other_files[@]}" 2>&1)" || return_code="1"
2928 djc 1.138 if ! has "$(_python_get_implementation "${PYTHON_ABI}")" Jython PyPy; then
2929 djc 1.126 "$(PYTHON ${PYTHON_ABI})" -O -m py_compile "${other_files[@]}" &> /dev/null || return_code="1"
2930 arfrever 1.88 fi
2931 arfrever 1.104 _python_clean_compiled_modules "${other_files[@]}"
2932 arfrever 1.56 fi
2933     eend "${return_code}"
2934 djc 1.119 if [[ -n "${stderr}" ]]; then
2935 djc 1.124 eerror "Syntax errors / warnings in Python modules placed outside of site-packages directories for $(python_get_implementation_and_version):" &> /dev/null
2936 djc 1.119 while read stderr_line; do
2937     eerror " ${stderr_line}"
2938     done <<< "${stderr}"
2939     fi
2940 arfrever 1.56 fi
2941 pythonhead 1.7 else
2942 arfrever 1.98 # Deprecated part of python_mod_optimize()
2943 djc 1.112 ewarn
2944     ewarn "Deprecation Warning: Usage of ${FUNCNAME}() in packages not supporting installation"
2945     ewarn "for multiple Python ABIs in EAPI <=2 is deprecated and will be disallowed on 2011-08-01."
2946     ewarn "Use EAPI >=3 and call ${FUNCNAME}() with paths having appropriate syntax."
2947     ewarn "The ebuild needs to be fixed. Please report a bug, if it has not been already reported."
2948     ewarn
2949 arfrever 1.98
2950 arfrever 1.63 local myroot mydirs=() myfiles=() myopts=() return_code="0"
2951 arfrever 1.56
2952     # strip trailing slash
2953 arfrever 1.87 myroot="${EROOT%/}"
2954 arfrever 1.56
2955 arfrever 1.97 # respect EROOT and options passed to compileall.py
2956 arfrever 1.56 while (($#)); do
2957     case "$1" in
2958     -l|-f|-q)
2959     myopts+=("$1")
2960     ;;
2961     -d|-x)
2962     myopts+=("$1" "$2")
2963     shift
2964     ;;
2965 arfrever 1.101 --)
2966     shift
2967     break
2968     ;;
2969 arfrever 1.56 -*)
2970 arfrever 1.101 die "${FUNCNAME}(): Unrecognized option '$1'"
2971 arfrever 1.56 ;;
2972     *)
2973 arfrever 1.98 break
2974 arfrever 1.56 ;;
2975     esac
2976     shift
2977     done
2978    
2979 arfrever 1.98 if [[ "$#" -eq 0 ]]; then
2980 arfrever 1.104 die "${FUNCNAME}(): Missing files or directories"
2981 arfrever 1.98 fi
2982    
2983     while (($#)); do
2984 arfrever 1.101 if [[ "$1" =~ ^($|(\.|\.\.|/)($|/)) ]]; then
2985     die "${FUNCNAME}(): Invalid argument '$1'"
2986     elif [[ -d "${myroot}/${1#/}" ]]; then
2987 arfrever 1.98 mydirs+=("${myroot}/${1#/}")
2988     elif [[ -f "${myroot}/${1#/}" ]]; then
2989 arfrever 1.104 myfiles+=("${myroot}/${1#/}")
2990 arfrever 1.98 elif [[ -e "${myroot}/${1#/}" ]]; then
2991 arfrever 1.101 eerror "${FUNCNAME}(): ${myroot}/${1#/} is not a regular file or directory"
2992 arfrever 1.98 else
2993 arfrever 1.101 eerror "${FUNCNAME}(): ${myroot}/${1#/} does not exist"
2994 arfrever 1.98 fi
2995     shift
2996     done
2997    
2998 arfrever 1.56 # set additional opts
2999     myopts+=(-q)
3000 pythonhead 1.7
3001 arfrever 1.98 PYTHON_ABI="${PYTHON_ABI:-$(PYTHON --ABI)}"
3002 arfrever 1.97
3003 arfrever 1.89 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
3004 arfrever 1.56 if ((${#mydirs[@]})); then
3005 arfrever 1.90 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
3006     "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
3007 arfrever 1.97 _python_clean_compiled_modules "${mydirs[@]}"
3008 arfrever 1.56 fi
3009 pythonhead 1.7
3010 arfrever 1.56 if ((${#myfiles[@]})); then
3011 arfrever 1.104 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" || return_code="1"
3012     "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null || return_code="1"
3013     _python_clean_compiled_modules "${myfiles[@]}"
3014 arfrever 1.56 fi
3015 hawking 1.36
3016 arfrever 1.63 eend "${return_code}"
3017 hawking 1.36 fi
3018 liquidx 1.1 }
3019    
3020 dev-zero 1.33 # @FUNCTION: python_mod_cleanup
3021 arfrever 1.104 # @USAGE: [--allow-evaluated-non-sitedir-paths] [--] <file|directory> [files|directories]
3022 dev-zero 1.33 # @DESCRIPTION:
3023 arfrever 1.104 # Delete orphaned byte-compiled Python modules corresponding to specified Python modules.
3024 hawking 1.37 #
3025 arfrever 1.92 # This function can be used only in pkg_postrm() phase.
3026 liquidx 1.1 python_mod_cleanup() {
3027 djc 1.114 if [[ "${EBUILD_PHASE}" != "postrm" ]]; then
3028     die "${FUNCNAME}() can be used only in pkg_postrm() phase"
3029     fi
3030    
3031 arfrever 1.101 _python_check_python_pkg_setup_execution
3032 arfrever 1.87 _python_initialize_prefix_variables
3033    
3034 arfrever 1.103 local allow_evaluated_non_sitedir_paths="0" dir iterated_PYTHON_ABIS PYTHON_ABI="${PYTHON_ABI}" root search_paths=() sitedir
3035 liquidx 1.16
3036 arfrever 1.97 if _python_package_supporting_installation_for_multiple_python_abis; then
3037 arfrever 1.101 if has "${EAPI:-0}" 0 1 2 3 && [[ -z "${PYTHON_ABIS}" ]]; then
3038     die "${FUNCNAME}(): python_pkg_setup() or python_execute_function() not called"
3039 arfrever 1.97 fi
3040     iterated_PYTHON_ABIS="${PYTHON_ABIS}"
3041     else
3042 arfrever 1.101 if has "${EAPI:-0}" 0 1 2 3; then
3043     iterated_PYTHON_ABIS="${PYTHON_ABI:-$(PYTHON --ABI)}"
3044     else
3045     iterated_PYTHON_ABIS="${PYTHON_ABI}"
3046     fi
3047 arfrever 1.97 fi
3048    
3049     # Strip trailing slash from EROOT.
3050 arfrever 1.87 root="${EROOT%/}"
3051 liquidx 1.1
3052 arfrever 1.103 while (($#)); do
3053     case "$1" in
3054     --allow-evaluated-non-sitedir-paths)
3055     allow_evaluated_non_sitedir_paths="1"
3056     ;;
3057     --)
3058     shift
3059     break
3060     ;;
3061     -*)
3062     die "${FUNCNAME}(): Unrecognized option '$1'"
3063     ;;
3064     *)
3065     break
3066     ;;
3067     esac
3068     shift
3069     done
3070    
3071     if [[ "${allow_evaluated_non_sitedir_paths}" == "1" ]] && ! _python_package_supporting_installation_for_multiple_python_abis; then
3072     die "${FUNCNAME}(): '--allow-evaluated-non-sitedir-paths' option cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
3073     fi
3074    
3075 arfrever 1.104 if [[ "$#" -eq 0 ]]; then
3076     die "${FUNCNAME}(): Missing files or directories"
3077     fi
3078    
3079     if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis || _python_implementation || [[ "${CATEGORY}/${PN}" == "sys-apps/portage" ]]; then
3080     while (($#)); do
3081     if [[ "$1" =~ ^($|(\.|\.\.|/)($|/)) ]]; then
3082     die "${FUNCNAME}(): Invalid argument '$1'"
3083     elif ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
3084     die "${FUNCNAME}(): Paths of directories / files in site-packages directories must be relative to site-packages directories"
3085     elif [[ "$1" =~ ^/ ]]; then
3086     if _python_package_supporting_installation_for_multiple_python_abis; then
3087     if [[ "${allow_evaluated_non_sitedir_paths}" != "1" ]]; then
3088     die "${FUNCNAME}(): Absolute paths cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
3089     fi
3090     if [[ "$1" != *\$* ]]; then
3091     die "${FUNCNAME}(): '$1' has invalid syntax"
3092 arfrever 1.97 fi
3093     for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
3094 arfrever 1.104 eval "search_paths+=(\"\${root}$1\")"
3095 arfrever 1.56 done
3096 arfrever 1.104 else
3097     search_paths+=("${root}$1")
3098 arfrever 1.56 fi
3099 arfrever 1.104 else
3100     for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
3101     search_paths+=("${root}$(python_get_sitedir)/$1")
3102 arfrever 1.78 done
3103     fi
3104 arfrever 1.104 shift
3105 arfrever 1.78 done
3106 arfrever 1.104 else
3107     # Deprecated part of python_mod_cleanup()
3108 djc 1.112 ewarn
3109     ewarn "Deprecation Warning: Usage of ${FUNCNAME}() in packages not supporting installation"
3110     ewarn "for multiple Python ABIs in EAPI <=2 is deprecated and will be disallowed on 2011-08-01."
3111     ewarn "Use EAPI >=3 and call ${FUNCNAME}() with paths having appropriate syntax."
3112     ewarn "The ebuild needs to be fixed. Please report a bug, if it has not been already reported."
3113     ewarn
3114 arfrever 1.104
3115     search_paths=("${@#/}")
3116     search_paths=("${search_paths[@]/#/${root}/}")
3117 arfrever 1.78 fi
3118    
3119 arfrever 1.97 _python_clean_compiled_modules "${search_paths[@]}"
3120 liquidx 1.1 }
3121 arfrever 1.85
3122     # ================================================================================================
3123     # ===================================== DEPRECATED FUNCTIONS =====================================
3124     # ================================================================================================
3125 floppym 1.144
3126     fi # _PYTHON_ECLASS_INHERITED

  ViewVC Help
Powered by ViewVC 1.1.20