commit:     d5b7d1b42500509d31a385fbae77facab02769b7
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Wed Jan 26 13:59:59 2022 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Fri Jan 28 23:01:20 2022 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=d5b7d1b4

distutils-r1.eclass: Set script shebangs to venv in PEP 517

Alter the shebangs of generated scripts to point out the to venv Python
executables in PEP 517 mode.  Otherwise, the executables are run with
system Python during the test run and therefore do not use the venv.

For convenience, rather than modifying them back just copy them
immediately into the python-exec directory.  This means that instead of
reverting some of our python_compile() changes in python_install()
and then moving scripts to the python-exec directory, we can just
discard /usr/bin and let the wrapping logic recreate it.

While at it, stop repeating ${root}${EPREFIX}/usr/bin multiple times
in python_compile().  Just shove it into a helper variable.  Also move
pyvenv.cfg inside bindir for convenience — Python is fine with either
location.

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

 eclass/distutils-r1.eclass | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/eclass/distutils-r1.eclass b/eclass/distutils-r1.eclass
index 9b2dc841054c..cf3eb639d5e6 100644
--- a/eclass/distutils-r1.eclass
+++ b/eclass/distutils-r1.eclass
@@ -982,14 +982,30 @@ distutils-r1_python_compile() {
                        esetup.py clean -a
                fi
 
+               # copy executables to python-exec directory
+               # we do it early so that we can alter bindir recklessly
+               local bindir=${root}${EPREFIX}/usr/bin
+               local rscriptdir=${root}$(python_get_scriptdir)
+               [[ -d ${rscriptdir} ]] &&
+                       die "${rscriptdir} should not exist!"
+               if [[ -d ${bindir} ]]; then
+                       mkdir -p "${rscriptdir}" || die
+                       cp -a --reflink=auto "${bindir}"/. "${rscriptdir}"/ || 
die
+               fi
+
                # enable venv magic inside the install tree
-               mkdir -p "${root}${EPREFIX}"/usr/bin || die
-               ln -s "${PYTHON}" "${root}${EPREFIX}/usr/bin/${EPYTHON}" || die
-               ln -s "${EPYTHON}" "${root}${EPREFIX}/usr/bin/python3" || die
-               ln -s "${EPYTHON}" "${root}${EPREFIX}/usr/bin/python" || die
-               cat > "${root}${EPREFIX}"/usr/pyvenv.cfg <<-EOF || die
+               mkdir -p "${bindir}" || die
+               ln -s "${PYTHON}" "${bindir}/${EPYTHON}" || die
+               ln -s "${EPYTHON}" "${bindir}/python3" || die
+               ln -s "${EPYTHON}" "${bindir}/python" || die
+               cat > "${bindir}"/pyvenv.cfg <<-EOF || die
                        include-system-site-packages = true
                EOF
+
+               # we need to change shebangs to point to the venv-python
+               find "${bindir}" -type f -exec sed -i \
+                       -e 
"1s@^#!\(${EPREFIX}/usr/bin/\(python\|pypy\)\)@#!${root}\1@" \
+                       {} + || die
        fi
 }
 
@@ -1105,16 +1121,9 @@ distutils-r1_python_install() {
        local scriptdir=${EPREFIX}/usr/bin
        if [[ ${DISTUTILS_USE_PEP517} ]]; then
                local root=${BUILD_DIR}/install
-               local rscriptdir=${root}$(python_get_scriptdir)
-               [[ -d ${rscriptdir} ]] &&
-                       die "${rscriptdir} should not exist!"
-               # remove venv magic
-               rm 
"${root}${EPREFIX}"/usr/{pyvenv.cfg,bin/{python,python3,${EPYTHON}}} || die
-               find "${root}${EPREFIX}"/usr/bin -empty -delete || die
-               if [[ ! ${DISTUTILS_SINGLE_IMPL} && -d 
${root}${EPREFIX}/usr/bin ]]; then
-                       mkdir -p "${rscriptdir%/*}" || die
-                       mv "${root}${EPREFIX}/usr/bin" "${rscriptdir}" || die
-               fi
+               # remove the altered bindir, executables from the package
+               # are already in scriptdir
+               rm -r "${root}${scriptdir}" || die
        else
                local root=${D%/}/_${EPYTHON}
                [[ ${DISTUTILS_SINGLE_IMPL} ]] && root=${D%/}

Reply via email to