Blame SOURCES/brp-mangle-shebangs

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