licensecheck.pl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. #!/usr/bin/perl
  2. # -*- tab-width: 8; indent-tabs-mode: t; cperl-indent-level: 4 -*-
  3. # This script was originally based on the script of the same name from
  4. # the KDE SDK (by dfaure@kde.org)
  5. #
  6. # This version is
  7. # Copyright (C) 2007, 2008 Adam D. Barratt
  8. # Copyright (C) 2012 Francesco Poli
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program. If not, see <https://www.gnu.org/licenses/>.
  22. # Originally copied from Debian's devscripts. A more modern version of
  23. # this can be found at
  24. # https://anonscm.debian.org/git/pkg-perl/packages/licensecheck.git/
  25. =head1 NAME
  26. licensecheck - simple license checker for source files
  27. =head1 SYNOPSIS
  28. B<licensecheck> B<--help>|B<--version>
  29. B<licensecheck> [B<--no-conf>] [B<--verbose>] [B<--copyright>]
  30. [B<-l>|B<--lines=>I<N>] [B<-i>|B<--ignore=>I<regex>] [B<-c>|B<--check=>I<regex>]
  31. [B<-m>|B<--machine>] [B<-r>|B<--recursive>] [B<-e>|B<--encoding=>I<...>]
  32. I<list of files and directories to check>
  33. =head1 DESCRIPTION
  34. B<licensecheck> attempts to determine the license that applies to each file
  35. passed to it, by searching the start of the file for text belonging to
  36. various licenses.
  37. If any of the arguments passed are directories, B<licensecheck> will add
  38. the files contained within to the list of files to process.
  39. =head1 OPTIONS
  40. =over 4
  41. =item B<--verbose>, B<--no-verbose>
  42. Specify whether to output the text being processed from each file before
  43. the corresponding license information.
  44. Default is to be quiet.
  45. =item B<-l=>I<N>, B<--lines=>I<N>
  46. Specify the number of lines of each file's header which should be parsed
  47. for license information. (Default is 60).
  48. =item B<--tail=>I<N>
  49. By default, the last 5k bytes of each files are parsed to get license
  50. information. You may use this option to set the size of this parsed chunk.
  51. You may set this value to 0 to avoid parsing the end of the file.
  52. =item B<-i=>I<regex>, B<--ignore=>I<regex>
  53. When processing the list of files and directories, the regular
  54. expression specified by this option will be used to indicate those which
  55. should not be considered (e.g. backup files, VCS metadata).
  56. =item B<-r>, B<--recursive>
  57. Specify that the contents of directories should be added
  58. recursively.
  59. =item B<-c=>I<regex>, B<--check=>I<regex>
  60. Specify a pattern against which filenames will be matched in order to
  61. decide which files to check the license of.
  62. The default includes common source files.
  63. =item B<-s>, B<--skipped>
  64. Specify whether to show skipped files, i.e. files found which do not
  65. match the check regexp (see C<--check> option). Default is to not show
  66. skipped files.
  67. Note that ignored files (like C<.git> or C<.svn>) are not shown even when
  68. this option is used.
  69. =item B<--copyright>
  70. Also display copyright text found within the file
  71. =item B<-e> B<--encoding>
  72. Specifies input encoding of source files. By default, input files are
  73. not decoded. When encoding is specified, license and copyright
  74. information are printed on STDOUT as utf8, or garbage if you got the
  75. encoding wrong.
  76. =item B<-m>, B<--machine>
  77. Display the information in a machine readable way, i.e. in the form
  78. <file><tab><license>[<tab><copyright>] so that it can be easily sorted
  79. and/or filtered, e.g. with the B<awk> and B<sort> commands.
  80. Note that using the B<--verbose> option will kill the readability.
  81. =item B<--no-conf>, B<--noconf>
  82. Do not read any configuration files. This can only be used as the first
  83. option given on the command line.
  84. =back
  85. =head1 CONFIGURATION VARIABLES
  86. The two configuration files F</etc/devscripts.conf> and
  87. F<~/.devscripts> are sourced by a shell in that order to set
  88. configuration variables. Command line options can be used to override
  89. configuration file settings. Environment variable settings are
  90. ignored for this purpose. The currently recognised variables are:
  91. =over 4
  92. =item B<LICENSECHECK_VERBOSE>
  93. If this is set to I<yes>, then it is the same as the B<--verbose> command
  94. line parameter being used. The default is I<no>.
  95. =item B<LICENSECHECK_PARSELINES>
  96. If this is set to a positive number then the specified number of lines
  97. at the start of each file will be read whilst attempting to determine
  98. the license(s) in use. This is equivalent to the B<--lines> command line
  99. option.
  100. =back
  101. =head1 LICENSE
  102. This code is copyright by Adam D. Barratt <I<adam@adam-barratt.org.uk>>,
  103. all rights reserved; based on a script of the same name from the KDE
  104. SDK, which is copyright by <I<dfaure@kde.org>>.
  105. This program comes with ABSOLUTELY NO WARRANTY.
  106. You are free to redistribute this code under the terms of the GNU
  107. General Public License, version 2 or later.
  108. =head1 AUTHOR
  109. Adam D. Barratt <adam@adam-barratt.org.uk>
  110. =cut
  111. # see http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/6163129#6163129
  112. use v5.14;
  113. use utf8;
  114. use strict;
  115. use autodie;
  116. use warnings;
  117. use warnings qw< FATAL utf8 >;
  118. use Getopt::Long qw(:config gnu_getopt);
  119. use File::Basename;
  120. use File::stat;
  121. use IO::File;
  122. use Fcntl qw/:seek/;
  123. binmode STDOUT, ':utf8';
  124. my $progname = basename($0);
  125. # From dpkg-source
  126. my $default_ignore_regex = qr!
  127. # Ignore general backup files
  128. ~$|
  129. # Ignore emacs recovery files
  130. (?:^|/)\.#|
  131. # Ignore vi swap files
  132. (?:^|/)\..*\.swp$|
  133. # Ignore baz-style junk files or directories
  134. (?:^|/),,.*(?:$|/.*$)|
  135. # File-names that should be ignored (never directories)
  136. (?:^|/)(?:DEADJOE|\.cvsignore|\.arch-inventory|\.bzrignore|\.gitignore)$|
  137. # File or directory names that should be ignored
  138. (?:^|/)(?:CVS|RCS|\.pc|\.deps|\{arch\}|\.arch-ids|\.svn|\.hg|_darcs|\.git|
  139. \.shelf|_MTN|\.bzr(?:\.backup|tags)?)(?:$|/.*$)
  140. !x;
  141. my $default_check_regex =
  142. qr!
  143. \.( # search for file suffix
  144. c(c|pp|xx)? # c and c++
  145. |h(h|pp|xx)? # header files for c and c++
  146. |S
  147. |css|less # HTML css and similar
  148. |f(77|90)?
  149. |go
  150. |groovy
  151. |lisp
  152. |scala
  153. |clj
  154. |p(l|m)?6?|t|xs|pod6? # perl5 or perl6
  155. |sh
  156. |php
  157. |py(|x)
  158. |rb
  159. |java
  160. |js
  161. |vala
  162. |el
  163. |sc(i|e)
  164. |cs
  165. |pas
  166. |inc
  167. |dtd|xsl
  168. |mod
  169. |m
  170. |md|markdown
  171. |tex
  172. |mli?
  173. |(c|l)?hs
  174. )
  175. $
  176. !x;
  177. # also used to cleanup
  178. my $copyright_indicator_regex
  179. = qr!
  180. (?:copyright # The full word
  181. |copr\. # Legally-valid abbreviation
  182. |\xc2\xa9 # Unicode copyright sign encoded in iso8859
  183. |\x{00a9} # Unicode character COPYRIGHT SIGN
  184. #|© # Unicode character COPYRIGHT SIGN
  185. |\(c\) # Legally-null representation of sign
  186. )
  187. !lix;
  188. my $copyright_indicator_regex_with_capture = qr!$copyright_indicator_regex(?::\s*|\s+)(\S.*)$!lix;
  189. # avoid ditching things like <info@foo.com>
  190. my $copyright_disindicator_regex
  191. = qr{
  192. \b(?:info(?:rmation)?(?!@) # Discussing copyright information
  193. |(notice|statement|claim|string)s? # Discussing the notice
  194. |is|in|to # Part of a sentence
  195. |(holder|owner)s? # Part of a sentence
  196. |ownership # Part of a sentence
  197. )\b
  198. }ix;
  199. my $copyright_predisindicator_regex
  200. = qr!(
  201. ^[#]define\s+.*\(c\) # #define foo(c) -- not copyright
  202. )!ix;
  203. my $modified_conf_msg;
  204. my %OPT=(
  205. verbose => '',
  206. lines => '',
  207. noconf => '',
  208. ignore => '',
  209. check => '',
  210. recursive => 0,
  211. copyright => 0,
  212. machine => 0,
  213. text => 0,
  214. skipped => 0,
  215. );
  216. my $def_lines = 60;
  217. my $def_tail = 5000; # roughly 60 lines of 80 chars
  218. # Read configuration files and then command line
  219. # This is boilerplate
  220. if (@ARGV and $ARGV[0] =~ /^--no-?conf$/) {
  221. $modified_conf_msg = " (no configuration files read)";
  222. shift;
  223. } else {
  224. my @config_files = ('/etc/devscripts.conf', '~/.devscripts');
  225. my %config_vars = (
  226. 'LICENSECHECK_VERBOSE' => 'no',
  227. 'LICENSECHECK_PARSELINES' => $def_lines,
  228. );
  229. my %config_default = %config_vars;
  230. my $shell_cmd;
  231. # Set defaults
  232. foreach my $var (keys %config_vars) {
  233. $shell_cmd .= qq[$var="$config_vars{$var}";\n];
  234. }
  235. $shell_cmd .= 'for file in ' . join(" ", @config_files) . "; do\n";
  236. $shell_cmd .= '[ -f $file ] && . $file; done;' . "\n";
  237. # Read back values
  238. foreach my $var (keys %config_vars) { $shell_cmd .= "echo \$$var;\n" }
  239. my $shell_out = `/bin/bash -c '$shell_cmd'`;
  240. @config_vars{keys %config_vars} = split /\n/, $shell_out, -1;
  241. # Check validity
  242. $config_vars{'LICENSECHECK_VERBOSE'} =~ /^(yes|no)$/
  243. or $config_vars{'LICENSECHECK_VERBOSE'} = 'no';
  244. $config_vars{'LICENSECHECK_PARSELINES'} =~ /^[1-9][0-9]*$/
  245. or $config_vars{'LICENSECHECK_PARSELINES'} = $def_lines;
  246. foreach my $var (sort keys %config_vars) {
  247. if ($config_vars{$var} ne $config_default{$var}) {
  248. $modified_conf_msg .= " $var=$config_vars{$var}\n";
  249. }
  250. }
  251. $modified_conf_msg ||= " (none)\n";
  252. chomp $modified_conf_msg;
  253. $OPT{'verbose'} = $config_vars{'LICENSECHECK_VERBOSE'} eq 'yes' ? 1 : 0;
  254. $OPT{'lines'} = $config_vars{'LICENSECHECK_PARSELINES'};
  255. }
  256. GetOptions(\%OPT,
  257. "help|h",
  258. "check|c=s",
  259. "copyright",
  260. "encoding|e=s",
  261. "ignore|i=s",
  262. "lines|l=i",
  263. "machine|m",
  264. "noconf|no-conf",
  265. "recursive|r",
  266. "skipped|s",
  267. "tail",
  268. "text|t",
  269. "verbose!",
  270. "version|v",
  271. ) or die "Usage: $progname [options] filelist\nRun $progname --help for more details\n";
  272. $OPT{'lines'} = $def_lines if $OPT{'lines'} !~ /^[1-9][0-9]*$/;
  273. my $ignore_regex = length($OPT{ignore}) ? qr/$OPT{ignore}/ : $default_ignore_regex;
  274. my $check_regex = $default_check_regex;
  275. $check_regex = qr/$OPT{check}/ if length $OPT{check};
  276. if ($OPT{'noconf'}) {
  277. fatal("--no-conf is only acceptable as the first command-line option!");
  278. }
  279. if ($OPT{'help'}) { help(); exit 0; }
  280. if ($OPT{'version'}) { version(); exit 0; }
  281. if ($OPT{text}) {
  282. warn "$0 warning: option -text is deprecated\n"; # remove -text end 2015
  283. }
  284. die "Usage: $progname [options] filelist\nRun $progname --help for more details\n" unless @ARGV;
  285. $OPT{'lines'} = $def_lines if not defined $OPT{'lines'};
  286. my @files = ();
  287. my @find_args = ();
  288. my $files_count = @ARGV;
  289. push @find_args, qw(-maxdepth 1) unless $OPT{'recursive'};
  290. push @find_args, qw(-follow -type f -print);
  291. while (@ARGV) {
  292. my $file = shift @ARGV;
  293. if (-d $file) {
  294. open my $FIND, '-|', 'find', $file, @find_args
  295. or die "$progname: couldn't exec find: $!\n";
  296. while (my $found = <$FIND>) {
  297. chomp ($found);
  298. # Silently skip empty files or ignored files
  299. next if -z $found or $found =~ $ignore_regex;
  300. if ( not $check_regex or $found =~ $check_regex ) {
  301. # Silently skip empty files or ignored files
  302. push @files, $found ;
  303. }
  304. else {
  305. warn "skipped file $found\n" if $OPT{skipped};
  306. }
  307. }
  308. close $FIND;
  309. }
  310. elsif ($file =~ $ignore_regex) {
  311. # Silently skip ignored files
  312. next;
  313. }
  314. elsif ( $files_count == 1 or not $check_regex or $file =~ $check_regex ) {
  315. push @files, $file;
  316. }
  317. else {
  318. warn "skipped file $file\n" if $OPT{skipped};
  319. }
  320. }
  321. while (@files) {
  322. my $file = shift @files;
  323. my $content = '';
  324. my $copyright_match;
  325. my $copyright = '';
  326. my $st = stat $file;
  327. my $enc = $OPT{encoding} ;
  328. my $mode = $enc ? "<:encoding($enc)" : '<';
  329. # need to use "<" when encoding is unknown otherwise we break compatibility
  330. my $fh = IO::File->new ($file ,$mode) or die "Unable to access $file\n";
  331. while ( my $line = $fh->getline ) {
  332. last if ($fh->input_line_number > $OPT{'lines'});
  333. $content .= $line;
  334. }
  335. my %copyrights = extract_copyright($content);
  336. print qq(----- $file header -----\n$content----- end header -----\n\n)
  337. if $OPT{'verbose'};
  338. my $license = parselicense(clean_cruft_and_spaces(clean_comments($content)));
  339. $copyright = join(" / ", reverse sort values %copyrights);
  340. if ( not $copyright and $license eq 'UNKNOWN') {
  341. my $position = $fh->tell; # See IO::Seekable
  342. my $tail_size = $OPT{tail} // $def_tail;
  343. my $jump = $st->size - $tail_size;
  344. $jump = $position if $jump < $position;
  345. my $tail ;
  346. if ( $tail_size and $jump < $st->size) {
  347. $fh->seek($jump, SEEK_SET) ; # also IO::Seekable
  348. $tail .= join('',$fh->getlines);
  349. }
  350. print qq(----- $file tail -----\n$tail----- end tail -----\n\n)
  351. if $OPT{'verbose'};
  352. %copyrights = extract_copyright($tail);
  353. $license = parselicense(clean_cruft_and_spaces(clean_comments($tail)));
  354. $copyright = join(" / ", reverse sort values %copyrights);
  355. }
  356. $fh->close;
  357. if ($OPT{'machine'}) {
  358. print "$file\t$license";
  359. print "\t" . ($copyright or "*No copyright*") if $OPT{'copyright'};
  360. print "\n";
  361. } else {
  362. print "$file: ";
  363. print "*No copyright* " unless $copyright;
  364. print $license . "\n";
  365. print " [Copyright: " . $copyright . "]\n"
  366. if $copyright and $OPT{'copyright'};
  367. print "\n" if $OPT{'copyright'};
  368. }
  369. }
  370. sub extract_copyright {
  371. my $content = shift;
  372. my @c = split /\n/, clean_comments($content);
  373. my %copyrights;
  374. my $lines_after_copyright_block = 0;
  375. my $in_copyright_block = 0;
  376. while (@c) {
  377. my $line = shift @c ;
  378. my $copyright_match = parse_copyright($line, \$in_copyright_block) ;
  379. if ($copyright_match) {
  380. while (@c and $copyright_match =~ /\d[,.]?\s*$/) {
  381. # looks like copyright end with a year, assume the owner is on next line(s)
  382. $copyright_match .= ' '. shift @c;
  383. }
  384. $copyright_match =~ s/\s+/ /g;
  385. $copyright_match =~ s/\s*$//;
  386. $copyrights{lc("$copyright_match")} = "$copyright_match";
  387. }
  388. elsif (scalar keys %copyrights) {
  389. # skip remaining lines if a copyright blocks was found more than 5 lines ago.
  390. # so a copyright block may contain up to 5 blank lines, but no more
  391. last if $lines_after_copyright_block++ > 5;
  392. }
  393. }
  394. return %copyrights;
  395. }
  396. sub parse_copyright {
  397. my $data = shift ;
  398. my $in_copyright_block_ref = shift;
  399. my $copyright = '';
  400. my $match;
  401. if ( $data !~ $copyright_predisindicator_regex) {
  402. #print "match against ->$data<-\n";
  403. if ($data =~ $copyright_indicator_regex_with_capture) {
  404. $match = $1;
  405. $$in_copyright_block_ref = 1;
  406. # Ignore lines matching "see foo for copyright information" etc.
  407. if ($match !~ $copyright_disindicator_regex) {
  408. # De-cruft
  409. $match =~ s/$copyright_indicator_regex//igx;
  410. $match =~ s/^\s+//;
  411. $match =~ s/\s*\bby\b\s*/ /;
  412. $match =~ s/([,.])?\s*$//;
  413. $match =~ s/\s{2,}/ /g;
  414. $match =~ s/\\//g; # de-cruft nroff files
  415. $match =~ s/\s*[*#]\s*$//;
  416. $copyright = $match;
  417. }
  418. }
  419. elsif ($$in_copyright_block_ref and $data =~ /^\d{2,}[,\s]+/) {
  420. # following lines beginning with a year are supposed to be
  421. # continued copyright blocks
  422. $copyright = $data;
  423. }
  424. else {
  425. $$in_copyright_block_ref = 0;
  426. }
  427. }
  428. return $copyright;
  429. }
  430. sub clean_comments {
  431. local $_ = shift or return q{};
  432. # Remove generic comments: look for 4 or more lines beginning with
  433. # regular comment pattern and trim it. Fall back to old algorithm
  434. # if no such pattern found.
  435. my @matches = m/^\s*((?:[^a-zA-Z0-9\s]{1,3}|\bREM\b))\s\w/mg;
  436. if (@matches >= 4) {
  437. my $comment_re = qr/\s*[\Q$matches[0]\E]{1,3}\s*/;
  438. s/^$comment_re//mg;
  439. }
  440. # Remove Fortran comments
  441. s/^[cC] //gm;
  442. # Remove C / C++ comments
  443. s#(\*/|/[/*])##g;
  444. return $_;
  445. }
  446. sub clean_cruft_and_spaces {
  447. local $_ = shift or return q{};
  448. tr/\t\r\n/ /;
  449. # this also removes quotes
  450. tr% A-Za-z.+,@:;0-9\(\)/-%%cd;
  451. tr/ //s;
  452. return $_;
  453. }
  454. sub help {
  455. print <<"EOF";
  456. Usage: $progname [options] filename [filename ...]
  457. Valid options are:
  458. --help, -h Display this message
  459. --version, -v Display version and copyright info
  460. --no-conf, --noconf Don't read devscripts config files; must be
  461. the first option given
  462. --verbose Display the header of each file before its
  463. license information
  464. --skipped, -s Show skipped files
  465. --lines, -l Specify how many lines of the file header
  466. should be parsed for license information
  467. (Default: $def_lines)
  468. --tail Specify how many bytes to parse at end of file
  469. (Default: $def_tail)
  470. --check, -c Specify a pattern indicating which files should
  471. be checked
  472. (Default: '$default_check_regex')
  473. --machine, -m Display in a machine readable way (good for awk)
  474. --recursive, -r Add the contents of directories recursively
  475. --copyright Also display the file's copyright
  476. --ignore, -i Specify that files / directories matching the
  477. regular expression should be ignored when
  478. checking files
  479. (Default: '$default_ignore_regex')
  480. Default settings modified by devscripts configuration files:
  481. $modified_conf_msg
  482. EOF
  483. }
  484. sub version {
  485. print <<"EOF";
  486. This is $progname, from the Debian devscripts package, version 2.16.2
  487. Copyright (C) 2007, 2008 by Adam D. Barratt <adam\@adam-barratt.org.uk>; based
  488. on a script of the same name from the KDE SDK by <dfaure\@kde.org>.
  489. This program comes with ABSOLUTELY NO WARRANTY.
  490. You are free to redistribute this code under the terms of the
  491. GNU General Public License, version 2, or (at your option) any
  492. later version.
  493. EOF
  494. }
  495. sub parselicense {
  496. my ($licensetext) = @_;
  497. my $gplver = "";
  498. my $extrainfo = "";
  499. my $license = "";
  500. if ($licensetext =~ /version ([^ ]+)(?: of the License)?,? or(?: \(at your option\))? version (\d(?:[.-]\d+)*)/) {
  501. $gplver = " (v$1 or v$2)";
  502. } elsif ($licensetext =~ /version ([^, ]+?)[.,]? (?:\(?only\)?.? )?(?:of the GNU (Affero )?(Lesser |Library )?General Public License )?(as )?published by the Free Software Foundation/i or
  503. $licensetext =~ /GNU (?:Affero )?(?:Lesser |Library )?General Public License (?:as )?published by the Free Software Foundation[;,] version ([^, ]+?)[.,]? /i) {
  504. $gplver = " (v$1)";
  505. } elsif ($licensetext =~ /GNU (?:Affero )?(?:Lesser |Library )?General Public License\s*(?:[(),GPL]+)\s*version (\d+(?:\.\d+)?)[ \.]/i) {
  506. $gplver = " (v$1)";
  507. } elsif ($licensetext =~ /either version ([^ ]+)(?: of the License)?, or (?:\(at your option\) )?any later version/) {
  508. $gplver = " (v$1 or later)";
  509. } elsif ($licensetext =~ /GPL\sas\spublished\sby\sthe\sFree\sSoftware\sFoundation,\sversion\s([\d.]+)/i ) {
  510. $gplver = " (v$1)";
  511. } elsif ($licensetext =~ /SPDX-License-Identifier:\s+GPL-([1-9])\.0-or-later/i ){
  512. $gplver = " (v$1 or later)";
  513. } elsif ($licensetext =~ /SPDX-License-Identifier:\s+GPL-([1-9])\.0[^+]/i ) {
  514. $gplver = " (v$1)";
  515. } elsif ($licensetext =~ /SPDX-License-Identifier:\s+GPL-([1-9])\.0\+/i ) {
  516. $gplver = " (v$1 or later)";
  517. } elsif ($licensetext =~ /SPDX-License-Identifier:\s+LGPL-([1-9])\.[0-1]\-or-later/i ) {
  518. $gplver = " (v$1 or later)";
  519. }
  520. if ($licensetext =~ /(?:675 Mass Ave|59 Temple Place|51 Franklin Steet|02139|02111-1307)/i) {
  521. $extrainfo = " (with incorrect FSF address)$extrainfo";
  522. }
  523. if ($licensetext =~ /permission (?:is (also granted|given))? to link (the code of )?this program with (any edition of )?(Qt|the Qt library)/i) {
  524. $extrainfo = " (with Qt exception)$extrainfo"
  525. }
  526. if ($licensetext =~ /As a special exception, you may create a larger work that contains part or all of the Bison parser skeleton and distribute that work under terms of your choice/) {
  527. $extrainfo = " (with Bison parser exception)$extrainfo";
  528. }
  529. # exclude blurb found in boost license text
  530. if ($licensetext =~ /(All changes made in this file will be lost|DO NOT (EDIT|delete this file)|Generated (automatically|by|from)|generated.*file)/i
  531. and $licensetext !~ /unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor/) {
  532. $license = "GENERATED FILE";
  533. }
  534. if ($licensetext =~ /(are made available|(is free software.? )?you can redistribute (it|them) and(?:\/|\s+)or modify (it|them)|is licensed) under the terms of (version [^ ]+ of )?the (GNU (Library |Lesser )General Public License|LGPL)/i) {
  535. $license = "LGPL$gplver$extrainfo $license";
  536. }
  537. # For Perl modules handled by Dist::Zilla
  538. elsif ($licensetext =~ /this is free software,? licensed under:? (?:the )?(?:GNU (?:Library |Lesser )General Public License|LGPL),? version ([\d\.]+)/i) {
  539. $license = "LGPL (v$1) $license";
  540. }
  541. if ($licensetext =~ /is free software.? you can redistribute (it|them) and(?:\/|\s+)or modify (it|them) under the terms of the (GNU Affero General Public License|AGPL)/i) {
  542. $license = "AGPL$gplver$extrainfo $license";
  543. }
  544. if ($licensetext =~ /(is free software.? )?you (can|may) redistribute (it|them) and(?:\/|\s+)or modify (it|them) under the terms of (?:version [^ ]+ (?:\(?only\)? )?of )?the GNU General Public License/i) {
  545. $license = "GPL$gplver$extrainfo $license";
  546. }
  547. if ($licensetext =~ /is distributed under the terms of the GNU General Public License,/
  548. and length $gplver) {
  549. $license = "GPL$gplver$extrainfo $license";
  550. }
  551. if ($licensetext =~ /SPDX-License-Identifier:\s+GPL/i and length $gplver) {
  552. $license = "GPL$gplver$extrainfo $license";
  553. }
  554. if ($licensetext =~ /SPDX-License-Identifier:\s+GPL-2.0-or-later/i and length $gplver) {
  555. $license = "GPL$gplver$extrainfo";
  556. }
  557. if ($licensetext =~ /SPDX-License-Identifier:\s+LGPL/i and length $gplver) {
  558. $license = "LGPL$gplver$extrainfo $license";
  559. }
  560. if ($licensetext =~ /SPDX-License-Identifier:\s+Zlib/i) {
  561. $license = "zlib/libpng $license";
  562. }
  563. if ($licensetext =~ /SPDX-License-Identifier:\s+BSD-3-Clause/i) {
  564. $license = 'BSD (3 clause)';
  565. }
  566. if ($licensetext =~ /SPDX-License-Identifier:\s+BSD-2-Clause/i) {
  567. $license = 'BSD (2 clause)';
  568. }
  569. if ($licensetext =~ /SPDX-License-Identifier:\s+BSD-1-Clause/i) {
  570. $license = 'BSD';
  571. }
  572. if ($licensetext =~ /SPDX-License-Identifier:\s+MIT/i) {
  573. $license = 'MIT/X11 (BSD like)';
  574. }
  575. if ($licensetext =~ /SPDX-License-Identifier:\s+ISC/i) {
  576. $license = 'ISC';
  577. }
  578. if ($licensetext =~ /(?:is|may be)\s(?:(?:distributed|used).*?terms|being\s+released).*?\b(L?GPL)\b/) {
  579. my $v = $gplver || ' (unversioned/unknown version)';
  580. $license = "$1$v $license";
  581. }
  582. if ($licensetext =~ /the rights to distribute and use this software as governed by the terms of the Lisp Lesser General Public License|\bLLGPL\b/ ) {
  583. $license = "LLGPL $license";
  584. }
  585. if ($licensetext =~ /This file is part of the .*Qt GUI Toolkit. This file may be distributed under the terms of the Q Public License as defined/) {
  586. $license = "QPL (part of Qt) $license";
  587. } elsif ($licensetext =~ /may (be distributed|redistribute it) under the terms of the Q Public License/) {
  588. $license = "QPL $license";
  589. }
  590. if ($licensetext =~ /opensource\.org\/licenses\/mit-license\.php/) {
  591. $license = "MIT/X11 (BSD like) $license";
  592. } elsif ($licensetext =~ /Permission is hereby granted, free of charge, to any person obtaining a copy of this software and(\/or)? associated documentation files \(the (Software|Materials)\), to deal in the (Software|Materials)/) {
  593. $license = "MIT/X11 (BSD like) $license";
  594. } elsif ($licensetext =~ /Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose/) {
  595. $license = "MIT/X11 (BSD like) $license";
  596. }
  597. if ($licensetext =~ /Permission to use, copy, modify, and(\/or)? distribute this software for any purpose with or without fee is hereby granted, provided.*copyright notice.*permission notice.*all copies/) {
  598. $license = "ISC $license";
  599. }
  600. if ($licensetext =~ /THIS SOFTWARE IS PROVIDED .*AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY/) {
  601. if ($licensetext =~ /All advertising materials mentioning features or use of this software must display the following acknowledge?ment.*This product includes software developed by/i) {
  602. $license = "BSD (4 clause) $license";
  603. } elsif ($licensetext =~ /(The name(?:\(s\))? .*? may not|Neither the (names? .*?|authors?) nor the names of( (its|their|other|any))? contributors may) be used to endorse or promote products derived from this software/i) {
  604. $license = "BSD (3 clause) $license";
  605. } elsif ($licensetext =~ /Redistributions in binary form must reproduce the above copyright notice/i) {
  606. $license = "BSD (2 clause) $license";
  607. } else {
  608. $license = "BSD $license";
  609. }
  610. }
  611. if ($licensetext =~ /Mozilla Public License,? (?:(?:Version|v\.)\s+)?(\d+(?:\.\d+)?)/) {
  612. $license = "MPL (v$1) $license";
  613. }
  614. elsif ($licensetext =~ /Mozilla Public License,? \((?:Version|v\.) (\d+(?:\.\d+)?)\)/) {
  615. $license = "MPL (v$1) $license";
  616. }
  617. # match when either:
  618. # - the text *begins* with "The Artistic license v2.0" which is (hopefully) the actual artistic license v2.0 text.
  619. # - a license grant is found. i.e something like "this is free software, licensed under the artistic license v2.0"
  620. if ($licensetext =~ /(?:^\s*|(?:This is free software, licensed|Released|be used|use and modify this (?:module|software)) under (?:the terms of )?)[Tt]he Artistic License ([v\d.]*\d)/) {
  621. $license = "Artistic (v$1) $license";
  622. }
  623. if ($licensetext =~ /is free software under the Artistic [Ll]icense/) {
  624. $license = "Artistic $license";
  625. }
  626. if ($licensetext =~ /This program is free software; you can redistribute it and\/or modify it under the same terms as Perl itself/) {
  627. $license = "Perl $license";
  628. }
  629. if ($licensetext =~ /under the Apache License, Version ([^ ]+)/) {
  630. $license = "Apache (v$1) $license";
  631. }
  632. if ($licensetext =~ /(THE BEER-WARE LICENSE)/i) {
  633. $license = "Beerware $license";
  634. }
  635. if ($licensetext =~ /distributed under the terms of the FreeType project/i) {
  636. $license = "FreeType $license"; # aka FTL see http://www.freetype.org/license.html
  637. }
  638. if ($licensetext =~ /This source file is subject to version ([^ ]+) of the PHP license/) {
  639. $license = "PHP (v$1) $license";
  640. }
  641. if ($licensetext =~ /under the terms of the CeCILL /) {
  642. $license = "CeCILL $license";
  643. }
  644. if ($licensetext =~ /under the terms of the CeCILL-([^ ]+) /) {
  645. $license = "CeCILL-$1 $license";
  646. }
  647. if ($licensetext =~ /under the SGI Free Software License B/) {
  648. $license = "SGI Free Software License B $license";
  649. }
  650. if ($licensetext =~ /is in the public domain/i) {
  651. $license = "Public domain $license";
  652. }
  653. if ($licensetext =~ /terms of the Common Development and Distribution License(, Version ([^(]+))? \(the License\)/) {
  654. $license = "CDDL " . ($1 ? "(v$2) " : '') . $license;
  655. }
  656. if ($licensetext =~ /Microsoft Permissive License \(Ms-PL\)/) {
  657. $license = "Ms-PL $license";
  658. }
  659. if ($licensetext =~ /Licensed under the Academic Free License version ([\d.]+)/) {
  660. $license = $1 ? "AFL-$1" : "AFL";
  661. }
  662. if ($licensetext =~ /This program and the accompanying materials are made available under the terms of the Eclipse Public License v?([\d.]+)/) {
  663. $license = $1 ? "EPL-$1" : "EPL";
  664. }
  665. # quotes were removed by clean_comments function
  666. if ($licensetext =~ /Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license \(the Software\)/ or
  667. $licensetext =~ /Boost Software License([ ,-]+Version ([^ ]+)?(\.))/i) {
  668. $license = "BSL " . ($1 ? "(v$2) " : '') . $license;
  669. }
  670. if ($licensetext =~ /PYTHON SOFTWARE FOUNDATION LICENSE (VERSION ([^ ]+))/i) {
  671. $license = "PSF " . ($1 ? "(v$2) " : '') . $license;
  672. }
  673. if ($licensetext =~ /The origin of this software must not be misrepresented.*Altered source versions must be plainly marked as such.*This notice may not be removed or altered from any source distribution/ or
  674. $licensetext =~ /see copyright notice in zlib\.h/) {
  675. $license = "zlib/libpng $license";
  676. } elsif ($licensetext =~ /This code is released under the libpng license/) {
  677. $license = "libpng $license";
  678. }
  679. if ($licensetext =~ /Do What The Fuck You Want To Public License, Version ([^, ]+)/i) {
  680. $license = "WTFPL (v$1) $license";
  681. }
  682. if ($licensetext =~ /Do what The Fuck You Want To Public License/i) {
  683. $license = "WTFPL $license";
  684. }
  685. if ($licensetext =~ /(License WTFPL|Under (the|a) WTFPL)/i) {
  686. $license = "WTFPL $license";
  687. }
  688. if ($licensetext =~ /SPDX-License-Identifier:\s+\(([a-zA-Z0-9-\.]+)\s+OR\s+([a-zA-Z0-9-\.]+)\)/i) {
  689. # print STDERR "OK ---$1---$2---";
  690. # print "PIPPO " . parselicense("SPDX-License-Identifier: $1") . " E " . parselicense("SPDX-License-Identifier: $2");
  691. if ($1 and $2) {
  692. $license = parselicense("SPDX-License-Identifier: $1") . " " . parselicense("SPDX-License-Identifier: $2");
  693. }
  694. }
  695. $license = "UNKNOWN" if (!length($license));
  696. # Remove trailing spaces.
  697. $license =~ s/\s+$//;
  698. return $license;
  699. }
  700. sub fatal {
  701. my ($pack,$file,$line);
  702. ($pack,$file,$line) = caller();
  703. (my $msg = "$progname: fatal error at line $line:\n@_\n") =~ tr/\0//d;
  704. $msg =~ s/\n\n$/\n/;
  705. die $msg;
  706. }