Gentoo Archives: gentoo-dev

From: James Le Cuirot <chewi@g.o>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH 02/12] python-utils-r1.eclass: New python_fix_shebang approach
Date: Sun, 06 Jan 2019 17:21:21
Message-Id: 20190106172059.5d57ffb9@symphony.aura-online.co.uk
In Reply to: Re: [gentoo-dev] [PATCH 02/12] python-utils-r1.eclass: New python_fix_shebang approach by "Michał Górny"
1 On Fri, 04 Jan 2019 07:02:40 +0100
2 Michał Górny <mgorny@g.o> wrote:
3
4 > On Thu, 2019-01-03 at 21:39 +0000, James Le Cuirot wrote:
5 > > The previous approach would erroneously match foopython. The new
6 > > approach requires the match to start the string or be preceeded by a
7 > > slash, the only two cases we actually want. It does this with slightly
8 > > less code and allows the replacement of whole path strings that would
9 > > be problematic when passed to sed. This will be needed when
10 > > cross-compiling is addressed.
11 > >
12 > > Signed-off-by: James Le Cuirot <chewi@g.o>
13 > > ---
14 > > eclass/python-utils-r1.eclass | 78 ++++++++++++++---------------------
15 > > 1 file changed, 31 insertions(+), 47 deletions(-)
16 > >
17 > > diff --git a/eclass/python-utils-r1.eclass b/eclass/python-utils-r1.eclass
18 > > index da76a755fb34..1eca0764a202 100644
19 > > --- a/eclass/python-utils-r1.eclass
20 > > +++ b/eclass/python-utils-r1.eclass
21 > > @@ -1165,8 +1165,7 @@ python_fix_shebang() {
22 > > [[ -d ${path} ]] && is_recursive=1
23 > >
24 > > while IFS= read -r -d '' f; do
25 > > - local shebang i
26 > > - local error= from=
27 > > + local shebang i= error= fix=
28 > >
29 > > # note: we can't ||die here since read will fail if file
30 > > # has no newline characters
31 > > @@ -1175,65 +1174,56 @@ python_fix_shebang() {
32 > > # First, check if it's shebang at all...
33 > > if [[ ${shebang} == '#!'* ]]; then
34 > > local split_shebang=()
35 > > - read -r -a split_shebang <<<${shebang} || die
36 > > + read -r -a split_shebang <<<${shebang#\#\!} || die
37 >
38 > Does '!' really need to be escaped?
39
40 Seemingly only in an interactive shell.
41
42 > >
43 > > # Match left-to-right in a loop, to avoid matching random
44 > > # repetitions like 'python2.7 python2'.
45 > > - for i in "${split_shebang[@]}"; do
46 > > - case "${i}" in
47 > > - *"${EPYTHON}")
48 > > + for i in $(seq 0 $((${#split_shebang[@]} - 1))); do
49 >
50 > for i in "${!split_shebang[@]}"; do
51
52 Interesting, didn't know about that.
53
54 > > + case "/${split_shebang[${i}]}" in
55 >
56 > case "/${split_shebang[i]}" in
57
58 ...or that.
59
60 > Also below.
61 >
62 > > + */${EPYTHON})
63 > > debug-print "${FUNCNAME}: in file ${f#${D%/}}"
64 > > debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
65 > >
66 > > # Nothing to do, move along.
67 > > any_correct=1
68 > > - from=${EPYTHON}
69 > > + continue 2
70 > > + ;;
71 > > + */python)
72 > > + fix=1
73 > > break
74 > > ;;
75 > > - *python|*python[23])
76 > > - debug-print "${FUNCNAME}: in file ${f#${D%/}}"
77 > > - debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
78 > > -
79 > > - if [[ ${i} == *python2 ]]; then
80 > > - from=python2
81 > > - if [[ ! ${force} ]]; then
82 > > - python_is_python3 "${EPYTHON}" && error=1
83 > > - fi
84 > > - elif [[ ${i} == *python3 ]]; then
85 > > - from=python3
86 > > - if [[ ! ${force} ]]; then
87 > > - python_is_python3 "${EPYTHON}" || error=1
88 > > - fi
89 > > - else
90 > > - from=python
91 > > + */python2)
92 > > + if [[ ! ${force} ]]; then
93 > > + python_is_python3 "${EPYTHON}" && error=1
94 > > fi
95 > > + fix=1
96 > > break
97 > > ;;
98 > > - *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789])
99 > > - # Explicit mismatch.
100 > > + */python3)
101 > > if [[ ! ${force} ]]; then
102 > > - error=1
103 > > - else
104 > > - case "${i}" in
105 > > - *python[23].[0123456789])
106 > > - from="python[23].[0123456789]";;
107 > > - *pypy)
108 > > - from="pypy";;
109 > > - *pypy3)
110 > > - from="pypy3";;
111 > > - *jython[23].[0123456789])
112 > > - from="jython[23].[0123456789]";;
113 > > - *)
114 > > - die "${FUNCNAME}: internal error in 2nd pattern match";;
115 > > - esac
116 > > + python_is_python3 "${EPYTHON}" || error=1
117 > > fi
118 > > + fix=1
119 > > + break
120 > > + ;;
121 > > + */python[2-3].[0-9]|*/pypy|*/pypy3|*/jython[2-3].[0-9])
122 > > + # Explicit mismatch.
123 > > + [[ ! ${force} ]] && error=1
124 > > + fix=1
125 > > break
126 > > ;;
127 > > esac
128 > > done
129 > > fi
130 > >
131 > > - if [[ ! ${error} && ! ${from} ]]; then
132 > > + if [[ ${fix} ]]; then
133 >
134 > Doesn't this mean errors from above code will be ignored? Diff makes it
135 > really hard to follow the logic here but I'm pretty sure you set both
136 > error=1 fix=1, then check for fix first.
137
138 It will still hit the error clause below regardless. The debug-print
139 lines were refactored from further up and they were printed even in
140 error cases. We could skip the split_shebang lines but it would just
141 make the code longer.
142
143 > > + debug-print "${FUNCNAME}: in file ${f#${D%/}}"
144 > > + debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
145 > > +
146 > > + split_shebang[${i}]=/${split_shebang[${i}]}
147 >
148 > A comment above this hack would be helpful.
149
150 Okay.
151
152 > > + split_shebang[${i}]=${split_shebang[${i}]%/*}
153 > > + split_shebang[${i}]=${split_shebang[${i}]#/}${split_shebang[${i}]:+/}${EPYTHON}
154 > > + elif [[ ! ${error} ]]; then
155 > > # Non-Python shebang. Allowed in recursive mode,
156 > > # disallowed when specifying file explicitly.
157 > > [[ ${is_recursive} ]] && continue
158 > > @@ -1245,13 +1235,7 @@ python_fix_shebang() {
159 > > fi
160 > >
161 > > if [[ ! ${error} ]]; then
162 > > - # We either want to match ${from} followed by space
163 > > - # or at end-of-string.
164 > > - if [[ ${shebang} == *${from}" "* ]]; then
165 > > - sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die
166 > > - else
167 > > - sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
168 > > - fi
169 > > + sed -i -e "1c#\!${split_shebang[*]}" "${f}" || die
170 > > any_fixed=1
171 > > else
172 > > eerror "The file has incompatible shebang:"
173 >
174 > I'll get to other patches later.
175 >
176
177
178 --
179 James Le Cuirot (chewi)
180 Gentoo Linux Developer