* [gentoo-python] [PATCH python-single-r1 1/2] Introduce python_fix_shebang() for Python scripts in single-r1.
@ 2012-12-16 22:52 Michał Górny
2012-12-16 22:52 ` [gentoo-python] [PATCH python-single-r1 2/2] Example use of python_fix_shebang() (xen-tools) Michał Górny
0 siblings, 1 reply; 2+ messages in thread
From: Michał Górny @ 2012-12-16 22:52 UTC (permalink / raw
To: gentoo-python; +Cc: python, Michał Górny
python-single-r1 installs all Python scripts and modules for a single
chosen Python implementation only. Therefore, there's no point
in replicating them and instead it is expected that all scripts are
installed with correct, versioned shebang.
If a particular build system doesn't do that, now python-single-r1
offers python_fix_shebang() function which does just that. It can be
passed either a list of files or directories, it finds all Python
scripts (through the shebang) and makes sure they point to the selected
Python impl.
Files without Python shebangs are ignored. Files with matching Python
shebangs are skipped (fixed already). Files with Python shebangs tied
to another Python version are treated as a fatal error.
---
gx86/eclass/python-single-r1.eclass | 44 +++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/gx86/eclass/python-single-r1.eclass b/gx86/eclass/python-single-r1.eclass
index 51807f2..88c6056 100644
--- a/gx86/eclass/python-single-r1.eclass
+++ b/gx86/eclass/python-single-r1.eclass
@@ -194,5 +194,49 @@ python-single-r1_pkg_setup() {
done
}
+# @FUNCTION: python_fix_shebang
+# @USAGE: <path>...
+# @DESCRIPTION:
+# Replace the shebang in Python scripts with the current Python
+# implementation (EPYTHON). If a directory is passed, works recursively
+# on all Python scripts.
+#
+# Only files having a 'python' shebang will be modified; other files
+# will be skipped. If a script has a complete shebang matching
+# the chosen interpreter version, it is left unmodified. If a script has
+# a complete shebang matching other version, the command dies.
+python_fix_shebang() {
+ debug-print-function ${FUNCNAME} "${@}"
+
+ [[ ${1} ]] || die "${FUNCNAME}: no paths given"
+ [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
+
+ local path f
+ for path; do
+ while IFS= read -r -d '' f; do
+ local shebang=$(head -n 1 "${f}")
+
+ case "${shebang}" in
+ '#!'*${EPYTHON}*)
+ debug-print "${FUNCNAME}: in file ${f#${D}}"
+ debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
+ ;;
+ '#!'*python[23].[0123456789]*|'#!'*pypy-c*|'#!'*jython*)
+ debug-print "${FUNCNAME}: in file ${f#${D}}"
+ debug-print "${FUNCNAME}: incorrect specific shebang: ${shebang}"
+
+ die "${f#${D}} has a specific Python shebang not matching EPYTHON"
+ ;;
+ '#!'*python*)
+ debug-print "${FUNCNAME}: in file ${f#${D}}"
+ debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
+
+ einfo "Fixing shebang in ${f#${D}}"
+ _python_rewrite_shebang "${f}"
+ esac
+ done < <(find "${path}" -type f -print0)
+ done
+}
+
_PYTHON_SINGLE_R1=1
fi
--
1.8.0.2
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2012-12-16 22:52 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-16 22:52 [gentoo-python] [PATCH python-single-r1 1/2] Introduce python_fix_shebang() for Python scripts in single-r1 Michał Górny
2012-12-16 22:52 ` [gentoo-python] [PATCH python-single-r1 2/2] Example use of python_fix_shebang() (xen-tools) Michał Górny
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox