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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.104 - (hide annotations) (download)
Mon Oct 25 11:54:19 2010 UTC (4 years, 4 months ago) by arfrever
Branch: MAIN
Changes since 1.103: +258 -171 lines
Set IUSE in EAPI >=4.
Rename _parse_PYTHON_DEPEND() to _python_parse_PYTHON_DEPEND() and unset it after its using.
Ban NEED_PYTHON variable.
Add python_abi_depend().
Fix exporting of python_pkg_setup() in EAPI >=4.
Update and improve _python_calculate_PYTHON_ABIS().
Support EAPI="0" in python_get_implementational_package().
Use non-deprecated parts of python_mod_optimize() and python_mod_cleanup() for dev-lang/python and sys-apps/portage.
Disallow not passing of paths to Python modules to python_mod_optimize() and python_mod_cleanup().
Fix a typo in python_mod_optimize().
Ban python_mod_compile().

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

  ViewVC Help
Powered by ViewVC 1.1.20