python-utils-r1.eclass: python_moduleinto, allow package dot-notation
[gentoo.git] / eclass / python-utils-r1.eclass
index 3ea23a81f2d11afc1503e343a6a854cf31ef2f28..863051337f0b1e12a779834229e36a7d7fbbe810 100644 (file)
@@ -1,6 +1,5 @@
-# Copyright 1999-2015 Gentoo Foundation
+# Copyright 1999-2017 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
-# $Id$
 
 # @ECLASS: python-utils-r1.eclass
 # @MAINTAINER:
@@ -40,12 +39,13 @@ inherit toolchain-funcs
 # @INTERNAL
 # @DESCRIPTION:
 # All supported Python implementations, most preferred last.
-declare -g -r _PYTHON_ALL_IMPLS=(
-       python2_7
-       python3_3 python3_4 python3_5
+_PYTHON_ALL_IMPLS=(
+       jython2_7
        pypy pypy3
-       jython2_5 jython2_7
+       python2_7
+       python3_4 python3_5 python3_6
 )
+readonly _PYTHON_ALL_IMPLS
 
 # @FUNCTION: _python_impl_supported
 # @USAGE: <impl>
@@ -67,10 +67,10 @@ _python_impl_supported() {
        # keep in sync with _PYTHON_ALL_IMPLS!
        # (not using that list because inline patterns shall be faster)
        case "${impl}" in
-               python2_7|python3_[345]|jython2_[57])
+               python2_7|python3_[456]|jython2_7)
                        return 0
                        ;;
-               pypy1_[89]|pypy2_0|python2_[56]|python3_[12])
+               pypy1_[89]|pypy2_0|python2_[56]|python3_[123])
                        return 1
                        ;;
                pypy|pypy3)
@@ -83,6 +83,72 @@ _python_impl_supported() {
        esac
 }
 
+# @FUNCTION: _python_set_impls
+# @INTERNAL
+# @DESCRIPTION:
+# Check PYTHON_COMPAT for well-formedness and validity, then set
+# two global variables:
+#
+# - _PYTHON_SUPPORTED_IMPLS containing valid implementations supported
+#   by the ebuild (PYTHON_COMPAT - dead implementations),
+#
+# - and _PYTHON_UNSUPPORTED_IMPLS containing valid implementations that
+#   are not supported by the ebuild.
+#
+# Implementations in both variables are ordered using the pre-defined
+# eclass implementation ordering.
+#
+# This function must be called once in global scope by an eclass
+# utilizing PYTHON_COMPAT.
+_python_set_impls() {
+       local i
+
+       if ! declare -p PYTHON_COMPAT &>/dev/null; then
+               die 'PYTHON_COMPAT not declared.'
+       fi
+       if [[ $(declare -p PYTHON_COMPAT) != "declare -a"* ]]; then
+               die 'PYTHON_COMPAT must be an array.'
+       fi
+       for i in "${PYTHON_COMPAT[@]}"; do
+               # trigger validity checks
+               _python_impl_supported "${i}"
+       done
+
+       local supp=() unsupp=()
+
+       for i in "${_PYTHON_ALL_IMPLS[@]}"; do
+               if has "${i}" "${PYTHON_COMPAT[@]}"; then
+                       supp+=( "${i}" )
+               else
+                       unsupp+=( "${i}" )
+               fi
+       done
+
+       if [[ ! ${supp[@]} ]]; then
+               die "No supported implementation in PYTHON_COMPAT."
+       fi
+
+       if [[ ${_PYTHON_SUPPORTED_IMPLS[@]} ]]; then
+               # set once already, verify integrity
+               if [[ ${_PYTHON_SUPPORTED_IMPLS[@]} != ${supp[@]} ]]; then
+                       eerror "Supported impls (PYTHON_COMPAT) changed between inherits!"
+                       eerror "Before: ${_PYTHON_SUPPORTED_IMPLS[*]}"
+                       eerror "Now   : ${supp[*]}"
+                       die "_PYTHON_SUPPORTED_IMPLS integrity check failed"
+               fi
+               if [[ ${_PYTHON_UNSUPPORTED_IMPLS[@]} != ${unsupp[@]} ]]; then
+                       eerror "Unsupported impls changed between inherits!"
+                       eerror "Before: ${_PYTHON_UNSUPPORTED_IMPLS[*]}"
+                       eerror "Now   : ${unsupp[*]}"
+                       die "_PYTHON_UNSUPPORTED_IMPLS integrity check failed"
+               fi
+       else
+               _PYTHON_SUPPORTED_IMPLS=( "${supp[@]}" )
+               _PYTHON_UNSUPPORTED_IMPLS=( "${unsupp[@]}" )
+               readonly _PYTHON_SUPPORTED_IMPLS _PYTHON_UNSUPPORTED_IMPLS
+       fi
+}
+
 # @ECLASS-VARIABLE: PYTHON
 # @DEFAULT_UNSET
 # @DESCRIPTION:
