Blame SOURCES/brp-mangle-shebangs

396caf
#!/bin/bash -eu
396caf
396caf
# If using normal root, avoid changing anything.
396caf
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
396caf
  exit 0
396caf
fi
396caf
396caf
exclude_files=""
396caf
exclude_files_from=""
396caf
exclude_shebangs=""
396caf
exclude_shebangs_from=""
396caf
396caf
usage() {
396caf
  local verbose=$1 && shift
396caf
  local outfile=$1 && shift
396caf
  local status=$1 && shift
396caf
396caf
  (
396caf
    echo 'usage: brp-mangle-shebangs [--files <regexp>] [--files-from <file>] [--shebangs <regexp>] [--shebangs-from <file>]'
396caf
    if [ "${verbose}" == "yes" ]; then
396caf
      echo '  --files: extended regexp of files to ignore'
396caf
      echo '  --files-from: file containing a list of extended regexps of files to ignore'
396caf
      echo '  --shebangs: extended regexp of shebangs to ignore'
396caf
      echo '  --shebangs-from: file containing a list of extended regexps of shebangs to ignore'
396caf
    fi
396caf
  ) >>${outfile}
396caf
  exit ${status}
396caf
}
396caf
396caf
while [ $# -gt 0 ] ; do
396caf
  case "$1" in
396caf
    --files)
396caf
      exclude_files="${2}"
396caf
      shift
396caf
      ;;
396caf
    --files=*)
396caf
      exclude_files="${1##--files=}"
396caf
      ;;
396caf
    --files-from)
396caf
      exclude_files_from="${2}"
396caf
      shift
396caf
      ;;
396caf
    --files-from=*)
396caf
      exclude_files_from="${1##--files-from=}"
396caf
      ;;
396caf
    --shebangs)
396caf
      exclude_shebangs="${2}"
396caf
      shift
396caf
      ;;
396caf
    --shebangs=*)
396caf
      exclude_shebangs="${1##--shebangs=}"
396caf
      ;;
396caf
    --shebangs-from)
396caf
      exclude_shebangs_from="${2}"
396caf
      shift
396caf
      ;;
396caf
    --shebangs-from=*)
396caf
      exclude_shebangs_from="${1##--shebangs-from=}"
396caf
      ;;
396caf
    --help|--usage|"-?"|-h)
396caf
      usage yes /dev/stdout 0
396caf
      ;;
396caf
    *)
396caf
      echo "Unknown option \"${1}\"" 1>&2
396caf
      usage no /dev/stderr 1
396caf
      ;;
396caf
  esac
396caf
  shift
396caf
done
396caf
396caf
cd "$RPM_BUILD_ROOT"
396caf
396caf
# Large packages such as kernel can have thousands of executable files.
396caf
# We take care to not fork/exec thousands of "file"s and "grep"s,
396caf
# but run just two of them.
396caf
# (Take care to exclude filenames which would mangle "file" output).
396caf
find -executable -type f ! -path '*:*' ! -path $'*\n*' \
396caf
| file -N --mime-type -f - \
75286c
| grep -P ".+(?=: (text/|application/javascript))" \
396caf
| {
396caf
fail=0
396caf
while IFS= read -r line; do
396caf
  f=${line%%:*}
396caf
396caf
  # Remove the dot
396caf
  path="${f#.}"
396caf
396caf
  if [ -n "$exclude_files" ]; then
396caf
    echo "$path" | grep -q -E "$exclude_files" && continue
396caf
  fi
396caf
  if [ -n "$exclude_files_from" ]; then
396caf
    echo "$path" | grep -q -E -f "$exclude_files_from" && continue
396caf
  fi
396caf
396caf
396caf
  if ! read shebang_line < "$f"; then
396caf
    echo >&2 "*** WARNING: Cannot read the first line from $f, removing executable bit"
396caf
    ts=$(stat -c %y "$f")
396caf
    chmod -x "$f"
396caf
    touch -d "$ts" "$f"
396caf
    continue
396caf
  fi
396caf
396caf
  orig_shebang="${shebang_line#\#!}"
396caf
  if [ "$orig_shebang" = "$shebang_line" ]; then
396caf
    echo >&2 "*** WARNING: $f is executable but has no shebang, removing executable bit"
396caf
    ts=$(stat -c %y "$f")
396caf
    chmod -x "$f"
396caf
    touch -d "$ts" "$f"
396caf
    continue
396caf
  fi
396caf
396caf
  # Trim spaces
396caf
  while shebang="${orig_shebang//  / }"; [ "$shebang" != "$orig_shebang" ]; do
396caf
    orig_shebang="$shebang"
396caf
  done
396caf
  # Treat "#! /path/to " as "#!/path/to"
396caf
  orig_shebang="${orig_shebang# }"
396caf
396caf
  shebang="$orig_shebang"
396caf
396caf
  if [ -z "$shebang" ]; then
396caf
    echo >&2 "*** WARNING: $f is executable but has empty shebang, removing executable bit"
396caf
    ts=$(stat -c %y "$f")
396caf
    chmod -x "$f"
396caf
    touch -d "$ts" "$f"
396caf
    continue
396caf
  fi
396caf
  if [ -n "${shebang##/*}" ]; then
396caf
    echo >&2 "*** ERROR: $f has shebang which doesn't start with '/' ($shebang)"
396caf
    fail=1
396caf
    continue
396caf
  fi
396caf
396caf
  if ! { echo "$shebang" | grep -q -P "^/(?:usr/)?(?:bin|sbin)/"; }; then
396caf
    continue
396caf
  fi
396caf
396caf
  # Replace "special" env shebang:
396caf
  # /whatsoever/env /whatever/foo → /whatever/foo
396caf
  shebang=$(echo "$shebang" | sed -r -e 's@^(.+)/env /(.+)$@/\2@')
396caf
  # /whatsoever/env foo → /whatsoever/foo
396caf
  shebang=$(echo "$shebang" | sed -r -e 's@^(.+/)env (.+)$@\1\2@')
396caf
396caf
  # If the shebang now starts with /bin, change it to /usr/bin
396caf
  # https://bugzilla.redhat.com/show_bug.cgi?id=1581757
396caf
  shebang=$(echo "$shebang" | sed -r -e 's@^/bin/@/usr/bin/@')
396caf
396caf
  # Replace ambiguous python with python2
396caf
  py_shebang=$(echo "$shebang" | sed -r -e 's@/usr/bin/python(\s|$)@/usr/bin/python2\1@')
396caf
396caf
  if [ "$shebang" != "$py_shebang" ]; then
396caf
    echo >&2 "*** ERROR: ambiguous python shebang in $path: #!$orig_shebang. Change it to python3 (or python2) explicitly."
396caf
    fail=1
396caf
  elif [ "#!$shebang" != "#!$orig_shebang" ]; then
396caf
    echo "mangling shebang in $path from $orig_shebang to #!$shebang"
396caf
    ts=$(stat -c %y "$f")
396caf
    sed -i -e "1c #!$shebang" "$f"
396caf
    touch -d "$ts" "$f"
396caf
  fi
396caf
396caf
done
396caf
396caf
exit $fail
396caf
}