obmc-phosphor-python3-autotools: workaround setuptools mangling

python-setuptools will mangle the #! line in a python script that
we install using setuptools.  OE already has workarounds for this
in distutils3.bbclass but we cannot use it here due to the conflict
of autotools and distutils.  Port the mangling fixes into our autotools
recipe.

See pypa/setuptools#494 for upstream report (years old).

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I898b478e0df910b414e3b682ffeffaa3f9c14daf
diff --git a/classes/obmc-phosphor-python3-autotools.bbclass b/classes/obmc-phosphor-python3-autotools.bbclass
index d76598a..89a884a 100644
--- a/classes/obmc-phosphor-python3-autotools.bbclass
+++ b/classes/obmc-phosphor-python3-autotools.bbclass
@@ -1,6 +1,9 @@
 inherit obmc-phosphor-utils
 inherit python3native
 
+OBMC_PYTHON_EXE="python3"
+OBMC_PYTHON_EXE_class-native="nativepython3"
+
 DEPENDS += "python3"
 
 export BUILD_SYS
@@ -15,3 +18,21 @@
         set_append(d, 'FILES_%s' % pkg,
                    d.getVar('PYTHON_SITEPACKAGES_DIR', True))
 }
+
+# python-setuptools does some mangling of the #! in any scripts it installs,
+# which has been reported for years at pypa/setuptools#494.  OE has
+# workarounds in distutils3.bbclass, but we cannot inherit that here because
+# it conflicts with autotools.bbclass.  Port the un-mangling code here.
+#
+# This finds any ${PYTHON} executable path that got put into the scripts
+# and reverts it back to "/usr/bin/env python3".  It also reverts any full
+# ${STAGING_BINDIR_NATIVE} path back to "/usr/bin".
+#
+do_install_append() {
+    for i in ${D}${bindir}/* ${D}${sbindir}/*; do
+        if [ -f "$i" ]; then
+            sed -i -e s:${PYTHON}:${USRBINPATH}/env\ ${OBMC_PYTHON_EXE}:g $i
+            sed -i -e s:${STAGING_BINDIR_NATIVE}:${bindir}:g $i
+        fi
+    done
+}