@@ -365,11 +431,9 @@ python_export() {
                                        python*)
                                                PYTHON_PKG_DEP="dev-lang/python:${impl#python}";;
                                        pypy)
-                                               PYTHON_PKG_DEP='virtual/pypy:0=';;
+                                               PYTHON_PKG_DEP='>=virtual/pypy-5:0=';;
                                        pypy3)
-                                               PYTHON_PKG_DEP='virtual/pypy3:0=';;
-                                       jython2.5)
-                                               PYTHON_PKG_DEP='>=dev-java/jython-2.5.3-r2:2.5';;
+                                               PYTHON_PKG_DEP='>=virtual/pypy3-5:0=';;
                                        jython2.7)
                                                PYTHON_PKG_DEP='dev-java/jython:2.7';;
                                        *)
@@ -592,10 +656,16 @@ python_optimize() {
                instpath=/${instpath##/}
 
                case "${EPYTHON}" in
-                       python*)
+                       python2.7|python3.[34])
                                "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
                                "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
                                ;;
+                       python*|pypy3)
+                               # both levels of optimization are separate since 3.5
+                               "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
+                               "${PYTHON}" -O -m compileall -q -f -d "${instpath}" "${d}"
+                               "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
+                               ;;
                        *)
                                "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
                                ;;
@@ -603,32 +673,23 @@ python_optimize() {
        done
 }
 
-# @ECLASS-VARIABLE: python_scriptroot
-# @DEFAULT_UNSET
+# @FUNCTION: python_scriptinto
+# @USAGE: <new-path>
 # @DESCRIPTION:
-# The current script destination for python_doscript(). The path
-# is relative to the installation root (${ED}).
-#
-# When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
+# Set the directory to which files passed to python_doexe(),
+# python_doscript(), python_newexe() and python_newscript()
+# are going to be installed. The new value needs to be relative
+# to the installation root (${ED}).
 #
-# Can be set indirectly through the python_scriptinto() function.
+# If not set explicitly, the directory defaults to /usr/bin.
 #
 # Example:
 # @CODE
 # src_install() {
-#   local python_scriptroot=${GAMES_BINDIR}
+#   python_scriptinto /usr/sbin
 #   python_foreach_impl python_doscript foo
 # }
 # @CODE
