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,
91 our @deleted_files=();
98 usage() unless(@ARGV);
99 usage() unless GetOptions("scan|s" => \$scan,
100 "merge|m" => \$merge,
101 "write|w" => \$writecopyright,
102 "help|h" => sub { usage(); });
107 write_copyright() if ($merge || $writecopyright);
114 for my $file (@filenames)
131 unless(keys(%$files))
133 die("$me: no files known, run $0 --scan\n");
135 unless(copy($COPYRIGHTSTUB, "debian/copyright"))
137 die("$me: cannot copy $COPYRIGHTSTUB to debian/copyright: $!\n");
139 unless(open(COPYRIGHT, ">>debian/copyright"))
141 die("$me: cannot append to debian/copyright: $!\n");
144 # group files by license/license_text/copyright
146 for my $file (sort keys(%$files))
148 my $license=$files->{$file}->{license_override} || $files->{$file}->{license};
149 my $copyright=$files->{$file}->{copyright};
150 my $license_text=$files->{$file}->{license_text_override} ||
151 $files->{$file}->{license_text};
152 push(@{$licenses->{$license}->{$license_text}->{$copyright}}, $file);
156 for my $license (sort keys(%$licenses))
158 for my $license_text (sort keys(%{$licenses->{$license}}))
160 my $licensestr=$license;
161 if(length($license_text))
164 # license_text + empty license = License: other
165 if(!length($license))
167 $licensestr = "other";
169 $licensestr .= " [REF$refnum]";
170 $refs{$licensestr}=$license_text;
172 for my $copyright (sort keys(%{$licenses->{$license}->{$license_text}}))
174 next if(!length($license) && !length($copyright) && !length($license_text));
175 my @filelist=sort @{$licenses->{$license}->{$license_text}->{$copyright}};
176 print COPYRIGHT "Files: ", join(', ', @filelist), "\n";
177 print COPYRIGHT "Copyright: $copyright\n" if length($copyright);
178 print COPYRIGHT "License: $licensestr\n" if length($licensestr);
179 print COPYRIGHT "\n";
183 for my $ref (sort byref keys(%refs))
185 print COPYRIGHT "License: $ref\n";
186 my @text=split(/\n/, $refs{$ref});
187 print COPYRIGHT map { " " . $_ . "\n" } @text;
188 print COPYRIGHT "\n";
190 print COPYRIGHT license_trailer(sort keys(%$licenses));
196 my $aref=($a=~/\[REF(\d+)\]/)[0];
197 my $bref=($b=~/\[REF(\d+)\]/)[0];
200 return($aref <=> $bref);
207 my @licenses_used=@_;
209 "Apache-2.0" => "Apache License Version 2.0",
210 "Artistic" => "Artistic License",
211 "GFDL-1.2" => "GNU Free Documentation License Version 1.2",
212 "GFDL-1.3" => "GNU Free Documentation License Version 1.3",
213 "GFDL" => "GNU Free Documentation License",
214 "GPL-2" => "GNU General Public License Version 2",
215 "GPL-3" => "GNU General Public License Version 3",
216 "GPL" => "GNU General Public License",
217 "LGPL-2" => "GNU Library General Public License Version 2",
218 "LGPL-2.1" => "GNU Lesser General Public License Version 2.1",
219 "LGPL-3" => "GNU Lesser General Public License Version 3",
220 "LGPL" => "GNU Lesser General Public License",
224 for my $type (reverse sort keys(%$license_data))
226 for my $license (@licenses_used)
228 if($license =~ /$type(\+|\s|$)/i)
230 $types_found{$type}=1;
235 # if just one, use standard style
236 if(keys(%types_found) == 1)
238 my ($file, $name)=each(%types_found);
239 $text .= "The complete text of the $name can be\n";
240 $text .= "found in /usr/share/common-licenses/$file\n";
244 # more than one, use table.
245 $text .= "The complete text of standard licenses referenced above\n";
246 $text .= "can be found in /usr/share/common-licenses/ as follows:\n\n";
247 $text .= sprintf("%-60s %s\n", "LICENSE", "FILE");
248 for my $type (sort keys(%types_found))
250 $text .= sprintf("%-60s %s\n", $license_data->{$type}, $type);
262 if(open(LICENSECHECK, "licensecheck --copyright \"$file\"|"))
264 while(<LICENSECHECK>)
277 $copyright =~ s/^\s*Copyright\s*:\s*//;
278 $license =~ s/.*UNKNOWN.*//;
279 $license =~ s/(L?GPL) \(v([\.\d]+) or later\)/$1-$2+/i;
280 $license =~ s/(L?GPL) \(v([\.\d]+)\)/$1-$2/i;
281 $license =~ s/G?FDL \(v([\.\d]+) or later\)/GFDL-$1+/i;
282 $license =~ s/G?FDL \(v([\.\d]+)\)/GFDL-$1/i;
283 $license =~ s/Apache \(v([\.\d]+) or later\)/Apache-$1+/i;
284 $license =~ s/Apache \(v([\.\d]+)\)/Apache-$1+/i;
286 return($license, $copyright);
292 unless(open(FILE, $filename))
294 warn("$me: $filename: cannot open: $!\n");
298 for(my $i=0; $i < 15; $i++)
305 my $hash=sha256_hex($header);
306 if( (!exists($files->{$filename})) ||
307 ($files->{$filename}->{hash} ne $hash))
309 filechanged($filename, $hash, $header);
316 my($filename, $hash, $header)=@_;
317 my($license_guess, $copyright_guess)=guess_license($filename);
320 license=>$license_guess,
321 copyright=>$copyright_guess,
324 if(exists($files->{$filename}))
326 if(exists($files->{$filename}->{copyright}))
328 $new->{$filename}->{copyright}=$files->{$filename}->{copyright};
329 $new->{$filename}->{copyright_guess}=$copyright_guess;
331 if(exists($files->{$filename}->{license}))
333 $new->{$filename}->{license}=$files->{$filename}->{license};
334 $new->{$filename}->{license_guess}=$license_guess;
336 if(exists($files->{$filename}->{license_text}))
338 $new->{$filename}->{license_text}=$files->{$filename}->{license_text};
345 find(\&wanted_files, ".");
350 if(/^\.git/ || /^\.cvs/ || /^debian/ || /^modules$/)
352 $File::Find::prune=1;
356 push(@filenames, $File::Find::name);
362 if(/^\.[^\/]/ || /^README$/ || /^COPYING$/)
364 $File::Find::prune=1;
369 unless(open(MOD, $File::Find::name))
371 warn("$me: cannot open $File::Find::name: $!\n");
382 $infiles = $inlicense = 0;
386 push(@{$overrides{$_}},@files);
408 find({ wanted => \&wanted_modules, no_chdir => 1}, "modules/");
409 for my $license (keys(%overrides))
411 if(!exists($module_licenses->{$license}))
413 die("$me: license override \"$license\" not found in \$module_licenses\n");
415 my @overridden_files=map { "./" . $_; } @{$overrides{$license}};
416 for my $file (@overridden_files)
418 my $override=$module_licenses->{$license};
419 if(length($override->{license}))
421 $files->{$file}->{license_override}=$override->{license};
423 if(length($override->{license_text}))
425 $files->{$file}->{license_text_override}=$override->{license_text};
434 unless(open(YAML,$FILESCACHE))
436 warn("$me: cannot load cache $FILESCACHE: $!\n");
451 unless(open(YAML,">$FILESCACHE"))
453 warn("$me: cannot save cache $FILESCACHE: $!\n");
456 print YAML Dump($files);
465 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 my @text=split(/\n/, $new->{$file}->{license_text});
488 print NEW "\t" . join("\n\t", @text), "\n";
490 print NEW "#Header: \n";
491 my @headerlines=split(/\n/, $new->{$file}->{header});
492 @headerlines=map { "#" . $_ } grep { defined $_; } @headerlines;
493 print NEW join("\n", @headerlines);
498 print NEW map { "Deleted: $_\n"; } @deleted_files;
505 unless(open(NEW, $NEWFILES))
507 die("$me: $NEWFILES: cannot open: $!\n");
514 my $in_license_text=0;
521 if($in_license_text && /^\s+(.*)/)
523 $license_text .= $1 . "\n";
529 elsif(/^File:\s*(.*)/)
532 # save previous entry
533 if(length($file) && length($hash))
535 $files->{$file}={ hash=>$hash,
536 copyright=>$copyright,
538 license_text=>$license_text };
545 $in_license_text = 0;
547 elsif(/^Hash:\s*(.*)/)
551 elsif(/^Copyright:\s*(.*)/)
555 elsif(/^License:\s*(.*)/)
561 elsif(/^Deleted:\s*(.*)/)
563 if(exists($files->{$1}))
565 delete($files->{$1});
570 warn("$me: $NEWFILES: line $line not recognized\n");
575 if(length($file) && length($hash))
577 $files->{$file}={ hash=>$hash,
578 copyright=>$copyright,
580 license_text=>$license_text };
586 my %newnames = map { $_ => 1 } @filenames;
587 for my $file (sort keys(%$files))
589 unless(exists($newnames{$file}))
591 push(@deleted_files, $file);
596 print "Removed files:\n";
597 print join("\n", @deleted_files),"\n";
614 die("usage: $me [--scan] [--merge]\n",
615 "scans for changed copyright/licenses\n",
616 " -s|-scan Scan for new files & files with changed copyright headers\n",
617 " Writes to debian/clscan/new.txt for manual review.\n",
618 " -m|--merge Merges new data from debian/clscan/new.txt\n",
619 " -w|--write Writes updated debian/copyright.\n",
620 " --merge implies --write.\n");