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 $lgpl2_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 my $lgpl3_boilerplate=<<"EOL";
53 This program is free software: you can redistribute it and/or modify
54 it under the terms of the GNU Lesser General Public License as published by
55 the Free Software Foundation; either version 3 of the License, or
56 (at your option) any later version.
58 This program is distributed in the hope that it will be useful,
59 but WITHOUT ANY WARRANTY; without even the implied warranty of
60 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 GNU General Public License for more details.
63 You should have received a copy of the GNU General Public License
64 along with this program. If not, see <http://www.gnu.org/licenses/>.
67 # license overrides as specified in modules/*
68 our $module_licenses = {
75 license_text => "This file is free software; the Free Software Foundation\n" .
76 "gives unlimited permission to copy and/or distribute it,\n" .
77 "with or without modifications, as long as this notice is preserved.\n",
81 license_text => $lgpl3_boilerplate,
85 license_text => $lgpl2_boilerplate,
89 license_text => $lgpl3_boilerplate,
91 "unmodifiable license text" => {
93 license_text => "Everyone is permitted to copy and distribute verbatim copies\n" .
94 "of this license document, but changing it is not allowed.\n",
96 "GPLed build tool" => {
98 license_text => $gpl_boilerplate,
102 license_text => $gpl_boilerplate,
110 our @deleted_files=();
116 my $writecopyright=0;
118 usage() unless(@ARGV);
119 usage() unless GetOptions("scan|s" => \$scan,
120 "merge|m" => \$merge,
121 "write|w" => \$writecopyright,
128 write_copyright() if ($merge || $writecopyright);
134 for my $file (@filenames)
151 unless(keys(%$files))
153 die("$me: no files known, run $0 --scan\n");
155 unless(copy($COPYRIGHTSTUB, "debian/copyright"))
157 die("$me: cannot copy $COPYRIGHTSTUB to debian/copyright: $!\n");
159 unless(open(COPYRIGHT, ">>debian/copyright"))
161 die("$me: cannot append to debian/copyright: $!\n");
164 # group files by license/license_text/copyright
166 for my $file (sort keys(%$files))
168 my $license=$files->{$file}->{license_override} || $files->{$file}->{license};
169 my $copyright=$files->{$file}->{copyright};
170 my $license_text=$files->{$file}->{license_text_override} ||
171 $files->{$file}->{license_text};
172 push(@{$licenses->{$license}->{$license_text}->{$copyright}}, $file);
176 for my $license (sort keys(%$licenses))
178 for my $license_text (sort keys(%{$licenses->{$license}}))
180 my $licensestr=$license;
181 if(length($license_text))
184 # license_text + empty license = License: other
185 if(!length($license))
187 $licensestr = "other";
189 $licensestr .= " [REF$refnum]";
190 $refs{$licensestr}=$license_text;
192 for my $copyright (sort keys(%{$licenses->{$license}->{$license_text}}))
194 next if(!length($license) && !length($copyright) && !length($license_text));
195 my @filelist=sort @{$licenses->{$license}->{$license_text}->{$copyright}};
196 print COPYRIGHT "Files: ", join(', ', @filelist), "\n";
197 print COPYRIGHT "Copyright: $copyright\n" if length($copyright);
198 print COPYRIGHT "License: $licensestr\n" if length($licensestr);
199 print COPYRIGHT "\n";
203 for my $ref (sort byref keys(%refs))
205 print COPYRIGHT "License: $ref\n";
206 my @text=split(/\n/, $refs{$ref});
207 print COPYRIGHT map { " " . $_ . "\n" } @text;
208 print COPYRIGHT "\n";
210 print COPYRIGHT license_trailer(sort keys(%$licenses));
216 my $aref=($a=~/\[REF(\d+)\]/)[0];
217 my $bref=($b=~/\[REF(\d+)\]/)[0];
220 return($aref <=> $bref);
227 my @licenses_used=@_;
229 "Apache-2.0" => "Apache License Version 2.0",
230 "Artistic" => "Artistic License",
231 "GFDL-1.2" => "GNU Free Documentation License Version 1.2",
232 "GFDL-1.3" => "GNU Free Documentation License Version 1.3",
233 "GFDL" => "GNU Free Documentation License",
234 "GPL-2" => "GNU General Public License Version 2",
235 "GPL-3" => "GNU General Public License Version 3",
236 "GPL" => "GNU General Public License",
237 "LGPL-2" => "GNU Library General Public License Version 2",
238 "LGPL-2.1" => "GNU Lesser General Public License Version 2.1",
239 "LGPL-3" => "GNU Lesser General Public License Version 3",
240 "LGPL" => "GNU Lesser General Public License",
244 for my $type (reverse sort keys(%$license_data))
246 for my $license (@licenses_used)
248 if($license =~ /$type(\+|\s|$)/i)
250 $types_found{$type}=1;
255 # if just one, use standard style
256 if(keys(%types_found) == 1)
258 my ($file, $name)=each(%types_found);
259 $text .= "The complete text of the $name can be\n";
260 $text .= "found in /usr/share/common-licenses/$file\n";
264 # more than one, use table.
265 $text .= "The complete text of standard licenses referenced above\n";
266 $text .= "can be found in /usr/share/common-licenses/ as follows:\n\n";
267 $text .= sprintf("%-60s %s\n", "LICENSE", "FILE");
268 for my $type (sort keys(%types_found))
270 $text .= sprintf("%-60s %s\n", $license_data->{$type}, $type);
282 if(open(LICENSECHECK, "licensecheck --copyright \"$file\"|"))
284 while(<LICENSECHECK>)
297 $copyright =~ s/^\s*Copyright\s*:\s*//;
298 $license =~ s/.*UNKNOWN.*//;
299 $license =~ s/(L?GPL) \(v([\.\d]+) or later\)/$1-$2+/i;
300 $license =~ s/(L?GPL) \(v([\.\d]+)\)/$1-$2/i;
301 $license =~ s/G?FDL \(v([\.\d]+) or later\)/GFDL-$1+/i;
302 $license =~ s/G?FDL \(v([\.\d]+)\)/GFDL-$1/i;
303 $license =~ s/Apache \(v([\.\d]+) or later\)/Apache-$1+/i;
304 $license =~ s/Apache \(v([\.\d]+)\)/Apache-$1+/i;
306 return($license, $copyright);
312 unless(open(FILE, $filename))
314 warn("$me: $filename: cannot open: $!\n");
318 for(my $i=0; $i < 15; $i++)
325 my $hash=sha256_hex($header);
326 if( (!exists($files->{$filename})) ||
327 ($files->{$filename}->{hash} ne $hash))
329 filechanged($filename, $hash, $header);
336 my($filename, $hash, $header)=@_;
337 my($license_guess, $copyright_guess)=guess_license($filename);
340 license=>$license_guess,
341 copyright=>$copyright_guess,
344 if(exists($files->{$filename}))
346 if(exists($files->{$filename}->{copyright}))
348 $new->{$filename}->{copyright}=$files->{$filename}->{copyright};
349 $new->{$filename}->{copyright_guess}=$copyright_guess;
351 if(exists($files->{$filename}->{license}))
353 $new->{$filename}->{license}=$files->{$filename}->{license};
354 $new->{$filename}->{license_guess}=$license_guess;
356 if(exists($files->{$filename}->{license_text}))
358 $new->{$filename}->{license_text}=$files->{$filename}->{license_text};
365 find(\&wanted_files, ".");
370 if(/^\.git/ || /^\.cvs/ || /^debian/ || /^modules$/ || /^\.pc/)
372 $File::Find::prune=1;
376 push(@filenames, $File::Find::name);
382 if(/^\.[^\/]/ || /^README$/ || /^COPYING$/)
384 $File::Find::prune=1;
389 unless(open(MOD, $File::Find::name))
391 warn("$me: cannot open $File::Find::name: $!\n");
402 $infiles = $inlicense = 0;
406 push(@{$overrides{$_}},@files);
428 find({ wanted => \&wanted_modules, no_chdir => 1}, "modules/");
429 for my $license (keys(%overrides))
431 if(!exists($module_licenses->{$license}))
433 die("$me: license override \"$license\" not found in \$module_licenses\n");
435 my @overridden_files=map { "./" . $_; } @{$overrides{$license}};
436 for my $file (@overridden_files)
438 my $override=$module_licenses->{$license};
439 if(length($override->{license}))
441 $files->{$file}->{license_override}=$override->{license};
443 if(length($override->{license_text}))
445 $files->{$file}->{license_text_override}=$override->{license_text};
454 unless(open(YAML,$FILESCACHE))
456 warn("$me: cannot load cache $FILESCACHE: $!\n");
471 unless(open(YAML,">$FILESCACHE"))
473 warn("$me: cannot save cache $FILESCACHE: $!\n");
476 print YAML Dump($files);
485 warn("$me: no new/changed files found\n");
487 unless(open(NEW,">$NEWFILES"))
489 die("$me: cannot write to $NEWFILES: $!\n");
491 for my $file (sort keys %$new)
493 print NEW "File: $file\n";
494 print NEW "Hash: ", $new->{$file}->{hash}, "\n";
495 print NEW "Copyright: ", $new->{$file}->{copyright}, "\n";
496 if($new->{$file}->{copyright_guess})
498 print NEW "#Copyright_guess: ", $new->{$file}->{copyright_guess}, "\n";
500 print NEW "License: ", $new->{$file}->{license}, "\n";
501 if($new->{$file}->{license_guess})
503 print NEW "#License_guess: ", $new->{$file}->{license_guess}, "\n";
505 if($new->{$file}->{license_text})
507 my @text=split(/\n/, $new->{$file}->{license_text});
508 print NEW "\t" . join("\n\t", @text), "\n";
510 print NEW "#Header: \n";
511 my @headerlines=split(/\n/, $new->{$file}->{header});
512 @headerlines=map { "#" . $_ } grep { defined $_; } @headerlines;
513 print NEW join("\n", @headerlines);
518 print NEW map { "Deleted: $_\n"; } @deleted_files;
525 unless(open(NEW, $NEWFILES))
527 die("$me: $NEWFILES: cannot open: $!\n");
534 my $in_license_text=0;
541 if($in_license_text && /^\s+(.*)/)
543 $license_text .= $1 . "\n";
549 elsif(/^File:\s*(.*)/)
552 # save previous entry
553 if(length($file) && length($hash))
555 $files->{$file}={ hash=>$hash,
556 copyright=>$copyright,
558 license_text=>$license_text };
565 $in_license_text = 0;
567 elsif(/^Hash:\s*(.*)/)
571 elsif(/^Copyright:\s*(.*)/)
575 elsif(/^License:\s*(.*)/)
581 elsif(/^Deleted:\s*(.*)/)
583 if(exists($files->{$1}))
585 delete($files->{$1});
590 warn("$me: $NEWFILES: line $line not recognized\n");
595 if(length($file) && length($hash))
597 $files->{$file}={ hash=>$hash,
598 copyright=>$copyright,
600 license_text=>$license_text };
606 my %newnames = map { $_ => 1 } @filenames;
607 for my $file (sort keys(%$files))
609 unless(exists($newnames{$file}))
611 push(@deleted_files, $file);
616 print "Removed files:\n";
617 print join("\n", @deleted_files),"\n";
634 die("usage: $me [--scan] [--merge]\n",
635 "scans for changed copyright/licenses\n",
636 " -s|-scan Scan for new files & files with changed copyright headers\n",
637 " Writes to debian/clscan/new.txt for manual review.\n",
638 " -m|--merge Merges new data from debian/clscan/new.txt\n",
639 " -w|--write Writes updated debian/copyright.\n",
640 " --merge implies --write.\n");