redhat-rpm-config/brp-mangle-shebangs
Igor Gnatenko 51f1c66748
brp-mangle-shebangs: Simplify/Fix check for shebang starting with "/"
Reported-by: Jun Aruga <jaruga@redhat.com>
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1541057
Signed-off-by: Igor Gnatenko <ignatenkobrain@fedoraproject.org>
2018-02-08 10:38:45 +01:00

57 lines
1.6 KiB
Bash
Executable File

#!/bin/sh -eu
# If using normal root, avoid changing anything.
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
exit 0
fi
cd "$RPM_BUILD_ROOT"
trim() {
printf '%s' "$*"
}
fail=0
for f in $(find -executable -type f | xargs --no-run-if-empty file -N --mime-type | grep -Po "^\K.+(?=: text/)"); do
ts=$(stat -c %y "$f")
read orig_shebang < "$f" || :
shebang=$(trim $(echo "$orig_shebang" | grep -Po "#!\K.*" || echo))
if [ -z "$shebang" ]; then
echo >&2 "*** WARNING: $f is executable but has empty or no shebang, removing executable bit"
chmod -x "$f"
touch -d "$ts" "$f"
continue
elif [ -n "${shebang##/*}" ]; then
echo >&2 "*** ERROR: $f has shebang which doesn't start with '/' ($shebang)"
fail=1
continue
fi
if ! { echo "$shebang" | grep -q -P "^/(?:usr/)?(?:bin|sbin)/"; }; then
continue
fi
# Replace "special" env shebang:
# /whatsoever/env /whatever/foo → /whatever/foo
shebang=$(echo "$shebang" | sed -r -e 's@^(.+)/env /(.+)$@/\2@')
# /whatsoever/env foo → /whatsoever/foo
shebang=$(echo "$shebang" | sed -r -e 's@^(.+/)env (.+)$@\1\2@')
# Replace ambiguous python with python2
py_shebang=$(echo "$shebang" | sed -r -e 's@/usr/bin/python(\s|$)@/usr/bin/python2\1@')
if [ "$shebang" != "$py_shebang" ]; then
sed -i -e "1c #!$py_shebang" "$f"
echo >&2 "*** WARNING: mangling shebang in $f from $orig_shebang to #!$py_shebang. This will become an ERROR, fix it manually!"
elif [ "#!$shebang" != "$orig_shebang" ]; then
sed -i -e "1c #!$shebang" "$f"
echo "mangling shebang in $f from $orig_shebang to #!$shebang"
fi
touch -d "$ts" "$f"
done
exit $fail