2411: Add build/check-copyright-notices.
[arvados.git] / build / check-copyright-notices
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 set -e
7
8 fix=false
9 while [[ "${@}" != "" ]]
10 do
11     arg=${1}
12     shift
13     case ${arg} in
14         --help)
15             cat <<EOF
16 Usage: $0 [--fix] [-- git-ls-args...]
17
18 Options:
19
20 --fix   Insert missing copyright notices where possible.
21
22 Git arguments:
23
24 Arguments after "--" are passed to \`git ls-files\`; this can be used to
25 restrict the set of files to check.
26
27 EOF
28             exit 2
29             ;;
30         --fix)
31             fix=true
32             ;;
33         --)
34             break
35             ;;
36         *)
37             echo >&2 "Unrecognized argument '${arg}'. Try $0 --help"
38             exit 2
39             ;;
40     esac
41 done
42
43 fixer() {
44     want="${want}" perl -pi~ -e 'if ($. == 1) { s{^(\#\!.*\n)?}{${1}$ENV{want}\n\n} }' "${1}"
45 }
46
47 IFS=$'\n' read -a ignores -r -d $'\000' <.licenseignore || true
48 result=0
49 git ls-files -z ${@} | \
50     while read -rd $'\000' fnm
51     do
52         grepAfter=2
53         grepBefore=0
54         cs=
55         cc=
56         ce=
57         fixer=
58         if [[ ! -f ${fnm} ]] || [[ -L ${fnm} ]] || [[ ! -s ${fnm} ]]
59         then
60             continue
61         fi
62
63         ignore=
64         for pattern in "${ignores[@]}"
65         do
66             if [[ ${fnm} == ${pattern} ]]
67             then
68                 ignore=1
69             fi
70         done
71         if [[ ${ignore} = 1 ]]; then continue; fi
72
73         case ${fnm} in
74             Makefile | */Makefile \
75                 | *.dockerfile | */Dockerfile.* | */Dockerfile | *.dockerignore \
76                 | */MANIFEST.in | */fuse.conf | */gitolite.rc \
77                 | *.pl | *.pm | *.PL \
78                 | *.rb | *.rb.example | *.rake | *.ru \
79                 | *.gemspec | */Gemfile | */Rakefile \
80                 | services/login-sync/bin/* \
81                 | sdk/cli/bin/* \
82                 | *.py \
83                 | sdk/python/bin/arv-* \
84                 | sdk/cwl/bin/* \
85                 | services/nodemanager/bin/* \
86                 | services/fuse/bin/* \
87                 | tools/crunchstat-summary/bin/* \
88                 | crunch_scripts/* \
89                 | *.yaml | *.yml | *.yml.example | *.cwl \
90                 | *.sh | *.service \
91                 | */run | */run-service | */restart-dns-server \
92                 | */nodemanager/doc/*.cfg \
93                 | */nginx.conf \
94                 | build/build.list)
95                 fixer=fixer
96                 cc="#"
97                 ;;
98             *.md)
99                 fixer=fixer
100                 cc="[//]: #"
101                 ;;
102             *.rst)
103                 fixer=fixer
104                 cc=".."
105                 ;;
106             *.erb)
107                 fixer=fixer
108                 cs="<%#"
109                 cc=""
110                 ce=" %>"
111                 ;;
112             *.liquid)
113                 fixer=fixer
114                 cs=$'{% comment %}\n'
115                 cc=
116                 ce=$'\n%{% endcomment %}'
117                 grepAfter=3
118                 grepBefore=1
119                 ;;
120             *.textile)
121                 fixer=fixer
122                 cs="###."
123                 cc="...."
124                 ce=
125                 ;;
126             *.css)
127                 fixer=fixer
128                 cs="/*"
129                 cc=""
130                 ce=" */"
131                 ;;
132             *.go | *.scss | *.java | *.js | *.coffee)
133                 fixer=fixer
134                 cc="//"
135                 ;;
136             *.sql)
137                 fixer=fixer
138                 cc="--"
139                 ;;
140             *.html | *.svg)
141                 fixer=fixer
142                 cs="<!-- "
143                 cc=
144                 ce=" -->"
145                 ;;
146             *)
147                 cc="#"
148                 hashbang=$(head -n1 ${fnm})
149                 if [[ ${hashbang} = "#!/bin/sh" ]] ||  [[ ${hashbang} = "#!/bin/bash" ]]
150                 then
151                     fixer=fixer
152                 fi
153                 ;;
154         esac
155         wantGPL="${cs:-${cc}} Copyright (C) The Arvados Authors. All rights reserved.
156 ${cc}
157 ${cc}${cc:+ }SPDX-License-Identifier: AGPL-3.0${ce}"
158         wantApache="${cs:-${cc}} Copyright (C) The Arvados Authors. All rights reserved.
159 ${cc}
160 ${cc}${cc:+ }SPDX-License-Identifier: Apache-2.0${ce}"
161         wantBYSA="${cs:-${cc}} Copyright (C) The Arvados Authors. All rights reserved.
162 ${cc}
163 ${cc}${cc:+ }SPDX-License-Identifier: CC-BY-SA-3.0${ce}"
164         found=$(head "$fnm" | egrep -A${grepAfter} -B${grepBefore} 'Copyright.*Arvados' || true)
165         case ${fnm} in
166             Makefile | build/* | lib/* | tools/* | apps/* | services/*)
167                 want=${wantGPL}
168                 ;;
169             crunch_scripts/* | backports/* | docker/* | sdk/*)
170                 want=${wantApache}
171                 ;;
172             README.md | doc/*)
173                 want=${wantBYSA}
174                 ;;
175             *)
176                 want=
177                 ;;
178         esac
179         case "$found" in
180             "$wantGPL")
181                 ;;
182             "$wantApache")
183                 ;;
184             "$wantBYSA")
185                 ;;
186             "")
187                 if [[ -z ${found} ]] && [[ -n ${want} ]] && [[ $fix = true ]] && [[ $fixer != "" ]]
188                 then
189                     ${fixer} ${fnm}
190                 else
191                     echo "missing copyright notice: $fnm"
192                     result=1
193                 fi
194                 ;;
195             *)
196                 echo "nonstandard copyright notice: $fnm '${found}'"
197                 result=1
198                 ;;
199         esac
200     done
201 exit $result