Merge branch '1.4.x'
[cvc5.git] / contrib / update-copyright.pl
1 #!/usr/bin/perl -w
2 #
3 # update-copyright.pl
4 # Morgan Deters <mdeters@cs.nyu.edu> for CVC4
5 # Copyright (c) 2009-2014 The CVC4 Project
6 #
7 # usage: update-copyright [-m] [files/directories...]
8 # update-copyright [-h | --help]
9 #
10 # This script goes through a source directory rewriting the top bits of
11 # source files to match a template (inline, below). For files with no
12 # top comment, it adds a fresh one.
13 #
14 # if no files/directories are unspecified, the script scans its own
15 # parent directory's "src" directory. Since it lives in contrib/ in
16 # the CVC4 source tree, that means src/ in the CVC4 source tree.
17 #
18 # If -m is specified as the first argument, all files and directories
19 # are scanned, but only ones modified in the index or working tree
20 # are modified (i.e., those that have at least one status M in
21 # "git status -s").
22 #
23 # It ignores any file/directory not starting with [a-zA-Z]
24 # (so, this includes . and .., vi swaps, .git meta-info,
25 # .deps, etc.)
26 #
27 # It ignores any file not ending with one of:
28 # .c .cc .cpp .C .h .hh .hpp .H .y .yy .ypp .Y .l .ll .lpp .L .g
29 # [ or those with ".in" also suffixed, e.g., .cpp.in ]
30 # (so, this includes emacs ~-backups, CVS detritus, etc.)
31 #
32 # It ignores any directory matching $excluded_directories
33 # (so, you should add here any sources imported but not covered under
34 # the license.)
35 #
36
37 my $excluded_directories = '^(minisat|bvminisat|CVS|generated)$';
38 my $excluded_paths = '^(src/parser/antlr_input_imports.cpp|src/bindings/compat/.*|src/util/channel.h)$';
39
40 # Years of copyright for the template. E.g., the string
41 # "1985, 1987, 1992, 1997, 2008" or "2006-2009" or whatever.
42 my $years = '2009-2014';
43
44 my $standard_template = <<EOF;
45 ** This file is part of the CVC4 project.
46 ** Copyright (c) $years New York University and The University of Iowa
47 ** See the file COPYING in the top-level source directory for licensing
48 ** information.\\endverbatim
49 EOF
50
51 ## end config ##
52
53 use strict;
54 use Fcntl ':mode';
55
56 my $dir = $0;
57 $dir =~ s,/[^/]+/*$,,;
58
59 if($#ARGV >= 0 && ($ARGV[0] eq '-h' || $ARGV[0] eq '--help')) {
60 open(my $SELF, $0) || die "error opening $0 for reading";
61 while($_ = <$SELF>) {
62 last if !/^#/;
63 print;
64 }
65 close $SELF;
66 exit;
67 }
68
69 # whether we ONLY process files with git status "M"
70 my $modonly = 0;
71
72 if($#ARGV >= 0 && $ARGV[0] eq '-m') {
73 $modonly = 1;
74 shift;
75 }
76
77 my @searchdirs = ();
78 if($#ARGV == -1) {
79 (chdir($dir."/..") && -f "src/include/cvc4_public.h") || die "can't find top-level source directory for CVC4";
80 my $pwd = `pwd`; chomp $pwd;
81
82 print <<EOF;
83 Warning: this script is dangerous. It will overwrite the header comments in your
84 source files to match the template in the script, attempting to retain file-specific
85 comments, but this isn't guaranteed. You should run this in a git working tree
86 and run "git diff" after to ensure everything was correctly rewritten.
87
88 The directories in which to search for and change sources is:
89 $pwd/src
90 $pwd/examples
91 $pwd/test
92
93 Continue? y or n:
94 EOF
95
96 $_ = <STDIN>; chomp;
97 die 'aborting operation' if !( $_ eq 'y' || $_ eq 'yes' || $_ eq 'Y' || $_ eq 'YES' );
98
99 $searchdirs[0] = 'src';
100 $searchdirs[1] = 'examples';
101 $searchdirs[2] = 'test';
102 } else {
103 @searchdirs = @ARGV;
104 }
105
106 print "Updating sources...\n";
107
108 while($#searchdirs >= 0) {
109 my $dir = shift @searchdirs;
110 my $mode = (stat($dir))[2] || warn "file or directory \`$dir' does not exist!";
111 my $is_directory = S_ISDIR($mode);
112 if($is_directory) {
113 recurse($dir);
114 } else {
115 if($dir =~ m,^(.*)\/([^/]*)$,) {
116 my($dir, $file) = ($1, $2);
117 if($dir eq "") {
118 $dir = "/";
119 }
120 handleFile($dir, $file);
121 } else {
122 handleFile(".", $dir);
123 }
124 }
125 }
126
127 sub handleFile {
128 my ($srcdir, $file) = @_;
129 return if !($file =~ /\.(c|cc|cpp|C|h|hh|hpp|H|y|yy|ypp|Y|l|ll|lpp|L|g|java)(\.in)?$/);
130 return if ($srcdir.'/'.$file) =~ /$excluded_paths/;
131 return if $modonly && `git status -s "$srcdir/$file" 2>/dev/null` !~ /^(M|.M)/;
132 print "$srcdir/$file...";
133 my $infile = $srcdir.'/'.$file;
134 my $outfile = $srcdir.'/#'.$file.'.tmp';
135 open(my $IN, $infile) || die "error opening $infile for reading";
136 open(my $OUT, '>', $outfile) || die "error opening $outfile for writing";
137 open(my $AUTHOR, "$dir/get-authors " . $infile . '|');
138 my $author = <$AUTHOR>; chomp $author;
139 my $major_contributors = <$AUTHOR>; chomp $major_contributors;
140 my $minor_contributors = <$AUTHOR>; chomp $minor_contributors;
141 close $AUTHOR;
142 $_ = <$IN>;
143 if(m,^(%{)?/\*(\*| )\*\*\*,) {
144 print "updating\n";
145 if($file =~ /\.(y|yy|ypp|Y)$/) {
146 print $OUT "%{/******************* */\n";
147 print $OUT "/** $file\n";
148 } elsif($file =~ /\.g$/) {
149 # avoid javadoc-style comment here; antlr complains
150 print $OUT "/* ******************* */\n";
151 print $OUT "/*! \\file $file\n";
152 } else {
153 print $OUT "/********************* */\n";
154 print $OUT "/*! \\file $file\n";
155 }
156 print $OUT " ** \\verbatim\n";
157 print $OUT " ** Original author: $author\n";
158 print $OUT " ** Major contributors: $major_contributors\n";
159 print $OUT " ** Minor contributors (to current version): $minor_contributors\n";
160 my $comment_stub = "";
161 while(my $line = <$IN>) {
162 if($line =~ /\b[Cc]opyright\b/ && $line !~ /\bNew York University and The University of Iowa\b/) {
163 # someone else holds this copyright
164 print $OUT $line;
165 }
166 last if $line =~ /^ \*\*\s*$/;
167 if($line =~ /\*\//) {
168 $comment_stub = " ** [[ Add lengthier description here ]]\n\
169 ** \\todo document this file\n\
170 $line";
171 last;
172 }
173 }
174 print $OUT $standard_template;
175 print $OUT " **\n";
176 if($comment_stub) {
177 print $OUT $comment_stub;
178 }
179 } else {
180 my $line = $_;
181 print "adding\n";
182 if($file =~ /\.(y|yy|ypp|Y)$/) {
183 print $OUT "%{/******************* */\n";
184 print $OUT "/*! \\file $file\n";
185 } elsif($file =~ /\.g$/) {
186 # avoid javadoc-style comment here; antlr complains
187 print $OUT "/* ******************* */\n";
188 print $OUT "/*! \\file $file\n";
189 } else {
190 print $OUT "/********************* */\n";
191 print $OUT "/*! \\file $file\n";
192 }
193 print $OUT " ** \\verbatim\n";
194 print $OUT " ** Original author: $author\n";
195 print $OUT " ** Major contributors: $major_contributors\n";
196 print $OUT " ** Minor contributors (to current version): $minor_contributors\n";
197 print $OUT $standard_template;
198 print $OUT " **\n";
199 print $OUT " ** \\brief [[ Add one-line brief description here ]]\n";
200 print $OUT " **\n";
201 print $OUT " ** [[ Add lengthier description here ]]\n";
202 print $OUT " ** \\todo document this file\n";
203 print $OUT " **/\n\n";
204 print $OUT $line;
205 if($file =~ /\.(y|yy|ypp|Y)$/) {
206 while(my $line = <$IN>) {
207 chomp $line;
208 if($line =~ '\s*%{(.*)') {
209 print $OUT "$1\n";
210 last;
211 }
212 # just in case something's weird with the file ?
213 if(!($line =~ '\s*')) {
214 print $OUT "$line\n";
215 last;
216 }
217 }
218 }
219 }
220 while(my $line = <$IN>) {
221 print $OUT $line;
222 }
223 close $IN;
224 close $OUT;
225 rename($outfile, $infile) || die "can't rename working file \`$outfile' to \`$infile'";
226 }
227
228 sub recurse {
229 my ($srcdir) = @_;
230 print "in dir $srcdir\n";
231 opendir(my $DIR, $srcdir);
232 while(my $file = readdir $DIR) {
233 next if !($file =~ /^[a-zA-Z]/);
234
235 my $mode = (stat($srcdir.'/'.$file))[2];
236 my $is_directory = S_ISDIR($mode);
237 if($is_directory) {
238 next if $file =~ /$excluded_directories/;
239 recurse($srcdir.'/'.$file);
240 } else {
241 handleFile($srcdir, $file);
242 }
243 }
244 closedir $DIR;
245 }
246
247 ### Local Variables:
248 ### perl-indent-level: 2
249 ### End: