commit:     157dbddd0dc3a6518ea1fed156ec6827fff1e1bb
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Sun Apr 10 09:44:54 2022 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Mon Apr 11 19:58:40 2022 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=157dbddd

distutils-r1.eclass: Introduce distutils_write_namespace

Introduce a distutils_write_namespace helper that can be used to
temporarily write a namespace __init__.py as needed to run tests
when legacy dev-python/namespace-* packages are installed.

Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>

 eclass/distutils-r1.eclass | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/eclass/distutils-r1.eclass b/eclass/distutils-r1.eclass
index f9cb41aa3d42..ccdf0e025758 100644
--- a/eclass/distutils-r1.eclass
+++ b/eclass/distutils-r1.eclass
@@ -750,6 +750,42 @@ distutils_install_for_testing() {
        esetup.py install "${add_args[@]}" "${@}"
 }
 
+# @FUNCTION: distutils_write_namespace
+# @USAGE: <namespace>...
+# @DESCRIPTION:
+# Write the __init__.py file for the requested namespace into PEP517
+# install tree, in order to fix running tests when legacy namespace
+# packages are installed (dev-python/namespace-*).
+#
+# This function must only be used in python_test().  The created file
+# will automatically be removed upon leaving the test phase.
+distutils_write_namespace() {
+       debug-print-function ${FUNCNAME} "${@}"
+
+       if [[ ! ${DISTUTILS_USE_PEP517} ]]; then
+               die "${FUNCNAME} is available only in PEP517 mode"
+       fi
+       if [[ ${EBUILD_PHASE} != test || ! ${BUILD_DIR} ]]; then
+               die "${FUNCNAME} should only be used in python_test"
+       fi
+
+       local namespace
+       for namespace; do
+               if [[ ${namespace} == *[./]* ]]; then
+                       die "${FUNCNAME} does not support nested namespaces at 
the moment"
+               fi
+
+               local 
path=${BUILD_DIR}/install$(python_get_sitedir)/${namespace}/__init__.py
+               if [[ -f ${path} ]]; then
+                       die "Requested namespace ${path} exists already!"
+               fi
+               cat > "${path}" <<-EOF || die
+                       __path__ = __import__('pkgutil').extend_path(__path__, 
__name__)
+               EOF
+               _DISTUTILS_POST_PHASE_RM+=( "${path}" )
+       done
+}
+
 # @FUNCTION: _distutils-r1_disable_ez_setup
 # @INTERNAL
 # @DESCRIPTION:
@@ -1477,10 +1513,15 @@ distutils-r1_run_phase() {
        esac
 
        local -x LDSHARED="${CC} ${ldopts}" LDCXXSHARED="${CXX} ${ldopts}"
+       local _DISTUTILS_POST_PHASE_RM=()
 
        "${@}"
        local ret=${?}
 
+       if [[ -n ${_DISTUTILS_POST_PHASE_RM} ]]; then
+               rm "${_DISTUTILS_POST_PHASE_RM[@]}" || die
+       fi
+
        cd "${_DISTUTILS_INITIAL_CWD}" || die
        return "${ret}"
 }

Reply via email to