2 # Copyright 2009 Ian Beckwith <ianb@erislabs.net>
3 # License: GPL v2 or later.
7 $me=($0=~/(?:.*\/)?(.*)/)[0];
11 use Digest::SHA qw(sha256_hex);
15 our $CLSCANDIR="debian/clscan";
16 our $FILESCACHE="$CLSCANDIR/files.yaml";
17 our $NEWFILES="$CLSCANDIR/new.txt";
18 our $COPYRIGHTSTUB="$CLSCANDIR/copyright.in";
20 my $gpl_boilerplate=<<"EOL";
21 This program is free software: you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation; either version 3 of the License, or
24 (at your option) any later version.
26 This program is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 my $lgpl_boilerplate=<<"EOL";
36 This program is free software; you can redistribute it and/or modify it
37 under the terms of the GNU Library General Public License as published
38 by the Free Software Foundation; either version 2, or (at your option)
41 This program is distributed in the hope that it will be useful,
42 but WITHOUT ANY WARRANTY; without even the implied warranty of
43 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44 Library General Public License for more details.
46 You should have received a copy of the GNU Library General Public
47 License along with this program; if not, write to the Free Software
48 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
52 # license overrides as specified in modules/*
53 our $module_licenses = {
60 license_text => "This file is free software; the Free Software Foundation\n" .
61 "gives unlimited permission to copy and/or distribute it,\n" .
62 "with or without modifications, as long as this notice is preserved.\n",
66 license_text => $lgpl_boilerplate,
70 license_text => $lgpl_boilerplate,
72 "unmodifiable license text" => {
74 license_text => "Everyone is permitted to copy and distribute verbatim copies\n" .
75 "of this license document, but changing it is not allowed.\n",
77 "GPLed build tool" => {
79 license_text => $gpl_boilerplate,
83 license_text => $gpl_boilerplate,
97 usage() unless(@ARGV);
98 usage() unless GetOptions("scan|s" => \$scan,
100 "write|w" => \$writecopyright,
101 "help|h" => sub { usage(); });
106 write_copyright() if ($merge || $writecopyright);
113 for my $file (@filenames)
130 unless(keys(%$files))
132 die("$me: no files known, run $0 --scan\n");
134 unless(copy($COPYRIGHTSTUB, "debian/copyright"))
136 die("$me: cannot copy $COPYRIGHTSTUB to debian/copyright: $!\n");
138 unless(open(COPYRIGHT, ">>debian/copyright"))
140 die("$me: cannot append to debian/copyright: $!\n");
143 # group files by license/license_text/copyright
145 for my $file (sort keys(%$files))
147 my $license=$files->{$file}->{license_override} || $files->{$file}->{license};
148 my $copyright=$files->{$file}->{copyright};
149 my $license_text=$files->{$file}->{license_text_override} ||
150 $files->{$file}->{license_text};
151 push(@{$licenses->{$license}->{$license_text}->{$copyright}}, $file);
155 for my $license (sort keys(%$licenses))
157 for my $license_text (sort keys(%{$licenses->{$license}}))
159 my $licensestr=$license;
160 if(length($license_text))
163 # license_text + empty license = License: other
164 if(!length($license))
166 $licensestr = "other";
168 $licensestr .= " [REF$refnum]";
169 $refs{$licensestr}=$license_text;
171 for my $copyright (sort keys(%{$licenses->{$license}->{$license_text}}))
173 next if(!length($license) && !length($copyright) && !length($license_text));
174 my @filelist=sort @{$licenses->{$license}->{$license_text}->{$copyright}};
175 print COPYRIGHT "Files: ", join(', ', @filelist), "\n";
176 print COPYRIGHT "Copyright: $copyright\n" if length($copyright);
177 print COPYRIGHT "License: $licensestr\n" if length($licensestr);
178 print COPYRIGHT "\n";
182 for my $ref (sort byref keys(%refs))
184 print COPYRIGHT "License: $ref\n";
185 my @text=split(/\n/, $refs{$ref});
186 print COPYRIGHT map { " " . $_ . "\n" } @text;
187 print COPYRIGHT "\n";
189 print COPYRIGHT license_trailer(sort keys(%$licenses));
195 my $aref=($a=~/\[REF(\d+)\]/)[0];
196 my $bref=($b=~/\[REF(\d+)\]/)[0];
199 return($aref <=> $bref);
206 my @licenses_used=@_;
208 "Apache-2.0" => "Apache License Version 2.0",
209 "Artistic" => "Artistic License",
210 "GFDL-1.2" => "GNU Free Documentation License Version 1.2",
211 "GFDL-1.3" => "GNU Free Documentation License Version 1.3",
212 "GFDL" => "GNU Free Documentation License",
213 "GPL-2" => "GNU General Public License Version 2",
214 "GPL-3" => "GNU General Public License Version 3",
215 "GPL" => "GNU General Public License",
216 "LGPL-2" => "GNU Library General Public License Version 2",
217 "LGPL-2.1" => "GNU Lesser General Public License Version 2.1",
218 "LGPL-3" => "GNU Lesser General Public License Version 3",
219 "LGPL" => "GNU Lesser General Public License",
223 for my $type (reverse sort keys(%$license_data))
225 for my $license (@licenses_used)
227 if($license =~ /$type(\+|\s|$)/i)
229 $types_found{$type}=1;
234 # if just one, use standard style
235 if(keys(%types_found) == 1)
237 my ($file, $name)=each(%types_found);
238 $text .= "The complete text of the $name can be\n";
239 $text .= "found in /usr/share/common-licenses/$file\n";
243 # more than one, use table.
244 $text .= "The complete text of standard licenses referenced above\n";
245 $text .= "can be found in /usr/share/common-licenses/ as follows:\n\n";
246 $text .= sprintf("%-60s %s\n", "LICENSE", "FILE");
247 for my $type (sort keys(%types_found))
249 $text .= sprintf("%-60s %s\n", $license_data->{$type}, $type);
261 if(open(LICENSECHECK, "licensecheck --copyright \"$file\"|"))
263 while(<LICENSECHECK>)
276 $copyright =~ s/^\s*Copyright\s*:\s*//;
277 $license =~ s/.*UNKNOWN.*//;
278 $license =~ s/(L?GPL) \(v([\.\d]+) or later\)/$1-$2+/i;
279 $license =~ s/(L?GPL) \(v([\.\d]+)\)/$1-$2/i;
280 $license =~ s/G?FDL \(v([\.\d]+) or later\)/GFDL-$1+/i;
281 $license =~ s/G?FDL \(v([\.\d]+)\)/GFDL-$1/i;
282 $license =~ s/Apache \(v([\.\d]+) or later\)/Apache-$1+/i;
283 $license =~ s/Apache \(v([\.\d]+)\)/Apache-$1+/i;
285 return($license, $copyright);
291 unless(open(FILE, $filename))
293 warn("$me: $filename: cannot open: $!\n");
297 for(my $i=0; $i < 15; $i++)
304 my $hash=sha256_hex($header);
305 if( (!exists($files->{$filename})) ||
306 ($files->{$filename}->{hash} ne $hash))
308 filechanged($filename, $hash, $header);
315 my($filename, $hash, $header)=@_;
316 my($license_guess, $copyright_guess)=guess_license($filename);
319 license=>$license_guess,
320 copyright=>$copyright_guess,
323 if(exists($files->{$filename}))
325 if(exists($files->{$filename}->{copyright}))
327 $new->{$filename}->{copyright}=$files->{$filename}->{copyright};
328 $new->{$filename}->{copyright_guess}=$copyright_guess;
330 if(exists($files->{$filename}->{license}))
332 $new->{$filename}->{license}=$files->{$filename}->{license};
333 $new->{$filename}->{license_guess}=$license_guess;
335 if(exists($files->{$filename}->{license_text}))
337 $new->{$filename}->{license_text}=$files->{$filename}->{license_text};
344 find(\&wanted_files, ".");
349 if(/^\.git/ || /^\.cvs/ || /^debian/ || /^modules$/)
351 $File::Find::prune=1;
355 push(@filenames, $File::Find::name);
361 if(/^\.[^\/]/ || /^README$/ || /^COPYING$/)
363 $File::Find::prune=1;
368 unless(open(MOD, $File::Find::name))
370 warn("$me: cannot open $File::Find::name: $!\n");
381 $infiles = $inlicense = 0;
385 push(@{$overrides{$_}},@files);
407 find({ wanted => \&wanted_modules, no_chdir => 1}, "modules/");
408 for my $license (keys(%overrides))
410 if(!exists($module_licenses->{$license}))
412 die("$me: license override \"$license\" not found in \$module_licenses\n");
414 my @overridden_files=map { "./" . $_; } @{$overrides{$license}};
415 for my $file (@overridden_files)
417 my $override=$module_licenses->{$license};
418 if(length($override->{license}))
420 $files->{$file}->{license_override}=$override->{license};
422 if(length($override->{license_text}))
424 $files->{$file}->{license_text_override}=$override->{license_text};
433 unless(open(YAML,$FILESCACHE))
435 warn("$me: cannot load cache $FILESCACHE: $!\n");
450 unless(open(YAML,">$FILESCACHE"))
452 warn("$me: cannot save cache $FILESCACHE: $!\n");
455 print YAML Dump($files);
464 warn("$me: no new/changed files found\n");
467 unless(open(NEW,">$NEWFILES"))
469 die("$me: cannot write to $NEWFILES: $!\n");
471 for my $file (sort keys %$new)
473 print NEW "File: $file\n";
474 print NEW "Hash: ", $new->{$file}->{hash}, "\n";
475 print NEW "Copyright: ", $new->{$file}->{copyright}, "\n";
476 if($new->{$file}->{copyright_guess})
478 print NEW "#Copyright_guess: ", $new->{$file}->{copyright_guess}, "\n";
480 print NEW "License: ", $new->{$file}->{license}, "\n";
481 if($new->{$file}->{license_guess})
483 print NEW "#License_guess: ", $new->{$file}->{license_guess}, "\n";
485 if($new->{$file}->{license_text})
487 print NEW $new->{$file}->{license_text}, "\n";
489 print NEW "#Header: \n";
490 my @headerlines=split(/\n/, $new->{$file}->{header});
491 @headerlines=map { "#" . $_ } grep { defined $_; } @headerlines;
492 print NEW join("\n", @headerlines);
500 unless(open(NEW, $NEWFILES))
502 die("$me: $NEWFILES: cannot open: $!\n");
509 my $in_license_text=0;
516 if($in_license_text && /^\s+(.*)/)
518 $license_text .= $1 . "\n";
524 elsif(/^File:\s*(.*)/)
527 # save previous entry
528 if(length($file) && length($hash))
530 $files->{$file}={ hash=>$hash,
531 copyright=>$copyright,
533 license_text=>$license_text };
540 $in_license_text = 0;
542 elsif(/^Hash:\s*(.*)/)
546 elsif(/^Copyright:\s*(.*)/)
550 elsif(/^License:\s*(.*)/)
558 warn("$me: $NEWFILES: line $line not recognized\n");
563 if(length($file) && length($hash))
565 $files->{$file}={ hash=>$hash,
566 copyright=>$copyright,
568 license_text=>$license_text };
575 my %newnames = map { $_ => 1 } @filenames;
576 for my $file (sort keys(%$files))
578 unless(exists($newnames{$file}))
580 push(@deleted, $file);
585 print "Removed files:\n";
586 print join("\n", @deleted),"\n";
603 die("usage: $me [--scan] [--merge]\n",
604 "scans for changed copyright/licenses\n",
605 " -s|-scan Scan for new files & files with changed copyright headers\n",
606 " Writes to debian/clscan/new.txt for manual review.\n",
607 " -m|--merge Merges new data from debian/clscan/new.txt\n",
608 " -w|--write Writes updated debian/copyright.\n",
609 " --merge implies --write.\n");