2018-02-15 09:02:44 +00:00
|
|
|
#!/bin/bash -eu
|
2018-01-10 12:38:28 +00:00
|
|
|
|
|
|
|
# If using normal root, avoid changing anything.
|
|
|
|
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2018-02-14 19:09:03 +00:00
|
|
|
exclude_files="${1:-}"
|
|
|
|
exclude_shebangs="${2:-}"
|
|
|
|
|
2018-01-10 12:38:28 +00:00
|
|
|
cd "$RPM_BUILD_ROOT"
|
|
|
|
|
|
|
|
trim() {
|
|
|
|
printf '%s' "$*"
|
|
|
|
}
|
|
|
|
|
|
|
|
fail=0
|
2018-02-15 09:02:44 +00:00
|
|
|
while IFS= read -r -d $'\0' f; do
|
|
|
|
file -N --mime-type "$f" | grep -q -P ".+(?=: text/)" || continue
|
2018-01-10 12:38:28 +00:00
|
|
|
|
2018-02-14 19:09:03 +00:00
|
|
|
# Remove the dot
|
|
|
|
path="${f#.}"
|
|
|
|
|
|
|
|
if [ -n "$exclude_files" ]; then
|
|
|
|
echo "$path" | grep -q -E "$exclude_files" && continue
|
|
|
|
fi
|
|
|
|
|
2018-01-10 12:38:28 +00:00
|
|
|
ts=$(stat -c %y "$f")
|
|
|
|
|
|
|
|
read orig_shebang < "$f" || :
|
|
|
|
shebang=$(trim $(echo "$orig_shebang" | grep -Po "#!\K.*" || echo))
|
2018-02-14 19:09:03 +00:00
|
|
|
if [ -n "$exclude_shebangs" ]; then
|
|
|
|
echo "$shebang" | grep -q -E "$exclude_shebangs" && continue
|
|
|
|
fi
|
|
|
|
|
2018-01-10 12:38:28 +00:00
|
|
|
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
|
2018-02-08 09:38:40 +00:00
|
|
|
elif [ -n "${shebang##/*}" ]; then
|
2018-01-10 12:38:28 +00:00
|
|
|
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:
|
2018-02-07 17:11:42 +00:00
|
|
|
# /whatsoever/env /whatever/foo → /whatever/foo
|
|
|
|
shebang=$(echo "$shebang" | sed -r -e 's@^(.+)/env /(.+)$@/\2@')
|
2018-01-10 12:38:28 +00:00
|
|
|
# /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"
|
2018-02-14 19:09:03 +00:00
|
|
|
echo >&2 "*** WARNING: mangling shebang in $path from $orig_shebang to #!$py_shebang. This will become an ERROR, fix it manually!"
|
2018-01-10 12:38:28 +00:00
|
|
|
elif [ "#!$shebang" != "$orig_shebang" ]; then
|
|
|
|
sed -i -e "1c #!$shebang" "$f"
|
2018-02-14 19:09:03 +00:00
|
|
|
echo "mangling shebang in $path from $orig_shebang to #!$shebang"
|
2018-01-10 12:38:28 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
touch -d "$ts" "$f"
|
2018-02-15 09:02:44 +00:00
|
|
|
done < <(find -executable -type f -print0)
|
2018-01-10 12:38:28 +00:00
|
|
|
|
|
|
|
exit $fail
|