media-plugins/gst-plugins-taglib-1.14.4: ppc stable, bug 674854
[gentoo.git] / eclass / python-utils-r1.eclass
1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: python-utils-r1.eclass
5 # @MAINTAINER:
6 # Python team <python@gentoo.org>
7 # @AUTHOR:
8 # Author: Michał Górny <mgorny@gentoo.org>
9 # Based on work of: Krzysztof Pawlik <nelchael@gentoo.org>
10 # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6 7
11 # @BLURB: Utility functions for packages with Python parts.
12 # @DESCRIPTION:
13 # A utility eclass providing functions to query Python implementations,
14 # install Python modules and scripts.
15 #
16 # This eclass does not set any metadata variables nor export any phase
17 # functions. It can be inherited safely.
18 #
19 # For more information, please see the wiki:
20 # https://wiki.gentoo.org/wiki/Project:Python/python-utils-r1
21
22 case "${EAPI:-0}" in
23         0|1|2|3|4|5|6|7)
24                 ;;
25         *)
26                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
27                 ;;
28 esac
29
30 if [[ ${_PYTHON_ECLASS_INHERITED} ]]; then
31         die 'python-r1 suite eclasses can not be used with python.eclass.'
32 fi
33
34 if [[ ! ${_PYTHON_UTILS_R1} ]]; then
35
36 [[ ${EAPI:-0} == [012345] ]] && inherit eutils multilib
37 inherit toolchain-funcs
38
39 # @ECLASS-VARIABLE: _PYTHON_ALL_IMPLS
40 # @INTERNAL
41 # @DESCRIPTION:
42 # All supported Python implementations, most preferred last.
43 _PYTHON_ALL_IMPLS=(
44         jython2_7
45         pypy pypy3
46         python2_7
47         python3_4 python3_5 python3_6 python3_7
48 )
49 readonly _PYTHON_ALL_IMPLS
50
51 # @ECLASS-VARIABLE: PYTHON_COMPAT_NO_STRICT
52 # @INTERNAL
53 # @DESCRIPTION:
54 # Set to a non-empty value in order to make eclass tolerate (ignore)
55 # unknown implementations in PYTHON_COMPAT.
56 #
57 # This is intended to be set by the user when using ebuilds that may
58 # have unknown (newer) implementations in PYTHON_COMPAT. The assumption
59 # is that the ebuilds are intended to be used within multiple contexts
60 # which can involve revisions of this eclass that support a different
61 # set of Python implementations.
62
63 # @FUNCTION: _python_impl_supported
64 # @USAGE: <impl>
65 # @INTERNAL
66 # @DESCRIPTION:
67 # Check whether the implementation <impl> (PYTHON_COMPAT-form)
68 # is still supported.
69 #
70 # Returns 0 if the implementation is valid and supported. If it is
71 # unsupported, returns 1 -- and the caller should ignore the entry.
72 # If it is invalid, dies with an appopriate error messages.
73 _python_impl_supported() {
74         debug-print-function ${FUNCNAME} "${@}"
75
76         [[ ${#} -eq 1 ]] || die "${FUNCNAME}: takes exactly 1 argument (impl)."
77
78         local impl=${1}
79
80         # keep in sync with _PYTHON_ALL_IMPLS!
81         # (not using that list because inline patterns shall be faster)
82         case "${impl}" in
83                 python2_7|python3_[4567]|jython2_7)
84                         return 0
85                         ;;
86                 pypy1_[89]|pypy2_0|python2_[56]|python3_[123])
87                         return 1
88                         ;;
89                 pypy|pypy3)
90                         if [[ ${EAPI:-0} == [01234] ]]; then
91                                 die "PyPy is supported in EAPI 5 and newer only."
92                         fi
93                         ;;
94                 *)
95                         [[ ${PYTHON_COMPAT_NO_STRICT} ]] && return 1
96                         die "Invalid implementation in PYTHON_COMPAT: ${impl}"
97         esac
98 }
99
100 # @FUNCTION: _python_set_impls
101 # @INTERNAL
102 # @DESCRIPTION:
103 # Check PYTHON_COMPAT for well-formedness and validity, then set
104 # two global variables:
105 #
106 # - _PYTHON_SUPPORTED_IMPLS containing valid implementations supported
107 #   by the ebuild (PYTHON_COMPAT - dead implementations),
108 #
109 # - and _PYTHON_UNSUPPORTED_IMPLS containing valid implementations that
110 #   are not supported by the ebuild.
111 #
112 # Implementations in both variables are ordered using the pre-defined
113 # eclass implementation ordering.
114 #
115 # This function must be called once in global scope by an eclass
116 # utilizing PYTHON_COMPAT.
117 _python_set_impls() {
118         local i
119
120         if ! declare -p PYTHON_COMPAT &>/dev/null; then
121                 die 'PYTHON_COMPAT not declared.'
122         fi
123         if [[ $(declare -p PYTHON_COMPAT) != "declare -a"* ]]; then
124                 die 'PYTHON_COMPAT must be an array.'
125         fi
126         for i in "${PYTHON_COMPAT[@]}"; do
127                 # trigger validity checks
128                 _python_impl_supported "${i}"
129         done
130
131         local supp=() unsupp=()
132
133         for i in "${_PYTHON_ALL_IMPLS[@]}"; do
134                 if has "${i}" "${PYTHON_COMPAT[@]}"; then
135                         supp+=( "${i}" )
136                 else
137                         unsupp+=( "${i}" )
138                 fi
139         done
140
141         if [[ ! ${supp[@]} ]]; then
142                 die "No supported implementation in PYTHON_COMPAT."
143         fi
144
145         if [[ ${_PYTHON_SUPPORTED_IMPLS[@]} ]]; then
146                 # set once already, verify integrity
147                 if [[ ${_PYTHON_SUPPORTED_IMPLS[@]} != ${supp[@]} ]]; then
148                         eerror "Supported impls (PYTHON_COMPAT) changed between inherits!"
149                         eerror "Before: ${_PYTHON_SUPPORTED_IMPLS[*]}"
150                         eerror "Now   : ${supp[*]}"
151                         die "_PYTHON_SUPPORTED_IMPLS integrity check failed"
152                 fi
153                 if [[ ${_PYTHON_UNSUPPORTED_IMPLS[@]} != ${unsupp[@]} ]]; then
154                         eerror "Unsupported impls changed between inherits!"
155                         eerror "Before: ${_PYTHON_UNSUPPORTED_IMPLS[*]}"
156                         eerror "Now   : ${unsupp[*]}"
157                         die "_PYTHON_UNSUPPORTED_IMPLS integrity check failed"
158                 fi
159         else
160                 _PYTHON_SUPPORTED_IMPLS=( "${supp[@]}" )
161                 _PYTHON_UNSUPPORTED_IMPLS=( "${unsupp[@]}" )
162                 readonly _PYTHON_SUPPORTED_IMPLS _PYTHON_UNSUPPORTED_IMPLS
163         fi
164 }
165
166 # @FUNCTION: _python_impl_matches
167 # @USAGE: <impl> <pattern>...
168 # @INTERNAL
169 # @DESCRIPTION:
170 # Check whether the specified <impl> matches at least one
171 # of the patterns following it. Return 0 if it does, 1 otherwise.
172 #
173 # <impl> can be in PYTHON_COMPAT or EPYTHON form. The patterns can be
174 # either:
175 # a) fnmatch-style patterns, e.g. 'python2*', 'pypy'...
176 # b) '-2' to indicate all Python 2 variants (= !python_is_python3)
177 # c) '-3' to indicate all Python 3 variants (= python_is_python3)
178 _python_impl_matches() {
179         [[ ${#} -ge 2 ]] || die "${FUNCNAME}: takes at least 2 parameters"
180
181         local impl=${1} pattern
182         shift
183
184         for pattern; do
185                 if [[ ${pattern} == -2 ]]; then
186                         ! python_is_python3 "${impl}"
187                         return
188                 elif [[ ${pattern} == -3 ]]; then
189                         python_is_python3 "${impl}"
190                         return
191                 # unify value style to allow lax matching
192                 elif [[ ${impl/./_} == ${pattern/./_} ]]; then
193                         return 0
194                 fi
195         done
196
197         return 1
198 }
199
200 # @ECLASS-VARIABLE: PYTHON
201 # @DEFAULT_UNSET
202 # @DESCRIPTION:
203 # The absolute path to the current Python interpreter.
204 #
205 # This variable is set automatically in the following contexts:
206 #
207 # python-r1: Set in functions called by python_foreach_impl() or after
208 # calling python_export_best().
209 #
210 # python-single-r1: Set after calling python-single-r1_pkg_setup().
211 #
212 # distutils-r1: Set within any of the python sub-phase functions.
213 #
214 # Example value:
215 # @CODE
216 # /usr/bin/python2.7
217 # @CODE
218
219 # @ECLASS-VARIABLE: EPYTHON
220 # @DEFAULT_UNSET
221 # @DESCRIPTION:
222 # The executable name of the current Python interpreter.
223 #
224 # This variable is set automatically in the following contexts:
225 #
226 # python-r1: Set in functions called by python_foreach_impl() or after
227 # calling python_export_best().
228 #
229 # python-single-r1: Set after calling python-single-r1_pkg_setup().
230 #
231 # distutils-r1: Set within any of the python sub-phase functions.
232 #
233 # Example value:
234 # @CODE
235 # python2.7
236 # @CODE
237
238 # @ECLASS-VARIABLE: PYTHON_SITEDIR
239 # @DEFAULT_UNSET
240 # @DESCRIPTION:
241 # The path to Python site-packages directory.
242 #
243 # Set and exported on request using python_export().
244 # Requires a proper build-time dependency on the Python implementation.
245 #
246 # Example value:
247 # @CODE
248 # /usr/lib64/python2.7/site-packages
249 # @CODE
250
251 # @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
252 # @DEFAULT_UNSET
253 # @DESCRIPTION:
254 # The path to Python include directory.
255 #
256 # Set and exported on request using python_export().
257 # Requires a proper build-time dependency on the Python implementation.
258 #
259 # Example value:
260 # @CODE
261 # /usr/include/python2.7
262 # @CODE
263
264 # @ECLASS-VARIABLE: PYTHON_LIBPATH
265 # @DEFAULT_UNSET
266 # @DESCRIPTION:
267 # The path to Python library.
268 #
269 # Set and exported on request using python_export().
270 # Valid only for CPython. Requires a proper build-time dependency
271 # on the Python implementation.
272 #
273 # Example value:
274 # @CODE
275 # /usr/lib64/libpython2.7.so
276 # @CODE
277
278 # @ECLASS-VARIABLE: PYTHON_CFLAGS
279 # @DEFAULT_UNSET
280 # @DESCRIPTION:
281 # Proper C compiler flags for building against Python. Obtained from
282 # pkg-config or python-config.
283 #
284 # Set and exported on request using python_export().
285 # Valid only for CPython. Requires a proper build-time dependency
286 # on the Python implementation and on pkg-config.
287 #
288 # Example value:
289 # @CODE
290 # -I/usr/include/python2.7
291 # @CODE
292
293 # @ECLASS-VARIABLE: PYTHON_LIBS
294 # @DEFAULT_UNSET
295 # @DESCRIPTION:
296 # Proper C compiler flags for linking against Python. Obtained from
297 # pkg-config or python-config.
298 #
299 # Set and exported on request using python_export().
300 # Valid only for CPython. Requires a proper build-time dependency
301 # on the Python implementation and on pkg-config.
302 #
303 # Example value:
304 # @CODE
305 # -lpython2.7
306 # @CODE
307
308 # @ECLASS-VARIABLE: PYTHON_CONFIG
309 # @DEFAULT_UNSET
310 # @DESCRIPTION:
311 # Path to the python-config executable.
312 #
313 # Set and exported on request using python_export().
314 # Valid only for CPython. Requires a proper build-time dependency
315 # on the Python implementation and on pkg-config.
316 #
317 # Example value:
318 # @CODE
319 # /usr/bin/python2.7-config
320 # @CODE
321
322 # @ECLASS-VARIABLE: PYTHON_PKG_DEP
323 # @DEFAULT_UNSET
324 # @DESCRIPTION:
325 # The complete dependency on a particular Python package as a string.
326 #
327 # Set and exported on request using python_export().
328 #
329 # Example value:
330 # @CODE
331 # dev-lang/python:2.7[xml]
332 # @CODE
333
334 # @ECLASS-VARIABLE: PYTHON_SCRIPTDIR
335 # @DEFAULT_UNSET
336 # @DESCRIPTION:
337 # The location where Python scripts must be installed for current impl.
338 #
339 # Set and exported on request using python_export().
340 #
341 # Example value:
342 # @CODE
343 # /usr/lib/python-exec/python2.7
344 # @CODE
345
346 # @FUNCTION: python_export
347 # @USAGE: [<impl>] <variables>...
348 # @DESCRIPTION:
349 # Set and export the Python implementation-relevant variables passed
350 # as parameters.
351 #
352 # The optional first parameter may specify the requested Python
353 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
354 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
355 # the current one will be obtained from ${EPYTHON}.
356 #
357 # The variables which can be exported are: PYTHON, EPYTHON,
358 # PYTHON_SITEDIR. They are described more completely in the eclass
359 # variable documentation.
360 python_export() {
361         debug-print-function ${FUNCNAME} "${@}"
362
363         local impl var
364
365         case "${1}" in
366                 python*|jython*)
367                         impl=${1/_/.}
368                         shift
369                         ;;
370                 pypy|pypy3)
371                         impl=${1}
372                         shift
373                         ;;
374                 *)
375                         impl=${EPYTHON}
376                         if [[ -z ${impl} ]]; then
377                                 die "python_export called without a python implementation and EPYTHON is unset"
378                         fi
379                         ;;
380         esac
381         debug-print "${FUNCNAME}: implementation: ${impl}"
382
383         for var; do
384                 case "${var}" in
385                         EPYTHON)
386                                 export EPYTHON=${impl}
387                                 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
388                                 ;;
389                         PYTHON)
390                                 export PYTHON=${EPREFIX}/usr/bin/${impl}
391                                 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
392                                 ;;
393                         PYTHON_SITEDIR)
394                                 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
395                                 # sysconfig can't be used because:
396                                 # 1) pypy doesn't give site-packages but stdlib
397                                 # 2) jython gives paths with wrong case
398                                 PYTHON_SITEDIR=$("${PYTHON}" -c 'import distutils.sysconfig; print(distutils.sysconfig.get_python_lib())') || die
399                                 export PYTHON_SITEDIR
400                                 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
401                                 ;;
402                         PYTHON_INCLUDEDIR)
403                                 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
404                                 PYTHON_INCLUDEDIR=$("${PYTHON}" -c 'import distutils.sysconfig; print(distutils.sysconfig.get_python_inc())') || die
405                                 export PYTHON_INCLUDEDIR
406                                 debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
407
408                                 # Jython gives a non-existing directory
409                                 if [[ ! -d ${PYTHON_INCLUDEDIR} ]]; then
410                                         die "${impl} does not install any header files!"
411                                 fi
412                                 ;;
413                         PYTHON_LIBPATH)
414                                 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
415                                 PYTHON_LIBPATH=$("${PYTHON}" -c 'import os.path, sysconfig; print(os.path.join(sysconfig.get_config_var("LIBDIR"), sysconfig.get_config_var("LDLIBRARY")) if sysconfig.get_config_var("LDLIBRARY") else "")') || die
416                                 export PYTHON_LIBPATH
417                                 debug-print "${FUNCNAME}: PYTHON_LIBPATH = ${PYTHON_LIBPATH}"
418
419                                 if [[ ! ${PYTHON_LIBPATH} ]]; then
420                                         die "${impl} lacks a (usable) dynamic library"
421                                 fi
422                                 ;;
423                         PYTHON_CFLAGS)
424                                 local val
425
426                                 case "${impl}" in
427                                         python*)
428                                                 # python-2.7, python-3.2, etc.
429                                                 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-}) || die
430                                                 ;;
431                                         *)
432                                                 die "${impl}: obtaining ${var} not supported"
433                                                 ;;
434                                 esac
435
436                                 export PYTHON_CFLAGS=${val}
437                                 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
438                                 ;;
439                         PYTHON_LIBS)
440                                 local val
441
442                                 case "${impl}" in
443                                         python*)
444                                                 # python-2.7, python-3.2, etc.
445                                                 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-}) || die
446                                                 ;;
447                                         *)
448                                                 die "${impl}: obtaining ${var} not supported"
449                                                 ;;
450                                 esac
451
452                                 export PYTHON_LIBS=${val}
453                                 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
454                                 ;;
455                         PYTHON_CONFIG)
456                                 local flags val
457
458                                 case "${impl}" in
459                                         python*)
460                                                 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
461                                                 flags=$("${PYTHON}" -c 'import sysconfig; print(sysconfig.get_config_var("ABIFLAGS") or "")') || die
462                                                 val=${PYTHON}${flags}-config
463                                                 ;;
464                                         *)
465                                                 die "${impl}: obtaining ${var} not supported"
466                                                 ;;
467                                 esac
468
469                                 export PYTHON_CONFIG=${val}
470                                 debug-print "${FUNCNAME}: PYTHON_CONFIG = ${PYTHON_CONFIG}"
471                                 ;;
472                         PYTHON_PKG_DEP)
473                                 local d
474                                 case ${impl} in
475                                         python2.7)
476                                                 PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
477                                         python3.3)
478                                                 PYTHON_PKG_DEP='>=dev-lang/python-3.3.2-r2:3.3';;
479                                         python*)
480                                                 PYTHON_PKG_DEP="dev-lang/python:${impl#python}";;
481                                         pypy)
482                                                 PYTHON_PKG_DEP='>=virtual/pypy-5:0=';;
483                                         pypy3)
484                                                 PYTHON_PKG_DEP='>=virtual/pypy3-5:0=';;
485                                         jython2.7)
486                                                 PYTHON_PKG_DEP='dev-java/jython:2.7';;
487                                         *)
488                                                 die "Invalid implementation: ${impl}"
489                                 esac
490
491                                 # use-dep
492                                 if [[ ${PYTHON_REQ_USE} ]]; then
493                                         PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
494                                 fi
495
496                                 export PYTHON_PKG_DEP
497                                 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
498                                 ;;
499                         PYTHON_SCRIPTDIR)
500                                 local dir
501                                 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
502                                 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
503                                 ;;
504                         *)
505                                 die "python_export: unknown variable ${var}"
506                 esac
507         done
508 }
509
510 # @FUNCTION: python_get_sitedir
511 # @USAGE: [<impl>]
512 # @DESCRIPTION:
513 # Obtain and print the 'site-packages' path for the given
514 # implementation. If no implementation is provided, ${EPYTHON} will
515 # be used.
516 #
517 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
518 # better to use python_export() directly instead.
519 python_get_sitedir() {
520         debug-print-function ${FUNCNAME} "${@}"
521
522         python_export "${@}" PYTHON_SITEDIR
523         echo "${PYTHON_SITEDIR}"
524 }
525
526 # @FUNCTION: python_get_includedir
527 # @USAGE: [<impl>]
528 # @DESCRIPTION:
529 # Obtain and print the include path for the given implementation. If no
530 # implementation is provided, ${EPYTHON} will be used.
531 #
532 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
533 # is better to use python_export() directly instead.
534 python_get_includedir() {
535         debug-print-function ${FUNCNAME} "${@}"
536
537         python_export "${@}" PYTHON_INCLUDEDIR
538         echo "${PYTHON_INCLUDEDIR}"
539 }
540
541 # @FUNCTION: python_get_library_path
542 # @USAGE: [<impl>]
543 # @DESCRIPTION:
544 # Obtain and print the Python library path for the given implementation.
545 # If no implementation is provided, ${EPYTHON} will be used.
546 #
547 # Please note that this function can be used with CPython only. Use
548 # in another implementation will result in a fatal failure.
549 python_get_library_path() {
550         debug-print-function ${FUNCNAME} "${@}"
551
552         python_export "${@}" PYTHON_LIBPATH
553         echo "${PYTHON_LIBPATH}"
554 }
555
556 # @FUNCTION: python_get_CFLAGS
557 # @USAGE: [<impl>]
558 # @DESCRIPTION:
559 # Obtain and print the compiler flags for building against Python,
560 # for the given implementation. If no implementation is provided,
561 # ${EPYTHON} will be used.
562 #
563 # Please note that this function can be used with CPython only.
564 # It requires Python and pkg-config installed, and therefore proper
565 # build-time dependencies need be added to the ebuild.
566 python_get_CFLAGS() {
567         debug-print-function ${FUNCNAME} "${@}"
568
569         python_export "${@}" PYTHON_CFLAGS
570         echo "${PYTHON_CFLAGS}"
571 }
572
573 # @FUNCTION: python_get_LIBS
574 # @USAGE: [<impl>]
575 # @DESCRIPTION:
576 # Obtain and print the compiler flags for linking against Python,
577 # for the given implementation. If no implementation is provided,
578 # ${EPYTHON} will be used.
579 #
580 # Please note that this function can be used with CPython only.
581 # It requires Python and pkg-config installed, and therefore proper
582 # build-time dependencies need be added to the ebuild.
583 python_get_LIBS() {
584         debug-print-function ${FUNCNAME} "${@}"
585
586         python_export "${@}" PYTHON_LIBS
587         echo "${PYTHON_LIBS}"
588 }
589
590 # @FUNCTION: python_get_PYTHON_CONFIG
591 # @USAGE: [<impl>]
592 # @DESCRIPTION:
593 # Obtain and print the PYTHON_CONFIG location for the given
594 # implementation. If no implementation is provided, ${EPYTHON} will be
595 # used.
596 #
597 # Please note that this function can be used with CPython only.
598 # It requires Python installed, and therefore proper build-time
599 # dependencies need be added to the ebuild.
600 python_get_PYTHON_CONFIG() {
601         debug-print-function ${FUNCNAME} "${@}"
602
603         python_export "${@}" PYTHON_CONFIG
604         echo "${PYTHON_CONFIG}"
605 }
606
607 # @FUNCTION: python_get_scriptdir
608 # @USAGE: [<impl>]
609 # @DESCRIPTION:
610 # Obtain and print the script install path for the given
611 # implementation. If no implementation is provided, ${EPYTHON} will
612 # be used.
613 python_get_scriptdir() {
614         debug-print-function ${FUNCNAME} "${@}"
615
616         python_export "${@}" PYTHON_SCRIPTDIR
617         echo "${PYTHON_SCRIPTDIR}"
618 }
619
620 # @FUNCTION: _python_ln_rel
621 # @USAGE: <from> <to>
622 # @INTERNAL
623 # @DESCRIPTION:
624 # Create a relative symlink.
625 _python_ln_rel() {
626         debug-print-function ${FUNCNAME} "${@}"
627
628         local target=${1}
629         local symname=${2}
630
631         local tgpath=${target%/*}/
632         local sympath=${symname%/*}/
633         local rel_target=
634
635         while [[ ${sympath} ]]; do
636                 local tgseg= symseg=
637
638                 while [[ ! ${tgseg} && ${tgpath} ]]; do
639                         tgseg=${tgpath%%/*}
640                         tgpath=${tgpath#${tgseg}/}
641                 done
642
643                 while [[ ! ${symseg} && ${sympath} ]]; do
644                         symseg=${sympath%%/*}
645                         sympath=${sympath#${symseg}/}
646                 done
647
648                 if [[ ${tgseg} != ${symseg} ]]; then
649                         rel_target=../${rel_target}${tgseg:+${tgseg}/}
650                 fi
651         done
652         rel_target+=${tgpath}${target##*/}
653
654         debug-print "${FUNCNAME}: ${symname} -> ${target}"
655         debug-print "${FUNCNAME}: rel_target = ${rel_target}"
656
657         ln -fs "${rel_target}" "${symname}"
658 }
659
660 # @FUNCTION: python_optimize
661 # @USAGE: [<directory>...]
662 # @DESCRIPTION:
663 # Compile and optimize Python modules in specified directories (absolute
664 # paths). If no directories are provided, the default system paths
665 # are used (prepended with ${D}).
666 python_optimize() {
667         debug-print-function ${FUNCNAME} "${@}"
668
669         if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
670                 eerror "The new Python eclasses expect the compiled Python files to"
671                 eerror "be controlled by the Package Manager. For this reason,"
672                 eerror "the python_optimize function can be used only during src_* phases"
673                 eerror "(src_install most commonly) and not during pkg_* phases."
674                 echo
675                 die "python_optimize is not to be used in pre/post* phases"
676         fi
677
678         [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
679
680         local PYTHON=${PYTHON}
681         [[ ${PYTHON} ]] || python_export PYTHON
682
683         # default to sys.path
684         if [[ ${#} -eq 0 ]]; then
685                 local f
686                 while IFS= read -r -d '' f; do
687                         # 1) accept only absolute paths
688                         #    (i.e. skip '', '.' or anything like that)
689                         # 2) skip paths which do not exist
690                         #    (python2.6 complains about them verbosely)
691
692                         if [[ ${f} == /* && -d ${D%/}${f} ]]; then
693                                 set -- "${D%/}${f}" "${@}"
694                         fi
695                 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))' || die)
696
697                 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
698         fi
699
700         local d
701         for d; do
702                 # make sure to get a nice path without //
703                 local instpath=${d#${D%/}}
704                 instpath=/${instpath##/}
705
706                 case "${EPYTHON}" in
707                         python2.7|python3.[34])
708                                 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
709                                 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
710                                 ;;
711                         python*|pypy3)
712                                 # both levels of optimization are separate since 3.5
713                                 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
714                                 "${PYTHON}" -O -m compileall -q -f -d "${instpath}" "${d}"
715                                 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
716                                 ;;
717                         *)
718                                 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
719                                 ;;
720                 esac
721         done
722 }
723
724 # @FUNCTION: python_scriptinto
725 # @USAGE: <new-path>
726 # @DESCRIPTION:
727 # Set the directory to which files passed to python_doexe(),
728 # python_doscript(), python_newexe() and python_newscript()
729 # are going to be installed. The new value needs to be relative
730 # to the installation root (${ED}).
731 #
732 # If not set explicitly, the directory defaults to /usr/bin.
733 #
734 # Example:
735 # @CODE
736 # src_install() {
737 #   python_scriptinto /usr/sbin
738 #   python_foreach_impl python_doscript foo
739 # }
740 # @CODE
741 python_scriptinto() {
742         debug-print-function ${FUNCNAME} "${@}"
743
744         python_scriptroot=${1}
745 }
746
747 # @FUNCTION: python_doexe
748 # @USAGE: <files>...
749 # @DESCRIPTION:
750 # Install the given executables into the executable install directory,
751 # for the current Python implementation (${EPYTHON}).
752 #
753 # The executable will be wrapped properly for the Python implementation,
754 # though no shebang mangling will be performed.
755 python_doexe() {
756         debug-print-function ${FUNCNAME} "${@}"
757
758         local f
759         for f; do
760                 python_newexe "${f}" "${f##*/}"
761         done
762 }
763
764 # @FUNCTION: python_newexe
765 # @USAGE: <path> <new-name>
766 # @DESCRIPTION:
767 # Install the given executable into the executable install directory,
768 # for the current Python implementation (${EPYTHON}).
769 #
770 # The executable will be wrapped properly for the Python implementation,
771 # though no shebang mangling will be performed. It will be renamed
772 # to <new-name>.
773 python_newexe() {
774         debug-print-function ${FUNCNAME} "${@}"
775
776         [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
777         [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
778         if [[ ${EAPI:-0} == [0123] ]]; then
779                 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
780         fi
781
782         local wrapd=${python_scriptroot:-/usr/bin}
783
784         local f=${1}
785         local newfn=${2}
786
787         local PYTHON_SCRIPTDIR d
788         python_export PYTHON_SCRIPTDIR
789         d=${PYTHON_SCRIPTDIR#${EPREFIX}}
790
791         (
792                 dodir "${wrapd}"
793                 exeopts -m 0755
794                 exeinto "${d}"
795                 newexe "${f}" "${newfn}" || return ${?}
796         )
797
798         # install the wrapper
799         _python_ln_rel "${ED%/}"/usr/lib/python-exec/python-exec2 \
800                 "${ED%/}/${wrapd}/${newfn}" || die
801
802         # don't use this at home, just call python_doscript() instead
803         if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
804                 python_fix_shebang -q "${ED%/}/${d}/${newfn}"
805         fi
806 }
807
808 # @FUNCTION: python_doscript
809 # @USAGE: <files>...
810 # @DESCRIPTION:
811 # Install the given scripts into the executable install directory,
812 # for the current Python implementation (${EPYTHON}).
813 #
814 # All specified files must start with a 'python' shebang. The shebang
815 # will be converted, and the files will be wrapped properly
816 # for the Python implementation.
817 #
818 # Example:
819 # @CODE
820 # src_install() {
821 #   python_foreach_impl python_doscript ${PN}
822 # }
823 # @CODE
824 python_doscript() {
825         debug-print-function ${FUNCNAME} "${@}"
826
827         local _PYTHON_REWRITE_SHEBANG=1
828         python_doexe "${@}"
829 }
830
831 # @FUNCTION: python_newscript
832 # @USAGE: <path> <new-name>
833 # @DESCRIPTION:
834 # Install the given script into the executable install directory
835 # for the current Python implementation (${EPYTHON}), and name it
836 # <new-name>.
837 #
838 # The file must start with a 'python' shebang. The shebang will be
839 # converted, and the file will be wrapped properly for the Python
840 # implementation. It will be renamed to <new-name>.
841 #
842 # Example:
843 # @CODE
844 # src_install() {
845 #   python_foreach_impl python_newscript foo.py foo
846 # }
847 # @CODE
848 python_newscript() {
849         debug-print-function ${FUNCNAME} "${@}"
850
851         local _PYTHON_REWRITE_SHEBANG=1
852         python_newexe "${@}"
853 }
854
855 # @FUNCTION: python_moduleinto
856 # @USAGE: <new-path>
857 # @DESCRIPTION:
858 # Set the Python module install directory for python_domodule().
859 # The <new-path> can either be an absolute target system path (in which
860 # case it needs to start with a slash, and ${ED} will be prepended to
861 # it) or relative to the implementation's site-packages directory
862 # (then it must not start with a slash). The relative path can be
863 # specified either using the Python package notation (separated by dots)
864 # or the directory notation (using slashes).
865 #
866 # When not set explicitly, the modules are installed to the top
867 # site-packages directory.
868 #
869 # In the relative case, the exact path is determined directly
870 # by each python_doscript/python_newscript function. Therefore,
871 # python_moduleinto can be safely called before establishing the Python
872 # interpreter and/or a single call can be used to set the path correctly
873 # for multiple implementations, as can be seen in the following example.
874 #
875 # Example:
876 # @CODE
877 # src_install() {
878 #   python_moduleinto bar
879 #   # installs ${PYTHON_SITEDIR}/bar/baz.py
880 #   python_foreach_impl python_domodule baz.py
881 # }
882 # @CODE
883 python_moduleinto() {
884         debug-print-function ${FUNCNAME} "${@}"
885
886         python_moduleroot=${1}
887 }
888
889 # @FUNCTION: python_domodule
890 # @USAGE: <files>...
891 # @DESCRIPTION:
892 # Install the given modules (or packages) into the current Python module
893 # installation directory. The list can mention both modules (files)
894 # and packages (directories). All listed files will be installed
895 # for all enabled implementations, and compiled afterwards.
896 #
897 # Example:
898 # @CODE
899 # src_install() {
900 #   # (${PN} being a directory)
901 #   python_foreach_impl python_domodule ${PN}
902 # }
903 # @CODE
904 python_domodule() {
905         debug-print-function ${FUNCNAME} "${@}"
906
907         [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
908         if [[ ${EAPI:-0} == [0123] ]]; then
909                 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
910         fi
911
912         local d
913         if [[ ${python_moduleroot} == /* ]]; then
914                 # absolute path
915                 d=${python_moduleroot}
916         else
917                 # relative to site-packages
918                 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
919                 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
920
921                 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot//.//}
922         fi
923
924         (
925                 insopts -m 0644
926                 insinto "${d}"
927                 doins -r "${@}" || return ${?}
928         )
929
930         python_optimize "${ED%/}/${d}"
931 }
932
933 # @FUNCTION: python_doheader
934 # @USAGE: <files>...
935 # @DESCRIPTION:
936 # Install the given headers into the implementation-specific include
937 # directory. This function is unconditionally recursive, i.e. you can
938 # pass directories instead of files.
939 #
940 # Example:
941 # @CODE
942 # src_install() {
943 #   python_foreach_impl python_doheader foo.h bar.h
944 # }
945 # @CODE
946 python_doheader() {
947         debug-print-function ${FUNCNAME} "${@}"
948
949         [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
950         if [[ ${EAPI:-0} == [0123] ]]; then
951                 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
952         fi
953
954         local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
955         [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
956
957         d=${PYTHON_INCLUDEDIR#${EPREFIX}}
958
959         (
960                 insopts -m 0644
961                 insinto "${d}"
962                 doins -r "${@}" || return ${?}
963         )
964 }
965
966 # @FUNCTION: python_wrapper_setup
967 # @USAGE: [<path> [<impl>]]
968 # @DESCRIPTION:
969 # Create proper 'python' executable and pkg-config wrappers
970 # (if available) in the directory named by <path>. Set up PATH
971 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
972 #
973 # The wrappers will be created for implementation named by <impl>,
974 # or for one named by ${EPYTHON} if no <impl> passed.
975 #
976 # If the named directory contains a python symlink already, it will
977 # be assumed to contain proper wrappers already and only environment
978 # setup will be done. If wrapper update is requested, the directory
979 # shall be removed first.
980 python_wrapper_setup() {
981         debug-print-function ${FUNCNAME} "${@}"
982
983         local workdir=${1:-${T}/${EPYTHON}}
984         local impl=${2:-${EPYTHON}}
985
986         [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
987         [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
988
989         if [[ ! -x ${workdir}/bin/python ]]; then
990                 _python_check_dead_variables
991
992                 mkdir -p "${workdir}"/{bin,pkgconfig} || die
993
994                 # Clean up, in case we were supposed to do a cheap update.
995                 rm -f "${workdir}"/bin/python{,2,3}{,-config} || die
996                 rm -f "${workdir}"/bin/2to3 || die
997                 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc || die
998
999                 local EPYTHON PYTHON
1000                 python_export "${impl}" EPYTHON PYTHON
1001
1002                 local pyver pyother
1003                 if python_is_python3; then
1004                         pyver=3
1005                         pyother=2
1006                 else
1007                         pyver=2
1008                         pyother=3
1009                 fi
1010
1011                 # Python interpreter
1012                 # note: we don't use symlinks because python likes to do some
1013                 # symlink reading magic that breaks stuff
1014                 # https://bugs.gentoo.org/show_bug.cgi?id=555752
1015                 cat > "${workdir}/bin/python" <<-_EOF_ || die
1016                         #!/bin/sh
1017                         exec "${PYTHON}" "\${@}"
1018                 _EOF_
1019                 cp "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
1020                 chmod +x "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
1021
1022                 local nonsupp=( "python${pyother}" "python${pyother}-config" )
1023
1024                 # CPython-specific
1025                 if [[ ${EPYTHON} == python* ]]; then
1026                         cat > "${workdir}/bin/python-config" <<-_EOF_ || die
1027                                 #!/bin/sh
1028                                 exec "${PYTHON}-config" "\${@}"
1029                         _EOF_
1030                         cp "${workdir}/bin/python-config" \
1031                                 "${workdir}/bin/python${pyver}-config" || die
1032                         chmod +x "${workdir}/bin/python-config" \
1033                                 "${workdir}/bin/python${pyver}-config" || die
1034
1035                         # Python 2.6+.
1036                         ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
1037
1038                         # Python 2.7+.
1039                         ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
1040                                 "${workdir}"/pkgconfig/python.pc || die
1041                         ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
1042                 else
1043                         nonsupp+=( 2to3 python-config "python${pyver}-config" )
1044                 fi
1045
1046                 local x
1047                 for x in "${nonsupp[@]}"; do
1048                         cat >"${workdir}"/bin/${x} <<-_EOF_ || die
1049                                 #!/bin/sh
1050                                 echo "${ECLASS}: ${FUNCNAME}: ${x} is not supported by ${EPYTHON} (PYTHON_COMPAT)" >&2
1051                                 exit 127
1052                         _EOF_
1053                         chmod +x "${workdir}"/bin/${x} || die
1054                 done
1055         fi
1056
1057         # Now, set the environment.
1058         # But note that ${workdir} may be shared with something else,
1059         # and thus already on top of PATH.
1060         if [[ ${PATH##:*} != ${workdir}/bin ]]; then
1061                 PATH=${workdir}/bin${PATH:+:${PATH}}
1062         fi
1063         if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
1064                 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
1065         fi
1066         export PATH PKG_CONFIG_PATH
1067 }
1068
1069 # @FUNCTION: python_is_python3
1070 # @USAGE: [<impl>]
1071 # @DESCRIPTION:
1072 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
1073 # (i.e. uses syntax and stdlib of Python 3.*).
1074 #
1075 # Returns 0 (true) if it is, 1 (false) otherwise.
1076 python_is_python3() {
1077         local impl=${1:-${EPYTHON}}
1078         [[ ${impl} ]] || die "python_is_python3: no impl nor EPYTHON"
1079
1080         [[ ${impl} == python3* || ${impl} == pypy3 ]]
1081 }
1082
1083 # @FUNCTION: python_is_installed
1084 # @USAGE: [<impl>]
1085 # @DESCRIPTION:
1086 # Check whether the interpreter for <impl> (or ${EPYTHON}) is installed.
1087 # Uses has_version with a proper dependency string.
1088 #
1089 # Returns 0 (true) if it is, 1 (false) otherwise.
1090 python_is_installed() {
1091         local impl=${1:-${EPYTHON}}
1092         [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
1093         local hasv_args=()
1094
1095         case ${EAPI:-0} in
1096                 0|1|2|3|4)
1097                         local -x ROOT=/
1098                         ;;
1099                 5|6)
1100                         hasv_args+=( --host-root )
1101                         ;;
1102                 *)
1103                         hasv_args+=( -b )
1104                         ;;
1105         esac
1106
1107         case "${impl}" in
1108                 pypy|pypy3)
1109                         local append=
1110                         if [[ ${PYTHON_REQ_USE} ]]; then
1111                                 append=[${PYTHON_REQ_USE}]
1112                         fi
1113
1114                         # be happy with just the interpeter, no need for the virtual
1115                         has_version "${hasv_args[@]}" "dev-python/${impl}${append}" \
1116                                 || has_version "${hasv_args[@]}" "dev-python/${impl}-bin${append}"
1117                         ;;
1118                 *)
1119                         local PYTHON_PKG_DEP
1120                         python_export "${impl}" PYTHON_PKG_DEP
1121                         has_version "${hasv_args[@]}" "${PYTHON_PKG_DEP}"
1122                         ;;
1123         esac
1124 }
1125
1126 # @FUNCTION: python_fix_shebang
1127 # @USAGE: [-f|--force] [-q|--quiet] <path>...
1128 # @DESCRIPTION:
1129 # Replace the shebang in Python scripts with the current Python
1130 # implementation (EPYTHON). If a directory is passed, works recursively
1131 # on all Python scripts.
1132 #
1133 # Only files having a 'python*' shebang will be modified. Files with
1134 # other shebang will either be skipped when working recursively
1135 # on a directory or treated as error when specified explicitly.
1136 #
1137 # Shebangs matching explicitly current Python version will be left
1138 # unmodified. Shebangs requesting another Python version will be treated
1139 # as fatal error, unless --force is given.
1140 #
1141 # --force causes the function to replace even shebangs that require
1142 # incompatible Python version. --quiet causes the function not to list
1143 # modified files verbosely.
1144 python_fix_shebang() {
1145         debug-print-function ${FUNCNAME} "${@}"
1146
1147         [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
1148
1149         local force quiet
1150         while [[ ${@} ]]; do
1151                 case "${1}" in
1152                         -f|--force) force=1; shift;;
1153                         -q|--quiet) quiet=1; shift;;
1154                         --) shift; break;;
1155                         *) break;;
1156                 esac
1157         done
1158
1159         [[ ${1} ]] || die "${FUNCNAME}: no paths given"
1160
1161         local path f
1162         for path; do
1163                 local any_correct any_fixed is_recursive
1164
1165                 [[ -d ${path} ]] && is_recursive=1
1166
1167                 while IFS= read -r -d '' f; do
1168                         local shebang i
1169                         local error= from=
1170
1171                         # note: we can't ||die here since read will fail if file
1172                         # has no newline characters
1173                         IFS= read -r shebang <"${f}"
1174
1175                         # First, check if it's shebang at all...
1176                         if [[ ${shebang} == '#!'* ]]; then
1177                                 local split_shebang=()
1178                                 read -r -a split_shebang <<<${shebang} || die
1179
1180                                 # Match left-to-right in a loop, to avoid matching random
1181                                 # repetitions like 'python2.7 python2'.
1182                                 for i in "${split_shebang[@]}"; do
1183                                         case "${i}" in
1184                                                 *"${EPYTHON}")
1185                                                         debug-print "${FUNCNAME}: in file ${f#${D%/}}"
1186                                                         debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
1187
1188                                                         # Nothing to do, move along.
1189                                                         any_correct=1
1190                                                         from=${EPYTHON}
1191                                                         break
1192                                                         ;;
1193                                                 *python|*python[23])
1194                                                         debug-print "${FUNCNAME}: in file ${f#${D%/}}"
1195                                                         debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
1196
1197                                                         if [[ ${i} == *python2 ]]; then
1198                                                                 from=python2
1199                                                                 if [[ ! ${force} ]]; then
1200                                                                         python_is_python3 "${EPYTHON}" && error=1
1201                                                                 fi
1202                                                         elif [[ ${i} == *python3 ]]; then
1203                                                                 from=python3
1204                                                                 if [[ ! ${force} ]]; then
1205                                                                         python_is_python3 "${EPYTHON}" || error=1
1206                                                                 fi
1207                                                         else
1208                                                                 from=python
1209                                                         fi
1210                                                         break
1211                                                         ;;
1212                                                 *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789])
1213                                                         # Explicit mismatch.
1214                                                         if [[ ! ${force} ]]; then
1215                                                                 error=1
1216                                                         else
1217                                                                 case "${i}" in
1218                                                                         *python[23].[0123456789])
1219                                                                                 from="python[23].[0123456789]";;
1220                                                                         *pypy)
1221                                                                                 from="pypy";;
1222                                                                         *pypy3)
1223                                                                                 from="pypy3";;
1224                                                                         *jython[23].[0123456789])
1225                                                                                 from="jython[23].[0123456789]";;
1226                                                                         *)
1227                                                                                 die "${FUNCNAME}: internal error in 2nd pattern match";;
1228                                                                 esac
1229                                                         fi
1230                                                         break
1231                                                         ;;
1232                                         esac
1233                                 done
1234                         fi
1235
1236                         if [[ ! ${error} && ! ${from} ]]; then
1237                                 # Non-Python shebang. Allowed in recursive mode,
1238                                 # disallowed when specifying file explicitly.
1239                                 [[ ${is_recursive} ]] && continue
1240                                 error=1
1241                         fi
1242
1243                         if [[ ! ${quiet} ]]; then
1244                                 einfo "Fixing shebang in ${f#${D%/}}."
1245                         fi
1246
1247                         if [[ ! ${error} ]]; then
1248                                 # We either want to match ${from} followed by space
1249                                 # or at end-of-string.
1250                                 if [[ ${shebang} == *${from}" "* ]]; then
1251                                         sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die
1252                                 else
1253                                         sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
1254                                 fi
1255                                 any_fixed=1
1256                         else
1257                                 eerror "The file has incompatible shebang:"
1258                                 eerror "  file: ${f#${D%/}}"
1259                                 eerror "  current shebang: ${shebang}"
1260                                 eerror "  requested impl: ${EPYTHON}"
1261                                 die "${FUNCNAME}: conversion of incompatible shebang requested"
1262                         fi
1263                 done < <(find -H "${path}" -type f -print0 || die)
1264
1265                 if [[ ! ${any_fixed} ]]; then
1266                         local cmd=eerror
1267                         [[ ${EAPI:-0} == [012345] ]] && cmd=eqawarn
1268
1269                         "${cmd}" "QA warning: ${FUNCNAME}, ${path#${D%/}} did not match any fixable files."
1270                         if [[ ${any_correct} ]]; then
1271                                 "${cmd}" "All files have ${EPYTHON} shebang already."
1272                         else
1273                                 "${cmd}" "There are no Python files in specified directory."
1274                         fi
1275
1276                         [[ ${cmd} == eerror ]] && die "${FUNCNAME} did not match any fixable files (QA warning fatal in EAPI ${EAPI})"
1277                 fi
1278         done
1279 }
1280
1281 # @FUNCTION: _python_check_locale_sanity
1282 # @USAGE: <locale>
1283 # @RETURN: 0 if sane, 1 otherwise
1284 # @DESCRIPTION:
1285 # Check whether the specified locale sanely maps between lowercase
1286 # and uppercase ASCII characters.
1287 _python_check_locale_sanity() {
1288         local -x LC_ALL=${1}
1289         local IFS=
1290
1291         local lc=( {a..z} )
1292         local uc=( {A..Z} )
1293         local input="${lc[*]}${uc[*]}"
1294
1295         local output=$(tr '[:lower:][:upper:]' '[:upper:][:lower:]' <<<"${input}")
1296         [[ ${output} == "${uc[*]}${lc[*]}" ]]
1297 }
1298
1299 # @FUNCTION: python_export_utf8_locale
1300 # @RETURN: 0 on success, 1 on failure.
1301 # @DESCRIPTION:
1302 # Attempts to export a usable UTF-8 locale in the LC_CTYPE variable. Does
1303 # nothing if LC_ALL is defined, or if the current locale uses a UTF-8 charmap.
1304 # This may be used to work around the quirky open() behavior of python3.
1305 python_export_utf8_locale() {
1306         debug-print-function ${FUNCNAME} "${@}"
1307
1308         # If the locale program isn't available, just return.
1309         type locale >/dev/null || return 0
1310
1311         if [[ $(locale charmap) != UTF-8 ]]; then
1312                 # Try English first, then everything else.
1313                 local lang locales="C.UTF-8 en_US.UTF-8 en_GB.UTF-8 $(locale -a)"
1314
1315                 for lang in ${locales}; do
1316                         if [[ $(LC_ALL=${lang} locale charmap 2>/dev/null) == UTF-8 ]]; then
1317                                 if _python_check_locale_sanity "${lang}"; then
1318                                         export LC_CTYPE=${lang}
1319                                         if [[ -n ${LC_ALL} ]]; then
1320                                                 export LC_NUMERIC=${LC_ALL}
1321                                                 export LC_TIME=${LC_ALL}
1322                                                 export LC_COLLATE=${LC_ALL}
1323                                                 export LC_MONETARY=${LC_ALL}
1324                                                 export LC_MESSAGES=${LC_ALL}
1325                                                 export LC_PAPER=${LC_ALL}
1326                                                 export LC_NAME=${LC_ALL}
1327                                                 export LC_ADDRESS=${LC_ALL}
1328                                                 export LC_TELEPHONE=${LC_ALL}
1329                                                 export LC_MEASUREMENT=${LC_ALL}
1330                                                 export LC_IDENTIFICATION=${LC_ALL}
1331                                                 export LC_ALL=
1332                                         fi
1333                                         return 0
1334                                 fi
1335                         fi
1336                 done
1337
1338                 ewarn "Could not find a UTF-8 locale. This may trigger build failures in"
1339                 ewarn "some python packages. Please ensure that a UTF-8 locale is listed in"
1340                 ewarn "/etc/locale.gen and run locale-gen."
1341                 return 1
1342         fi
1343
1344         return 0
1345 }
1346
1347 # -- python.eclass functions --
1348
1349 _python_check_dead_variables() {
1350         local v
1351
1352         for v in PYTHON_DEPEND PYTHON_USE_WITH{,_OR,_OPT} {RESTRICT,SUPPORT}_PYTHON_ABIS
1353         do
1354                 if [[ ${!v} ]]; then
1355                         die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Ebuild_head"
1356                 fi
1357         done
1358
1359         for v in PYTHON_{CPPFLAGS,CFLAGS,CXXFLAGS,LDFLAGS}
1360         do
1361                 if [[ ${!v} ]]; then
1362                         die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#PYTHON_CFLAGS"
1363                 fi
1364         done
1365
1366         for v in PYTHON_TESTS_RESTRICTED_ABIS PYTHON_EXPORT_PHASE_FUNCTIONS \
1367                 PYTHON_VERSIONED_{SCRIPTS,EXECUTABLES} PYTHON_NONVERSIONED_EXECUTABLES
1368         do
1369                 if [[ ${!v} ]]; then
1370                         die "${v} is invalid for python-r1 suite"
1371                 fi
1372         done
1373
1374         for v in DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES DISTUTILS_SETUP_FILES \
1375                 DISTUTILS_GLOBAL_OPTIONS DISTUTILS_SRC_TEST PYTHON_MODNAME
1376         do
1377                 if [[ ${!v} ]]; then
1378                         die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#${v}"
1379                 fi
1380         done
1381
1382         if [[ ${DISTUTILS_DISABLE_TEST_DEPENDENCY} ]]; then
1383                 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#DISTUTILS_SRC_TEST"
1384         fi
1385
1386         # python.eclass::progress
1387         for v in PYTHON_BDEPEND PYTHON_MULTIPLE_ABIS PYTHON_ABI_TYPE \
1388                 PYTHON_RESTRICTED_ABIS PYTHON_TESTS_FAILURES_TOLERANT_ABIS \
1389                 PYTHON_CFFI_MODULES_GENERATION_COMMANDS
1390         do
1391                 if [[ ${!v} ]]; then
1392                         die "${v} is invalid for python-r1 suite"
1393                 fi
1394         done
1395 }
1396
1397 python_pkg_setup() {
1398         die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1399 }
1400
1401 python_convert_shebangs() {
1402         die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#python_convert_shebangs"
1403 }
1404
1405 python_clean_py-compile_files() {
1406         die "${FUNCNAME}() is invalid for python-r1 suite"
1407 }
1408
1409 python_clean_installation_image() {
1410         die "${FUNCNAME}() is invalid for python-r1 suite"
1411 }
1412
1413 python_execute_function() {
1414         die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#python_execute_function"
1415 }
1416
1417 python_generate_wrapper_scripts() {
1418         die "${FUNCNAME}() is invalid for python-r1 suite"
1419 }
1420
1421 python_merge_intermediate_installation_images() {
1422         die "${FUNCNAME}() is invalid for python-r1 suite"
1423 }
1424
1425 python_set_active_version() {
1426         die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1427 }
1428
1429 python_need_rebuild() {
1430         die "${FUNCNAME}() is invalid for python-r1 suite"
1431 }
1432
1433 PYTHON() {
1434         die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#.24.28PYTHON.29.2C_.24.7BEPYTHON.7D"
1435 }
1436
1437 python_get_implementation() {
1438         die "${FUNCNAME}() is invalid for python-r1 suite"
1439 }
1440
1441 python_get_implementational_package() {
1442         die "${FUNCNAME}() is invalid for python-r1 suite"
1443 }
1444
1445 python_get_libdir() {
1446         die "${FUNCNAME}() is invalid for python-r1 suite"
1447 }
1448
1449 python_get_library() {
1450         die "${FUNCNAME}() is invalid for python-r1 suite"
1451 }
1452
1453 python_get_version() {
1454         die "${FUNCNAME}() is invalid for python-r1 suite"
1455 }
1456
1457 python_get_implementation_and_version() {
1458         die "${FUNCNAME}() is invalid for python-r1 suite"
1459 }
1460
1461 python_execute_nosetests() {
1462         die "${FUNCNAME}() is invalid for python-r1 suite"
1463 }
1464
1465 python_execute_py.test() {
1466         die "${FUNCNAME}() is invalid for python-r1 suite"
1467 }
1468
1469 python_execute_trial() {
1470         die "${FUNCNAME}() is invalid for python-r1 suite"
1471 }
1472
1473 python_enable_pyc() {
1474         die "${FUNCNAME}() is invalid for python-r1 suite"
1475 }
1476
1477 python_disable_pyc() {
1478         die "${FUNCNAME}() is invalid for python-r1 suite"
1479 }
1480
1481 python_mod_optimize() {
1482         die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Python_byte-code_compilation"
1483 }
1484
1485 python_mod_cleanup() {
1486         die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Python_byte-code_compilation"
1487 }
1488
1489 # python.eclass::progress
1490
1491 python_abi_depend() {
1492         die "${FUNCNAME}() is invalid for python-r1 suite"
1493 }
1494
1495 python_install_executables() {
1496         die "${FUNCNAME}() is invalid for python-r1 suite"
1497 }
1498
1499 python_get_extension_module_suffix() {
1500         die "${FUNCNAME}() is invalid for python-r1 suite"
1501 }
1502
1503 python_byte-compile_modules() {
1504         die "${FUNCNAME}() is invalid for python-r1 suite"
1505 }
1506
1507 python_clean_byte-compiled_modules() {
1508         die "${FUNCNAME}() is invalid for python-r1 suite"
1509 }
1510
1511 python_generate_cffi_modules() {
1512         die "${FUNCNAME}() is invalid for python-r1 suite"
1513 }
1514
1515 _PYTHON_UTILS_R1=1
1516 fi