From 157dbddd0dc3a6518ea1fed156ec6827fff1e1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Sun, 10 Apr 2022 11:44:54 +0200 Subject: [PATCH] distutils-r1.eclass: Introduce distutils_write_namespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- eclass/distutils-r1.eclass | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/eclass/distutils-r1.eclass b/eclass/distutils-r1.eclass index f9cb41aa3d42b..ccdf0e0257583 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: ... +# @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}" }