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