Gentoo Archives: gentoo-python

From: "Michał Górny" <mgorny@g.o>
To: gentoo-python@l.g.o
Cc: python@g.o, "Michał Górny" <mgorny@g.o>
Subject: [gentoo-python] [PATCH python-single-r1 1/2] Introduce python_fix_shebang() for Python scripts in single-r1.
Date: Sun, 16 Dec 2012 22:52:24
Message-Id: 1355698330-15660-1-git-send-email-mgorny@gentoo.org
1 python-single-r1 installs all Python scripts and modules for a single
2 chosen Python implementation only. Therefore, there's no point
3 in replicating them and instead it is expected that all scripts are
4 installed with correct, versioned shebang.
5
6 If a particular build system doesn't do that, now python-single-r1
7 offers python_fix_shebang() function which does just that. It can be
8 passed either a list of files or directories, it finds all Python
9 scripts (through the shebang) and makes sure they point to the selected
10 Python impl.
11
12 Files without Python shebangs are ignored. Files with matching Python
13 shebangs are skipped (fixed already). Files with Python shebangs tied
14 to another Python version are treated as a fatal error.
15 ---
16 gx86/eclass/python-single-r1.eclass | 44 +++++++++++++++++++++++++++++++++++++
17 1 file changed, 44 insertions(+)
18
19 diff --git a/gx86/eclass/python-single-r1.eclass b/gx86/eclass/python-single-r1.eclass
20 index 51807f2..88c6056 100644
21 --- a/gx86/eclass/python-single-r1.eclass
22 +++ b/gx86/eclass/python-single-r1.eclass
23 @@ -194,5 +194,49 @@ python-single-r1_pkg_setup() {
24 done
25 }
26
27 +# @FUNCTION: python_fix_shebang
28 +# @USAGE: <path>...
29 +# @DESCRIPTION:
30 +# Replace the shebang in Python scripts with the current Python
31 +# implementation (EPYTHON). If a directory is passed, works recursively
32 +# on all Python scripts.
33 +#
34 +# Only files having a 'python' shebang will be modified; other files
35 +# will be skipped. If a script has a complete shebang matching
36 +# the chosen interpreter version, it is left unmodified. If a script has
37 +# a complete shebang matching other version, the command dies.
38 +python_fix_shebang() {
39 + debug-print-function ${FUNCNAME} "${@}"
40 +
41 + [[ ${1} ]] || die "${FUNCNAME}: no paths given"
42 + [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
43 +
44 + local path f
45 + for path; do
46 + while IFS= read -r -d '' f; do
47 + local shebang=$(head -n 1 "${f}")
48 +
49 + case "${shebang}" in
50 + '#!'*${EPYTHON}*)
51 + debug-print "${FUNCNAME}: in file ${f#${D}}"
52 + debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
53 + ;;
54 + '#!'*python[23].[0123456789]*|'#!'*pypy-c*|'#!'*jython*)
55 + debug-print "${FUNCNAME}: in file ${f#${D}}"
56 + debug-print "${FUNCNAME}: incorrect specific shebang: ${shebang}"
57 +
58 + die "${f#${D}} has a specific Python shebang not matching EPYTHON"
59 + ;;
60 + '#!'*python*)
61 + debug-print "${FUNCNAME}: in file ${f#${D}}"
62 + debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
63 +
64 + einfo "Fixing shebang in ${f#${D}}"
65 + _python_rewrite_shebang "${f}"
66 + esac
67 + done < <(find "${path}" -type f -print0)
68 + done
69 +}
70 +
71 _PYTHON_SINGLE_R1=1
72 fi
73 --
74 1.8.0.2

Replies