1 # Copyright 1999-2015 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 # @ECLASS-VARIABLE: python_scriptroot
657 # The current script destination for python_doscript(). The path
658 # is relative to the installation root (${ED}).
660 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
662 # Can be set indirectly through the python_scriptinto() function.
667 # local python_scriptroot=${GAMES_BINDIR}
668 # python_foreach_impl python_doscript foo
672 # @FUNCTION: python_scriptinto
675 # Set the current scriptroot. The new value will be stored
676 # in the 'python_scriptroot' environment variable. The new value need
677 # be relative to the installation root (${ED}).
679 # Alternatively, you can set the variable directly.
680 python_scriptinto() {
681 debug-print-function ${FUNCNAME} "${@}"
683 python_scriptroot=${1}
686 # @FUNCTION: python_doexe
689 # Install the given executables into current python_scriptroot,
690 # for the current Python implementation (${EPYTHON}).
692 # The executable will be wrapped properly for the Python implementation,
693 # though no shebang mangling will be performed.
695 debug-print-function ${FUNCNAME} "${@}"
699 python_newexe "${f}" "${f##*/}"
703 # @FUNCTION: python_newexe
704 # @USAGE: <path> <new-name>
706 # Install the given executable into current python_scriptroot,
707 # for the current Python implementation (${EPYTHON}).
709 # The executable will be wrapped properly for the Python implementation,
710 # though no shebang mangling will be performed. It will be renamed
713 debug-print-function ${FUNCNAME} "${@}"
715 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
716 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
717 if [[ ${EAPI:-0} == [0123] ]]; then
718 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
721 local wrapd=${python_scriptroot:-${DESTTREE}/bin}
726 local PYTHON_SCRIPTDIR d
727 python_export PYTHON_SCRIPTDIR
728 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
733 newexe "${f}" "${newfn}" || return ${?}
736 # install the wrapper
737 _python_ln_rel "${ED%/}"/usr/lib/python-exec/python-exec2 \
738 "${ED%/}/${wrapd}/${newfn}" || die
740 # don't use this at home, just call python_doscript() instead
741 if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
742 python_fix_shebang -q "${ED%/}/${d}/${newfn}"
746 # @FUNCTION: python_doscript
749 # Install the given scripts into current python_scriptroot,
750 # for the current Python implementation (${EPYTHON}).
752 # All specified files must start with a 'python' shebang. The shebang
753 # will be converted, and the files will be wrapped properly
754 # for the Python implementation.
759 # python_foreach_impl python_doscript ${PN}
763 debug-print-function ${FUNCNAME} "${@}"
765 local _PYTHON_REWRITE_SHEBANG=1
769 # @FUNCTION: python_newscript
770 # @USAGE: <path> <new-name>
772 # Install the given script into current python_scriptroot
773 # for the current Python implementation (${EPYTHON}), and name it
776 # The file must start with a 'python' shebang. The shebang will be
777 # converted, and the file will be wrapped properly for the Python
778 # implementation. It will be renamed to <new-name>.
783 # python_foreach_impl python_newscript foo.py foo
787 debug-print-function ${FUNCNAME} "${@}"
789 local _PYTHON_REWRITE_SHEBANG=1
793 # @ECLASS-VARIABLE: python_moduleroot
796 # The current module root for python_domodule(). The path can be either
797 # an absolute system path (it must start with a slash, and ${ED} will be
798 # prepended to it) or relative to the implementation's site-packages directory
799 # (then it must start with a non-slash character).
801 # When unset, the modules will be installed in the site-packages root.
803 # Can be set indirectly through the python_moduleinto() function.
808 # local python_moduleroot=bar
809 # # installs ${PYTHON_SITEDIR}/bar/baz.py
810 # python_foreach_impl python_domodule baz.py
814 # @FUNCTION: python_moduleinto
817 # Set the current module root. The new value will be stored
818 # in the 'python_moduleroot' environment variable. The new value need
819 # be relative to the site-packages root.
821 # Alternatively, you can set the variable directly.
822 python_moduleinto() {
823 debug-print-function ${FUNCNAME} "${@}"
825 python_moduleroot=${1}
828 # @FUNCTION: python_domodule
831 # Install the given modules (or packages) into the current
832 # python_moduleroot. The list can mention both modules (files)
833 # and packages (directories). All listed files will be installed
834 # for all enabled implementations, and compiled afterwards.
839 # # (${PN} being a directory)
840 # python_foreach_impl python_domodule ${PN}
844 debug-print-function ${FUNCNAME} "${@}"
846 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
847 if [[ ${EAPI:-0} == [0123] ]]; then
848 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
852 if [[ ${python_moduleroot} == /* ]]; then
854 d=${python_moduleroot}
856 # relative to site-packages
857 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
858 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
860 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
865 doins -r "${@}" || return ${?}
868 python_optimize "${ED}/${d}"
871 # @FUNCTION: python_doheader
874 # Install the given headers into the implementation-specific include
875 # directory. This function is unconditionally recursive, i.e. you can
876 # pass directories instead of files.
881 # python_foreach_impl python_doheader foo.h bar.h
885 debug-print-function ${FUNCNAME} "${@}"
887 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
888 if [[ ${EAPI:-0} == [0123] ]]; then
889 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
892 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
893 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
895 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
899 doins -r "${@}" || return ${?}
903 # @FUNCTION: python_wrapper_setup
904 # @USAGE: [<path> [<impl>]]
906 # Create proper 'python' executable and pkg-config wrappers
907 # (if available) in the directory named by <path>. Set up PATH
908 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
910 # The wrappers will be created for implementation named by <impl>,
911 # or for one named by ${EPYTHON} if no <impl> passed.
913 # If the named directory contains a python symlink already, it will
914 # be assumed to contain proper wrappers already and only environment
915 # setup will be done. If wrapper update is requested, the directory
916 # shall be removed first.
917 python_wrapper_setup() {
918 debug-print-function ${FUNCNAME} "${@}"
920 local workdir=${1:-${T}/${EPYTHON}}
921 local impl=${2:-${EPYTHON}}
923 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
924 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
926 if [[ ! -x ${workdir}/bin/python ]]; then
927 _python_check_dead_variables
929 mkdir -p "${workdir}"/{bin,pkgconfig} || die
931 # Clean up, in case we were supposed to do a cheap update.
932 rm -f "${workdir}"/bin/python{,2,3,-config} || die
933 rm -f "${workdir}"/bin/2to3 || die
934 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc || die
936 local EPYTHON PYTHON PYTHON_CONFIG
937 python_export "${impl}" EPYTHON PYTHON
940 if python_is_python3; then
949 # note: we don't use symlinks because python likes to do some
950 # symlink reading magic that breaks stuff
951 # https://bugs.gentoo.org/show_bug.cgi?id=555752
952 cat > "${workdir}/bin/python" <<-_EOF_ || die
954 exec "${PYTHON}" "\${@}"
956 cp "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
957 chmod +x "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
959 local nonsupp=( "python${pyother}" "python${pyother}-config" )
962 if [[ ${EPYTHON} == python* ]]; then
963 python_export "${impl}" PYTHON_CONFIG
965 cat > "${workdir}/bin/python-config" <<-_EOF_ || die
967 exec "${PYTHON_CONFIG}" "\${@}"
969 cp "${workdir}/bin/python-config" \
970 "${workdir}/bin/python${pyver}-config" || die
971 chmod +x "${workdir}/bin/python-config" \
972 "${workdir}/bin/python${pyver}-config" || die
975 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
978 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
979 "${workdir}"/pkgconfig/python.pc || die
980 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
982 nonsupp+=( 2to3 python-config "python${pyver}-config" )
986 for x in "${nonsupp[@]}"; do
987 cat >"${workdir}"/bin/${x} <<-_EOF_ || die
989 echo "${x} is not supported by ${EPYTHON}" >&2
992 chmod +x "${workdir}"/bin/${x} || die
995 # Now, set the environment.
996 # But note that ${workdir} may be shared with something else,
997 # and thus already on top of PATH.
998 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
999 PATH=${workdir}/bin${PATH:+:${PATH}}
1001 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
1002 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
1004 export PATH PKG_CONFIG_PATH
1008 # @FUNCTION: python_is_python3
1011 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
1012 # (i.e. uses syntax and stdlib of Python 3.*).
1014 # Returns 0 (true) if it is, 1 (false) otherwise.
1015 python_is_python3() {
1016 local impl=${1:-${EPYTHON}}
1017 [[ ${impl} ]] || die "python_is_python3: no impl nor EPYTHON"
1019 [[ ${impl} == python3* || ${impl} == pypy3 ]]
1022 # @FUNCTION: python_is_installed
1025 # Check whether the interpreter for <impl> (or ${EPYTHON}) is installed.
1026 # Uses has_version with a proper dependency string.
1028 # Returns 0 (true) if it is, 1 (false) otherwise.
1029 python_is_installed() {
1030 local impl=${1:-${EPYTHON}}
1031 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
1038 if [[ ${PYTHON_REQ_USE} ]]; then
1039 append=[${PYTHON_REQ_USE}]
1042 # be happy with just the interpeter, no need for the virtual
1043 has_version "dev-python/${impl}${append}" \
1044 || has_version "dev-python/${impl}-bin${append}"
1047 local PYTHON_PKG_DEP
1048 python_export "${impl}" PYTHON_PKG_DEP
1049 has_version "${PYTHON_PKG_DEP}"
1054 # @FUNCTION: python_fix_shebang
1055 # @USAGE: [-f|--force] [-q|--quiet] <path>...
1057 # Replace the shebang in Python scripts with the current Python
1058 # implementation (EPYTHON). If a directory is passed, works recursively
1059 # on all Python scripts.
1061 # Only files having a 'python*' shebang will be modified. Files with
1062 # other shebang will either be skipped when working recursively
1063 # on a directory or treated as error when specified explicitly.
1065 # Shebangs matching explicitly current Python version will be left
1066 # unmodified. Shebangs requesting another Python version will be treated
1067 # as fatal error, unless --force is given.
1069 # --force causes the function to replace even shebangs that require
1070 # incompatible Python version. --quiet causes the function not to list
1071 # modified files verbosely.
1072 python_fix_shebang() {
1073 debug-print-function ${FUNCNAME} "${@}"
1075 [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
1078 while [[ ${@} ]]; do
1080 -f|--force) force=1; shift;;
1081 -q|--quiet) quiet=1; shift;;
1087 [[ ${1} ]] || die "${FUNCNAME}: no paths given"
1091 local any_correct any_fixed is_recursive
1093 [[ -d ${path} ]] && is_recursive=1
1095 while IFS= read -r -d '' f; do
1099 # note: we can't ||die here since read will fail if file
1100 # has no newline characters
1101 IFS= read -r shebang <"${f}"
1103 # First, check if it's shebang at all...
1104 if [[ ${shebang} == '#!'* ]]; then
1105 local split_shebang=()
1106 read -r -a split_shebang <<<${shebang} || die
1108 # Match left-to-right in a loop, to avoid matching random
1109 # repetitions like 'python2.7 python2'.
1110 for i in "${split_shebang[@]}"; do
1113 debug-print "${FUNCNAME}: in file ${f#${D}}"
1114 debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
1116 # Nothing to do, move along.
1121 *python|*python[23])
1122 debug-print "${FUNCNAME}: in file ${f#${D}}"
1123 debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
1125 if [[ ${i} == *python2 ]]; then
1127 if [[ ! ${force} ]]; then
1128 python_is_python3 "${EPYTHON}" && error=1
1130 elif [[ ${i} == *python3 ]]; then
1132 if [[ ! ${force} ]]; then
1133 python_is_python3 "${EPYTHON}" || error=1
1140 *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789])
1141 # Explicit mismatch.
1142 if [[ ! ${force} ]]; then
1146 *python[23].[0123456789])
1147 from="python[23].[0123456789]";;
1152 *jython[23].[0123456789])
1153 from="jython[23].[0123456789]";;
1155 die "${FUNCNAME}: internal error in 2nd pattern match";;
1164 if [[ ! ${error} && ! ${from} ]]; then
1165 # Non-Python shebang. Allowed in recursive mode,
1166 # disallowed when specifying file explicitly.
1167 [[ ${is_recursive} ]] && continue
1171 if [[ ! ${quiet} ]]; then
1172 einfo "Fixing shebang in ${f#${D}}."
1175 if [[ ! ${error} ]]; then
1176 # We either want to match ${from} followed by space
1177 # or at end-of-string.
1178 if [[ ${shebang} == *${from}" "* ]]; then
1179 sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die
1181 sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
1185 eerror "The file has incompatible shebang:"
1186 eerror " file: ${f#${D}}"
1187 eerror " current shebang: ${shebang}"
1188 eerror " requested impl: ${EPYTHON}"
1189 die "${FUNCNAME}: conversion of incompatible shebang requested"
1191 done < <(find -H "${path}" -type f -print0 || die)
1193 if [[ ! ${any_fixed} ]]; then
1195 [[ ${EAPI:-0} == [012345] ]] && cmd=eqawarn
1197 "${cmd}" "QA warning: ${FUNCNAME}, ${path#${D}} did not match any fixable files."
1198 if [[ ${any_correct} ]]; then
1199 "${cmd}" "All files have ${EPYTHON} shebang already."
1201 "${cmd}" "There are no Python files in specified directory."
1204 [[ ${cmd} == eerror ]] && die "${FUNCNAME} did not match any fixable files (QA warning fatal in EAPI ${EAPI})"
1209 # @FUNCTION: _python_check_locale_sanity
1211 # @RETURN: 0 if sane, 1 otherwise
1213 # Check whether the specified locale sanely maps between lowercase
1214 # and uppercase ASCII characters.
1215 _python_check_locale_sanity() {
1216 local -x LC_CTYPE=${1}
1221 local input="${lc[*]}${uc[*]}"
1223 local output=$(tr '[:lower:][:upper:]' '[:upper:][:lower:]' <<<"${input}")
1224 [[ ${output} == "${uc[*]}${lc[*]}" ]]
1227 # @FUNCTION: python_export_utf8_locale
1228 # @RETURN: 0 on success, 1 on failure.
1230 # Attempts to export a usable UTF-8 locale in the LC_CTYPE variable. Does
1231 # nothing if LC_ALL is defined, or if the current locale uses a UTF-8 charmap.
1232 # This may be used to work around the quirky open() behavior of python3.
1233 python_export_utf8_locale() {
1234 debug-print-function ${FUNCNAME} "${@}"
1236 # If the locale program isn't available, just return.
1237 type locale >/dev/null || return 0
1239 if [[ $(locale charmap) != UTF-8 ]]; then
1240 # Try English first, then everything else.
1241 local lang locales="en_US.UTF-8 $(locale -a)"
1243 for lang in ${locales}; do
1244 if [[ $(LC_ALL=${lang} locale charmap 2>/dev/null) == UTF-8 ]]; then
1245 if _python_check_locale_sanity "${lang}"; then
1246 export LC_CTYPE=${lang}
1247 if [[ -n ${LC_ALL} ]]; then
1248 export LC_NUMERIC=${LC_ALL}
1249 export LC_TIME=${LC_ALL}
1250 export LC_COLLATE=${LC_ALL}
1251 export LC_MONETARY=${LC_ALL}
1252 export LC_MESSAGES=${LC_ALL}
1253 export LC_PAPER=${LC_ALL}
1254 export LC_NAME=${LC_ALL}
1255 export LC_ADDRESS=${LC_ALL}
1256 export LC_TELEPHONE=${LC_ALL}
1257 export LC_MEASUREMENT=${LC_ALL}
1258 export LC_IDENTIFICATION=${LC_ALL}
1266 ewarn "Could not find a UTF-8 locale. This may trigger build failures in"
1267 ewarn "some python packages. Please ensure that a UTF-8 locale is listed in"
1268 ewarn "/etc/locale.gen and run locale-gen."
1275 # -- python.eclass functions --
1277 _python_check_dead_variables() {
1280 for v in PYTHON_DEPEND PYTHON_USE_WITH{,_OR,_OPT} {RESTRICT,SUPPORT}_PYTHON_ABIS
1282 if [[ ${!v} ]]; then
1283 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Ebuild_head"
1287 for v in PYTHON_{CPPFLAGS,CFLAGS,CXXFLAGS,LDFLAGS}
1289 if [[ ${!v} ]]; then
1290 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#PYTHON_CFLAGS"
1294 for v in PYTHON_TESTS_RESTRICTED_ABIS PYTHON_EXPORT_PHASE_FUNCTIONS \
1295 PYTHON_VERSIONED_{SCRIPTS,EXECUTABLES} PYTHON_NONVERSIONED_EXECUTABLES
1297 if [[ ${!v} ]]; then
1298 die "${v} is invalid for python-r1 suite"
1302 for v in DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES DISTUTILS_SETUP_FILES \
1303 DISTUTILS_GLOBAL_OPTIONS DISTUTILS_SRC_TEST PYTHON_MODNAME
1305 if [[ ${!v} ]]; then
1306 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#${v}"
1310 if [[ ${DISTUTILS_DISABLE_TEST_DEPENDENCY} ]]; then
1311 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#DISTUTILS_SRC_TEST"
1314 # python.eclass::progress
1315 for v in PYTHON_BDEPEND PYTHON_MULTIPLE_ABIS PYTHON_ABI_TYPE \
1316 PYTHON_RESTRICTED_ABIS PYTHON_TESTS_FAILURES_TOLERANT_ABIS \
1317 PYTHON_CFFI_MODULES_GENERATION_COMMANDS
1319 if [[ ${!v} ]]; then
1320 die "${v} is invalid for python-r1 suite"
1325 python_pkg_setup() {
1326 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1329 python_convert_shebangs() {
1330 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"
1333 python_clean_py-compile_files() {
1334 die "${FUNCNAME}() is invalid for python-r1 suite"
1337 python_clean_installation_image() {
1338 die "${FUNCNAME}() is invalid for python-r1 suite"
1341 python_execute_function() {
1342 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"
1345 python_generate_wrapper_scripts() {
1346 die "${FUNCNAME}() is invalid for python-r1 suite"
1349 python_merge_intermediate_installation_images() {
1350 die "${FUNCNAME}() is invalid for python-r1 suite"
1353 python_set_active_version() {
1354 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1357 python_need_rebuild() {
1358 die "${FUNCNAME}() is invalid for python-r1 suite"
1362 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"
1365 python_get_implementation() {
1366 die "${FUNCNAME}() is invalid for python-r1 suite"
1369 python_get_implementational_package() {
1370 die "${FUNCNAME}() is invalid for python-r1 suite"
1373 python_get_libdir() {
1374 die "${FUNCNAME}() is invalid for python-r1 suite"
1377 python_get_library() {
1378 die "${FUNCNAME}() is invalid for python-r1 suite"
1381 python_get_version() {
1382 die "${FUNCNAME}() is invalid for python-r1 suite"
1385 python_get_implementation_and_version() {
1386 die "${FUNCNAME}() is invalid for python-r1 suite"
1389 python_execute_nosetests() {
1390 die "${FUNCNAME}() is invalid for python-r1 suite"
1393 python_execute_py.test() {
1394 die "${FUNCNAME}() is invalid for python-r1 suite"
1397 python_execute_trial() {
1398 die "${FUNCNAME}() is invalid for python-r1 suite"
1401 python_enable_pyc() {
1402 die "${FUNCNAME}() is invalid for python-r1 suite"
1405 python_disable_pyc() {
1406 die "${FUNCNAME}() is invalid for python-r1 suite"
1409 python_mod_optimize() {
1410 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"
1413 python_mod_cleanup() {
1414 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"
1417 # python.eclass::progress
1419 python_abi_depend() {
1420 die "${FUNCNAME}() is invalid for python-r1 suite"
1423 python_install_executables() {
1424 die "${FUNCNAME}() is invalid for python-r1 suite"
1427 python_get_extension_module_suffix() {
1428 die "${FUNCNAME}() is invalid for python-r1 suite"
1431 python_byte-compile_modules() {
1432 die "${FUNCNAME}() is invalid for python-r1 suite"
1435 python_clean_byte-compiled_modules() {
1436 die "${FUNCNAME}() is invalid for python-r1 suite"
1439 python_generate_cffi_modules() {
1440 die "${FUNCNAME}() is invalid for python-r1 suite"