From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id D5BE8138334 for ; Fri, 4 Jan 2019 06:02:51 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id CE2E6E0ABE; Fri, 4 Jan 2019 06:02:47 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 6DE57E0A68 for ; Fri, 4 Jan 2019 06:02:47 +0000 (UTC) Received: from pomiot (d202-252.icpnet.pl [109.173.202.252]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: mgorny) by smtp.gentoo.org (Postfix) with ESMTPSA id 1CA33335C39; Fri, 4 Jan 2019 06:02:44 +0000 (UTC) Message-ID: <1546581760.973.4.camel@gentoo.org> Subject: Re: [gentoo-dev] [PATCH 02/12] python-utils-r1.eclass: New python_fix_shebang approach From: =?UTF-8?Q?Micha=C5=82_G=C3=B3rny?= To: gentoo-dev@lists.gentoo.org Cc: James Le Cuirot Date: Fri, 04 Jan 2019 07:02:40 +0100 In-Reply-To: <20190103213924.22835-3-chewi@gentoo.org> References: <20190103213924.22835-1-chewi@gentoo.org> <20190103213924.22835-3-chewi@gentoo.org> Organization: Gentoo Content-Type: multipart/signed; micalg="pgp-sha512"; protocol="application/pgp-signature"; boundary="=-FKupU8XFKCcHNtx9eoEh" X-Mailer: Evolution 3.26.6 Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply Mime-Version: 1.0 X-Archives-Salt: 91cd0700-368e-4500-b098-4c4fe22f0ebc X-Archives-Hash: 4d8ba40c9f93c9ffbb4a78f3a7ddccc7 --=-FKupU8XFKCcHNtx9eoEh Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, 2019-01-03 at 21:39 +0000, James Le Cuirot wrote: > The previous approach would erroneously match foopython. The new > approach requires the match to start the string or be preceeded by a > slash, the only two cases we actually want. It does this with slightly > less code and allows the replacement of whole path strings that would > be problematic when passed to sed. This will be needed when > cross-compiling is addressed. >=20 > Signed-off-by: James Le Cuirot > --- > eclass/python-utils-r1.eclass | 78 ++++++++++++++--------------------- > 1 file changed, 31 insertions(+), 47 deletions(-) >=20 > diff --git a/eclass/python-utils-r1.eclass b/eclass/python-utils-r1.eclas= s > index da76a755fb34..1eca0764a202 100644 > --- a/eclass/python-utils-r1.eclass > +++ b/eclass/python-utils-r1.eclass > @@ -1165,8 +1165,7 @@ python_fix_shebang() { > [[ -d ${path} ]] && is_recursive=3D1 > =20 > while IFS=3D read -r -d '' f; do > - local shebang i > - local error=3D from=3D > + local shebang i=3D error=3D fix=3D > =20 > # note: we can't ||die here since read will fail if file > # has no newline characters > @@ -1175,65 +1174,56 @@ python_fix_shebang() { > # First, check if it's shebang at all... > if [[ ${shebang} =3D=3D '#!'* ]]; then > local split_shebang=3D() > - read -r -a split_shebang <<<${shebang} || die > + read -r -a split_shebang <<<${shebang#\#\!} || die Does '!' really need to be escaped? > =20 > # Match left-to-right in a loop, to avoid matching random > # repetitions like 'python2.7 python2'. > - for i in "${split_shebang[@]}"; do > - case "${i}" in > - *"${EPYTHON}") > + for i in $(seq 0 $((${#split_shebang[@]} - 1))); do for i in "${!split_shebang[@]}"; do > + case "/${split_shebang[${i}]}" in case "/${split_shebang[i]}" in Also below. > + */${EPYTHON}) > debug-print "${FUNCNAME}: in file ${f#${D%/}}" > debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}" > =20 > # Nothing to do, move along. > any_correct=3D1 > - from=3D${EPYTHON} > + continue 2 > + ;; > + */python) > + fix=3D1 > break > ;; > - *python|*python[23]) > - debug-print "${FUNCNAME}: in file ${f#${D%/}}" > - debug-print "${FUNCNAME}: rewriting shebang: ${shebang}" > - > - if [[ ${i} =3D=3D *python2 ]]; then > - from=3Dpython2 > - if [[ ! ${force} ]]; then > - python_is_python3 "${EPYTHON}" && error=3D1 > - fi > - elif [[ ${i} =3D=3D *python3 ]]; then > - from=3Dpython3 > - if [[ ! ${force} ]]; then > - python_is_python3 "${EPYTHON}" || error=3D1 > - fi > - else > - from=3Dpython > + */python2) > + if [[ ! ${force} ]]; then > + python_is_python3 "${EPYTHON}" && error=3D1 > fi > + fix=3D1 > break > ;; > - *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789]) > - # Explicit mismatch. > + */python3) > if [[ ! ${force} ]]; then > - error=3D1 > - else > - case "${i}" in > - *python[23].[0123456789]) > - from=3D"python[23].[0123456789]";; > - *pypy) > - from=3D"pypy";; > - *pypy3) > - from=3D"pypy3";; > - *jython[23].[0123456789]) > - from=3D"jython[23].[0123456789]";; > - *) > - die "${FUNCNAME}: internal error in 2nd pattern match";; > - esac > + python_is_python3 "${EPYTHON}" || error=3D1 > fi > + fix=3D1 > + break > + ;; > + */python[2-3].[0-9]|*/pypy|*/pypy3|*/jython[2-3].[0-9]) > + # Explicit mismatch. > + [[ ! ${force} ]] && error=3D1 > + fix=3D1 > break > ;; > esac > done > fi > =20 > - if [[ ! ${error} && ! ${from} ]]; then > + if [[ ${fix} ]]; then Doesn't this mean errors from above code will be ignored? Diff makes it really hard to follow the logic here but I'm pretty sure you set both error=3D1 fix=3D1, then check for fix first. > + debug-print "${FUNCNAME}: in file ${f#${D%/}}" > + debug-print "${FUNCNAME}: rewriting shebang: ${shebang}" > + > + split_shebang[${i}]=3D/${split_shebang[${i}]} A comment above this hack would be helpful. > + split_shebang[${i}]=3D${split_shebang[${i}]%/*} > + split_shebang[${i}]=3D${split_shebang[${i}]#/}${split_shebang[${i}]:= +/}${EPYTHON} > + elif [[ ! ${error} ]]; then > # Non-Python shebang. Allowed in recursive mode, > # disallowed when specifying file explicitly. > [[ ${is_recursive} ]] && continue > @@ -1245,13 +1235,7 @@ python_fix_shebang() { > fi > =20 > if [[ ! ${error} ]]; then > - # We either want to match ${from} followed by space > - # or at end-of-string. > - if [[ ${shebang} =3D=3D *${from}" "* ]]; then > - sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die > - else > - sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die > - fi > + sed -i -e "1c#\!${split_shebang[*]}" "${f}" || die > any_fixed=3D1 > else > eerror "The file has incompatible shebang:" I'll get to other patches later. --=20 Best regards, Micha=C5=82 G=C3=B3rny --=-FKupU8XFKCcHNtx9eoEh Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- iQKTBAABCgB9FiEEXr8g+Zb7PCLMb8pAur8dX/jIEQoFAlwu9wFfFIAAAAAALgAo aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDVF QkYyMEY5OTZGQjNDMjJDQzZGQ0E0MEJBQkYxRDVGRjhDODExMEEACgkQur8dX/jI EQrtzxAAr7pLcGSfndAF/53orkBTXSU5Ls6qSe82TZOBaFhhgeecUTZ3cGjiLB/m kxjepjnf8SI8+Zkw4YlP7FfTJcjkyoC8+MNwXXgoCslbB51zkJB766JcjmoH3Wuq zX7myEuHBG0f+uNCa40uWCMREUlYLvqEslGi1yd538qfte42pvqJEtdRqQHciddl +NB3QrX6G6LpO67LdQrK6+ti8RH6blQR4yB9GlkP4UrpvsdX3mGrBRWm1+ErWRNO 3TWRbMowh3iUfyuEQqjHwyTwtGJWK4FHmp7VdJ9ESe0vkqlIxX3satV4VC5TzV5v A90L614Dzbd70VlrhAM3PmCAC8vPEWGxe1QaNN3gENEN03w1m3Wr2AOUAWC8jsaR vPrc6A5wgi6582WPV9lcVyzfeZucMwtW92UuV/twkk7La78BCtTGn1kyaS9CIobJ KZu5LlMB1OvJkXLL8MO+HJikCcKaiTY0GN85bEcoJfteT22wpdP05Y9JfmyNVfkt p+Do5sWqiEbsjCa252LDjLi13dgIFZvGDBktYFM1EYXJwob3Qqc8SuhAqTqwVEN/ OD1YWbW39g34BxkXYeFJWAYRSRDIljs72BO5cMOlG9J9QKuSGjbjbhNnYvWS+EM7 i1hTfVfTjNYbvbSSKJitN7PmoA5+Z1AFFLL5xWuaw5uPfaFytgE= =Ck0v -----END PGP SIGNATURE----- --=-FKupU8XFKCcHNtx9eoEh--