From b56a3b06509155ab3be6f23f0d96d9a1bc758d1f Mon Sep 17 00:00:00 2001 From: "Andrew J. Hesford" Date: Mon, 8 Jun 2020 22:36:59 -0400 Subject: [PATCH] xbps-src: improve python shebang rewrites --- .../pre-pkg/03-rewrite-python-shebang.sh | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/common/hooks/pre-pkg/03-rewrite-python-shebang.sh b/common/hooks/pre-pkg/03-rewrite-python-shebang.sh index 3450c3a3a8f..99e1205f142 100644 --- a/common/hooks/pre-pkg/03-rewrite-python-shebang.sh +++ b/common/hooks/pre-pkg/03-rewrite-python-shebang.sh @@ -13,15 +13,28 @@ hook() { fi if [ -n "$pyver" ]; then - shebang="#!/usr/bin/python${pyver%.*}" + default_shebang="#!/usr/bin/python${pyver%.*}" fi find "${PKGDESTDIR}" -type f -print0 | \ while IFS= read -r -d '' file; do [ ! -s "$file" ] && continue - [ -z "$(sed -n -E -e 2q -e '/^#!.*([[:space:]]|\/)python([0-9]\.[0-9])?([[:space:]]+|$)/p' "$file")" ] && continue - [ -n "$shebang" ] || msg_error "cannot convert shebang, set python_version\n" - echo " Shebang converted to '$shebang': ${file#$PKGDESTDIR}" + + pyinterp=$(sed -n -E -e 2q -e 's@^#!.*([[:space:]]|/)(python([0-9](\.[0-9]+)?)?)([[:space:]]+|$)@\2@p' "$file") + [ -z "$pyinterp" ] && continue + + pyver=${pyinterp#python} + if [ -n "$pyver" ]; then + shebang="#!/usr/bin/python${pyver%.*}" + else + shebang="$default_shebang" + fi + + basefile=${file#$PKGDESTDIR} + + [ -n "$shebang" ] || msg_error "python_version missing in template: unable to convert shebang in $basefile\n" + + echo " Shebang converted to '$shebang': $basefile" sed -i "1s@.*python.*@${shebang}@" -- "$file" done }