Merge branch 'master' into 13822-nm-delayed-daemon
[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~ - "${1}" <<'EOF'
45 BEGIN { undef $/ }
46 s{^((\#\!.*?\n|\n*---\n.*?\n\.\.\.\n|<\?xml.*?>\n)\n?)?}{${2}$ENV{want}\n\n}ms
47 EOF
48 }
49
50 IFS=$'\n' read -a ignores -r -d $'\000' <.licenseignore || true
51 result=0
52
53 coproc git ls-files -z ${@} </dev/null
54 while read -rd $'\000' fnm
55 do
56     grepAfter=2
57     grepBefore=0
58     cs=
59     cc=
60     ce=
61     fixer=
62     if [[ ! -f ${fnm} ]] || [[ -L ${fnm} ]] || [[ ! -s ${fnm} ]]
63     then
64         continue
65     fi
66
67     ignore=
68     for pattern in "${ignores[@]}"
69     do
70         if [[ ${fnm} == ${pattern} ]]
71         then
72             ignore=1
73         fi
74     done
75     if [[ ${ignore} = 1 ]]; then continue; fi
76
77     case ${fnm} in
78         Makefile | */Makefile \
79             | *.dockerfile | */Dockerfile.* | */Dockerfile | *.dockerignore \
80             | */MANIFEST.in | */fuse.conf | */gitolite.rc \
81             | *.pl | *.pm | *.PL \
82             | *.rb | *.rb.example | *.rake | *.ru \
83             | *.gemspec | */Gemfile | */Rakefile \
84             | services/login-sync/bin/* \
85             | sdk/cli/bin/* \
86             | *.py \
87             | sdk/python/bin/arv-* \
88             | sdk/cwl/bin/* \
89             | services/nodemanager/bin/* \
90             | services/fuse/bin/* \
91             | tools/crunchstat-summary/bin/* \
92             | crunch_scripts/* \
93             | *.yaml | *.yml | *.yml.example | *.cwl \
94             | *.sh | *.service \
95             | */run | */run-service | */restart-dns-server \
96             | */nodemanager/doc/*.cfg \
97             | */nodemanager/tests/fake*.cfg.template \
98             | */nginx.conf \
99             | build/build.list | *.R)
100             fixer=fixer
101             cc="#"
102             ;;
103         *.md)
104             fixer=fixer
105             cc="[//]: #"
106             ;;
107         *.rst)
108             fixer=fixer
109             cc=".."
110             ;;
111         *.erb)
112             fixer=fixer
113             cs="<%# "
114             cc=""
115             ce=" %>"
116             ;;
117         *.liquid)
118             fixer=fixer
119             cs=$'{% comment %}\n'
120             cc=""
121             ce=$'\n{% endcomment %}'
122             grepAfter=3
123             grepBefore=1
124             ;;
125         *.textile)
126             fixer=fixer
127             cs="###. "
128             cc="...."
129             ce=
130             ;;
131         *.css)
132             fixer=fixer
133             cs="/* "
134             cc=""
135             ce=" */"
136             ;;
137         *.coffee)
138             fixer=fixer
139             cs="### "
140             cc=""
141             ce=" ###"
142             ;;
143         *.go | *.scss | *.java | *.js)
144             fixer=fixer
145             cc="//"
146             ;;
147         *.sql)
148             fixer=fixer
149             cc="--"
150             ;;
151         *.html | *.svg)
152             fixer=fixer
153             cs="<!-- "
154             cc=""
155             ce=" -->"
156             ;;
157         *)
158             cc="#"
159             hashbang=$(head -n1 ${fnm})
160             if [[ ${hashbang} = "#!/bin/sh" ]] ||  [[ ${hashbang} = "#!/bin/bash" ]]
161             then
162                 fixer=fixer
163             fi
164             ;;
165     esac
166     wantGPL="${cs:-${cc}${cc:+ }}Copyright (C) The Arvados Authors. All rights reserved.
167 ${cc}
168 ${cc}${cc:+ }SPDX-License-Identifier: AGPL-3.0${ce}"
169     wantApache="${cs:-${cc}${cc:+ }}Copyright (C) The Arvados Authors. All rights reserved.
170 ${cc}
171 ${cc}${cc:+ }SPDX-License-Identifier: Apache-2.0${ce}"
172     wantBYSA="${cs:-${cc}${cc:+ }}Copyright (C) The Arvados Authors. All rights reserved.
173 ${cc}
174 ${cc}${cc:+ }SPDX-License-Identifier: CC-BY-SA-3.0${ce}"
175     wantBYSAmd="[comment]: # (Copyright © The Arvados Authors. All rights reserved.)
176 [comment]: # ()
177 [comment]: # (SPDX-License-Identifier: CC-BY-SA-3.0)"
178     found=$(head -n20 "$fnm" | egrep -A${grepAfter} -B${grepBefore} 'Copyright.*All rights reserved.' || true)
179     case ${fnm} in
180         Makefile | build/* | lib/* | tools/* | apps/* | services/* | sdk/cli/bin/crunch-job)
181             want=${wantGPL}
182             ;;
183         crunch_scripts/* | backports/* | docker/* | sdk/*)
184             want=${wantApache}
185             ;;
186         doc/*)
187             want=${wantBYSA}
188             ;;
189         README.md)
190             want=${wantBYSAmd}
191             ;;
192         *)
193             want=
194             ;;
195     esac
196     case "$found" in
197         "$wantGPL")
198             ;;
199         "$wantApache")
200             ;;
201         "$wantBYSA")
202             ;;
203         "$wantBYSAmd")
204             ;;
205         "")
206             if [[ -z ${found} ]] && [[ -n ${want} ]] && [[ $fix = true ]] && [[ $fixer != "" ]]
207             then
208                 ${fixer} ${fnm}
209             else
210                 echo "missing copyright notice: $fnm"
211                 result=1
212             fi
213             ;;
214         *)
215             echo "nonstandard copyright notice: $fnm '${found}'"
216             result=1
217             ;;
218     esac
219 done <&${COPROC[0]}
220 exit $result