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