1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: python-utils-r1.eclass
6 # Python team <python@gentoo.org>
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.
12 # A utility eclass providing functions to query Python implementations,
13 # install Python modules and scripts.
15 # This eclass does not set any metadata variables nor export any phase
16 # functions. It can be inherited safely.
18 # For more information, please see the wiki:
19 # https://wiki.gentoo.org/wiki/Project:Python/python-utils-r1
25 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
29 if [[ ${_PYTHON_ECLASS_INHERITED} ]]; then
30 die 'python-r1 suite eclasses can not be used with python.eclass.'
33 if [[ ! ${_PYTHON_UTILS_R1} ]]; then
35 [[ ${EAPI:-0} == [012345] ]] && inherit eutils multilib
36 inherit toolchain-funcs
38 # @ECLASS-VARIABLE: _PYTHON_ALL_IMPLS
41 # All supported Python implementations, most preferred last.
46 python3_4 python3_5 python3_6
48 readonly _PYTHON_ALL_IMPLS
50 # @ECLASS-VARIABLE: PYTHON_COMPAT_NO_STRICT
53 # Set to a non-empty value in order to make eclass tolerate (ignore)
54 # unknown implementations in PYTHON_COMPAT.
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.
62 # @FUNCTION: _python_impl_supported
66 # Check whether the implementation <impl> (PYTHON_COMPAT-form)
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} "${@}"
75 [[ ${#} -eq 1 ]] || die "${FUNCNAME}: takes exactly 1 argument (impl)."
79 # keep in sync with _PYTHON_ALL_IMPLS!
80 # (not using that list because inline patterns shall be faster)
82 python2_7|python3_[456]|jython2_7)
85 pypy1_[89]|pypy2_0|python2_[56]|python3_[123])
89 if [[ ${EAPI:-0} == [01234] ]]; then
90 die "PyPy is supported in EAPI 5 and newer only."
94 [[ ${PYTHON_COMPAT_NO_STRICT} ]] && return 1
95 die "Invalid implementation in PYTHON_COMPAT: ${impl}"
99 # @FUNCTION: _python_set_impls
102 # Check PYTHON_COMPAT for well-formedness and validity, then set
103 # two global variables:
105 # - _PYTHON_SUPPORTED_IMPLS containing valid implementations supported
106 # by the ebuild (PYTHON_COMPAT - dead implementations),
108 # - and _PYTHON_UNSUPPORTED_IMPLS containing valid implementations that
109 # are not supported by the ebuild.
111 # Implementations in both variables are ordered using the pre-defined
112 # eclass implementation ordering.
114 # This function must be called once in global scope by an eclass
115 # utilizing PYTHON_COMPAT.
116 _python_set_impls() {
119 if ! declare -p PYTHON_COMPAT &>/dev/null; then
120 die 'PYTHON_COMPAT not declared.'
122 if [[ $(declare -p PYTHON_COMPAT) != "declare -a"* ]]; then
123 die 'PYTHON_COMPAT must be an array.'
125 for i in "${PYTHON_COMPAT[@]}"; do
126 # trigger validity checks
127 _python_impl_supported "${i}"
130 local supp=() unsupp=()
132 for i in "${_PYTHON_ALL_IMPLS[@]}"; do
133 if has "${i}" "${PYTHON_COMPAT[@]}"; then
140 if [[ ! ${supp[@]} ]]; then
141 die "No supported implementation in PYTHON_COMPAT."
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"
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"
159 _PYTHON_SUPPORTED_IMPLS=( "${supp[@]}" )
160 _PYTHON_UNSUPPORTED_IMPLS=( "${unsupp[@]}" )
161 readonly _PYTHON_SUPPORTED_IMPLS _PYTHON_UNSUPPORTED_IMPLS
165 # @FUNCTION: _python_impl_matches
166 # @USAGE: <impl> <pattern>...
169 # Check whether the specified <impl> matches at least one
170 # of the patterns following it. Return 0 if it does, 1 otherwise.
172 # <impl> can be in PYTHON_COMPAT or EPYTHON form. The patterns can be
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"
180 local impl=${1} pattern
184 if [[ ${pattern} == -2 ]]; then
185 ! python_is_python3 "${impl}"
187 elif [[ ${pattern} == -3 ]]; then
188 python_is_python3 "${impl}"
190 # unify value style to allow lax matching
191 elif [[ ${impl/./_} == ${pattern/./_} ]]; then
199 # @ECLASS-VARIABLE: PYTHON
202 # The absolute path to the current Python interpreter.
204 # This variable is set automatically in the following contexts:
206 # python-r1: Set in functions called by python_foreach_impl() or after
207 # calling python_export_best().
209 # python-single-r1: Set after calling python-single-r1_pkg_setup().
211 # distutils-r1: Set within any of the python sub-phase functions.
218 # @ECLASS-VARIABLE: EPYTHON
221 # The executable name of the current Python interpreter.
223 # This variable is set automatically in the following contexts:
225 # python-r1: Set in functions called by python_foreach_impl() or after
226 # calling python_export_best().
228 # python-single-r1: Set after calling python-single-r1_pkg_setup().
230 # distutils-r1: Set within any of the python sub-phase functions.
237 # @ECLASS-VARIABLE: PYTHON_SITEDIR
240 # The path to Python site-packages directory.
242 # Set and exported on request using python_export().
243 # Requires a proper build-time dependency on the Python implementation.
247 # /usr/lib64/python2.7/site-packages
250 # @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
253 # The path to Python include directory.
255 # Set and exported on request using python_export().
256 # Requires a proper build-time dependency on the Python implementation.
260 # /usr/include/python2.7
263 # @ECLASS-VARIABLE: PYTHON_LIBPATH
266 # The path to Python library.
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.
274 # /usr/lib64/libpython2.7.so
277 # @ECLASS-VARIABLE: PYTHON_CFLAGS
280 # Proper C compiler flags for building against Python. Obtained from
281 # pkg-config or python-config.
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.
289 # -I/usr/include/python2.7
292 # @ECLASS-VARIABLE: PYTHON_LIBS
295 # Proper C compiler flags for linking against Python. Obtained from
296 # pkg-config or python-config.
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.
307 # @ECLASS-VARIABLE: PYTHON_CONFIG
310 # Path to the python-config executable.
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.
318 # /usr/bin/python2.7-config
321 # @ECLASS-VARIABLE: PYTHON_PKG_DEP
324 # The complete dependency on a particular Python package as a string.
326 # Set and exported on request using python_export().
330 # dev-lang/python:2.7[xml]
333 # @ECLASS-VARIABLE: PYTHON_SCRIPTDIR
336 # The location where Python scripts must be installed for current impl.
338 # Set and exported on request using python_export().
342 # /usr/lib/python-exec/python2.7
345 # @FUNCTION: python_export
346 # @USAGE: [<impl>] <variables>...
348 # Set and export the Python implementation-relevant variables passed
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}.
356 # The variables which can be exported are: PYTHON, EPYTHON,
357 # PYTHON_SITEDIR. They are described more completely in the eclass
358 # variable documentation.
360 debug-print-function ${FUNCNAME} "${@}"
375 if [[ -z ${impl} ]]; then
376 die "python_export called without a python implementation and EPYTHON is unset"
380 debug-print "${FUNCNAME}: implementation: ${impl}"
385 export EPYTHON=${impl}
386 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
389 export PYTHON=${EPREFIX}/usr/bin/${impl}
390 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
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}"
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}"
407 # Jython gives a non-existing directory
408 if [[ ! -d ${PYTHON_INCLUDEDIR} ]]; then
409 die "${impl} does not install any header files!"
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}"
418 if [[ ! ${PYTHON_LIBPATH} ]]; then
419 die "${impl} lacks a (usable) dynamic library"
427 # python-2.7, python-3.2, etc.
428 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-}) || die
431 die "${impl}: obtaining ${var} not supported"
435 export PYTHON_CFLAGS=${val}
436 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
443 # python-2.7, python-3.2, etc.
444 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-}) || die
447 die "${impl}: obtaining ${var} not supported"
451 export PYTHON_LIBS=${val}
452 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
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
464 die "${impl}: obtaining ${var} not supported"
468 export PYTHON_CONFIG=${val}
469 debug-print "${FUNCNAME}: PYTHON_CONFIG = ${PYTHON_CONFIG}"
475 PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
477 PYTHON_PKG_DEP='>=dev-lang/python-3.3.2-r2:3.3';;
479 PYTHON_PKG_DEP="dev-lang/python:${impl#python}";;
481 PYTHON_PKG_DEP='>=virtual/pypy-5:0=';;
483 PYTHON_PKG_DEP='>=virtual/pypy3-5:0=';;
485 PYTHON_PKG_DEP='dev-java/jython:2.7';;
487 die "Invalid implementation: ${impl}"
491 if [[ ${PYTHON_REQ_USE} ]]; then
492 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
495 export PYTHON_PKG_DEP
496 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
500 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
501 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
504 die "python_export: unknown variable ${var}"
509 # @FUNCTION: python_get_sitedir
512 # Obtain and print the 'site-packages' path for the given
513 # implementation. If no implementation is provided, ${EPYTHON} will
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} "${@}"
521 python_export "${@}" PYTHON_SITEDIR
522 echo "${PYTHON_SITEDIR}"
525 # @FUNCTION: python_get_includedir
528 # Obtain and print the include path for the given implementation. If no
529 # implementation is provided, ${EPYTHON} will be used.
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} "${@}"
536 python_export "${@}" PYTHON_INCLUDEDIR
537 echo "${PYTHON_INCLUDEDIR}"
540 # @FUNCTION: python_get_library_path
543 # Obtain and print the Python library path for the given implementation.
544 # If no implementation is provided, ${EPYTHON} will be used.
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} "${@}"
551 python_export "${@}" PYTHON_LIBPATH
552 echo "${PYTHON_LIBPATH}"
555 # @FUNCTION: python_get_CFLAGS
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.
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} "${@}"
568 python_export "${@}" PYTHON_CFLAGS
569 echo "${PYTHON_CFLAGS}"
572 # @FUNCTION: python_get_LIBS
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.
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.
583 debug-print-function ${FUNCNAME} "${@}"
585 python_export "${@}" PYTHON_LIBS
586 echo "${PYTHON_LIBS}"
589 # @FUNCTION: python_get_PYTHON_CONFIG
592 # Obtain and print the PYTHON_CONFIG location for the given
593 # implementation. If no implementation is provided, ${EPYTHON} will be
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} "${@}"
602 python_export "${@}" PYTHON_CONFIG
603 echo "${PYTHON_CONFIG}"
606 # @FUNCTION: python_get_scriptdir
609 # Obtain and print the script install path for the given
610 # implementation. If no implementation is provided, ${EPYTHON} will
612 python_get_scriptdir() {
613 debug-print-function ${FUNCNAME} "${@}"
615 python_export "${@}" PYTHON_SCRIPTDIR
616 echo "${PYTHON_SCRIPTDIR}"
619 # @FUNCTION: _python_ln_rel
620 # @USAGE: <from> <to>
623 # Create a relative symlink.
625 debug-print-function ${FUNCNAME} "${@}"
630 local tgpath=${target%/*}/
631 local sympath=${symname%/*}/
634 while [[ ${sympath} ]]; do
637 while [[ ! ${tgseg} && ${tgpath} ]]; do
639 tgpath=${tgpath#${tgseg}/}
642 while [[ ! ${symseg} && ${sympath} ]]; do
643 symseg=${sympath%%/*}
644 sympath=${sympath#${symseg}/}
647 if [[ ${tgseg} != ${symseg} ]]; then
648 rel_target=../${rel_target}${tgseg:+${tgseg}/}
651 rel_target+=${tgpath}${target##*/}
653 debug-print "${FUNCNAME}: ${symname} -> ${target}"
654 debug-print "${FUNCNAME}: rel_target = ${rel_target}"
656 ln -fs "${rel_target}" "${symname}"
659 # @FUNCTION: python_optimize
660 # @USAGE: [<directory>...]
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}).
666 debug-print-function ${FUNCNAME} "${@}"
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."
674 die "python_optimize is not to be used in pre/post* phases"
677 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
679 local PYTHON=${PYTHON}
680 [[ ${PYTHON} ]] || python_export PYTHON
682 # default to sys.path
683 if [[ ${#} -eq 0 ]]; then
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)
691 if [[ ${f} == /* && -d ${D}${f} ]]; then
692 set -- "${D}${f}" "${@}"
694 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))' || die)
696 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
701 # make sure to get a nice path without //
702 local instpath=${d#${D}}
703 instpath=/${instpath##/}
706 python2.7|python3.[34])
707 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
708 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
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}"
717 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
723 # @FUNCTION: python_scriptinto
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}).
731 # If not set explicitly, the directory defaults to /usr/bin.
736 # python_scriptinto /usr/sbin
737 # python_foreach_impl python_doscript foo
740 python_scriptinto() {
741 debug-print-function ${FUNCNAME} "${@}"
743 python_scriptroot=${1}
746 # @FUNCTION: python_doexe
749 # Install the given executables into the executable install directory,
750 # for the current Python implementation (${EPYTHON}).
752 # The executable will be wrapped properly for the Python implementation,
753 # though no shebang mangling will be performed.
755 debug-print-function ${FUNCNAME} "${@}"
759 python_newexe "${f}" "${f##*/}"
763 # @FUNCTION: python_newexe
764 # @USAGE: <path> <new-name>
766 # Install the given executable into the executable install directory,
767 # for the current Python implementation (${EPYTHON}).
769 # The executable will be wrapped properly for the Python implementation,
770 # though no shebang mangling will be performed. It will be renamed
773 debug-print-function ${FUNCNAME} "${@}"
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."
781 local wrapd=${python_scriptroot:-/usr/bin}
786 local PYTHON_SCRIPTDIR d
787 python_export PYTHON_SCRIPTDIR
788 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
793 newexe "${f}" "${newfn}" || return ${?}
796 # install the wrapper
797 _python_ln_rel "${ED%/}"/usr/lib/python-exec/python-exec2 \
798 "${ED%/}/${wrapd}/${newfn}" || die
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}"
806 # @FUNCTION: python_doscript
809 # Install the given scripts into the executable install directory,
810 # for the current Python implementation (${EPYTHON}).
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.
819 # python_foreach_impl python_doscript ${PN}
823 debug-print-function ${FUNCNAME} "${@}"
825 local _PYTHON_REWRITE_SHEBANG=1
829 # @FUNCTION: python_newscript
830 # @USAGE: <path> <new-name>
832 # Install the given script into the executable install directory
833 # for the current Python implementation (${EPYTHON}), and name it
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>.
843 # python_foreach_impl python_newscript foo.py foo
847 debug-print-function ${FUNCNAME} "${@}"
849 local _PYTHON_REWRITE_SHEBANG=1
853 # @FUNCTION: python_moduleinto
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).
864 # When not set explicitly, the modules are installed to the top
865 # site-packages directory.
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.
876 # python_moduleinto bar
877 # # installs ${PYTHON_SITEDIR}/bar/baz.py
878 # python_foreach_impl python_domodule baz.py
881 python_moduleinto() {
882 debug-print-function ${FUNCNAME} "${@}"
884 python_moduleroot=${1}
887 # @FUNCTION: python_domodule
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.
898 # # (${PN} being a directory)
899 # python_foreach_impl python_domodule ${PN}
903 debug-print-function ${FUNCNAME} "${@}"
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."
911 if [[ ${python_moduleroot} == /* ]]; then
913 d=${python_moduleroot}
915 # relative to site-packages
916 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
917 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
919 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot//.//}
924 doins -r "${@}" || return ${?}
927 python_optimize "${ED}/${d}"
930 # @FUNCTION: python_doheader
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.
940 # python_foreach_impl python_doheader foo.h bar.h
944 debug-print-function ${FUNCNAME} "${@}"
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."
951 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
952 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
954 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
958 doins -r "${@}" || return ${?}
962 # @FUNCTION: python_wrapper_setup
963 # @USAGE: [<path> [<impl>]]
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}.
969 # The wrappers will be created for implementation named by <impl>,
970 # or for one named by ${EPYTHON} if no <impl> passed.
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} "${@}"
979 local workdir=${1:-${T}/${EPYTHON}}
980 local impl=${2:-${EPYTHON}}
982 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
983 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
985 if [[ ! -x ${workdir}/bin/python ]]; then
986 _python_check_dead_variables
988 mkdir -p "${workdir}"/{bin,pkgconfig} || die
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
996 python_export "${impl}" EPYTHON PYTHON
999 if python_is_python3; then
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
1013 exec "${PYTHON}" "\${@}"
1015 cp "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
1016 chmod +x "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
1018 local nonsupp=( "python${pyother}" "python${pyother}-config" )
1021 if [[ ${EPYTHON} == python* ]]; then
1022 cat > "${workdir}/bin/python-config" <<-_EOF_ || die
1024 exec "${PYTHON}-config" "\${@}"
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
1032 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
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
1039 nonsupp+=( 2to3 python-config "python${pyver}-config" )
1043 for x in "${nonsupp[@]}"; do
1044 cat >"${workdir}"/bin/${x} <<-_EOF_ || die
1046 echo "${ECLASS}: ${FUNCNAME}: ${x} is not supported by ${EPYTHON} (PYTHON_COMPAT)" >&2
1049 chmod +x "${workdir}"/bin/${x} || die
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}}
1059 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
1060 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
1062 export PATH PKG_CONFIG_PATH
1065 # @FUNCTION: python_is_python3
1068 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
1069 # (i.e. uses syntax and stdlib of Python 3.*).
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"
1076 [[ ${impl} == python3* || ${impl} == pypy3 ]]
1079 # @FUNCTION: python_is_installed
1082 # Check whether the interpreter for <impl> (or ${EPYTHON}) is installed.
1083 # Uses has_version with a proper dependency string.
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"
1095 if [[ ${PYTHON_REQ_USE} ]]; then
1096 append=[${PYTHON_REQ_USE}]
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}"
1104 local PYTHON_PKG_DEP
1105 python_export "${impl}" PYTHON_PKG_DEP
1106 has_version "${PYTHON_PKG_DEP}"
1111 # @FUNCTION: python_fix_shebang
1112 # @USAGE: [-f|--force] [-q|--quiet] <path>...
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.
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.
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.
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} "${@}"
1132 [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
1135 while [[ ${@} ]]; do
1137 -f|--force) force=1; shift;;
1138 -q|--quiet) quiet=1; shift;;
1144 [[ ${1} ]] || die "${FUNCNAME}: no paths given"
1148 local any_correct any_fixed is_recursive
1150 [[ -d ${path} ]] && is_recursive=1
1152 while IFS= read -r -d '' f; do
1156 # note: we can't ||die here since read will fail if file
1157 # has no newline characters
1158 IFS= read -r shebang <"${f}"
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
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
1170 debug-print "${FUNCNAME}: in file ${f#${D}}"
1171 debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
1173 # Nothing to do, move along.
1178 *python|*python[23])
1179 debug-print "${FUNCNAME}: in file ${f#${D}}"
1180 debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
1182 if [[ ${i} == *python2 ]]; then
1184 if [[ ! ${force} ]]; then
1185 python_is_python3 "${EPYTHON}" && error=1
1187 elif [[ ${i} == *python3 ]]; then
1189 if [[ ! ${force} ]]; then
1190 python_is_python3 "${EPYTHON}" || error=1
1197 *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789])
1198 # Explicit mismatch.
1199 if [[ ! ${force} ]]; then
1203 *python[23].[0123456789])
1204 from="python[23].[0123456789]";;
1209 *jython[23].[0123456789])
1210 from="jython[23].[0123456789]";;
1212 die "${FUNCNAME}: internal error in 2nd pattern match";;
1221 if [[ ! ${error} && ! ${from} ]]; then
1222 # Non-Python shebang. Allowed in recursive mode,
1223 # disallowed when specifying file explicitly.
1224 [[ ${is_recursive} ]] && continue
1228 if [[ ! ${quiet} ]]; then
1229 einfo "Fixing shebang in ${f#${D}}."
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
1238 sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
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"
1248 done < <(find -H "${path}" -type f -print0 || die)
1250 if [[ ! ${any_fixed} ]]; then
1252 [[ ${EAPI:-0} == [012345] ]] && cmd=eqawarn
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."
1258 "${cmd}" "There are no Python files in specified directory."
1261 [[ ${cmd} == eerror ]] && die "${FUNCNAME} did not match any fixable files (QA warning fatal in EAPI ${EAPI})"
1266 # @FUNCTION: _python_check_locale_sanity
1268 # @RETURN: 0 if sane, 1 otherwise
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}
1278 local input="${lc[*]}${uc[*]}"
1280 local output=$(tr '[:lower:][:upper:]' '[:upper:][:lower:]' <<<"${input}")
1281 [[ ${output} == "${uc[*]}${lc[*]}" ]]
1284 # @FUNCTION: python_export_utf8_locale
1285 # @RETURN: 0 on success, 1 on failure.
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} "${@}"
1293 # If the locale program isn't available, just return.
1294 type locale >/dev/null || return 0
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)"
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}
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."
1332 # -- python.eclass functions --
1334 _python_check_dead_variables() {
1337 for v in PYTHON_DEPEND PYTHON_USE_WITH{,_OR,_OPT} {RESTRICT,SUPPORT}_PYTHON_ABIS
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"
1344 for v in PYTHON_{CPPFLAGS,CFLAGS,CXXFLAGS,LDFLAGS}
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"
1351 for v in PYTHON_TESTS_RESTRICTED_ABIS PYTHON_EXPORT_PHASE_FUNCTIONS \
1352 PYTHON_VERSIONED_{SCRIPTS,EXECUTABLES} PYTHON_NONVERSIONED_EXECUTABLES
1354 if [[ ${!v} ]]; then
1355 die "${v} is invalid for python-r1 suite"
1359 for v in DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES DISTUTILS_SETUP_FILES \
1360 DISTUTILS_GLOBAL_OPTIONS DISTUTILS_SRC_TEST PYTHON_MODNAME
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}"
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"
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
1376 if [[ ${!v} ]]; then
1377 die "${v} is invalid for python-r1 suite"
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"
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"
1390 python_clean_py-compile_files() {
1391 die "${FUNCNAME}() is invalid for python-r1 suite"
1394 python_clean_installation_image() {
1395 die "${FUNCNAME}() is invalid for python-r1 suite"
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"
1402 python_generate_wrapper_scripts() {
1403 die "${FUNCNAME}() is invalid for python-r1 suite"
1406 python_merge_intermediate_installation_images() {
1407 die "${FUNCNAME}() is invalid for python-r1 suite"
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"
1414 python_need_rebuild() {
1415 die "${FUNCNAME}() is invalid for python-r1 suite"
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"
1422 python_get_implementation() {
1423 die "${FUNCNAME}() is invalid for python-r1 suite"
1426 python_get_implementational_package() {
1427 die "${FUNCNAME}() is invalid for python-r1 suite"
1430 python_get_libdir() {
1431 die "${FUNCNAME}() is invalid for python-r1 suite"
1434 python_get_library() {
1435 die "${FUNCNAME}() is invalid for python-r1 suite"
1438 python_get_version() {
1439 die "${FUNCNAME}() is invalid for python-r1 suite"
1442 python_get_implementation_and_version() {
1443 die "${FUNCNAME}() is invalid for python-r1 suite"
1446 python_execute_nosetests() {
1447 die "${FUNCNAME}() is invalid for python-r1 suite"
1450 python_execute_py.test() {
1451 die "${FUNCNAME}() is invalid for python-r1 suite"
1454 python_execute_trial() {
1455 die "${FUNCNAME}() is invalid for python-r1 suite"
1458 python_enable_pyc() {
1459 die "${FUNCNAME}() is invalid for python-r1 suite"
1462 python_disable_pyc() {
1463 die "${FUNCNAME}() is invalid for python-r1 suite"
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"
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"
1474 # python.eclass::progress
1476 python_abi_depend() {
1477 die "${FUNCNAME}() is invalid for python-r1 suite"
1480 python_install_executables() {
1481 die "${FUNCNAME}() is invalid for python-r1 suite"
1484 python_get_extension_module_suffix() {
1485 die "${FUNCNAME}() is invalid for python-r1 suite"
1488 python_byte-compile_modules() {
1489 die "${FUNCNAME}() is invalid for python-r1 suite"
1492 python_clean_byte-compiled_modules() {
1493 die "${FUNCNAME}() is invalid for python-r1 suite"
1496 python_generate_cffi_modules() {
1497 die "${FUNCNAME}() is invalid for python-r1 suite"