aboutsummaryrefslogtreecommitdiff
path: root/common/hooks/pre-pkg/03-rewrite-python-shebang.sh
blob: e5fb215744160785148ce058cdee2577ca0ab6ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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
		pycompile_version="$(find ${PKGDESTDIR}/usr/lib/python* -type d | grep -o '[[:digit:]]\.[[:digit:]]$')"
	fi

	if [ -n "$pycompile_version" ]; then
		pyver="$pycompile_version"
	fi

	shebang="#!/usr/bin/python${pyver%.*}"
	find ${PKGDESTDIR} -type f -print0 | \
		xargs -0 grep -H -b -m 1 "^#!.*\([[:space:]]\|/\)python\([[:space:]]*\|$\)" -- | while IFS=: read -r f off _; do
		[ -z "$off" ] &&  continue
		echo "   Shebang converted to '$shebang': ${f#$PKGDESTDIR}"
		sed -i "1s@.*python.*@${shebang}@" -- "$f"
	done
}