Debian Codegraph @

tools/check-license

raw symbols SCIP
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 # Pretty fancy method to get reliable the absolute path of a shell
5 # script, *even if it is sourced*. Credits go to GreenFox on
6 # stackoverflow: http://stackoverflow.com/a/12197518/194894
7 pushd . > /dev/null
8 SCRIPTDIR="${BASH_SOURCE[0]}";
9 while([ -h "${SCRIPTDIR}" ]); do
10 cd "`dirname "${SCRIPTDIR}"`"
11 SCRIPTDIR="$(readlink "`basename "${SCRIPTDIR}"`")";
12 done
13 cd "`dirname "${SCRIPTDIR}"`" > /dev/null
14 SCRIPTDIR="`pwd`";
15 popd > /dev/null
16
17 while getopts :d OPT; do
18 case $OPT in
19 d)
20 set -x
21 ;;
22 *)
23 echo "usage: ${0##*/} [-d]"
24 exit 2
25 esac
26 done
27 shift $(( OPTIND - 1 ))
28 OPTIND=1
29
30 export ROOTDIR
31 ROOTDIR=$(readlink -f "${SCRIPTDIR}/..")
32
33 export CHECKED_FILES_FILE
34 CHECKED_FILES_FILE=$(mktemp)
35 trap 'rm "${CHECKED_FILES_FILE}"' EXIT
36
37 # Safely call bash function from xargs
38 # https://stackoverflow.com/questions/11003418/calling-shell-functions-with-xargs/11003457#11003457
39 check_license() {
40 local license_identifier="# SPDX-License-Identifier: GPL-3.0-or-later"
41 local sed_cmd="{N; /^${license_identifier}\n/!{q1}}"
42
43 local full_path="${ROOTDIR}/${1}"
44 if ! head --lines=3 "${full_path}" |\
45 sed --regexp-extended --quiet "${sed_cmd}" >/dev/null
46 then
47 echo "$1": missing or invalid license header
48 return 1
49 fi
50
51 echo -e "${1}\0" >> "${CHECKED_FILES_FILE}"
52 }
53 export -f check_license
54
55
56 MAX_PROCS=$(nproc)
57
58 cd "${ROOTDIR}"
59 find . \( -path '*/\.*' -o -path "./build*" -o -path "./bin/*" \) -prune -o \
60 -type f -regextype posix-extended -regex '.*\.(py)' -print0 |\
61 xargs --null --max-args=1 --max-procs="${MAX_PROCS}" -I {} \
62 bash -c 'check_license "$@"' _ "{}"
63
64 FILE_COUNT=$(<"${CHECKED_FILES_FILE}" tr -cd '\0' | wc -c)
65 echo "Checked ${FILE_COUNT} files for license headers"
66