void-packages/common/hooks/pre-pkg/03-rewrite-python-shebang.sh
Đoàn Trần Công Danh f130fb058a hook: python-shebang: check for shebang in the first line only
- Grepping whole files is inefficient
- git-instaweb (in git package) has the code to generate python file in
  a here doc in the middle of its code, old hook generates false
  positive with this package
2019-12-28 18:02:39 +01:00

29 lines
850 B
Bash

# This hook executes the following tasks:
# - rewrites python shebangs with the corresponding python version
hook() {
local pyver= shebang= off=
: ${pyver:=2}
if [ -d ${PKGDESTDIR}/usr/lib/python* ]; then
pyver="$(find ${PKGDESTDIR}/usr/lib/python* -prune -type d | grep -o '[[:digit:]]\.[[:digit:]]$')"
fi
if [ -n "$pycompile_version" ]; then
pyver="$pycompile_version"
fi
if [ "$python_version" = "3" ]; then
pyver="$python_version"
fi
shebang="#!/usr/bin/python${pyver%.*}"
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
echo " Shebang converted to '$shebang': ${file#$PKGDESTDIR}"
sed -i "1s@.*python.*@${shebang}@" -- "$file"
done
}