Gentoo Archives: gentoo-dev

From: James Le Cuirot <chewi@g.o>
To: gentoo-dev <gentoo-dev@l.g.o>
Cc: James Le Cuirot <chewi@g.o>
Subject: [gentoo-dev] [PATCH 02/12] python-utils-r1.eclass: New python_fix_shebang approach
Date: Thu, 03 Jan 2019 21:40:52
Message-Id: 20190103213924.22835-3-chewi@gentoo.org
In Reply to: [gentoo-dev] [PATCH] Eclass changes for cross-compiling Python modules by James Le Cuirot
1 The previous approach would erroneously match foopython. The new
2 approach requires the match to start the string or be preceeded by a
3 slash, the only two cases we actually want. It does this with slightly
4 less code and allows the replacement of whole path strings that would
5 be problematic when passed to sed. This will be needed when
6 cross-compiling is addressed.
7
8 Signed-off-by: James Le Cuirot <chewi@g.o>
9 ---
10 eclass/python-utils-r1.eclass | 78 ++++++++++++++---------------------
11 1 file changed, 31 insertions(+), 47 deletions(-)
12
13 diff --git a/eclass/python-utils-r1.eclass b/eclass/python-utils-r1.eclass
14 index da76a755fb34..1eca0764a202 100644
15 --- a/eclass/python-utils-r1.eclass
16 +++ b/eclass/python-utils-r1.eclass
17 @@ -1165,8 +1165,7 @@ python_fix_shebang() {
18 [[ -d ${path} ]] && is_recursive=1
19
20 while IFS= read -r -d '' f; do
21 - local shebang i
22 - local error= from=
23 + local shebang i= error= fix=
24
25 # note: we can't ||die here since read will fail if file
26 # has no newline characters
27 @@ -1175,65 +1174,56 @@ python_fix_shebang() {
28 # First, check if it's shebang at all...
29 if [[ ${shebang} == '#!'* ]]; then
30 local split_shebang=()
31 - read -r -a split_shebang <<<${shebang} || die
32 + read -r -a split_shebang <<<${shebang#\#\!} || die
33
34 # Match left-to-right in a loop, to avoid matching random
35 # repetitions like 'python2.7 python2'.
36 - for i in "${split_shebang[@]}"; do
37 - case "${i}" in
38 - *"${EPYTHON}")
39 + for i in $(seq 0 $((${#split_shebang[@]} - 1))); do
40 + case "/${split_shebang[${i}]}" in
41 + */${EPYTHON})
42 debug-print "${FUNCNAME}: in file ${f#${D%/}}"
43 debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
44
45 # Nothing to do, move along.
46 any_correct=1
47 - from=${EPYTHON}
48 + continue 2
49 + ;;
50 + */python)
51 + fix=1
52 break
53 ;;
54 - *python|*python[23])
55 - debug-print "${FUNCNAME}: in file ${f#${D%/}}"
56 - debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
57 -
58 - if [[ ${i} == *python2 ]]; then
59 - from=python2
60 - if [[ ! ${force} ]]; then
61 - python_is_python3 "${EPYTHON}" && error=1
62 - fi
63 - elif [[ ${i} == *python3 ]]; then
64 - from=python3
65 - if [[ ! ${force} ]]; then
66 - python_is_python3 "${EPYTHON}" || error=1
67 - fi
68 - else
69 - from=python
70 + */python2)
71 + if [[ ! ${force} ]]; then
72 + python_is_python3 "${EPYTHON}" && error=1
73 fi
74 + fix=1
75 break
76 ;;
77 - *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789])
78 - # Explicit mismatch.
79 + */python3)
80 if [[ ! ${force} ]]; then
81 - error=1
82 - else
83 - case "${i}" in
84 - *python[23].[0123456789])
85 - from="python[23].[0123456789]";;
86 - *pypy)
87 - from="pypy";;
88 - *pypy3)
89 - from="pypy3";;
90 - *jython[23].[0123456789])
91 - from="jython[23].[0123456789]";;
92 - *)
93 - die "${FUNCNAME}: internal error in 2nd pattern match";;
94 - esac
95 + python_is_python3 "${EPYTHON}" || error=1
96 fi
97 + fix=1
98 + break
99 + ;;
100 + */python[2-3].[0-9]|*/pypy|*/pypy3|*/jython[2-3].[0-9])
101 + # Explicit mismatch.
102 + [[ ! ${force} ]] && error=1
103 + fix=1
104 break
105 ;;
106 esac
107 done
108 fi
109
110 - if [[ ! ${error} && ! ${from} ]]; then
111 + if [[ ${fix} ]]; then
112 + debug-print "${FUNCNAME}: in file ${f#${D%/}}"
113 + debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
114 +
115 + split_shebang[${i}]=/${split_shebang[${i}]}
116 + split_shebang[${i}]=${split_shebang[${i}]%/*}
117 + split_shebang[${i}]=${split_shebang[${i}]#/}${split_shebang[${i}]:+/}${EPYTHON}
118 + elif [[ ! ${error} ]]; then
119 # Non-Python shebang. Allowed in recursive mode,
120 # disallowed when specifying file explicitly.
121 [[ ${is_recursive} ]] && continue
122 @@ -1245,13 +1235,7 @@ python_fix_shebang() {
123 fi
124
125 if [[ ! ${error} ]]; then
126 - # We either want to match ${from} followed by space
127 - # or at end-of-string.
128 - if [[ ${shebang} == *${from}" "* ]]; then
129 - sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die
130 - else
131 - sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
132 - fi
133 + sed -i -e "1c#\!${split_shebang[*]}" "${f}" || die
134 any_fixed=1
135 else
136 eerror "The file has incompatible shebang:"
137 --
138 2.19.2

Replies