1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: python-utils-r1.eclass
7 # Python team <python@gentoo.org>
9 # Author: Michał Górny <mgorny@gentoo.org>
10 # Based on work of: Krzysztof Pawlik <nelchael@gentoo.org>
11 # @BLURB: Utility functions for packages with Python parts.
13 # A utility eclass providing functions to query Python implementations,
14 # install Python modules and scripts.
16 # This eclass does not set any metadata variables nor export any phase
17 # functions. It can be inherited safely.
19 # For more information, please see the wiki:
20 # https://wiki.gentoo.org/wiki/Project:Python/python-utils-r1
26 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
30 if [[ ${_PYTHON_ECLASS_INHERITED} ]]; then
31 die 'python-r1 suite eclasses can not be used with python.eclass.'
34 if [[ ! ${_PYTHON_UTILS_R1} ]]; then
36 [[ ${EAPI:-0} == [012345] ]] && inherit eutils multilib
37 inherit toolchain-funcs
39 # @ECLASS-VARIABLE: _PYTHON_ALL_IMPLS
42 # All supported Python implementations, most preferred last.
47 python3_3 python3_4 python3_5
49 readonly _PYTHON_ALL_IMPLS
51 # @FUNCTION: _python_impl_supported
55 # Check whether the implementation <impl> (PYTHON_COMPAT-form)
58 # Returns 0 if the implementation is valid and supported. If it is
59 # unsupported, returns 1 -- and the caller should ignore the entry.
60 # If it is invalid, dies with an appopriate error messages.
61 _python_impl_supported() {
62 debug-print-function ${FUNCNAME} "${@}"
64 [[ ${#} -eq 1 ]] || die "${FUNCNAME}: takes exactly 1 argument (impl)."
68 # keep in sync with _PYTHON_ALL_IMPLS!
69 # (not using that list because inline patterns shall be faster)
71 python2_7|python3_[345]|jython2_7)
74 pypy1_[89]|pypy2_0|python2_[56]|python3_[12])
78 if [[ ${EAPI:-0} == [01234] ]]; then
79 die "PyPy is supported in EAPI 5 and newer only."
83 die "Invalid implementation in PYTHON_COMPAT: ${impl}"
87 # @FUNCTION: _python_set_impls
90 # Check PYTHON_COMPAT for well-formedness and validity, then set
91 # two global variables:
93 # - _PYTHON_SUPPORTED_IMPLS containing valid implementations supported
94 # by the ebuild (PYTHON_COMPAT - dead implementations),
96 # - and _PYTHON_UNSUPPORTED_IMPLS containing valid implementations that
97 # are not supported by the ebuild.
99 # Implementations in both variables are ordered using the pre-defined
100 # eclass implementation ordering.
102 # This function must be called once in global scope by an eclass
103 # utilizing PYTHON_COMPAT.
104 _python_set_impls() {
107 if ! declare -p PYTHON_COMPAT &>/dev/null; then
108 die 'PYTHON_COMPAT not declared.'
110 if [[ $(declare -p PYTHON_COMPAT) != "declare -a"* ]]; then
111 die 'PYTHON_COMPAT must be an array.'
113 for i in "${PYTHON_COMPAT[@]}"; do
114 # trigger validity checks
115 _python_impl_supported "${i}"
118 _PYTHON_SUPPORTED_IMPLS=()
119 _PYTHON_UNSUPPORTED_IMPLS=()
121 for i in "${_PYTHON_ALL_IMPLS[@]}"; do
122 if has "${i}" "${PYTHON_COMPAT[@]}"; then
123 _PYTHON_SUPPORTED_IMPLS+=( "${i}" )
125 _PYTHON_UNSUPPORTED_IMPLS+=( "${i}" )
129 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 0 ]]; then
130 die "No supported implementation in PYTHON_COMPAT."
133 readonly _PYTHON_SUPPORTED_IMPLS _PYTHON_UNSUPPORTED_IMPLS
136 # @ECLASS-VARIABLE: PYTHON
139 # The absolute path to the current Python interpreter.
141 # This variable is set automatically in the following contexts:
143 # python-r1: Set in functions called by python_foreach_impl() or after
144 # calling python_export_best().
146 # python-single-r1: Set after calling python-single-r1_pkg_setup().
148 # distutils-r1: Set within any of the python sub-phase functions.
155 # @ECLASS-VARIABLE: EPYTHON
158 # The executable name of the current Python interpreter.
160 # This variable is set automatically in the following contexts:
162 # python-r1: Set in functions called by python_foreach_impl() or after
163 # calling python_export_best().
165 # python-single-r1: Set after calling python-single-r1_pkg_setup().
167 # distutils-r1: Set within any of the python sub-phase functions.
174 # @ECLASS-VARIABLE: PYTHON_SITEDIR
177 # The path to Python site-packages directory.
179 # Set and exported on request using python_export().
180 # Requires a proper build-time dependency on the Python implementation.
184 # /usr/lib64/python2.7/site-packages
187 # @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
190 # The path to Python include directory.
192 # Set and exported on request using python_export().
193 # Requires a proper build-time dependency on the Python implementation.
197 # /usr/include/python2.7
200 # @ECLASS-VARIABLE: PYTHON_LIBPATH
203 # The path to Python library.
205 # Set and exported on request using python_export().
206 # Valid only for CPython. Requires a proper build-time dependency
207 # on the Python implementation.
211 # /usr/lib64/libpython2.7.so
214 # @ECLASS-VARIABLE: PYTHON_CFLAGS
217 # Proper C compiler flags for building against Python. Obtained from
218 # pkg-config or python-config.
220 # Set and exported on request using python_export().
221 # Valid only for CPython. Requires a proper build-time dependency
222 # on the Python implementation and on pkg-config.
226 # -I/usr/include/python2.7
229 # @ECLASS-VARIABLE: PYTHON_LIBS
232 # Proper C compiler flags for linking against Python. Obtained from
233 # pkg-config or python-config.
235 # Set and exported on request using python_export().
236 # Valid only for CPython. Requires a proper build-time dependency
237 # on the Python implementation and on pkg-config.
244 # @ECLASS-VARIABLE: PYTHON_CONFIG
247 # Path to the python-config executable.
249 # Set and exported on request using python_export().
250 # Valid only for CPython. Requires a proper build-time dependency
251 # on the Python implementation and on pkg-config.
255 # /usr/bin/python2.7-config
258 # @ECLASS-VARIABLE: PYTHON_PKG_DEP
261 # The complete dependency on a particular Python package as a string.
263 # Set and exported on request using python_export().
267 # dev-lang/python:2.7[xml]
270 # @ECLASS-VARIABLE: PYTHON_SCRIPTDIR
273 # The location where Python scripts must be installed for current impl.
275 # Set and exported on request using python_export().
279 # /usr/lib/python-exec/python2.7
282 # @FUNCTION: python_export
283 # @USAGE: [<impl>] <variables>...
285 # Set and export the Python implementation-relevant variables passed
288 # The optional first parameter may specify the requested Python
289 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
290 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
291 # the current one will be obtained from ${EPYTHON}.
293 # The variables which can be exported are: PYTHON, EPYTHON,
294 # PYTHON_SITEDIR. They are described more completely in the eclass
295 # variable documentation.
297 debug-print-function ${FUNCNAME} "${@}"
312 if [[ -z ${impl} ]]; then
313 die "python_export called without a python implementation and EPYTHON is unset"
317 debug-print "${FUNCNAME}: implementation: ${impl}"
322 export EPYTHON=${impl}
323 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
326 export PYTHON=${EPREFIX}/usr/bin/${impl}
327 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
330 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
331 # sysconfig can't be used because:
332 # 1) pypy doesn't give site-packages but stdlib
333 # 2) jython gives paths with wrong case
334 PYTHON_SITEDIR=$("${PYTHON}" -c 'import distutils.sysconfig; print(distutils.sysconfig.get_python_lib())') || die
335 export PYTHON_SITEDIR
336 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
339 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
340 PYTHON_INCLUDEDIR=$("${PYTHON}" -c 'import distutils.sysconfig; print(distutils.sysconfig.get_python_inc())') || die
341 export PYTHON_INCLUDEDIR
342 debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
344 # Jython gives a non-existing directory
345 if [[ ! -d ${PYTHON_INCLUDEDIR} ]]; then
346 die "${impl} does not install any header files!"
350 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
351 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
352 export PYTHON_LIBPATH
353 debug-print "${FUNCNAME}: PYTHON_LIBPATH = ${PYTHON_LIBPATH}"
355 if [[ ! ${PYTHON_LIBPATH} ]]; then
356 die "${impl} lacks a (usable) dynamic library"
364 # python-2.7, python-3.2, etc.
365 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-}) || die
368 die "${impl}: obtaining ${var} not supported"
372 export PYTHON_CFLAGS=${val}
373 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
380 # python-2.7, python-3.2, etc.
381 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-}) || die
384 die "${impl}: obtaining ${var} not supported"
388 export PYTHON_LIBS=${val}
389 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
396 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
397 flags=$("${PYTHON}" -c 'import sysconfig; print(sysconfig.get_config_var("ABIFLAGS") or "")') || die
398 val=${PYTHON}${flags}-config
401 die "${impl}: obtaining ${var} not supported"
405 export PYTHON_CONFIG=${val}
406 debug-print "${FUNCNAME}: PYTHON_CONFIG = ${PYTHON_CONFIG}"
412 PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
414 PYTHON_PKG_DEP='>=dev-lang/python-3.3.2-r2:3.3';;
416 PYTHON_PKG_DEP="dev-lang/python:${impl#python}";;
418 PYTHON_PKG_DEP='virtual/pypy:0=';;
420 PYTHON_PKG_DEP='virtual/pypy3:0=';;
422 PYTHON_PKG_DEP='dev-java/jython:2.7';;
424 die "Invalid implementation: ${impl}"
428 if [[ ${PYTHON_REQ_USE} ]]; then
429 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
432 export PYTHON_PKG_DEP
433 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
437 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
438 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
441 die "python_export: unknown variable ${var}"
446 # @FUNCTION: python_get_sitedir
449 # Obtain and print the 'site-packages' path for the given
450 # implementation. If no implementation is provided, ${EPYTHON} will
453 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
454 # better to use python_export() directly instead.
455 python_get_sitedir() {
456 debug-print-function ${FUNCNAME} "${@}"
458 python_export "${@}" PYTHON_SITEDIR
459 echo "${PYTHON_SITEDIR}"
462 # @FUNCTION: python_get_includedir
465 # Obtain and print the include path for the given implementation. If no
466 # implementation is provided, ${EPYTHON} will be used.
468 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
469 # is better to use python_export() directly instead.
470 python_get_includedir() {
471 debug-print-function ${FUNCNAME} "${@}"
473 python_export "${@}" PYTHON_INCLUDEDIR
474 echo "${PYTHON_INCLUDEDIR}"
477 # @FUNCTION: python_get_library_path
480 # Obtain and print the Python library path for the given implementation.
481 # If no implementation is provided, ${EPYTHON} will be used.
483 # Please note that this function can be used with CPython only. Use
484 # in another implementation will result in a fatal failure.
485 python_get_library_path() {
486 debug-print-function ${FUNCNAME} "${@}"
488 python_export "${@}" PYTHON_LIBPATH
489 echo "${PYTHON_LIBPATH}"
492 # @FUNCTION: python_get_CFLAGS
495 # Obtain and print the compiler flags for building against Python,
496 # for the given implementation. If no implementation is provided,
497 # ${EPYTHON} will be used.
499 # Please note that this function can be used with CPython only.
500 # It requires Python and pkg-config installed, and therefore proper
501 # build-time dependencies need be added to the ebuild.
502 python_get_CFLAGS() {
503 debug-print-function ${FUNCNAME} "${@}"
505 python_export "${@}" PYTHON_CFLAGS
506 echo "${PYTHON_CFLAGS}"
509 # @FUNCTION: python_get_LIBS
512 # Obtain and print the compiler flags for linking against Python,
513 # for the given implementation. If no implementation is provided,
514 # ${EPYTHON} will be used.
516 # Please note that this function can be used with CPython only.
517 # It requires Python and pkg-config installed, and therefore proper
518 # build-time dependencies need be added to the ebuild.
520 debug-print-function ${FUNCNAME} "${@}"
522 python_export "${@}" PYTHON_LIBS
523 echo "${PYTHON_LIBS}"
526 # @FUNCTION: python_get_PYTHON_CONFIG
529 # Obtain and print the PYTHON_CONFIG location for the given
530 # implementation. If no implementation is provided, ${EPYTHON} will be
533 # Please note that this function can be used with CPython only.
534 # It requires Python installed, and therefore proper build-time
535 # dependencies need be added to the ebuild.
536 python_get_PYTHON_CONFIG() {
537 debug-print-function ${FUNCNAME} "${@}"
539 python_export "${@}" PYTHON_CONFIG
540 echo "${PYTHON_CONFIG}"
543 # @FUNCTION: python_get_scriptdir
546 # Obtain and print the script install path for the given
547 # implementation. If no implementation is provided, ${EPYTHON} will
549 python_get_scriptdir() {
550 debug-print-function ${FUNCNAME} "${@}"
552 python_export "${@}" PYTHON_SCRIPTDIR
553 echo "${PYTHON_SCRIPTDIR}"
556 # @FUNCTION: _python_ln_rel
557 # @USAGE: <from> <to>
560 # Create a relative symlink.
562 debug-print-function ${FUNCNAME} "${@}"
567 local tgpath=${target%/*}/
568 local sympath=${symname%/*}/
571 while [[ ${sympath} ]]; do
574 while [[ ! ${tgseg} && ${tgpath} ]]; do
576 tgpath=${tgpath#${tgseg}/}
579 while [[ ! ${symseg} && ${sympath} ]]; do
580 symseg=${sympath%%/*}
581 sympath=${sympath#${symseg}/}
584 if [[ ${tgseg} != ${symseg} ]]; then
585 rel_target=../${rel_target}${tgseg:+${tgseg}/}
588 rel_target+=${tgpath}${target##*/}
590 debug-print "${FUNCNAME}: ${symname} -> ${target}"
591 debug-print "${FUNCNAME}: rel_target = ${rel_target}"
593 ln -fs "${rel_target}" "${symname}"
596 # @FUNCTION: python_optimize
597 # @USAGE: [<directory>...]
599 # Compile and optimize Python modules in specified directories (absolute
600 # paths). If no directories are provided, the default system paths
601 # are used (prepended with ${D}).
603 debug-print-function ${FUNCNAME} "${@}"
605 if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
606 eerror "The new Python eclasses expect the compiled Python files to"
607 eerror "be controlled by the Package Manager. For this reason,"
608 eerror "the python_optimize function can be used only during src_* phases"
609 eerror "(src_install most commonly) and not during pkg_* phases."
611 die "python_optimize is not to be used in pre/post* phases"
614 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
616 local PYTHON=${PYTHON}
617 [[ ${PYTHON} ]] || python_export PYTHON
619 # default to sys.path
620 if [[ ${#} -eq 0 ]]; then
622 while IFS= read -r -d '' f; do
623 # 1) accept only absolute paths
624 # (i.e. skip '', '.' or anything like that)
625 # 2) skip paths which do not exist
626 # (python2.6 complains about them verbosely)
628 if [[ ${f} == /* && -d ${D}${f} ]]; then
629 set -- "${D}${f}" "${@}"
631 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))' || die)
633 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
638 # make sure to get a nice path without //
639 local instpath=${d#${D}}
640 instpath=/${instpath##/}
644 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
645 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
648 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
654 # @FUNCTION: python_scriptinto
657 # Set the directory to which files passed to python_doexe(),
658 # python_doscript(), python_newexe() and python_newscript()
659 # are going to be installed. The new value needs to be relative
660 # to the installation root (${ED}).
662 # If not set explicitly, the directory defaults to /usr/bin.
667 # python_scriptinto /usr/sbin
668 # python_foreach_impl python_doscript foo
671 python_scriptinto() {
672 debug-print-function ${FUNCNAME} "${@}"
674 python_scriptroot=${1}
677 # @FUNCTION: python_doexe
680 # Install the given executables into the executable install directory,
681 # for the current Python implementation (${EPYTHON}).
683 # The executable will be wrapped properly for the Python implementation,
684 # though no shebang mangling will be performed.
686 debug-print-function ${FUNCNAME} "${@}"
690 python_newexe "${f}" "${f##*/}"
694 # @FUNCTION: python_newexe
695 # @USAGE: <path> <new-name>
697 # Install the given executable into the executable install directory,
698 # for the current Python implementation (${EPYTHON}).
700 # The executable will be wrapped properly for the Python implementation,
701 # though no shebang mangling will be performed. It will be renamed
704 debug-print-function ${FUNCNAME} "${@}"
706 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
707 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
708 if [[ ${EAPI:-0} == [0123] ]]; then
709 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
712 local wrapd=${python_scriptroot:-/usr/bin}
717 local PYTHON_SCRIPTDIR d
718 python_export PYTHON_SCRIPTDIR
719 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
724 newexe "${f}" "${newfn}" || return ${?}
727 # install the wrapper
728 _python_ln_rel "${ED%/}"/usr/lib/python-exec/python-exec2 \
729 "${ED%/}/${wrapd}/${newfn}" || die
731 # don't use this at home, just call python_doscript() instead
732 if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
733 python_fix_shebang -q "${ED%/}/${d}/${newfn}"
737 # @FUNCTION: python_doscript
740 # Install the given scripts into the executable install directory,
741 # for the current Python implementation (${EPYTHON}).
743 # All specified files must start with a 'python' shebang. The shebang
744 # will be converted, and the files will be wrapped properly
745 # for the Python implementation.
750 # python_foreach_impl python_doscript ${PN}
754 debug-print-function ${FUNCNAME} "${@}"
756 local _PYTHON_REWRITE_SHEBANG=1
760 # @FUNCTION: python_newscript
761 # @USAGE: <path> <new-name>
763 # Install the given script into the executable install directory
764 # for the current Python implementation (${EPYTHON}), and name it
767 # The file must start with a 'python' shebang. The shebang will be
768 # converted, and the file will be wrapped properly for the Python
769 # implementation. It will be renamed to <new-name>.
774 # python_foreach_impl python_newscript foo.py foo
778 debug-print-function ${FUNCNAME} "${@}"
780 local _PYTHON_REWRITE_SHEBANG=1
784 # @FUNCTION: python_moduleinto
787 # Set the Python module install directory for python_domodule().
788 # The <new-path> can either be an absolute target system path (in which
789 # case it needs to start with a slash, and ${ED} will be prepended to
790 # it) or relative to the implementation's site-packages directory
791 # (then it must not start with a slash).
793 # When not set explicitly, the modules are installed to the top
794 # site-packages directory.
799 # python_moduleinto bar
800 # # installs ${PYTHON_SITEDIR}/bar/baz.py
801 # python_foreach_impl python_domodule baz.py
805 # Set the current module root. The new value will be stored
806 # in the 'python_moduleroot' environment variable. The new value need
807 # be relative to the site-packages root.
809 # Alternatively, you can set the variable directly.
810 python_moduleinto() {
811 debug-print-function ${FUNCNAME} "${@}"
813 python_moduleroot=${1}
816 # @FUNCTION: python_domodule
819 # Install the given modules (or packages) into the current Python module
820 # installation directory. The list can mention both modules (files)
821 # and packages (directories). All listed files will be installed
822 # for all enabled implementations, and compiled afterwards.
827 # # (${PN} being a directory)
828 # python_foreach_impl python_domodule ${PN}
832 debug-print-function ${FUNCNAME} "${@}"
834 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
835 if [[ ${EAPI:-0} == [0123] ]]; then
836 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
840 if [[ ${python_moduleroot} == /* ]]; then
842 d=${python_moduleroot}
844 # relative to site-packages
845 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
846 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
848 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
853 doins -r "${@}" || return ${?}
856 python_optimize "${ED}/${d}"
859 # @FUNCTION: python_doheader
862 # Install the given headers into the implementation-specific include
863 # directory. This function is unconditionally recursive, i.e. you can
864 # pass directories instead of files.
869 # python_foreach_impl python_doheader foo.h bar.h
873 debug-print-function ${FUNCNAME} "${@}"
875 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
876 if [[ ${EAPI:-0} == [0123] ]]; then
877 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
880 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
881 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
883 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
887 doins -r "${@}" || return ${?}
891 # @FUNCTION: python_wrapper_setup
892 # @USAGE: [<path> [<impl>]]
894 # Create proper 'python' executable and pkg-config wrappers
895 # (if available) in the directory named by <path>. Set up PATH
896 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
898 # The wrappers will be created for implementation named by <impl>,
899 # or for one named by ${EPYTHON} if no <impl> passed.
901 # If the named directory contains a python symlink already, it will
902 # be assumed to contain proper wrappers already and only environment
903 # setup will be done. If wrapper update is requested, the directory
904 # shall be removed first.
905 python_wrapper_setup() {
906 debug-print-function ${FUNCNAME} "${@}"
908 local workdir=${1:-${T}/${EPYTHON}}
909 local impl=${2:-${EPYTHON}}
911 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
912 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
914 if [[ ! -x ${workdir}/bin/python ]]; then
915 _python_check_dead_variables
917 mkdir -p "${workdir}"/{bin,pkgconfig} || die
919 # Clean up, in case we were supposed to do a cheap update.
920 rm -f "${workdir}"/bin/python{,2,3}{,-config} || die
921 rm -f "${workdir}"/bin/2to3 || die
922 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc || die
924 local EPYTHON PYTHON PYTHON_CONFIG
925 python_export "${impl}" EPYTHON PYTHON
928 if python_is_python3; then
937 # note: we don't use symlinks because python likes to do some
938 # symlink reading magic that breaks stuff
939 # https://bugs.gentoo.org/show_bug.cgi?id=555752
940 cat > "${workdir}/bin/python" <<-_EOF_ || die
942 exec "${PYTHON}" "\${@}"
944 cp "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
945 chmod +x "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
947 local nonsupp=( "python${pyother}" "python${pyother}-config" )
950 if [[ ${EPYTHON} == python* ]]; then
951 python_export "${impl}" PYTHON_CONFIG
953 cat > "${workdir}/bin/python-config" <<-_EOF_ || die
955 exec "${PYTHON_CONFIG}" "\${@}"
957 cp "${workdir}/bin/python-config" \
958 "${workdir}/bin/python${pyver}-config" || die
959 chmod +x "${workdir}/bin/python-config" \
960 "${workdir}/bin/python${pyver}-config" || die
963 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
966 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
967 "${workdir}"/pkgconfig/python.pc || die
968 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
970 nonsupp+=( 2to3 python-config "python${pyver}-config" )
974 for x in "${nonsupp[@]}"; do
975 cat >"${workdir}"/bin/${x} <<-_EOF_ || die
977 echo "${x} is not supported by ${EPYTHON}" >&2
980 chmod +x "${workdir}"/bin/${x} || die
983 # Now, set the environment.
984 # But note that ${workdir} may be shared with something else,
985 # and thus already on top of PATH.
986 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
987 PATH=${workdir}/bin${PATH:+:${PATH}}
989 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
990 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
992 export PATH PKG_CONFIG_PATH
996 # @FUNCTION: python_is_python3
999 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
1000 # (i.e. uses syntax and stdlib of Python 3.*).
1002 # Returns 0 (true) if it is, 1 (false) otherwise.
1003 python_is_python3() {
1004 local impl=${1:-${EPYTHON}}
1005 [[ ${impl} ]] || die "python_is_python3: no impl nor EPYTHON"
1007 [[ ${impl} == python3* || ${impl} == pypy3 ]]
1010 # @FUNCTION: python_is_installed
1013 # Check whether the interpreter for <impl> (or ${EPYTHON}) is installed.
1014 # Uses has_version with a proper dependency string.
1016 # Returns 0 (true) if it is, 1 (false) otherwise.
1017 python_is_installed() {
1018 local impl=${1:-${EPYTHON}}
1019 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
1026 if [[ ${PYTHON_REQ_USE} ]]; then
1027 append=[${PYTHON_REQ_USE}]
1030 # be happy with just the interpeter, no need for the virtual
1031 has_version "dev-python/${impl}${append}" \
1032 || has_version "dev-python/${impl}-bin${append}"
1035 local PYTHON_PKG_DEP
1036 python_export "${impl}" PYTHON_PKG_DEP
1037 has_version "${PYTHON_PKG_DEP}"
1042 # @FUNCTION: python_fix_shebang
1043 # @USAGE: [-f|--force] [-q|--quiet] <path>...
1045 # Replace the shebang in Python scripts with the current Python
1046 # implementation (EPYTHON). If a directory is passed, works recursively
1047 # on all Python scripts.
1049 # Only files having a 'python*' shebang will be modified. Files with
1050 # other shebang will either be skipped when working recursively
1051 # on a directory or treated as error when specified explicitly.
1053 # Shebangs matching explicitly current Python version will be left
1054 # unmodified. Shebangs requesting another Python version will be treated
1055 # as fatal error, unless --force is given.
1057 # --force causes the function to replace even shebangs that require
1058 # incompatible Python version. --quiet causes the function not to list
1059 # modified files verbosely.
1060 python_fix_shebang() {
1061 debug-print-function ${FUNCNAME} "${@}"
1063 [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
1066 while [[ ${@} ]]; do
1068 -f|--force) force=1; shift;;
1069 -q|--quiet) quiet=1; shift;;
1075 [[ ${1} ]] || die "${FUNCNAME}: no paths given"
1079 local any_correct any_fixed is_recursive
1081 [[ -d ${path} ]] && is_recursive=1
1083 while IFS= read -r -d '' f; do
1087 # note: we can't ||die here since read will fail if file
1088 # has no newline characters
1089 IFS= read -r shebang <"${f}"
1091 # First, check if it's shebang at all...
1092 if [[ ${shebang} == '#!'* ]]; then
1093 local split_shebang=()
1094 read -r -a split_shebang <<<${shebang} || die
1096 # Match left-to-right in a loop, to avoid matching random
1097 # repetitions like 'python2.7 python2'.
1098 for i in "${split_shebang[@]}"; do
1101 debug-print "${FUNCNAME}: in file ${f#${D}}"
1102 debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
1104 # Nothing to do, move along.
1109 *python|*python[23])
1110 debug-print "${FUNCNAME}: in file ${f#${D}}"
1111 debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
1113 if [[ ${i} == *python2 ]]; then
1115 if [[ ! ${force} ]]; then
1116 python_is_python3 "${EPYTHON}" && error=1
1118 elif [[ ${i} == *python3 ]]; then
1120 if [[ ! ${force} ]]; then
1121 python_is_python3 "${EPYTHON}" || error=1
1128 *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789])
1129 # Explicit mismatch.
1130 if [[ ! ${force} ]]; then
1134 *python[23].[0123456789])
1135 from="python[23].[0123456789]";;
1140 *jython[23].[0123456789])
1141 from="jython[23].[0123456789]";;
1143 die "${FUNCNAME}: internal error in 2nd pattern match";;
1152 if [[ ! ${error} && ! ${from} ]]; then
1153 # Non-Python shebang. Allowed in recursive mode,
1154 # disallowed when specifying file explicitly.
1155 [[ ${is_recursive} ]] && continue
1159 if [[ ! ${quiet} ]]; then
1160 einfo "Fixing shebang in ${f#${D}}."
1163 if [[ ! ${error} ]]; then
1164 # We either want to match ${from} followed by space
1165 # or at end-of-string.
1166 if [[ ${shebang} == *${from}" "* ]]; then
1167 sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die
1169 sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
1173 eerror "The file has incompatible shebang:"
1174 eerror " file: ${f#${D}}"
1175 eerror " current shebang: ${shebang}"
1176 eerror " requested impl: ${EPYTHON}"
1177 die "${FUNCNAME}: conversion of incompatible shebang requested"
1179 done < <(find -H "${path}" -type f -print0 || die)
1181 if [[ ! ${any_fixed} ]]; then
1183 [[ ${EAPI:-0} == [012345] ]] && cmd=eqawarn
1185 "${cmd}" "QA warning: ${FUNCNAME}, ${path#${D}} did not match any fixable files."
1186 if [[ ${any_correct} ]]; then
1187 "${cmd}" "All files have ${EPYTHON} shebang already."
1189 "${cmd}" "There are no Python files in specified directory."
1192 [[ ${cmd} == eerror ]] && die "${FUNCNAME} did not match any fixable files (QA warning fatal in EAPI ${EAPI})"
1197 # @FUNCTION: _python_check_locale_sanity
1199 # @RETURN: 0 if sane, 1 otherwise
1201 # Check whether the specified locale sanely maps between lowercase
1202 # and uppercase ASCII characters.
1203 _python_check_locale_sanity() {
1204 local -x LC_CTYPE=${1}
1209 local input="${lc[*]}${uc[*]}"
1211 local output=$(tr '[:lower:][:upper:]' '[:upper:][:lower:]' <<<"${input}")
1212 [[ ${output} == "${uc[*]}${lc[*]}" ]]
1215 # @FUNCTION: python_export_utf8_locale
1216 # @RETURN: 0 on success, 1 on failure.
1218 # Attempts to export a usable UTF-8 locale in the LC_CTYPE variable. Does
1219 # nothing if LC_ALL is defined, or if the current locale uses a UTF-8 charmap.
1220 # This may be used to work around the quirky open() behavior of python3.
1221 python_export_utf8_locale() {
1222 debug-print-function ${FUNCNAME} "${@}"
1224 # If the locale program isn't available, just return.
1225 type locale >/dev/null || return 0
1227 if [[ $(locale charmap) != UTF-8 ]]; then
1228 # Try English first, then everything else.
1229 local lang locales="en_US.UTF-8 $(locale -a)"
1231 for lang in ${locales}; do
1232 if [[ $(LC_ALL=${lang} locale charmap 2>/dev/null) == UTF-8 ]]; then
1233 if _python_check_locale_sanity "${lang}"; then
1234 export LC_CTYPE=${lang}
1235 if [[ -n ${LC_ALL} ]]; then
1236 export LC_NUMERIC=${LC_ALL}
1237 export LC_TIME=${LC_ALL}
1238 export LC_COLLATE=${LC_ALL}
1239 export LC_MONETARY=${LC_ALL}
1240 export LC_MESSAGES=${LC_ALL}
1241 export LC_PAPER=${LC_ALL}
1242 export LC_NAME=${LC_ALL}
1243 export LC_ADDRESS=${LC_ALL}
1244 export LC_TELEPHONE=${LC_ALL}
1245 export LC_MEASUREMENT=${LC_ALL}
1246 export LC_IDENTIFICATION=${LC_ALL}
1254 ewarn "Could not find a UTF-8 locale. This may trigger build failures in"
1255 ewarn "some python packages. Please ensure that a UTF-8 locale is listed in"
1256 ewarn "/etc/locale.gen and run locale-gen."
1263 # -- python.eclass functions --
1265 _python_check_dead_variables() {
1268 for v in PYTHON_DEPEND PYTHON_USE_WITH{,_OR,_OPT} {RESTRICT,SUPPORT}_PYTHON_ABIS
1270 if [[ ${!v} ]]; then
1271 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Ebuild_head"
1275 for v in PYTHON_{CPPFLAGS,CFLAGS,CXXFLAGS,LDFLAGS}
1277 if [[ ${!v} ]]; then
1278 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#PYTHON_CFLAGS"
1282 for v in PYTHON_TESTS_RESTRICTED_ABIS PYTHON_EXPORT_PHASE_FUNCTIONS \
1283 PYTHON_VERSIONED_{SCRIPTS,EXECUTABLES} PYTHON_NONVERSIONED_EXECUTABLES
1285 if [[ ${!v} ]]; then
1286 die "${v} is invalid for python-r1 suite"
1290 for v in DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES DISTUTILS_SETUP_FILES \
1291 DISTUTILS_GLOBAL_OPTIONS DISTUTILS_SRC_TEST PYTHON_MODNAME
1293 if [[ ${!v} ]]; then
1294 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#${v}"
1298 if [[ ${DISTUTILS_DISABLE_TEST_DEPENDENCY} ]]; then
1299 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#DISTUTILS_SRC_TEST"
1302 # python.eclass::progress
1303 for v in PYTHON_BDEPEND PYTHON_MULTIPLE_ABIS PYTHON_ABI_TYPE \
1304 PYTHON_RESTRICTED_ABIS PYTHON_TESTS_FAILURES_TOLERANT_ABIS \
1305 PYTHON_CFFI_MODULES_GENERATION_COMMANDS
1307 if [[ ${!v} ]]; then
1308 die "${v} is invalid for python-r1 suite"
1313 python_pkg_setup() {
1314 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1317 python_convert_shebangs() {
1318 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"
1321 python_clean_py-compile_files() {
1322 die "${FUNCNAME}() is invalid for python-r1 suite"
1325 python_clean_installation_image() {
1326 die "${FUNCNAME}() is invalid for python-r1 suite"
1329 python_execute_function() {
1330 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"
1333 python_generate_wrapper_scripts() {
1334 die "${FUNCNAME}() is invalid for python-r1 suite"
1337 python_merge_intermediate_installation_images() {
1338 die "${FUNCNAME}() is invalid for python-r1 suite"
1341 python_set_active_version() {
1342 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1345 python_need_rebuild() {
1346 die "${FUNCNAME}() is invalid for python-r1 suite"
1350 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"
1353 python_get_implementation() {
1354 die "${FUNCNAME}() is invalid for python-r1 suite"
1357 python_get_implementational_package() {
1358 die "${FUNCNAME}() is invalid for python-r1 suite"
1361 python_get_libdir() {
1362 die "${FUNCNAME}() is invalid for python-r1 suite"
1365 python_get_library() {
1366 die "${FUNCNAME}() is invalid for python-r1 suite"
1369 python_get_version() {
1370 die "${FUNCNAME}() is invalid for python-r1 suite"
1373 python_get_implementation_and_version() {
1374 die "${FUNCNAME}() is invalid for python-r1 suite"
1377 python_execute_nosetests() {
1378 die "${FUNCNAME}() is invalid for python-r1 suite"
1381 python_execute_py.test() {
1382 die "${FUNCNAME}() is invalid for python-r1 suite"
1385 python_execute_trial() {
1386 die "${FUNCNAME}() is invalid for python-r1 suite"
1389 python_enable_pyc() {
1390 die "${FUNCNAME}() is invalid for python-r1 suite"
1393 python_disable_pyc() {
1394 die "${FUNCNAME}() is invalid for python-r1 suite"
1397 python_mod_optimize() {
1398 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"
1401 python_mod_cleanup() {
1402 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"
1405 # python.eclass::progress
1407 python_abi_depend() {
1408 die "${FUNCNAME}() is invalid for python-r1 suite"
1411 python_install_executables() {
1412 die "${FUNCNAME}() is invalid for python-r1 suite"
1415 python_get_extension_module_suffix() {
1416 die "${FUNCNAME}() is invalid for python-r1 suite"
1419 python_byte-compile_modules() {
1420 die "${FUNCNAME}() is invalid for python-r1 suite"
1423 python_clean_byte-compiled_modules() {
1424 die "${FUNCNAME}() is invalid for python-r1 suite"
1427 python_generate_cffi_modules() {
1428 die "${FUNCNAME}() is invalid for python-r1 suite"