-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-copyright-notice
executable file
·357 lines (301 loc) · 10.3 KB
/
add-copyright-notice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
# *** MOCS-COPYRIGHT-NOTICE-BEGIN ***
#
# This copyright notice is auto-generated by ./add-copyright-notice.
# Additional copyright notices must be added below the last line of this notice.
#
# MoCS (https://lewis.cs.uni-saarland.de/tools/mocs/): "add-copyright-notice".
# The content of this file is copyright of Saarland University -
# Copyright (C) 2009 Saarland University, Reactive Systems Group, Lars Kuhtz.
#
# This file is part of MoCS (https://lewis.cs.uni-saarland.de/tools/mocs/).
#
# License: three-clause BSD style license.
# The license text can be found in the file LICENSE.
#
# *** MOCS-COPYRIGHT-NOTICE-END ***
# TODO: Move comment-syntax definitions into Configuration section
# Usage: See usagestring variable defined below.
#
# Check the pattern below in order to make sure that they match
# the files in your project.
#
# WARNING WARNING WARNING:
# make a backup first (or better: run on a clean working copy).
# Things _will not_ work on the first try!
# NOTE: If you edit this file make sure that the string "copy-right" without
# the hyphen does not occur neither literaly nor with any character converted
# to upper case. Use the variables $COPYR or $CCOPYR instead:
COPYR="copy""right"
CCOPYR="Copy""right"
################################################################################
# Configuration Section
################################################################################
# General Configuration Settings
skip_unknown=true
# please fill out or specify on the command line
tool=MoCS
url=https://lewis.cs.uni-saarland.de/tools/mocs/
year=2009
# copyrholder=
copyrholder="Saarland University"
# holderaddr=
holderaddr="Reactive Systems Group, Lars Kuhtz"
# choose a license by uncommenting one of the following lines
# license="binaryrestricted"
# license="gpl3"
license="bsd3"
# license="bsd2"
################################################################################
# Check dependencies
declare -A testarray || {
echo "ERROR: in order to run this program you need a version of Bash that supports associative arrays (e.g. Bash version 4.0)." >&2 ;
exit 1;
}
[ `hg version | head -n 1 | sed -n -e 's/^.*(version \([0-9]\{1,2\}\).*$/\1/p'` -ge '2' ] || {
echo "ERROR: in order to run this program you need mercurial (hg) of version at least 2.0." >&2 ;
exit 1;
}
sed --version 2>&1 | head -n 1 | grep -q '^GNU' || {
echo "Error: in order to run this program you need the GNU version of sed." >&2 ;
exit 1;
}
################################################################################
# Patterns (edit according to the needs of your project)
# Blacklist for files that do not deserve a copy-right notice
BL='(^doxyconfig$)|(docbooksrc/.*\.(pl|dsl|xsl|dtd|cfg)$)|(test/.*/output/.*$)|(^.hgempty$)|(^\.hgsigs$)|(^\.hgtags)|(^dist/.*)|(TODO)|(LICENSE)|(^results/.*)'
# Files that contain the term copy-right (without hyphen, either upper or lower case) and have not a third party copy-right.
THIRDPARTYBL='(^.*\.cabal$)'
# patterns to match on file name
CC='(\.cxx|\.txx|\.hxx|\.h|\.c|\.c0|\.hpp|\.cc|\.tcc|\.hh)$'
TEX='(\.tex)$'
SHELL='((\.sh|\.pl)$)|(/?Makefile)'
XML='(\.xml|\.xsl|\.html|\.xsd|\.docbook)$'
PYTHON='(\.py)$'
VD='(\.vd|\.abs|\.cfg)$'
LTL='(\.ltl|\.tltl)$'
DSL='\.dsl$'
PRED='\.pred$'
TXT='(\.txt$)|(test/.*/.*\.log$)'
HS='(\.hs)$'
VHDL='(\.vhdl)$'
CABAL='(\.cabal)$'
CSS='(\.css)$'
# patterns to match on result of file -b
F_SHELL='^Bourne.*shell'
F_CC='^ASCII C program text'
F_BINARY='^ELF.* executable'
# explicit mapping for files that do not match a pattern
declare -A extra
extra=(
[.hgignore]='SHELL'
[.config/functions]='SHELL'
[add-$COPYR-notice]='SHELL'
[configure]='SHELL'
[config.make]='SHELL'
[README]='TXT'
[INSTALL]='TXT'
[TODO]='TXT'
[formulas/12-formulas.txt]='LTL'
[Setup.lhs]='HS'
)
################################################################################
# End of Configuration Section
################################################################################
# Main Loop
# command line takes precedence
# FIXME use options here.
# Provide the possiblity to specify only some of the parameters.
if [ $# -ge 3 ] ; then
tool=$1; shift
url=$1; shift
year=$1; shift
fi
usagestring="Usage: $0 [<tool name> <tool url> <year>] [<file>]"
# check for correct number of arguments (0, 1, 3, or 4)
if [[ $# -gt 1 ]] ; then
echo "ERROR: illegal number of parameters"
echo "$usagestring"
exit 1
fi
# check if everything we need was specified
if [[ -z "$tool" || -z "$url" || -z "$year" ]] ; then
echo "ERROR: please specify <tool name> <url> and <year>"
echo " either by editing $0 or on the command line."
echo "$usagestring"
exit 1
fi
# we need to prevent word-spliting to be save on file names with spaces.
IFS='
'
# Get the list of files to work on (newline separated array!)
if [ -n "$1" ] ; then
echo "Run on single file: $1"
files=( $1 )
else
echo "Run on all project files (listed by 'hg manifest')."
files=( `hg manifest` )
echo "Number of files: ${#files[@]}."
fi
# Define some invariant variables
function upper() { echo $@ | tr [a-z] [A-Z]; }
utool=`upper $tool`
NOTEB="$utool-COPY""RIGHT-NOTICE""-BEGIN" # split it in order to avoid matching when running on it self
NOTEE="$utool-COPY""RIGHT-NOTICE""-END"
# Loop on files
for file in "${files[@]}" ; do
################################################################################
# sanity checkes for file
if [ ! -f "$file" ] ; then
echo "ERROR: $file is not a regular file"
continue
fi
if [ ! -w "$file" ] ; then
echo "ERROR: $file can not be written"
continue
fi
################################################################################
# Blacklisting and detection of 3. party copy-right
# Check for 3. party copy-right notice
# FIXME: we definitely want something more sofisticated here
if [[ ! "$file" =~ $THIRDPARTYBL ]] && grep -q -i -e "$COPYR" "$file" && ! grep -q -i -e "$NOTEB" "$file" ; then
echo "$file: third party $COPYR"
continue
fi
# check blacklist
if [[ "$file" =~ $BL ]] ; then
echo "$file: blacklisted"
continue
fi
################################################################################
# detect comment style
# give file a try
type=`file -b "$file"`
# techincal stuff
function check()
{
p=${!1}
qvar="F_$1"
q=${!qvar}
[[ "${extra[$file]}" == "$1" || -z "${extra[$file]}" && ( -n "$p" && "$file" =~ $p ) || ( -n "$q" && "$type" =~ $q ) ]]
}
CS=''
C=''
CE=''
function use()
{
echo "$file: type $1"
CS=$2
C=$3
CE=$4
}
# comment syntax for the file types: comment start (CC), comment line start (C), comment end (CE)
if check 'CC' ; then use 'CC' '/* ' ' * ' '*/';
elif check 'CSS' ; then use 'CSS' '/* ' ' * ' '*/';
elif check 'TEX' ; then use 'TEX' '% ' '% ' '';
elif check 'SHELL' ; then use 'SHELL' '# ' '# ' '';
elif check 'XML' ; then use 'XML' '<!-- ' '' ' -->';
elif check 'PYTHON' ; then use 'PYTHON' '# ' '# ' '';
elif check 'VD' ; then use 'VD' '// ' '// ' '';
elif check 'PRED' ; then use 'PRED' '// ' '// ' '';
elif check 'DSL' ; then use 'DSL' '; ' '; ' '';
elif check 'TXT' ; then use 'TXT' '' '' '';
elif check 'HS' ; then use 'HS' '{- ' ' - ' '-}';
elif check 'LTL' ; then use 'LTL' '-- ' '-- ' '';
elif check 'VHDL' ; then use 'VHDL' '-- ' '-- ' '';
elif check 'CABAL' ; then use 'CABAL' '-- ' '-- ' '';
elif check 'BINARY'; then
echo "$file: type binary"
continue
else
echo "$file: type unknown"
[ -n "$skip_unknown" ] && continue
CS=''; C=''; CE=''
fi
################################################################################
# copy-right notice
# the copy-right notice formated to be used with sed (binaryrestricted version)
restrictedtext="\
${CS}*** $NOTEB ***\\
${C}\\
${C}This $COPYR notice is auto-generated by $0.\\
${C}Additional $COPYR notices must be added below the last line of this notice.\\
${C}\\
${C}$tool ($url): \\\"$file\\\".\\
${C}The content of this file is $COPYR of $copyrholder -\\
${C}$CCOPYR (C) $year $copyrholder, $holderaddr.\\
${C}All rights reserved.\\
${C}\\
${C}License information can be found in the file LICENSE.\\
${C}\\
${C}*** $NOTEE *** $CE\\
"
# the copy-right notice formated to be used with sed (BSD2 version)
bsd2text="\
${CS}*** $NOTEB ***\\
${C}\\
${C}This $COPYR notice is auto-generated by $0.\\
${C}Additional $COPYR notices must be added below the last line of this notice.\\
${C}\\
${C}$tool ($url): \\\"$file\\\".\\
${C}The content of this file is $COPYR of $copyrholder -\\
${C}$CCOPYR (C) $year $copyrholder, $holderaddr.\\
${C}\\
${C}This file is part of $tool ($url).\\
${C}\\
${C}License: two-clause BSD style license.\\
${C}The license text can be found in the file LICENSE.\\
${C}\\
${C}*** $NOTEE *** $CE\\
"
# the copy-right notice formated to be used with sed (BSD3 version)
bsd3text="\
${CS}*** $NOTEB ***\\
${C}\\
${C}This $COPYR notice is auto-generated by $0.\\
${C}Additional $COPYR notices must be added below the last line of this notice.\\
${C}\\
${C}$tool ($url): \\\"$file\\\".\\
${C}The content of this file is $COPYR of $copyrholder -\\
${C}$CCOPYR (C) $year $copyrholder, $holderaddr.\\
${C}\\
${C}This file is part of $tool ($url).\\
${C}\\
${C}License: three-clause BSD style license.\\
${C}The license text can be found in the file LICENSE.\\
${C}\\
${C}*** $NOTEE *** $CE\\
"
case "$license" in
binaryrestricted) text="$restrictedtext";;
gpl3) text="$gpl3text";;
bsd2) text="$bsd2text";;
bsd3) text="$bsd3text";;
*) echo "unknown LICENSE type" && exit 1;;
esac
################################################################################
# insert notice
# Check for existing copy-right notice
if grep -q -i -e "$NOTEB" "$file" ; then
echo "$file: replace $COPYR"
sed -i -e "/$NOTEB/a $text
/$NOTEB/,/$NOTEE/d" "$file"
else
fl=`head -n 1 "$file"`
xml_pat='^ *<\?(xml|XML).*\?> *$'
shell_pat='^ *#\!'
if [[ ("$fl" =~ $xml_pat) || ("$fl" =~ $shell_pat) ]] ; then
echo "$file: new $COPYR (first line preserved)"
sed -i -e "1{a \\
a $text}" "$file"
else
echo "$file: new $COPYR"
sed -i -e "1{x;i $text ;G}" "$file"
fi
fi
# Make sure there is exactly one blank line after the notice
sed -i -n -e "/$NOTEE/{p;: lab; n;s/^\\s*$/&/;t lab;i \\
};p" "$file"
# trim trailing white space in copy-right notice
sed -i -e "/$NOTEB/,/$NOTEE/s/\\s*$//" "$file"
done