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.
46 python3_3 python3_4 python3_5
50 # @FUNCTION: _python_impl_supported
54 # Check whether the implementation <impl> (PYTHON_COMPAT-form)
57 # Returns 0 if the implementation is valid and supported. If it is
58 # unsupported, returns 1 -- and the caller should ignore the entry.
59 # If it is invalid, dies with an appopriate error messages.
60 _python_impl_supported() {
61 debug-print-function ${FUNCNAME} "${@}"
63 [[ ${#} -eq 1 ]] || die "${FUNCNAME}: takes exactly 1 argument (impl)."
67 # keep in sync with _PYTHON_ALL_IMPLS!
68 # (not using that list because inline patterns shall be faster)
70 python2_7|python3_[345]|jython2_[57])
73 pypy1_[89]|pypy2_0|python2_[56]|python3_[12])
77 if [[ ${EAPI:-0} == [01234] ]]; then
78 die "PyPy is supported in EAPI 5 and newer only."
82 die "Invalid implementation in PYTHON_COMPAT: ${impl}"
86 # @ECLASS-VARIABLE: PYTHON
89 # The absolute path to the current Python interpreter.
91 # This variable is set automatically in the following contexts:
93 # python-r1: Set in functions called by python_foreach_impl() or after
94 # calling python_export_best().
96 # python-single-r1: Set after calling python-single-r1_pkg_setup().
98 # distutils-r1: Set within any of the python sub-phase functions.
105 # @ECLASS-VARIABLE: EPYTHON
108 # The executable name of the current Python interpreter.
110 # This variable is set automatically in the following contexts:
112 # python-r1: Set in functions called by python_foreach_impl() or after
113 # calling python_export_best().
115 # python-single-r1: Set after calling python-single-r1_pkg_setup().
117 # distutils-r1: Set within any of the python sub-phase functions.
124 # @ECLASS-VARIABLE: PYTHON_SITEDIR
127 # The path to Python site-packages directory.
129 # Set and exported on request using python_export().
130 # Requires a proper build-time dependency on the Python implementation.
134 # /usr/lib64/python2.7/site-packages
137 # @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
140 # The path to Python include directory.
142 # Set and exported on request using python_export().
143 # Requires a proper build-time dependency on the Python implementation.
147 # /usr/include/python2.7
150 # @ECLASS-VARIABLE: PYTHON_LIBPATH
153 # The path to Python library.
155 # Set and exported on request using python_export().
156 # Valid only for CPython. Requires a proper build-time dependency
157 # on the Python implementation.
161 # /usr/lib64/libpython2.7.so
164 # @ECLASS-VARIABLE: PYTHON_CFLAGS
167 # Proper C compiler flags for building against Python. Obtained from
168 # pkg-config or python-config.
170 # Set and exported on request using python_export().
171 # Valid only for CPython. Requires a proper build-time dependency
172 # on the Python implementation and on pkg-config.
176 # -I/usr/include/python2.7
179 # @ECLASS-VARIABLE: PYTHON_LIBS
182 # Proper C compiler flags for linking against Python. Obtained from
183 # pkg-config or python-config.
185 # Set and exported on request using python_export().
186 # Valid only for CPython. Requires a proper build-time dependency
187 # on the Python implementation and on pkg-config.
194 # @ECLASS-VARIABLE: PYTHON_CONFIG
197 # Path to the python-config executable.
199 # Set and exported on request using python_export().
200 # Valid only for CPython. Requires a proper build-time dependency
201 # on the Python implementation and on pkg-config.
205 # /usr/bin/python2.7-config
208 # @ECLASS-VARIABLE: PYTHON_PKG_DEP
211 # The complete dependency on a particular Python package as a string.
213 # Set and exported on request using python_export().
217 # dev-lang/python:2.7[xml]
220 # @ECLASS-VARIABLE: PYTHON_SCRIPTDIR
223 # The location where Python scripts must be installed for current impl.
225 # Set and exported on request using python_export().
229 # /usr/lib/python-exec/python2.7
232 # @FUNCTION: python_export
233 # @USAGE: [<impl>] <variables>...
235 # Set and export the Python implementation-relevant variables passed
238 # The optional first parameter may specify the requested Python
239 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
240 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
241 # the current one will be obtained from ${EPYTHON}.
243 # The variables which can be exported are: PYTHON, EPYTHON,
244 # PYTHON_SITEDIR. They are described more completely in the eclass
245 # variable documentation.
247 debug-print-function ${FUNCNAME} "${@}"
262 if [[ -z ${impl} ]]; then
263 die "python_export called without a python implementation and EPYTHON is unset"
267 debug-print "${FUNCNAME}: implementation: ${impl}"
272 export EPYTHON=${impl}
273 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
276 export PYTHON=${EPREFIX}/usr/bin/${impl}
277 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
280 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
281 # sysconfig can't be used because:
282 # 1) pypy doesn't give site-packages but stdlib
283 # 2) jython gives paths with wrong case
284 PYTHON_SITEDIR=$("${PYTHON}" -c 'import distutils.sysconfig; print(distutils.sysconfig.get_python_lib())') || die
285 export PYTHON_SITEDIR
286 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
289 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
290 PYTHON_INCLUDEDIR=$("${PYTHON}" -c 'import distutils.sysconfig; print(distutils.sysconfig.get_python_inc())') || die
291 export PYTHON_INCLUDEDIR
292 debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
294 # Jython gives a non-existing directory
295 if [[ ! -d ${PYTHON_INCLUDEDIR} ]]; then
296 die "${impl} does not install any header files!"
300 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
301 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
302 export PYTHON_LIBPATH
303 debug-print "${FUNCNAME}: PYTHON_LIBPATH = ${PYTHON_LIBPATH}"
305 if [[ ! ${PYTHON_LIBPATH} ]]; then
306 die "${impl} lacks a (usable) dynamic library"
314 # python-2.7, python-3.2, etc.
315 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-}) || die
318 die "${impl}: obtaining ${var} not supported"
322 export PYTHON_CFLAGS=${val}
323 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
330 # python-2.7, python-3.2, etc.
331 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-}) || die
334 die "${impl}: obtaining ${var} not supported"
338 export PYTHON_LIBS=${val}
339 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
346 [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
347 flags=$("${PYTHON}" -c 'import sysconfig; print(sysconfig.get_config_var("ABIFLAGS") or "")') || die
348 val=${PYTHON}${flags}-config
351 die "${impl}: obtaining ${var} not supported"
355 export PYTHON_CONFIG=${val}
356 debug-print "${FUNCNAME}: PYTHON_CONFIG = ${PYTHON_CONFIG}"
362 PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
364 PYTHON_PKG_DEP='>=dev-lang/python-3.3.2-r2:3.3';;
366 PYTHON_PKG_DEP="dev-lang/python:${impl#python}";;
368 PYTHON_PKG_DEP='virtual/pypy:0=';;
370 PYTHON_PKG_DEP='virtual/pypy3:0=';;
372 PYTHON_PKG_DEP='>=dev-java/jython-2.5.3-r2:2.5';;
374 PYTHON_PKG_DEP='dev-java/jython:2.7';;
376 die "Invalid implementation: ${impl}"
380 if [[ ${PYTHON_REQ_USE} ]]; then
381 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
384 export PYTHON_PKG_DEP
385 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
389 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
390 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
393 die "python_export: unknown variable ${var}"
398 # @FUNCTION: python_get_sitedir
401 # Obtain and print the 'site-packages' path for the given
402 # implementation. If no implementation is provided, ${EPYTHON} will
405 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
406 # better to use python_export() directly instead.
407 python_get_sitedir() {
408 debug-print-function ${FUNCNAME} "${@}"
410 python_export "${@}" PYTHON_SITEDIR
411 echo "${PYTHON_SITEDIR}"
414 # @FUNCTION: python_get_includedir
417 # Obtain and print the include path for the given implementation. If no
418 # implementation is provided, ${EPYTHON} will be used.
420 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
421 # is better to use python_export() directly instead.
422 python_get_includedir() {
423 debug-print-function ${FUNCNAME} "${@}"
425 python_export "${@}" PYTHON_INCLUDEDIR
426 echo "${PYTHON_INCLUDEDIR}"
429 # @FUNCTION: python_get_library_path
432 # Obtain and print the Python library path for the given implementation.
433 # If no implementation is provided, ${EPYTHON} will be used.
435 # Please note that this function can be used with CPython only. Use
436 # in another implementation will result in a fatal failure.
437 python_get_library_path() {
438 debug-print-function ${FUNCNAME} "${@}"
440 python_export "${@}" PYTHON_LIBPATH
441 echo "${PYTHON_LIBPATH}"
444 # @FUNCTION: python_get_CFLAGS
447 # Obtain and print the compiler flags for building against Python,
448 # for the given implementation. If no implementation is provided,
449 # ${EPYTHON} will be used.
451 # Please note that this function can be used with CPython only.
452 # It requires Python and pkg-config installed, and therefore proper
453 # build-time dependencies need be added to the ebuild.
454 python_get_CFLAGS() {
455 debug-print-function ${FUNCNAME} "${@}"
457 python_export "${@}" PYTHON_CFLAGS
458 echo "${PYTHON_CFLAGS}"
461 # @FUNCTION: python_get_LIBS
464 # Obtain and print the compiler flags for linking against Python,
465 # for the given implementation. If no implementation is provided,
466 # ${EPYTHON} will be used.
468 # Please note that this function can be used with CPython only.
469 # It requires Python and pkg-config installed, and therefore proper
470 # build-time dependencies need be added to the ebuild.
472 debug-print-function ${FUNCNAME} "${@}"
474 python_export "${@}" PYTHON_LIBS
475 echo "${PYTHON_LIBS}"
478 # @FUNCTION: python_get_PYTHON_CONFIG
481 # Obtain and print the PYTHON_CONFIG location for the given
482 # implementation. If no implementation is provided, ${EPYTHON} will be
485 # Please note that this function can be used with CPython only.
486 # It requires Python installed, and therefore proper build-time
487 # dependencies need be added to the ebuild.
488 python_get_PYTHON_CONFIG() {
489 debug-print-function ${FUNCNAME} "${@}"
491 python_export "${@}" PYTHON_CONFIG
492 echo "${PYTHON_CONFIG}"
495 # @FUNCTION: python_get_scriptdir
498 # Obtain and print the script install path for the given
499 # implementation. If no implementation is provided, ${EPYTHON} will
501 python_get_scriptdir() {
502 debug-print-function ${FUNCNAME} "${@}"
504 python_export "${@}" PYTHON_SCRIPTDIR
505 echo "${PYTHON_SCRIPTDIR}"
508 # @FUNCTION: _python_ln_rel
509 # @USAGE: <from> <to>
512 # Create a relative symlink.
514 debug-print-function ${FUNCNAME} "${@}"
519 local tgpath=${target%/*}/
520 local sympath=${symname%/*}/
523 while [[ ${sympath} ]]; do
526 while [[ ! ${tgseg} && ${tgpath} ]]; do
528 tgpath=${tgpath#${tgseg}/}
531 while [[ ! ${symseg} && ${sympath} ]]; do
532 symseg=${sympath%%/*}
533 sympath=${sympath#${symseg}/}
536 if [[ ${tgseg} != ${symseg} ]]; then
537 rel_target=../${rel_target}${tgseg:+${tgseg}/}
540 rel_target+=${tgpath}${target##*/}
542 debug-print "${FUNCNAME}: ${symname} -> ${target}"
543 debug-print "${FUNCNAME}: rel_target = ${rel_target}"
545 ln -fs "${rel_target}" "${symname}"
548 # @FUNCTION: python_optimize
549 # @USAGE: [<directory>...]
551 # Compile and optimize Python modules in specified directories (absolute
552 # paths). If no directories are provided, the default system paths
553 # are used (prepended with ${D}).
555 debug-print-function ${FUNCNAME} "${@}"
557 if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
558 eerror "The new Python eclasses expect the compiled Python files to"
559 eerror "be controlled by the Package Manager. For this reason,"
560 eerror "the python_optimize function can be used only during src_* phases"
561 eerror "(src_install most commonly) and not during pkg_* phases."
563 die "python_optimize is not to be used in pre/post* phases"
566 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
568 local PYTHON=${PYTHON}
569 [[ ${PYTHON} ]] || python_export PYTHON
571 # default to sys.path
572 if [[ ${#} -eq 0 ]]; then
574 while IFS= read -r -d '' f; do
575 # 1) accept only absolute paths
576 # (i.e. skip '', '.' or anything like that)
577 # 2) skip paths which do not exist
578 # (python2.6 complains about them verbosely)
580 if [[ ${f} == /* && -d ${D}${f} ]]; then
581 set -- "${D}${f}" "${@}"
583 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))' || die)
585 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
590 # make sure to get a nice path without //
591 local instpath=${d#${D}}
592 instpath=/${instpath##/}
596 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
597 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
600 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
606 # @ECLASS-VARIABLE: python_scriptroot
609 # The current script destination for python_doscript(). The path
610 # is relative to the installation root (${ED}).
612 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
614 # Can be set indirectly through the python_scriptinto() function.
619 # local python_scriptroot=${GAMES_BINDIR}
620 # python_foreach_impl python_doscript foo
624 # @FUNCTION: python_scriptinto
627 # Set the current scriptroot. The new value will be stored
628 # in the 'python_scriptroot' environment variable. The new value need
629 # be relative to the installation root (${ED}).
631 # Alternatively, you can set the variable directly.
632 python_scriptinto() {
633 debug-print-function ${FUNCNAME} "${@}"
635 python_scriptroot=${1}
638 # @FUNCTION: python_doexe
641 # Install the given executables into current python_scriptroot,
642 # for the current Python implementation (${EPYTHON}).
644 # The executable will be wrapped properly for the Python implementation,
645 # though no shebang mangling will be performed.
647 debug-print-function ${FUNCNAME} "${@}"
651 python_newexe "${f}" "${f##*/}"
655 # @FUNCTION: python_newexe
656 # @USAGE: <path> <new-name>
658 # Install the given executable into current python_scriptroot,
659 # for the current Python implementation (${EPYTHON}).
661 # The executable will be wrapped properly for the Python implementation,
662 # though no shebang mangling will be performed. It will be renamed
665 debug-print-function ${FUNCNAME} "${@}"
667 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
668 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
669 if [[ ${EAPI:-0} == [0123] ]]; then
670 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
673 local wrapd=${python_scriptroot:-${DESTTREE}/bin}
678 local PYTHON_SCRIPTDIR d
679 python_export PYTHON_SCRIPTDIR
680 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
685 newexe "${f}" "${newfn}" || return ${?}
688 # install the wrapper
689 _python_ln_rel "${ED%/}"/usr/lib/python-exec/python-exec2 \
690 "${ED%/}/${wrapd}/${newfn}" || die
692 # don't use this at home, just call python_doscript() instead
693 if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
694 python_fix_shebang -q "${ED%/}/${d}/${newfn}"
698 # @FUNCTION: python_doscript
701 # Install the given scripts into current python_scriptroot,
702 # for the current Python implementation (${EPYTHON}).
704 # All specified files must start with a 'python' shebang. The shebang
705 # will be converted, and the files will be wrapped properly
706 # for the Python implementation.
711 # python_foreach_impl python_doscript ${PN}
715 debug-print-function ${FUNCNAME} "${@}"
717 local _PYTHON_REWRITE_SHEBANG=1
721 # @FUNCTION: python_newscript
722 # @USAGE: <path> <new-name>
724 # Install the given script into current python_scriptroot
725 # for the current Python implementation (${EPYTHON}), and name it
728 # The file must start with a 'python' shebang. The shebang will be
729 # converted, and the file will be wrapped properly for the Python
730 # implementation. It will be renamed to <new-name>.
735 # python_foreach_impl python_newscript foo.py foo
739 debug-print-function ${FUNCNAME} "${@}"
741 local _PYTHON_REWRITE_SHEBANG=1
745 # @ECLASS-VARIABLE: python_moduleroot
748 # The current module root for python_domodule(). The path can be either
749 # an absolute system path (it must start with a slash, and ${ED} will be
750 # prepended to it) or relative to the implementation's site-packages directory
751 # (then it must start with a non-slash character).
753 # When unset, the modules will be installed in the site-packages root.
755 # Can be set indirectly through the python_moduleinto() function.
760 # local python_moduleroot=bar
761 # # installs ${PYTHON_SITEDIR}/bar/baz.py
762 # python_foreach_impl python_domodule baz.py
766 # @FUNCTION: python_moduleinto
769 # Set the current module root. The new value will be stored
770 # in the 'python_moduleroot' environment variable. The new value need
771 # be relative to the site-packages root.
773 # Alternatively, you can set the variable directly.
774 python_moduleinto() {
775 debug-print-function ${FUNCNAME} "${@}"
777 python_moduleroot=${1}
780 # @FUNCTION: python_domodule
783 # Install the given modules (or packages) into the current
784 # python_moduleroot. The list can mention both modules (files)
785 # and packages (directories). All listed files will be installed
786 # for all enabled implementations, and compiled afterwards.
791 # # (${PN} being a directory)
792 # python_foreach_impl python_domodule ${PN}
796 debug-print-function ${FUNCNAME} "${@}"
798 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
799 if [[ ${EAPI:-0} == [0123] ]]; then
800 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
804 if [[ ${python_moduleroot} == /* ]]; then
806 d=${python_moduleroot}
808 # relative to site-packages
809 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
810 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
812 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
817 doins -r "${@}" || return ${?}
820 python_optimize "${ED}/${d}"
823 # @FUNCTION: python_doheader
826 # Install the given headers into the implementation-specific include
827 # directory. This function is unconditionally recursive, i.e. you can
828 # pass directories instead of files.
833 # python_foreach_impl python_doheader foo.h bar.h
837 debug-print-function ${FUNCNAME} "${@}"
839 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
840 if [[ ${EAPI:-0} == [0123] ]]; then
841 die "python_do* and python_new* helpers are banned in EAPIs older than 4."
844 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
845 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
847 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
851 doins -r "${@}" || return ${?}
855 # @FUNCTION: python_wrapper_setup
856 # @USAGE: [<path> [<impl>]]
858 # Create proper 'python' executable and pkg-config wrappers
859 # (if available) in the directory named by <path>. Set up PATH
860 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
862 # The wrappers will be created for implementation named by <impl>,
863 # or for one named by ${EPYTHON} if no <impl> passed.
865 # If the named directory contains a python symlink already, it will
866 # be assumed to contain proper wrappers already and only environment
867 # setup will be done. If wrapper update is requested, the directory
868 # shall be removed first.
869 python_wrapper_setup() {
870 debug-print-function ${FUNCNAME} "${@}"
872 local workdir=${1:-${T}/${EPYTHON}}
873 local impl=${2:-${EPYTHON}}
875 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
876 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
878 if [[ ! -x ${workdir}/bin/python ]]; then
879 _python_check_dead_variables
881 mkdir -p "${workdir}"/{bin,pkgconfig} || die
883 # Clean up, in case we were supposed to do a cheap update.
884 rm -f "${workdir}"/bin/python{,2,3,-config} || die
885 rm -f "${workdir}"/bin/2to3 || die
886 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc || die
888 local EPYTHON PYTHON PYTHON_CONFIG
889 python_export "${impl}" EPYTHON PYTHON
892 if python_is_python3; then
901 # note: we don't use symlinks because python likes to do some
902 # symlink reading magic that breaks stuff
903 # https://bugs.gentoo.org/show_bug.cgi?id=555752
904 cat > "${workdir}/bin/python" <<-_EOF_ || die
906 exec "${PYTHON}" "\${@}"
908 cp "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
909 chmod +x "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
911 local nonsupp=( "python${pyother}" "python${pyother}-config" )
914 if [[ ${EPYTHON} == python* ]]; then
915 python_export "${impl}" PYTHON_CONFIG
917 cat > "${workdir}/bin/python-config" <<-_EOF_ || die
919 exec "${PYTHON_CONFIG}" "\${@}"
921 cp "${workdir}/bin/python-config" \
922 "${workdir}/bin/python${pyver}-config" || die
923 chmod +x "${workdir}/bin/python-config" \
924 "${workdir}/bin/python${pyver}-config" || die
927 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
930 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
931 "${workdir}"/pkgconfig/python.pc || die
932 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
934 nonsupp+=( 2to3 python-config "python${pyver}-config" )
938 for x in "${nonsupp[@]}"; do
939 cat >"${workdir}"/bin/${x} <<-_EOF_ || die
941 echo "${x} is not supported by ${EPYTHON}" >&2
944 chmod +x "${workdir}"/bin/${x} || die
947 # Now, set the environment.
948 # But note that ${workdir} may be shared with something else,
949 # and thus already on top of PATH.
950 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
951 PATH=${workdir}/bin${PATH:+:${PATH}}
953 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
954 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
956 export PATH PKG_CONFIG_PATH
960 # @FUNCTION: python_is_python3
963 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
964 # (i.e. uses syntax and stdlib of Python 3.*).
966 # Returns 0 (true) if it is, 1 (false) otherwise.
967 python_is_python3() {
968 local impl=${1:-${EPYTHON}}
969 [[ ${impl} ]] || die "python_is_python3: no impl nor EPYTHON"
971 [[ ${impl} == python3* || ${impl} == pypy3 ]]
974 # @FUNCTION: python_is_installed
977 # Check whether the interpreter for <impl> (or ${EPYTHON}) is installed.
978 # Uses has_version with a proper dependency string.
980 # Returns 0 (true) if it is, 1 (false) otherwise.
981 python_is_installed() {
982 local impl=${1:-${EPYTHON}}
983 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
990 if [[ ${PYTHON_REQ_USE} ]]; then
991 append=[${PYTHON_REQ_USE}]
994 # be happy with just the interpeter, no need for the virtual
995 has_version "dev-python/${impl}${append}" \
996 || has_version "dev-python/${impl}-bin${append}"
1000 python_export "${impl}" PYTHON_PKG_DEP
1001 has_version "${PYTHON_PKG_DEP}"
1006 # @FUNCTION: python_fix_shebang
1007 # @USAGE: [-f|--force] [-q|--quiet] <path>...
1009 # Replace the shebang in Python scripts with the current Python
1010 # implementation (EPYTHON). If a directory is passed, works recursively
1011 # on all Python scripts.
1013 # Only files having a 'python*' shebang will be modified. Files with
1014 # other shebang will either be skipped when working recursively
1015 # on a directory or treated as error when specified explicitly.
1017 # Shebangs matching explicitly current Python version will be left
1018 # unmodified. Shebangs requesting another Python version will be treated
1019 # as fatal error, unless --force is given.
1021 # --force causes the function to replace even shebangs that require
1022 # incompatible Python version. --quiet causes the function not to list
1023 # modified files verbosely.
1024 python_fix_shebang() {
1025 debug-print-function ${FUNCNAME} "${@}"
1027 [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
1030 while [[ ${@} ]]; do
1032 -f|--force) force=1; shift;;
1033 -q|--quiet) quiet=1; shift;;
1039 [[ ${1} ]] || die "${FUNCNAME}: no paths given"
1043 local any_correct any_fixed is_recursive
1045 [[ -d ${path} ]] && is_recursive=1
1047 while IFS= read -r -d '' f; do
1051 # note: we can't ||die here since read will fail if file
1052 # has no newline characters
1053 IFS= read -r shebang <"${f}"
1055 # First, check if it's shebang at all...
1056 if [[ ${shebang} == '#!'* ]]; then
1057 local split_shebang=()
1058 read -r -a split_shebang <<<${shebang} || die
1060 # Match left-to-right in a loop, to avoid matching random
1061 # repetitions like 'python2.7 python2'.
1062 for i in "${split_shebang[@]}"; do
1065 debug-print "${FUNCNAME}: in file ${f#${D}}"
1066 debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
1068 # Nothing to do, move along.
1073 *python|*python[23])
1074 debug-print "${FUNCNAME}: in file ${f#${D}}"
1075 debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
1077 if [[ ${i} == *python2 ]]; then
1079 if [[ ! ${force} ]]; then
1080 python_is_python3 "${EPYTHON}" && error=1
1082 elif [[ ${i} == *python3 ]]; then
1084 if [[ ! ${force} ]]; then
1085 python_is_python3 "${EPYTHON}" || error=1
1092 *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789])
1093 # Explicit mismatch.
1094 if [[ ! ${force} ]]; then
1098 *python[23].[0123456789])
1099 from="python[23].[0123456789]";;
1104 *jython[23].[0123456789])
1105 from="jython[23].[0123456789]";;
1107 die "${FUNCNAME}: internal error in 2nd pattern match";;
1116 if [[ ! ${error} && ! ${from} ]]; then
1117 # Non-Python shebang. Allowed in recursive mode,
1118 # disallowed when specifying file explicitly.
1119 [[ ${is_recursive} ]] && continue
1123 if [[ ! ${quiet} ]]; then
1124 einfo "Fixing shebang in ${f#${D}}."
1127 if [[ ! ${error} ]]; then
1128 # We either want to match ${from} followed by space
1129 # or at end-of-string.
1130 if [[ ${shebang} == *${from}" "* ]]; then
1131 sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die
1133 sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
1137 eerror "The file has incompatible shebang:"
1138 eerror " file: ${f#${D}}"
1139 eerror " current shebang: ${shebang}"
1140 eerror " requested impl: ${EPYTHON}"
1141 die "${FUNCNAME}: conversion of incompatible shebang requested"
1143 done < <(find -H "${path}" -type f -print0 || die)
1145 if [[ ! ${any_fixed} ]]; then
1147 [[ ${EAPI:-0} == [012345] ]] && cmd=eqawarn
1149 "${cmd}" "QA warning: ${FUNCNAME}, ${path#${D}} did not match any fixable files."
1150 if [[ ${any_correct} ]]; then
1151 "${cmd}" "All files have ${EPYTHON} shebang already."
1153 "${cmd}" "There are no Python files in specified directory."
1156 [[ ${cmd} == eerror ]] && die "${FUNCNAME} did not match any fixable files (QA warning fatal in EAPI ${EAPI})"
1161 # @FUNCTION: _python_check_locale_sanity
1163 # @RETURN: 0 if sane, 1 otherwise
1165 # Check whether the specified locale sanely maps between lowercase
1166 # and uppercase ASCII characters.
1167 _python_check_locale_sanity() {
1168 local -x LC_CTYPE=${1}
1173 local input="${lc[*]}${uc[*]}"
1175 local output=$(tr '[:lower:][:upper:]' '[:upper:][:lower:]' <<<"${input}")
1176 [[ ${output} == "${uc[*]}${lc[*]}" ]]
1179 # @FUNCTION: python_export_utf8_locale
1180 # @RETURN: 0 on success, 1 on failure.
1182 # Attempts to export a usable UTF-8 locale in the LC_CTYPE variable. Does
1183 # nothing if LC_ALL is defined, or if the current locale uses a UTF-8 charmap.
1184 # This may be used to work around the quirky open() behavior of python3.
1185 python_export_utf8_locale() {
1186 debug-print-function ${FUNCNAME} "${@}"
1188 # If the locale program isn't available, just return.
1189 type locale >/dev/null || return 0
1191 if [[ $(locale charmap) != UTF-8 ]]; then
1192 # Try English first, then everything else.
1193 local lang locales="en_US.UTF-8 $(locale -a)"
1195 for lang in ${locales}; do
1196 if [[ $(LC_ALL=${lang} locale charmap 2>/dev/null) == UTF-8 ]]; then
1197 if _python_check_locale_sanity "${lang}"; then
1198 export LC_CTYPE=${lang}
1199 if [[ -n ${LC_ALL} ]]; then
1200 export LC_NUMERIC=${LC_ALL}
1201 export LC_TIME=${LC_ALL}
1202 export LC_COLLATE=${LC_ALL}
1203 export LC_MONETARY=${LC_ALL}
1204 export LC_MESSAGES=${LC_ALL}
1205 export LC_PAPER=${LC_ALL}
1206 export LC_NAME=${LC_ALL}
1207 export LC_ADDRESS=${LC_ALL}
1208 export LC_TELEPHONE=${LC_ALL}
1209 export LC_MEASUREMENT=${LC_ALL}
1210 export LC_IDENTIFICATION=${LC_ALL}
1218 ewarn "Could not find a UTF-8 locale. This may trigger build failures in"
1219 ewarn "some python packages. Please ensure that a UTF-8 locale is listed in"
1220 ewarn "/etc/locale.gen and run locale-gen."
1227 # -- python.eclass functions --
1229 _python_check_dead_variables() {
1232 for v in PYTHON_DEPEND PYTHON_USE_WITH{,_OR,_OPT} {RESTRICT,SUPPORT}_PYTHON_ABIS
1234 if [[ ${!v} ]]; then
1235 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Ebuild_head"
1239 for v in PYTHON_{CPPFLAGS,CFLAGS,CXXFLAGS,LDFLAGS}
1241 if [[ ${!v} ]]; then
1242 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#PYTHON_CFLAGS"
1246 for v in PYTHON_TESTS_RESTRICTED_ABIS PYTHON_EXPORT_PHASE_FUNCTIONS \
1247 PYTHON_VERSIONED_{SCRIPTS,EXECUTABLES} PYTHON_NONVERSIONED_EXECUTABLES
1249 if [[ ${!v} ]]; then
1250 die "${v} is invalid for python-r1 suite"
1254 for v in DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES DISTUTILS_SETUP_FILES \
1255 DISTUTILS_GLOBAL_OPTIONS DISTUTILS_SRC_TEST PYTHON_MODNAME
1257 if [[ ${!v} ]]; then
1258 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#${v}"
1262 if [[ ${DISTUTILS_DISABLE_TEST_DEPENDENCY} ]]; then
1263 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#DISTUTILS_SRC_TEST"
1266 # python.eclass::progress
1267 for v in PYTHON_BDEPEND PYTHON_MULTIPLE_ABIS PYTHON_ABI_TYPE \
1268 PYTHON_RESTRICTED_ABIS PYTHON_TESTS_FAILURES_TOLERANT_ABIS \
1269 PYTHON_CFFI_MODULES_GENERATION_COMMANDS
1271 if [[ ${!v} ]]; then
1272 die "${v} is invalid for python-r1 suite"
1277 python_pkg_setup() {
1278 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1281 python_convert_shebangs() {
1282 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"
1285 python_clean_py-compile_files() {
1286 die "${FUNCNAME}() is invalid for python-r1 suite"
1289 python_clean_installation_image() {
1290 die "${FUNCNAME}() is invalid for python-r1 suite"
1293 python_execute_function() {
1294 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"
1297 python_generate_wrapper_scripts() {
1298 die "${FUNCNAME}() is invalid for python-r1 suite"
1301 python_merge_intermediate_installation_images() {
1302 die "${FUNCNAME}() is invalid for python-r1 suite"
1305 python_set_active_version() {
1306 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1309 python_need_rebuild() {
1310 die "${FUNCNAME}() is invalid for python-r1 suite"
1314 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"
1317 python_get_implementation() {
1318 die "${FUNCNAME}() is invalid for python-r1 suite"
1321 python_get_implementational_package() {
1322 die "${FUNCNAME}() is invalid for python-r1 suite"
1325 python_get_libdir() {
1326 die "${FUNCNAME}() is invalid for python-r1 suite"
1329 python_get_library() {
1330 die "${FUNCNAME}() is invalid for python-r1 suite"
1333 python_get_version() {
1334 die "${FUNCNAME}() is invalid for python-r1 suite"
1337 python_get_implementation_and_version() {
1338 die "${FUNCNAME}() is invalid for python-r1 suite"
1341 python_execute_nosetests() {
1342 die "${FUNCNAME}() is invalid for python-r1 suite"
1345 python_execute_py.test() {
1346 die "${FUNCNAME}() is invalid for python-r1 suite"
1349 python_execute_trial() {
1350 die "${FUNCNAME}() is invalid for python-r1 suite"
1353 python_enable_pyc() {
1354 die "${FUNCNAME}() is invalid for python-r1 suite"
1357 python_disable_pyc() {
1358 die "${FUNCNAME}() is invalid for python-r1 suite"
1361 python_mod_optimize() {
1362 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"
1365 python_mod_cleanup() {
1366 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"
1369 # python.eclass::progress
1371 python_abi_depend() {
1372 die "${FUNCNAME}() is invalid for python-r1 suite"
1375 python_install_executables() {
1376 die "${FUNCNAME}() is invalid for python-r1 suite"
1379 python_get_extension_module_suffix() {
1380 die "${FUNCNAME}() is invalid for python-r1 suite"
1383 python_byte-compile_modules() {
1384 die "${FUNCNAME}() is invalid for python-r1 suite"
1387 python_clean_byte-compiled_modules() {
1388 die "${FUNCNAME}() is invalid for python-r1 suite"
1391 python_generate_cffi_modules() {
1392 die "${FUNCNAME}() is invalid for python-r1 suite"