-
-# @FUNCTION: python_scriptinto
-# @USAGE: <new-path>
-# @DESCRIPTION:
-# Set the current scriptroot. The new value will be stored
-# in the 'python_scriptroot' environment variable. The new value need
-# be relative to the installation root (${ED}).
-#
-# Alternatively, you can set the variable directly.
 python_scriptinto() {
        debug-print-function ${FUNCNAME} "${@}"
 
@@ -638,7 +699,7 @@ python_scriptinto() {
 # @FUNCTION: python_doexe
 # @USAGE: <files>...
 # @DESCRIPTION:
-# Install the given executables into current python_scriptroot,
+# Install the given executables into the executable install directory,
 # for the current Python implementation (${EPYTHON}).
 #
 # The executable will be wrapped properly for the Python implementation,
@@ -655,7 +716,7 @@ python_doexe() {
 # @FUNCTION: python_newexe
 # @USAGE: <path> <new-name>
 # @DESCRIPTION:
-# Install the given executable into current python_scriptroot,
+# Install the given executable into the executable install directory,
 # for the current Python implementation (${EPYTHON}).
 #
 # The executable will be wrapped properly for the Python implementation,
@@ -670,7 +731,7 @@ python_newexe() {
                die "python_do* and python_new* helpers are banned in EAPIs older than 4."
        fi
 
-       local wrapd=${python_scriptroot:-${DESTTREE}/bin}
+       local wrapd=${python_scriptroot:-/usr/bin}
 
        local f=${1}
        local newfn=${2}
@@ -698,7 +759,7 @@ python_newexe() {
 # @FUNCTION: python_doscript
 # @USAGE: <files>...
 # @DESCRIPTION:
-# Install the given scripts into current python_scriptroot,
+# Install the given scripts into the executable install directory,
 # for the current Python implementation (${EPYTHON}).
 #
 # All specified files must start with a 'python' shebang. The shebang
@@ -721,7 +782,7 @@ python_doscript() {
 # @FUNCTION: python_newscript
 # @USAGE: <path> <new-name>
 # @DESCRIPTION:
-# Install the given script into current python_scriptroot
+# Install the given script into the executable install directory
 # for the current Python implementation (${EPYTHON}), and name it
 # <new-name>.
 #
@@ -742,35 +803,34 @@ python_newscript() {
        python_newexe "${@}"
 }
 
-# @ECLASS-VARIABLE: python_moduleroot
-# @DEFAULT_UNSET
+# @FUNCTION: python_moduleinto
+# @USAGE: <new-path>
 # @DESCRIPTION:
-# The current module root for python_domodule(). The path can be either
-# an absolute system path (it must start with a slash, and ${ED} will be
-# prepended to it) or relative to the implementation's site-packages directory
-# (then it must start with a non-slash character).
+# Set the Python module install directory for python_domodule().
+# The <new-path> can either be an absolute target system path (in which
+# case it needs to start with a slash, and ${ED} will be prepended to
+# it) or relative to the implementation's site-packages directory
+# (then it must not start with a slash). The relative path can be
+# specified either using the Python package notation (separated by dots)
+# or the directory notation (using slashes).
 #
-# When unset, the modules will be installed in the site-packages root.
+# When not set explicitly, the modules are installed to the top
+# site-packages directory.
 #
-# Can be set indirectly through the python_moduleinto() function.
+# In the relative case, the exact path is determined directly
+# by each python_doscript/python_newscript function. Therefore,
+# python_moduleinto can be safely called before establishing the Python
+# interpreter and/or a single call can be used to set the path correctly
+# for multiple implementations, as can be seen in the following example.
 #
 # Example:
 # @CODE
 # src_install() {
-#   local python_moduleroot=bar
+#   python_moduleinto bar
 #   # installs ${PYTHON_SITEDIR}/bar/baz.py
 #   python_foreach_impl python_domodule baz.py
 # }
 # @CODE
-
-# @FUNCTION: python_moduleinto
-# @USAGE: <new-path>
-# @DESCRIPTION:
-# Set the current module root. The new value will be stored
-# in the 'python_moduleroot' environment variable. The new value need
-# be relative to the site-packages root.
-#
-# Alternatively, you can set the variable directly.
 python_moduleinto() {
        debug-print-function ${FUNCNAME} "${@}"
 
@@ -780,8 +840,8 @@ python_moduleinto() {
 # @FUNCTION: python_domodule
 # @USAGE: <files>...
 # @DESCRIPTION:
-# Install the given modules (or packages) into the current
-# python_moduleroot. The list can mention both modules (files)
+# Install the given modules (or packages) into the current Python module
+# installation directory. The list can mention both modules (files)
 # and packages (directories). All listed files will be installed
 # for all enabled implementations, and compiled afterwards.
 #
@@ -809,7 +869,7 @@ python_domodule() {
                local PYTHON_SITEDIR=${PYTHON_SITEDIR}
                [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
 
-               d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
+               d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot//.//}
        fi
 
        (
@@ -881,11 +941,11 @@ python_wrapper_setup() {
                mkdir -p "${workdir}"/{bin,pkgconfig} || die
 
                # Clean up, in case we were supposed to do a cheap update.
-               rm -f "${workdir}"/bin/python{,2,3,-config} || die
+               rm -f "${workdir}"/bin/python{,2,3}{,-config} || die
                rm -f "${workdir}"/bin/2to3 || die
                rm -f "${workdir}"/pkgconfig/python{,2,3}.pc || die
 
-               local EPYTHON PYTHON PYTHON_CONFIG
+               local EPYTHON PYTHON
                python_export "${impl}" EPYTHON PYTHON
 
                local pyver pyother
@@ -912,11 +972,9 @@ python_wrapper_setup() {
 
                # CPython-specific
                if [[ ${EPYTHON} == python* ]]; then
-                       python_export "${impl}" PYTHON_CONFIG
-
                        cat > "${workdir}/bin/python-config" <<-_EOF_ || die
                                #!/bin/sh
-                               exec "${PYTHON_CONFIG}" "\${@}"
+                               exec "${PYTHON}-config" "\${@}"
                        _EOF_
                        cp "${workdir}/bin/python-config" \
                                "${workdir}/bin/python${pyver}-config" || die
@@ -943,18 +1001,18 @@ python_wrapper_setup() {
                        _EOF_
                        chmod +x "${workdir}"/bin/${x} || die
                done
+       fi
 
-               # Now, set the environment.
-               # But note that ${workdir} may be shared with something else,
-               # and thus already on top of PATH.
-               if [[ ${PATH##:*} != ${workdir}/bin ]]; then
-                       PATH=${workdir}/bin${PATH:+:${PATH}}
-               fi
-               if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
-                       PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
-               fi
-               export PATH PKG_CONFIG_PATH
+       # Now, set the environment.
+       # But note that ${workdir} may be shared with something else,
+       # and thus already on top of PATH.
+       if [[ ${PATH##:*} != ${workdir}/bin ]]; then
+               PATH=${workdir}/bin${PATH:+:${PATH}}
        fi
+       if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
+               PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
+       fi
+       export PATH PKG_CONFIG_PATH
 }
 
 # @FUNCTION: python_is_python3
@@ -1165,7 +1223,7 @@ python_fix_shebang() {
 # Check whether the specified locale sanely maps between lowercase
 # and uppercase ASCII characters.
 _python_check_locale_sanity() {
-       local -x LC_CTYPE=${1}
+       local -x LC_ALL=${1}
        local IFS=
 
        local lc=( {a..z} )
@@ -1190,7 +1248,7 @@ python_export_utf8_locale() {
 
        if [[ $(locale charmap) != UTF-8 ]]; then
                # Try English first, then everything else.
-               local lang locales="en_US.UTF-8 $(locale -a)"
+               local lang locales="C.UTF-8 en_US.UTF-8 en_GB.UTF-8 $(locale -a)"
 
                for lang in ${locales}; do
                        if [[ $(LC_ALL=${lang} locale charmap 2>/dev/null) == UTF-8 ]]; then
@@ -1212,14 +1270,14 @@ python_export_utf8_locale() {
                                        fi
                                        return 0
                                fi
-                       fi  
+                       fi
                done
 
                ewarn "Could not find a UTF-8 locale. This may trigger build failures in"
                ewarn "some python packages. Please ensure that a UTF-8 locale is listed in"
                ewarn "/etc/locale.gen and run locale-gen."
                return 1
-       fi  
+       fi
 
        return 0
 }