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