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 inherit eutils multilib toolchain-funcs
38 # @ECLASS-VARIABLE: _PYTHON_ALL_IMPLS
41 # All supported Python implementations, most preferred last.
45 python3_3 python3_4 python3_5
49 # @FUNCTION: _python_impl_supported
53 # Check whether the implementation <impl> (PYTHON_COMPAT-form)
56 # Returns 0 if the implementation is valid and supported. If it is
57 # unsupported, returns 1 -- and the caller should ignore the entry.
58 # If it is invalid, dies with an appopriate error messages.
59 _python_impl_supported() {
60 debug-print-function ${FUNCNAME} "${@}"
62 [[ ${#} -eq 1 ]] || die "${FUNCNAME}: takes exactly 1 argument (impl)."
66 # keep in sync with _PYTHON_ALL_IMPLS!
67 # (not using that list because inline patterns shall be faster)
69 python2_7|python3_[345]|jython2_[57])
72 pypy1_[89]|pypy2_0|python2_[56]|python3_[12])
76 if [[ ${EAPI:-0} == [01234] ]]; then
77 die "PyPy is supported in EAPI 5 and newer only."
81 die "Invalid implementation in PYTHON_COMPAT: ${impl}"
85 # @ECLASS-VARIABLE: PYTHON
88 # The absolute path to the current Python interpreter.
90 # This variable is set automatically in the following contexts:
92 # python-r1: Set in functions called by python_foreach_impl() or after
93 # calling python_export_best().
95 # python-single-r1: Set after calling python-single-r1_pkg_setup().
97 # distutils-r1: Set within any of the python sub-phase functions.
104 # @ECLASS-VARIABLE: EPYTHON
107 # The executable name of the current Python interpreter.
109 # This variable is set automatically in the following contexts:
111 # python-r1: Set in functions called by python_foreach_impl() or after
112 # calling python_export_best().
114 # python-single-r1: Set after calling python-single-r1_pkg_setup().
116 # distutils-r1: Set within any of the python sub-phase functions.
123 # @ECLASS-VARIABLE: PYTHON_SITEDIR
126 # The path to Python site-packages directory.
128 # Set and exported on request using python_export().
132 # /usr/lib64/python2.7/site-packages
135 # @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
138 # The path to Python include directory.
140 # Set and exported on request using python_export().
144 # /usr/include/python2.7
147 # @ECLASS-VARIABLE: PYTHON_LIBPATH
150 # The path to Python library.
152 # Set and exported on request using python_export().
153 # Valid only for CPython.
157 # /usr/lib64/libpython2.7.so
160 # @ECLASS-VARIABLE: PYTHON_CFLAGS
163 # Proper C compiler flags for building against Python. Obtained from
164 # pkg-config or python-config.
166 # Set and exported on request using python_export().
167 # Valid only for CPython. Requires a proper build-time dependency
168 # on the Python implementation and on pkg-config.
172 # -I/usr/include/python2.7
175 # @ECLASS-VARIABLE: PYTHON_LIBS
178 # Proper C compiler flags for linking against Python. Obtained from
179 # pkg-config or python-config.
181 # Set and exported on request using python_export().
182 # Valid only for CPython. Requires a proper build-time dependency
183 # on the Python implementation and on pkg-config.
190 # @ECLASS-VARIABLE: PYTHON_PKG_DEP
193 # The complete dependency on a particular Python package as a string.
195 # Set and exported on request using python_export().
199 # dev-lang/python:2.7[xml]
202 # @ECLASS-VARIABLE: PYTHON_SCRIPTDIR
205 # The location where Python scripts must be installed for current impl.
207 # Set and exported on request using python_export().
211 # /usr/lib/python-exec/python2.7
214 # @FUNCTION: python_export
215 # @USAGE: [<impl>] <variables>...
217 # Set and export the Python implementation-relevant variables passed
220 # The optional first parameter may specify the requested Python
221 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
222 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
223 # the current one will be obtained from ${EPYTHON}.
225 # The variables which can be exported are: PYTHON, EPYTHON,
226 # PYTHON_SITEDIR. They are described more completely in the eclass
227 # variable documentation.
229 debug-print-function ${FUNCNAME} "${@}"
244 if [[ -z ${impl} ]]; then
245 die "python_export called without a python implementation and EPYTHON is unset"
249 debug-print "${FUNCNAME}: implementation: ${impl}"
254 export EPYTHON=${impl}
255 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
258 export PYTHON=${EPREFIX}/usr/bin/${impl}
259 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
265 dir=/usr/$(get_libdir)/${impl}
268 dir=/usr/share/${impl/n/n-}/Lib
272 export PYTHON_SITEDIR=${EPREFIX}${dir}/site-packages
273 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
279 dir=/usr/include/${impl}
282 dir=/usr/$(get_libdir)/${impl}/include
285 die "${impl} lacks header files"
289 export PYTHON_INCLUDEDIR=${EPREFIX}${dir}
290 debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
299 die "${impl} lacks a dynamic library"
303 local path=${EPREFIX}/usr/$(get_libdir)
305 export PYTHON_LIBPATH=${path}/${libname}$(get_libname)
306 debug-print "${FUNCNAME}: PYTHON_LIBPATH = ${PYTHON_LIBPATH}"
313 # python-2.7, python-3.2, etc.
314 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-})
317 die "${impl}: obtaining ${var} not supported"
321 export PYTHON_CFLAGS=${val}
322 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
329 # python-2.7, python-3.2, etc.
330 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-})
333 die "${impl}: obtaining ${var} not supported"
337 export PYTHON_LIBS=${val}
338 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
344 PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
346 PYTHON_PKG_DEP='>=dev-lang/python-3.3.2-r2:3.3';;
348 PYTHON_PKG_DEP="dev-lang/python:${impl#python}";;
350 PYTHON_PKG_DEP='virtual/pypy:0=';;
352 PYTHON_PKG_DEP='virtual/pypy3:0=';;
354 PYTHON_PKG_DEP='>=dev-java/jython-2.5.3-r2:2.5';;
356 PYTHON_PKG_DEP='dev-java/jython:2.7';;
358 die "Invalid implementation: ${impl}"
362 if [[ ${PYTHON_REQ_USE} ]]; then
363 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
366 export PYTHON_PKG_DEP
367 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
371 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
372 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
375 die "python_export: unknown variable ${var}"
380 # @FUNCTION: python_get_sitedir
383 # Obtain and print the 'site-packages' path for the given
384 # implementation. If no implementation is provided, ${EPYTHON} will
387 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
388 # better to use python_export() directly instead.
389 python_get_sitedir() {
390 debug-print-function ${FUNCNAME} "${@}"
392 python_export "${@}" PYTHON_SITEDIR
393 echo "${PYTHON_SITEDIR}"
396 # @FUNCTION: python_get_includedir
399 # Obtain and print the include path for the given implementation. If no
400 # implementation is provided, ${EPYTHON} will be used.
402 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
403 # is better to use python_export() directly instead.
404 python_get_includedir() {
405 debug-print-function ${FUNCNAME} "${@}"
407 python_export "${@}" PYTHON_INCLUDEDIR
408 echo "${PYTHON_INCLUDEDIR}"
411 # @FUNCTION: python_get_library_path
414 # Obtain and print the Python library path for the given implementation.
415 # If no implementation is provided, ${EPYTHON} will be used.
417 # Please note that this function can be used with CPython only. Use
418 # in another implementation will result in a fatal failure.
419 python_get_library_path() {
420 debug-print-function ${FUNCNAME} "${@}"
422 python_export "${@}" PYTHON_LIBPATH
423 echo "${PYTHON_LIBPATH}"
426 # @FUNCTION: python_get_CFLAGS
429 # Obtain and print the compiler flags for building against Python,
430 # for the given implementation. If no implementation is provided,
431 # ${EPYTHON} will be used.
433 # Please note that this function can be used with CPython only.
434 # It requires Python and pkg-config installed, and therefore proper
435 # build-time dependencies need be added to the ebuild.
436 python_get_CFLAGS() {
437 debug-print-function ${FUNCNAME} "${@}"
439 python_export "${@}" PYTHON_CFLAGS
440 echo "${PYTHON_CFLAGS}"
443 # @FUNCTION: python_get_LIBS
446 # Obtain and print the compiler flags for linking against Python,
447 # for the given implementation. If no implementation is provided,
448 # ${EPYTHON} will be used.
450 # Please note that this function can be used with CPython only.
451 # It requires Python and pkg-config installed, and therefore proper
452 # build-time dependencies need be added to the ebuild.
454 debug-print-function ${FUNCNAME} "${@}"
456 python_export "${@}" PYTHON_LIBS
457 echo "${PYTHON_LIBS}"
460 # @FUNCTION: python_get_scriptdir
463 # Obtain and print the script install path for the given
464 # implementation. If no implementation is provided, ${EPYTHON} will
466 python_get_scriptdir() {
467 debug-print-function ${FUNCNAME} "${@}"
469 python_export "${@}" PYTHON_SCRIPTDIR
470 echo "${PYTHON_SCRIPTDIR}"
473 # @FUNCTION: _python_ln_rel
474 # @USAGE: <from> <to>
477 # Create a relative symlink.
479 debug-print-function ${FUNCNAME} "${@}"
484 local tgpath=${target%/*}/
485 local sympath=${symname%/*}/
488 while [[ ${sympath} ]]; do
491 while [[ ! ${tgseg} && ${tgpath} ]]; do
493 tgpath=${tgpath#${tgseg}/}
496 while [[ ! ${symseg} && ${sympath} ]]; do
497 symseg=${sympath%%/*}
498 sympath=${sympath#${symseg}/}
501 if [[ ${tgseg} != ${symseg} ]]; then
502 rel_target=../${rel_target}${tgseg:+${tgseg}/}
505 rel_target+=${tgpath}${target##*/}
507 debug-print "${FUNCNAME}: ${symname} -> ${target}"
508 debug-print "${FUNCNAME}: rel_target = ${rel_target}"
510 ln -fs "${rel_target}" "${symname}"
513 # @FUNCTION: python_optimize
514 # @USAGE: [<directory>...]
516 # Compile and optimize Python modules in specified directories (absolute
517 # paths). If no directories are provided, the default system paths
518 # are used (prepended with ${D}).
520 debug-print-function ${FUNCNAME} "${@}"
522 if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
523 eerror "The new Python eclasses expect the compiled Python files to"
524 eerror "be controlled by the Package Manager. For this reason,"
525 eerror "the python_optimize function can be used only during src_* phases"
526 eerror "(src_install most commonly) and not during pkg_* phases."
528 die "python_optimize is not to be used in pre/post* phases"
531 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
533 local PYTHON=${PYTHON}
534 [[ ${PYTHON} ]] || python_export PYTHON
536 # Note: python2.6 can't handle passing files to compileall...
537 # TODO: we do not support 2.6 any longer
539 # default to sys.path
540 if [[ ${#} -eq 0 ]]; then
542 while IFS= read -r -d '' f; do
543 # 1) accept only absolute paths
544 # (i.e. skip '', '.' or anything like that)
545 # 2) skip paths which do not exist
546 # (python2.6 complains about them verbosely)
548 if [[ ${f} == /* && -d ${D}${f} ]]; then
549 set -- "${D}${f}" "${@}"
551 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))')
553 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
558 # make sure to get a nice path without //
559 local instpath=${d#${D}}
560 instpath=/${instpath##/}
564 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
565 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
568 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
574 # @ECLASS-VARIABLE: python_scriptroot
577 # The current script destination for python_doscript(). The path
578 # is relative to the installation root (${ED}).
580 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
582 # Can be set indirectly through the python_scriptinto() function.
587 # local python_scriptroot=${GAMES_BINDIR}
588 # python_foreach_impl python_doscript foo
592 # @FUNCTION: python_scriptinto
595 # Set the current scriptroot. The new value will be stored
596 # in the 'python_scriptroot' environment variable. The new value need
597 # be relative to the installation root (${ED}).
599 # Alternatively, you can set the variable directly.
600 python_scriptinto() {
601 debug-print-function ${FUNCNAME} "${@}"
603 python_scriptroot=${1}
606 # @FUNCTION: python_doexe
609 # Install the given executables into current python_scriptroot,
610 # for the current Python implementation (${EPYTHON}).
612 # The executable will be wrapped properly for the Python implementation,
613 # though no shebang mangling will be performed.
615 debug-print-function ${FUNCNAME} "${@}"
619 python_newexe "${f}" "${f##*/}"
623 # @FUNCTION: python_newexe
624 # @USAGE: <path> <new-name>
626 # Install the given executable into current python_scriptroot,
627 # for the current Python implementation (${EPYTHON}).
629 # The executable will be wrapped properly for the Python implementation,
630 # though no shebang mangling will be performed. It will be renamed
633 debug-print-function ${FUNCNAME} "${@}"
635 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
636 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
638 local wrapd=${python_scriptroot:-${DESTTREE}/bin}
643 local PYTHON_SCRIPTDIR d
644 python_export PYTHON_SCRIPTDIR
645 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
650 newexe "${f}" "${newfn}" || die
653 # install the wrapper
654 _python_ln_rel "${ED%/}"/usr/lib/python-exec/python-exec2 \
655 "${ED%/}/${wrapd}/${newfn}" || die
657 # don't use this at home, just call python_doscript() instead
658 if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
659 python_fix_shebang -q "${ED%/}/${d}/${newfn}"
663 # @FUNCTION: python_doscript
666 # Install the given scripts into current python_scriptroot,
667 # for the current Python implementation (${EPYTHON}).
669 # All specified files must start with a 'python' shebang. The shebang
670 # will be converted, and the files will be wrapped properly
671 # for the Python implementation.
676 # python_foreach_impl python_doscript ${PN}
680 debug-print-function ${FUNCNAME} "${@}"
682 local _PYTHON_REWRITE_SHEBANG=1
686 # @FUNCTION: python_newscript
687 # @USAGE: <path> <new-name>
689 # Install the given script into current python_scriptroot
690 # for the current Python implementation (${EPYTHON}), and name it
693 # The file must start with a 'python' shebang. The shebang will be
694 # converted, and the file will be wrapped properly for the Python
695 # implementation. It will be renamed to <new-name>.
700 # python_foreach_impl python_newscript foo.py foo
704 debug-print-function ${FUNCNAME} "${@}"
706 local _PYTHON_REWRITE_SHEBANG=1
710 # @ECLASS-VARIABLE: python_moduleroot
713 # The current module root for python_domodule(). The path can be either
714 # an absolute system path (it must start with a slash, and ${ED} will be
715 # prepended to it) or relative to the implementation's site-packages directory
716 # (then it must start with a non-slash character).
718 # When unset, the modules will be installed in the site-packages root.
720 # Can be set indirectly through the python_moduleinto() function.
725 # local python_moduleroot=bar
726 # # installs ${PYTHON_SITEDIR}/bar/baz.py
727 # python_foreach_impl python_domodule baz.py
731 # @FUNCTION: python_moduleinto
734 # Set the current module root. The new value will be stored
735 # in the 'python_moduleroot' environment variable. The new value need
736 # be relative to the site-packages root.
738 # Alternatively, you can set the variable directly.
739 python_moduleinto() {
740 debug-print-function ${FUNCNAME} "${@}"
742 python_moduleroot=${1}
745 # @FUNCTION: python_domodule
748 # Install the given modules (or packages) into the current
749 # python_moduleroot. The list can mention both modules (files)
750 # and packages (directories). All listed files will be installed
751 # for all enabled implementations, and compiled afterwards.
756 # # (${PN} being a directory)
757 # python_foreach_impl python_domodule ${PN}
761 debug-print-function ${FUNCNAME} "${@}"
763 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
766 if [[ ${python_moduleroot} == /* ]]; then
768 d=${python_moduleroot}
770 # relative to site-packages
771 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
772 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
774 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
780 doins -r "${@}" || die
782 python_optimize "${ED}/${d}"
785 # @FUNCTION: python_doheader
788 # Install the given headers into the implementation-specific include
789 # directory. This function is unconditionally recursive, i.e. you can
790 # pass directories instead of files.
795 # python_foreach_impl python_doheader foo.h bar.h
799 debug-print-function ${FUNCNAME} "${@}"
801 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
803 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
804 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
806 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
811 doins -r "${@}" || die
814 # @FUNCTION: python_wrapper_setup
815 # @USAGE: [<path> [<impl>]]
817 # Create proper 'python' executable and pkg-config wrappers
818 # (if available) in the directory named by <path>. Set up PATH
819 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
821 # The wrappers will be created for implementation named by <impl>,
822 # or for one named by ${EPYTHON} if no <impl> passed.
824 # If the named directory contains a python symlink already, it will
825 # be assumed to contain proper wrappers already and only environment
826 # setup will be done. If wrapper update is requested, the directory
827 # shall be removed first.
828 python_wrapper_setup() {
829 debug-print-function ${FUNCNAME} "${@}"
831 local workdir=${1:-${T}/${EPYTHON}}
832 local impl=${2:-${EPYTHON}}
834 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
835 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
837 if [[ ! -x ${workdir}/bin/python ]]; then
838 _python_check_dead_variables
840 mkdir -p "${workdir}"/{bin,pkgconfig} || die
842 # Clean up, in case we were supposed to do a cheap update.
843 rm -f "${workdir}"/bin/python{,2,3,-config} || die
844 rm -f "${workdir}"/bin/2to3 || die
845 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc || die
848 python_export "${impl}" EPYTHON PYTHON
851 if python_is_python3; then
860 # note: we don't use symlinks because python likes to do some
861 # symlink reading magic that breaks stuff
862 # https://bugs.gentoo.org/show_bug.cgi?id=555752
863 cat > "${workdir}/bin/python" <<-_EOF_
865 exec "${PYTHON}" "\${@}"
867 cp "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
868 chmod +x "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
870 local nonsupp=( "python${pyother}" "python${pyother}-config" )
873 if [[ ${EPYTHON} == python* ]]; then
874 cat > "${workdir}/bin/python-config" <<-_EOF_
876 exec "${PYTHON}-config" "\${@}"
878 cp "${workdir}/bin/python-config" \
879 "${workdir}/bin/python${pyver}-config" || die
880 chmod +x "${workdir}/bin/python-config" \
881 "${workdir}/bin/python${pyver}-config" || die
884 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
887 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
888 "${workdir}"/pkgconfig/python.pc || die
889 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
891 nonsupp+=( 2to3 python-config "python${pyver}-config" )
895 for x in "${nonsupp[@]}"; do
896 cat >"${workdir}"/bin/${x} <<__EOF__
898 echo "${x} is not supported by ${EPYTHON}" >&2
901 chmod +x "${workdir}"/bin/${x} || die
904 # Now, set the environment.
905 # But note that ${workdir} may be shared with something else,
906 # and thus already on top of PATH.
907 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
908 PATH=${workdir}/bin${PATH:+:${PATH}}
910 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
911 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
913 export PATH PKG_CONFIG_PATH
917 # @FUNCTION: python_is_python3
920 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
921 # (i.e. uses syntax and stdlib of Python 3.*).
923 # Returns 0 (true) if it is, 1 (false) otherwise.
924 python_is_python3() {
925 local impl=${1:-${EPYTHON}}
926 [[ ${impl} ]] || die "python_is_python3: no impl nor EPYTHON"
928 [[ ${impl} == python3* || ${impl} == pypy3 ]]
931 # @FUNCTION: python_is_installed
934 # Check whether the interpreter for <impl> (or ${EPYTHON}) is installed.
935 # Uses has_version with a proper dependency string.
937 # Returns 0 (true) if it is, 1 (false) otherwise.
938 python_is_installed() {
939 local impl=${1:-${EPYTHON}}
940 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
947 if [[ ${PYTHON_REQ_USE} ]]; then
948 append=[${PYTHON_REQ_USE}]
951 # be happy with just the interpeter, no need for the virtual
952 has_version "dev-python/${impl}${append}" \
953 || has_version "dev-python/${impl}-bin${append}"
957 python_export "${impl}" PYTHON_PKG_DEP
958 has_version "${PYTHON_PKG_DEP}"
963 # @FUNCTION: python_fix_shebang
964 # @USAGE: [-f|--force] [-q|--quiet] <path>...
966 # Replace the shebang in Python scripts with the current Python
967 # implementation (EPYTHON). If a directory is passed, works recursively
968 # on all Python scripts.
970 # Only files having a 'python*' shebang will be modified. Files with
971 # other shebang will either be skipped when working recursively
972 # on a directory or treated as error when specified explicitly.
974 # Shebangs matching explicitly current Python version will be left
975 # unmodified. Shebangs requesting another Python version will be treated
976 # as fatal error, unless --force is given.
978 # --force causes the function to replace even shebangs that require
979 # incompatible Python version. --quiet causes the function not to list
980 # modified files verbosely.
981 python_fix_shebang() {
982 debug-print-function ${FUNCNAME} "${@}"
984 [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
989 -f|--force) force=1; shift;;
990 -q|--quiet) quiet=1; shift;;
996 [[ ${1} ]] || die "${FUNCNAME}: no paths given"
1000 local any_correct any_fixed is_recursive
1002 [[ -d ${path} ]] && is_recursive=1
1004 while IFS= read -r -d '' f; do
1008 IFS= read -r shebang <"${f}"
1010 # First, check if it's shebang at all...
1011 if [[ ${shebang} == '#!'* ]]; then
1012 local split_shebang=()
1013 read -r -a split_shebang <<<${shebang}
1015 # Match left-to-right in a loop, to avoid matching random
1016 # repetitions like 'python2.7 python2'.
1017 for i in "${split_shebang[@]}"; do
1020 debug-print "${FUNCNAME}: in file ${f#${D}}"
1021 debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
1023 # Nothing to do, move along.
1028 *python|*python[23])
1029 debug-print "${FUNCNAME}: in file ${f#${D}}"
1030 debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
1032 if [[ ${i} == *python2 ]]; then
1034 if [[ ! ${force} ]]; then
1035 python_is_python3 "${EPYTHON}" && error=1
1037 elif [[ ${i} == *python3 ]]; then
1039 if [[ ! ${force} ]]; then
1040 python_is_python3 "${EPYTHON}" || error=1
1047 *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789])
1048 # Explicit mismatch.
1049 if [[ ! ${force} ]]; then
1053 *python[23].[0123456789])
1054 from="python[23].[0123456789]";;
1059 *jython[23].[0123456789])
1060 from="jython[23].[0123456789]";;
1062 die "${FUNCNAME}: internal error in 2nd pattern match";;
1071 if [[ ! ${error} && ! ${from} ]]; then
1072 # Non-Python shebang. Allowed in recursive mode,
1073 # disallowed when specifying file explicitly.
1074 [[ ${is_recursive} ]] && continue
1078 if [[ ! ${quiet} ]]; then
1079 einfo "Fixing shebang in ${f#${D}}."
1082 if [[ ! ${error} ]]; then
1083 # We either want to match ${from} followed by space
1084 # or at end-of-string.
1085 if [[ ${shebang} == *${from}" "* ]]; then
1086 sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die
1088 sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
1092 eerror "The file has incompatible shebang:"
1093 eerror " file: ${f#${D}}"
1094 eerror " current shebang: ${shebang}"
1095 eerror " requested impl: ${EPYTHON}"
1096 die "${FUNCNAME}: conversion of incompatible shebang requested"
1098 done < <(find "${path}" -type f -print0)
1100 if [[ ! ${any_fixed} ]]; then
1101 eqawarn "QA warning: ${FUNCNAME}, ${path#${D}} did not match any fixable files."
1102 if [[ ${any_correct} ]]; then
1103 eqawarn "All files have ${EPYTHON} shebang already."
1105 eqawarn "There are no Python files in specified directory."
1111 # @FUNCTION: python_export_utf8_locale
1112 # @RETURN: 0 on success, 1 on failure.
1114 # Attempts to export a usable UTF-8 locale in the LC_CTYPE variable. Does
1115 # nothing if LC_ALL is defined, or if the current locale uses a UTF-8 charmap.
1116 # This may be used to work around the quirky open() behavior of python3.
1117 python_export_utf8_locale() {
1118 debug-print-function ${FUNCNAME} "${@}"
1120 # If the locale program isn't available, just return.
1121 type locale >/dev/null || return 0
1123 if [[ $(locale charmap) != UTF-8 ]]; then
1124 if [[ -n ${LC_ALL} ]]; then
1125 ewarn "LC_ALL is set to a locale with a charmap other than UTF-8."
1126 ewarn "This may trigger build failures in some python packages."
1130 # Try English first, then everything else.
1131 local lang locales="en_US.UTF-8 $(locale -a)"
1133 for lang in ${locales}; do
1134 if [[ $(LC_CTYPE=${lang} locale charmap 2>/dev/null) == UTF-8 ]]; then
1135 export LC_CTYPE=${lang}
1140 ewarn "Could not find a UTF-8 locale. This may trigger build failures in"
1141 ewarn "some python packages. Please ensure that a UTF-8 locale is listed in"
1142 ewarn "/etc/locale.gen and run locale-gen."
1149 # -- python.eclass functions --
1151 _python_check_dead_variables() {
1154 for v in PYTHON_DEPEND PYTHON_USE_WITH{,_OR,_OPT} {RESTRICT,SUPPORT}_PYTHON_ABIS
1156 if [[ ${!v} ]]; then
1157 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Ebuild_head"
1161 for v in PYTHON_{CPPFLAGS,CFLAGS,CXXFLAGS,LDFLAGS}
1163 if [[ ${!v} ]]; then
1164 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#PYTHON_CFLAGS"
1168 for v in PYTHON_TESTS_RESTRICTED_ABIS PYTHON_EXPORT_PHASE_FUNCTIONS \
1169 PYTHON_VERSIONED_{SCRIPTS,EXECUTABLES} PYTHON_NONVERSIONED_EXECUTABLES
1171 if [[ ${!v} ]]; then
1172 die "${v} is invalid for python-r1 suite"
1176 for v in DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES DISTUTILS_SETUP_FILES \
1177 DISTUTILS_GLOBAL_OPTIONS DISTUTILS_SRC_TEST PYTHON_MODNAME
1179 if [[ ${!v} ]]; then
1180 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#${v}"
1184 if [[ ${DISTUTILS_DISABLE_TEST_DEPENDENCY} ]]; then
1185 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#DISTUTILS_SRC_TEST"
1188 # python.eclass::progress
1189 for v in PYTHON_BDEPEND PYTHON_MULTIPLE_ABIS PYTHON_ABI_TYPE \
1190 PYTHON_RESTRICTED_ABIS PYTHON_TESTS_FAILURES_TOLERANT_ABIS \
1191 PYTHON_CFFI_MODULES_GENERATION_COMMANDS
1193 if [[ ${!v} ]]; then
1194 die "${v} is invalid for python-r1 suite"
1199 python_pkg_setup() {
1200 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1203 python_convert_shebangs() {
1204 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"
1207 python_clean_py-compile_files() {
1208 die "${FUNCNAME}() is invalid for python-r1 suite"
1211 python_clean_installation_image() {
1212 die "${FUNCNAME}() is invalid for python-r1 suite"
1215 python_execute_function() {
1216 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"
1219 python_generate_wrapper_scripts() {
1220 die "${FUNCNAME}() is invalid for python-r1 suite"
1223 python_merge_intermediate_installation_images() {
1224 die "${FUNCNAME}() is invalid for python-r1 suite"
1227 python_set_active_version() {
1228 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1231 python_need_rebuild() {
1232 die "${FUNCNAME}() is invalid for python-r1 suite"
1236 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"
1239 python_get_implementation() {
1240 die "${FUNCNAME}() is invalid for python-r1 suite"
1243 python_get_implementational_package() {
1244 die "${FUNCNAME}() is invalid for python-r1 suite"
1247 python_get_libdir() {
1248 die "${FUNCNAME}() is invalid for python-r1 suite"
1251 python_get_library() {
1252 die "${FUNCNAME}() is invalid for python-r1 suite"
1255 python_get_version() {
1256 die "${FUNCNAME}() is invalid for python-r1 suite"
1259 python_get_implementation_and_version() {
1260 die "${FUNCNAME}() is invalid for python-r1 suite"
1263 python_execute_nosetests() {
1264 die "${FUNCNAME}() is invalid for python-r1 suite"
1267 python_execute_py.test() {
1268 die "${FUNCNAME}() is invalid for python-r1 suite"
1271 python_execute_trial() {
1272 die "${FUNCNAME}() is invalid for python-r1 suite"
1275 python_enable_pyc() {
1276 die "${FUNCNAME}() is invalid for python-r1 suite"
1279 python_disable_pyc() {
1280 die "${FUNCNAME}() is invalid for python-r1 suite"
1283 python_mod_optimize() {
1284 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"
1287 python_mod_cleanup() {
1288 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"
1291 # python.eclass::progress
1293 python_abi_depend() {
1294 die "${FUNCNAME}() is invalid for python-r1 suite"
1297 python_install_executables() {
1298 die "${FUNCNAME}() is invalid for python-r1 suite"
1301 python_get_extension_module_suffix() {
1302 die "${FUNCNAME}() is invalid for python-r1 suite"
1305 python_byte-compile_modules() {
1306 die "${FUNCNAME}() is invalid for python-r1 suite"
1309 python_clean_byte-compiled_modules() {
1310 die "${FUNCNAME}() is invalid for python-r1 suite"
1313 python_generate_cffi_modules() {
1314 die "${FUNCNAME}() is invalid for python-r1 suite"