Merge branch 'upstream'
[gnulib.git] / debian / clscan / clscan
1 #!/usr/bin/perl -w
2 # Copyright 2009 Ian Beckwith <ianb@erislabs.net>
3 # License: GPL v2 or later.
4
5 use strict;
6 use vars qw($me);
7 $me=($0=~/(?:.*\/)?(.*)/)[0];
8
9 use Getopt::Long;
10 use YAML::Any;
11 use Digest::SHA qw(sha256_hex);
12 use File::Find;
13 use File::Copy;
14
15 our $CLSCANDIR="debian/clscan";
16 our $FILESCACHE="$CLSCANDIR/files.yaml";
17 our $NEWFILES="$CLSCANDIR/new.txt";
18 our $COPYRIGHTSTUB="$CLSCANDIR/copyright.in";
19
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.
25
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.
30
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/>.
33 EOL
34
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)
39 any later version.
40
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.
45
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,
49 USA.
50 EOL
51
52 # license overrides as specified in modules/*
53 our $module_licenses = {
54     "public domain" => {
55         license => "PD",
56         license_text => "",
57     },
58     "unlimited" => {
59         license => "other",
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",
63     },
64     "LGPL" => {
65         license => "LGPL",
66         license_text => $lgpl_boilerplate,
67     },
68     "LGPLv2+" => {
69         license => "LGPL-2+",
70         license_text => $lgpl_boilerplate,
71     },
72     "unmodifiable license text" => {
73         license => "other",
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",
76     },
77     "GPLed build tool" => {
78         license => "GPL",
79         license_text => $gpl_boilerplate,
80     },
81     "GPL" => {
82         license => "GPL",
83         license_text => $gpl_boilerplate,
84     },
85 };
86
87 our @filenames=();
88 our %overrides=();
89 our $files={};
90 our $new={};
91
92 # actions
93 my $scan=0;
94 my $merge=0;
95 my $writecopyright=0;
96
97 usage() unless(@ARGV);
98 usage() unless GetOptions("scan|s" => \$scan,
99                           "merge|m" => \$merge,
100                           "write|w" => \$writecopyright,
101                           "help|h" => sub { usage(); });
102
103 load_cache();
104 scan() if($scan);
105 merge() if($merge);
106 write_copyright() if ($merge || $writecopyright);
107
108
109
110 sub scan
111 {
112     get_filenames();
113     for my $file (@filenames)
114     {
115         scan_file($file);
116     }
117     write_new();
118     find_deleted();
119 }
120
121 sub merge
122 {
123     merge_new();
124     load_overrides();
125     save_cache();
126 }
127
128 sub write_copyright
129 {
130     unless(keys(%$files))
131     {
132         die("$me: no files known, run $0 --scan\n");
133     }
134     unless(copy($COPYRIGHTSTUB, "debian/copyright"))
135     {
136         die("$me: cannot copy $COPYRIGHTSTUB to debian/copyright: $!\n");
137     }
138     unless(open(COPYRIGHT, ">>debian/copyright"))
139     {
140         die("$me: cannot append to debian/copyright: $!\n");
141     }
142
143     # group files by license/license_text/copyright
144     my $licenses={};
145     for my $file (sort keys(%$files))
146     {
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);
152     }
153     my %refs=();
154     my $refnum="00";
155     for my $license (sort keys(%$licenses))
156     {
157         for my $license_text (sort keys(%{$licenses->{$license}}))
158         {
159             my $licensestr=$license;
160             if(length($license_text))
161             {
162                 $refnum++;
163                 # license_text + empty license = License: other
164                 if(!length($license))
165                 {
166                     $licensestr = "other";
167                 }
168                 $licensestr .= " [REF$refnum]";
169                 $refs{$licensestr}=$license_text;
170             }
171             for my $copyright (sort keys(%{$licenses->{$license}->{$license_text}}))
172             {
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";
179             }
180         }
181     }
182     for my $ref (sort byref keys(%refs))
183     {
184         print COPYRIGHT "License: $ref\n";
185         my @text=split(/\n/, $refs{$ref});
186         print COPYRIGHT map { "    " . $_ . "\n" } @text;
187         print COPYRIGHT "\n";
188     }
189     print COPYRIGHT license_trailer(sort keys(%$licenses));
190     close(COPYRIGHT);
191 }
192
193 sub byref
194 {
195     my $aref=($a=~/\[REF(\d+)\]/)[0];
196     my $bref=($b=~/\[REF(\d+)\]/)[0];
197     if($aref && $bref)
198     {
199         return($aref <=> $bref);
200     }
201     return($a cmp $b);
202 }
203
204 sub license_trailer
205 {
206     my @licenses_used=@_;
207     my $license_data = {
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",
220     };
221
222     my %types_found=();
223     for my $type (reverse sort keys(%$license_data))
224     {
225         for my $license (@licenses_used)
226         {
227             if($license =~ /$type(\+|\s|$)/i)
228             {
229                 $types_found{$type}=1;
230             }
231         }
232     }
233     my $text="\n";
234     # if just one, use standard style
235     if(keys(%types_found) == 1)
236     {
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";
240     }
241     else
242     {
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))
248         {
249             $text .= sprintf("%-60s %s\n", $license_data->{$type}, $type);
250         }
251     }
252     return $text;
253 }
254
255
256 sub guess_license
257 {
258     my $file=shift;
259     my $license='';
260     my $copyright='';
261     if(open(LICENSECHECK, "licensecheck --copyright \"$file\"|"))
262     {
263         while(<LICENSECHECK>)
264         {
265             chomp;
266             if(/^\s+\[(.*)\]$/)
267             {
268                 $copyright=$1;
269             }
270             elsif(/.*:\s+(.*)/)
271             {
272                 $license=$1;
273             }
274         }
275         close(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;
284     }
285     return($license, $copyright);
286 }
287
288 sub scan_file
289 {
290     my $filename=shift;
291     unless(open(FILE, $filename))
292     {
293         warn("$me: $filename: cannot open: $!\n");
294         return;
295     }
296     my $header='';
297     for(my $i=0; $i < 15; $i++)
298     {
299         my $line=<FILE>;
300         last unless($line);
301         $header .= $line;
302     }
303     close(FILE);
304     my $hash=sha256_hex($header);
305     if( (!exists($files->{$filename})) ||
306         ($files->{$filename}->{hash} ne $hash))
307     {
308         filechanged($filename, $hash, $header);
309     }
310 }
311
312
313 sub filechanged
314 {
315     my($filename, $hash, $header)=@_;
316     my($license_guess, $copyright_guess)=guess_license($filename);
317     $new->{$filename}={
318         hash=>$hash,
319         license=>$license_guess,
320         copyright=>$copyright_guess,
321         header=>$header,
322     };
323     if(exists($files->{$filename}))
324     {
325         if(exists($files->{$filename}->{copyright}))
326         {
327             $new->{$filename}->{copyright_old}=$files->{$filename}->{copyright};
328         }
329         if(exists($files->{$filename}->{license}))
330         {
331             $new->{$filename}->{license_old}=$files->{$filename}->{license};
332         }
333         if(exists($files->{$filename}->{license_text}))
334         {
335             $new->{$filename}->{license_text_old}=$files->{$filename}->{license_text};
336         }
337     }
338 }
339
340 sub get_filenames
341 {
342     find(\&wanted_files, ".");
343 }
344
345 sub wanted_files
346 {
347     if(/^\.git/ || /^\.cvs/ || /^debian/ || /^modules$/)
348     {
349         $File::Find::prune=1;
350     }
351     elsif(-f)
352     {
353         push(@filenames, $File::Find::name);
354     }
355 }
356
357 sub wanted_modules
358 {
359     if(/^\.[^\/]/ || /^README$/ || /^COPYING$/)
360     {
361         $File::Find::prune=1;
362         return;
363     }
364     elsif(-f)
365     {
366         unless(open(MOD, $File::Find::name))
367         {
368             warn("$me: cannot open $File::Find::name: $!\n");
369             return;
370         }
371         my $infiles=0;
372         my $inlicense=0;
373         my @files=();
374         while(<MOD>)
375         {
376             chomp;
377             if(/^$/)
378             {
379                 $infiles = $inlicense = 0;
380             }
381             if($inlicense)
382             {
383                 push(@{$overrides{$_}},@files);
384                 $inlicense=0;
385             }
386             elsif($infiles)
387             {
388                 push(@files, $_);
389             }
390             elsif(/^License:/)
391             {
392                 $inlicense=1;
393             }
394             elsif(/^Files:/)
395             {
396                 $infiles=1;
397             }
398         }
399         close(MOD);
400     }
401 }
402
403 sub load_overrides
404 {
405     find({ wanted => \&wanted_modules, no_chdir => 1}, "modules/");
406     for my $license (keys(%overrides))
407     {
408         if(!exists($module_licenses->{$license}))
409         {
410             die("$me: license override \"$license\" not found in \$module_licenses\n");
411         }
412         my @overridden_files=map { "./" . $_; } @{$overrides{$license}};
413         for my $file (@overridden_files)
414         {
415             my $override=$module_licenses->{$license};
416             if(length($override->{license}))
417             {
418                 $files->{$file}->{license_override}=$override->{license};
419             }
420             if(length($override->{license_text}))
421             {
422                 $files->{$file}->{license_text_override}=$override->{license_text};
423             }
424         }
425     }
426 }
427
428
429 sub load_cache
430 {
431     unless(open(YAML,$FILESCACHE))
432     {
433         warn("$me: cannot load cache $FILESCACHE: $!\n");
434         return;
435     }
436     my $yaml;
437     {
438         local $/=undef;
439         $yaml=<YAML>;
440     }
441     close(YAML);
442     $files=Load($yaml);
443 }
444
445 sub save_cache
446 {
447     backup($FILESCACHE);
448     unless(open(YAML,">$FILESCACHE"))
449     {
450         warn("$me: cannot save cache $FILESCACHE: $!\n");
451         return;
452     }
453     print YAML Dump($files);
454     close(YAML);
455 }
456
457 sub write_new
458 {
459     backup($NEWFILES);
460     unless(keys(%$new))
461     {
462         warn("$me: no new/changed files found\n");
463         return;
464     }
465     unless(open(NEW,">$NEWFILES"))
466     {
467         die("$me: cannot write to $NEWFILES: $!\n");
468     }
469     for my $file (sort keys %$new)
470     {
471         print NEW "File: $file\n";
472         print NEW "Hash: ", $new->{$file}->{hash}, "\n";
473         print NEW "Copyright: ", $new->{$file}->{copyright}, "\n";
474         print NEW "License: ", $new->{$file}->{license}, "\n";
475         print NEW "License_Text: \n";
476         if($new->{$file}->{license_old})
477         {
478             print NEW "#License_old: ", $new->{$file}->{license_old}, "\n";
479         }
480         if($new->{$file}->{copyright_old})
481         {
482             print NEW "#Copyright_old: ", $new->{$file}->{copyright_old}, "\n";
483         }
484         if($new->{$file}->{licence_text_old})
485         {
486             print NEW "#License_text_old: ", $new->{$file}->{licence_text_old}, "\n";
487         }
488         print NEW "#Header: \n";
489         my @headerlines=split(/\n/, $new->{$file}->{header});
490         @headerlines=map { "#" . $_ } grep { defined $_; } @headerlines;
491         print NEW join("\n", @headerlines);
492         print NEW "\n\n";
493     }
494     close NEW;
495 }
496
497 sub merge_new
498 {
499     unless(open(NEW, $NEWFILES))
500     {
501         die("$me: $NEWFILES: cannot open: $!\n");
502     }
503     my $license='';
504     my $copyright='';
505     my $hash='';
506     my $file='';
507     my $license_text='';
508     my $in_license_text=0;
509     my $line=0;
510     while(<NEW>)
511     {
512         $line++;
513         chomp;
514         next if(/^\s*\#/);
515         if($in_license_text && /^\s+(.*)/)
516         {
517             $license_text .= $1 . "\n";
518         }
519         elsif(/^\s*$/)
520         {
521             next;
522         }
523         elsif(/^File:\s*(.*)/)
524         {
525             my $newfile=$1;
526             # save previous entry
527             if(length($file) && length($hash))
528             {
529                 $files->{$file}={ hash=>$hash,
530                                   copyright=>$copyright,
531                                   license=>$license,
532                                   license_text=>$license_text };
533             }
534             $file=$newfile;
535             $license='';
536             $copyright='';
537             $hash='';
538             $license_text='';
539             $in_license_text = 0;
540         }
541         elsif(/^Hash:\s*(.*)/)
542         {
543             $hash=$1;
544         }
545         elsif(/^Copyright:\s*(.*)/)
546         {
547             $copyright=$1;
548         }
549         elsif(/^License:\s*(.*)/)
550         {
551             $license=$1;
552             $in_license_text=1;
553             $license_text='';
554         }
555         else
556         {
557             warn("$me: $NEWFILES: line $line not recognized\n");
558         }
559     }
560     close(NEW);
561     # save last entry
562     if(length($file) && length($hash))
563     {
564         $files->{$file}={ hash=>$hash,
565                           copyright=>$copyright,
566                           license=>$license,
567                           license_text=>$license_text };
568     }
569 }
570
571 sub find_deleted
572 {
573     my @deleted=();
574     my %newnames = map { $_ => 1 } @filenames;
575     for my $file (sort keys(%$files))
576     {
577         unless(exists($newnames{$file}))
578         {
579             push(@deleted, $file);
580         }
581     }
582     if(@deleted)
583     {
584         print "Removed files:\n";
585         print join("\n", @deleted),"\n";
586     }
587 }
588
589 sub backup
590 {
591     my $base=shift;
592     my $old="$base.old";
593     if(-f $base)
594     {
595         unlink($base);
596         move($base, $old);
597     }
598 }
599
600 sub usage
601 {
602     die("usage: $me [--scan] [--merge]\n",
603         "scans for changed copyright/licenses\n",
604         "  -s|-scan      Scan for new files & files with changed copyright headers\n",
605         "                Writes to debian/clscan/new.txt for manual review.\n",
606         "  -m|--merge    Merges new data from debian/clscan/new.txt\n",
607         "  -w|--write    Writes updated debian/copyright.\n",
608         "                --merge implies --write.\n");
609 }