Gentoo Archives: gentoo-dev

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

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies