|
|
a008c1 |
#!/bin/bash
|
|
|
a008c1 |
|
|
|
a008c1 |
# If using normal root, avoid changing anything.
|
|
|
a008c1 |
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
|
|
|
a008c1 |
exit 0
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
|
|
|
a008c1 |
exclude_files=""
|
|
|
a008c1 |
exclude_files_from=""
|
|
|
a008c1 |
exclude_shebangs=""
|
|
|
a008c1 |
exclude_shebangs_from=""
|
|
|
a008c1 |
|
|
|
a008c1 |
usage() {
|
|
|
a008c1 |
local verbose=$1 && shift
|
|
|
a008c1 |
local outfile=$1 && shift
|
|
|
a008c1 |
local status=$1 && shift
|
|
|
a008c1 |
|
|
|
a008c1 |
(
|
|
|
a008c1 |
echo 'usage: brp-mangle-shebangs [--files <regexp>] [--files-from <file>] [--shebangs <regexp>] [--shebangs-from <file>]'
|
|
|
a008c1 |
if [ "${verbose}" == "yes" ]; then
|
|
|
a008c1 |
echo ' --files: extended regexp of files to ignore'
|
|
|
a008c1 |
echo ' --files-from: file containing a list of extended regexps of files to ignore'
|
|
|
a008c1 |
echo ' --shebangs: extended regexp of shebangs to ignore'
|
|
|
a008c1 |
echo ' --shebangs-from: file containing a list of extended regexps of shebangs to ignore'
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
) >>${outfile}
|
|
|
a008c1 |
exit ${status}
|
|
|
a008c1 |
}
|
|
|
a008c1 |
|
|
|
a008c1 |
while [ $# -gt 0 ] ; do
|
|
|
a008c1 |
case "$1" in
|
|
|
a008c1 |
--files)
|
|
|
a008c1 |
exclude_files="${2}"
|
|
|
a008c1 |
shift
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
--files=*)
|
|
|
a008c1 |
exclude_files="${1##--files=}"
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
--files-from)
|
|
|
a008c1 |
exclude_files_from="${2}"
|
|
|
a008c1 |
shift
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
--files-from=*)
|
|
|
a008c1 |
exclude_files_from="${1##--files-from=}"
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
--shebangs)
|
|
|
a008c1 |
exclude_shebangs="${2}"
|
|
|
a008c1 |
shift
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
--shebangs=*)
|
|
|
a008c1 |
exclude_shebangs="${1##--shebangs=}"
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
--shebangs-from)
|
|
|
a008c1 |
exclude_shebangs_from="${2}"
|
|
|
a008c1 |
shift
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
--shebangs-from=*)
|
|
|
a008c1 |
exclude_shebangs_from="${1##--shebangs-from=}"
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
--help|--usage|"-?"|-h)
|
|
|
a008c1 |
usage yes /dev/stdout 0
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
*)
|
|
|
a008c1 |
echo "Unknown option \"${1}\"" 1>&2
|
|
|
a008c1 |
usage no /dev/stderr 1
|
|
|
a008c1 |
;;
|
|
|
a008c1 |
esac
|
|
|
a008c1 |
shift
|
|
|
a008c1 |
done
|
|
|
a008c1 |
|
|
|
a008c1 |
cd "$RPM_BUILD_ROOT"
|
|
|
a008c1 |
|
|
|
a008c1 |
trim() {
|
|
|
a008c1 |
printf '%s' "$*"
|
|
|
a008c1 |
}
|
|
|
a008c1 |
|
|
|
a008c1 |
fail=0
|
|
|
a008c1 |
while IFS= read -r -d $'\0' f; do
|
|
|
a008c1 |
file -N --mime-type "$f" | grep -q -P ".+(?=: text/)" || continue
|
|
|
a008c1 |
|
|
|
a008c1 |
# Remove the dot
|
|
|
a008c1 |
path="${f#.}"
|
|
|
a008c1 |
|
|
|
a008c1 |
if [ -n "$exclude_files" ]; then
|
|
|
a008c1 |
echo "$path" | grep -q -E "$exclude_files" && continue
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
if [ -n "$exclude_files_from" ]; then
|
|
|
a008c1 |
echo "$path" | grep -q -E -f "$exclude_files_from" && continue
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
|
|
|
a008c1 |
ts=$(stat -c %y "$f")
|
|
|
a008c1 |
|
|
|
a008c1 |
read shebang_line < "$f" || :
|
|
|
a008c1 |
orig_shebang=$(trim $(echo "$shebang_line" | grep -Po "#!\K.*" || echo))
|
|
|
a008c1 |
shebang="$orig_shebang"
|
|
|
a008c1 |
if [ -n "$exclude_shebangs" ]; then
|
|
|
a008c1 |
echo "$shebang" | grep -q -E "$exclude_shebangs" && continue
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
if [ -n "$exclude_shebangs_from" ]; then
|
|
|
a008c1 |
echo "$shebang" | grep -q -E -f "$exclude_shebangs_from" && continue
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
|
|
|
a008c1 |
if [ -z "$shebang" ]; then
|
|
|
a008c1 |
echo >&2 "*** WARNING: $f is executable but has empty or no shebang, removing executable bit"
|
|
|
a008c1 |
chmod -x "$f"
|
|
|
a008c1 |
touch -d "$ts" "$f"
|
|
|
a008c1 |
continue
|
|
|
a008c1 |
elif [ -n "${shebang##/*}" ]; then
|
|
|
a008c1 |
echo >&2 "*** ERROR: $f has shebang which doesn't start with '/' ($shebang)"
|
|
|
a008c1 |
fail=1
|
|
|
a008c1 |
continue
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
|
|
|
a008c1 |
if ! { echo "$shebang" | grep -q -P "^/(?:usr/)?(?:bin|sbin)/"; }; then
|
|
|
a008c1 |
continue
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
|
|
|
a008c1 |
# Replace "special" env shebang:
|
|
|
a008c1 |
# /whatsoever/env /whatever/foo → /whatever/foo
|
|
|
a008c1 |
shebang=$(echo "$shebang" | sed -r -e 's@^(.+)/env /(.+)$@/\2@')
|
|
|
a008c1 |
# /whatsoever/env foo → /whatsoever/foo
|
|
|
a008c1 |
shebang=$(echo "$shebang" | sed -r -e 's@^(.+/)env (.+)$@\1\2@')
|
|
|
a008c1 |
|
|
|
a008c1 |
# Replace python3 with the desired Python 3 shebang,
|
|
|
a008c1 |
# if passed as an non-empty environment variable PYTHON3
|
|
|
a008c1 |
if [ -n "${PYTHON3:+x}" ]; then
|
|
|
a008c1 |
shebang=$(echo "$shebang" | sed -r -e "s@/usr/bin/python3(\s|$)@${PYTHON3}\1@")
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
|
|
|
a008c1 |
# Replace ambiguous python with python2
|
|
|
a008c1 |
py_shebang=$(echo "$shebang" | sed -r -e 's@/usr/bin/python(\s|$)@/usr/bin/python2\1@')
|
|
|
a008c1 |
|
|
|
a008c1 |
if [ "$shebang" != "$py_shebang" ]; then
|
|
|
a008c1 |
echo >&2 "*** ERROR: ambiguous python shebang in $path: #!$orig_shebang. Change it to python3 (or python2) explicitly."
|
|
|
a008c1 |
fail=1
|
|
|
a008c1 |
elif [ "#!$shebang" != "#!$orig_shebang" ]; then
|
|
|
a008c1 |
sed -i -e "1c #!$shebang" "$f"
|
|
|
a008c1 |
echo "mangling shebang in $path from $orig_shebang to #!$shebang"
|
|
|
a008c1 |
fi
|
|
|
a008c1 |
|
|
|
a008c1 |
touch -d "$ts" "$f"
|
|
|
a008c1 |
done < <(find -executable -type f -print0)
|
|
|
a008c1 |
|
|
|
a008c1 |
exit $fail
|