2 # This script was originally based on the script of the same name from
3 # the KDE SDK (by dfaure@kde.org)
6 # Copyright (C) 2007, 2008 Adam D. Barratt
7 # Copyright (C) 2012 Francesco Poli
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program. If not, see <http://www.gnu.org/licenses/>.
24 licensecheck - simple license checker for source files
28 B<licensecheck> B<--help>|B<--version>
30 B<licensecheck> [B<--no-conf>] [B<--verbose>] [B<--copyright>]
31 [B<-l>|B<--lines=>I<N>] [B<-i>|B<--ignore=>I<regex>] [B<-c>|B<--check=>I<regex>]
32 [B<-m>|B<--machine>] [B<-r>|B<--recursive>]
33 I<list of files and directories to check>
37 B<licensecheck> attempts to determine the license that applies to each file
38 passed to it, by searching the start of the file for text belonging to
41 If any of the arguments passed are directories, B<licensecheck> will add
42 the files contained within to the list of files to process.
48 =item B<--verbose>, B<--no-verbose>
50 Specify whether to output the text being processed from each file before
51 the corresponding license information.
53 Default is to be quiet.
55 =item B<-l=>I<N>, B<--lines=>I<N>
57 Specify the number of lines of each file's header which should be parsed
58 for license information. (Default is 60).
60 =item B<-i=>I<regex>, B<--ignore=>I<regex>
62 When processing the list of files and directories, the regular
63 expression specified by this option will be used to indicate those which
64 should not be considered (e.g. backup files, VCS metadata).
66 =item B<-r>, B<--recursive>
68 Specify that the contents of directories should be added
71 =item B<-c=>I<regex>, B<--check=>I<regex>
73 Specify a pattern against which filenames will be matched in order to
74 decide which files to check the license of.
76 The default includes common source files.
80 Also display copyright text found within the file
82 =item B<-m>, B<--machine>
84 Display the information in a machine readable way, i.e. in the form
85 <file><tab><license>[<tab><copyright>] so that it can be easily sorted
86 and/or filtered, e.g. with the B<awk> and B<sort> commands.
87 Note that using the B<--verbose> option will kill the readability.
89 =item B<--no-conf>, B<--noconf>
91 Do not read any configuration files. This can only be used as the first
92 option given on the command-line.
96 =head1 CONFIGURATION VARIABLES
98 The two configuration files F</etc/devscripts.conf> and
99 F<~/.devscripts> are sourced by a shell in that order to set
100 configuration variables. Command line options can be used to override
101 configuration file settings. Environment variable settings are
102 ignored for this purpose. The currently recognised variables are:
106 =item B<LICENSECHECK_VERBOSE>
108 If this is set to I<yes>, then it is the same as the B<--verbose> command
109 line parameter being used. The default is I<no>.
111 =item B<LICENSECHECK_PARSELINES>
113 If this is set to a positive number then the specified number of lines
114 at the start of each file will be read whilst attempting to determine
115 the license(s) in use. This is equivalent to the B<--lines> command line
122 This code is copyright by Adam D. Barratt <I<adam@adam-barratt.org.uk>>,
123 all rights reserved; based on a script of the same name from the KDE
124 SDK, which is copyright by <I<dfaure@kde.org>>.
125 This program comes with ABSOLUTELY NO WARRANTY.
126 You are free to redistribute this code under the terms of the GNU
127 General Public License, version 2 or later.
131 Adam D. Barratt <adam@adam-barratt.org.uk>
137 use Getopt
::Long
qw(:config gnu_getopt);
141 sub parse_copyright
($);
144 my $progname = basename
($0);
147 my $default_ignore_regex = '
148 # Ignore general backup files
150 # Ignore emacs recovery files
152 # Ignore vi swap files
154 # Ignore baz-style junk files or directories
155 (?:^|/),,.*(?:$|/.*$)|
156 # File-names that should be ignored (never directories)
157 (?:^|/)(?:DEADJOE|\.cvsignore|\.arch-inventory|\.bzrignore|\.gitignore)$|
158 # File or directory names that should be ignored
159 (?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|\.hg|_darcs|\.git|
160 \.shelf|_MTN|\.bzr(?:\.backup|tags)?)(?:$|/.*$)
163 # Take out comments and newlines
164 $default_ignore_regex =~ s/^#.*$//mg;
165 $default_ignore_regex =~ s/\n//sg;
167 my $default_check_regex = '\.(c(c|pp|xx)?|h(h|pp|xx)?|f(77|90)?|p(l|m)|xs|sh|php|py(|x)|rb|java|vala|el|sc(i|e)|cs|pas|inc|dtd|xsl|mod|m|tex|mli?)$';
169 my $modified_conf_msg;
171 my ($opt_verbose, $opt_lines, $opt_noconf, $opt_ignore_regex, $opt_check_regex)
172 = ('', '', '', '', '');
173 my $opt_recursive = 0;
174 my $opt_copyright = 0;
176 my ($opt_help, $opt_version);
179 # Read configuration files and then command line
180 # This is boilerplate
182 if (@ARGV and $ARGV[0] =~ /^--no-?conf$/) {
183 $modified_conf_msg = " (no configuration files read)";
186 my @config_files = ('/etc/devscripts.conf', '~/.devscripts');
188 'LICENSECHECK_VERBOSE' => 'no',
189 'LICENSECHECK_PARSELINES' => $def_lines,
191 my %config_default = %config_vars;
195 foreach my $var (keys %config_vars) {
196 $shell_cmd .= qq[$var="$config_vars{$var}";\n];
198 $shell_cmd .= 'for file in ' . join(" ", @config_files) . "; do\n";
199 $shell_cmd .= '[ -f $file ] && . $file; done;' . "\n";
201 foreach my $var (keys %config_vars) { $shell_cmd .= "echo \$$var;\n" }
202 my $shell_out = `/bin/bash -c '$shell_cmd'`;
203 @config_vars{keys %config_vars} = split /\n/, $shell_out, -1;
206 $config_vars{'LICENSECHECK_VERBOSE'} =~ /^(yes|no)$/
207 or $config_vars{'LICENSECHECK_VERBOSE'} = 'no';
208 $config_vars{'LICENSECHECK_PARSELINES'} =~ /^[1-9][0-9]*$/
209 or $config_vars{'LICENSECHECK_PARSELINES'} = $def_lines;
211 foreach my $var (sort keys %config_vars) {
212 if ($config_vars{$var} ne $config_default{$var}) {
213 $modified_conf_msg .= " $var=$config_vars{$var}\n";
216 $modified_conf_msg ||= " (none)\n";
217 chomp $modified_conf_msg;
219 $opt_verbose = $config_vars{'LICENSECHECK_VERBOSE'} eq 'yes' ?
1 : 0;
220 $opt_lines = $config_vars{'LICENSECHECK_PARSELINES'};
223 GetOptions
("help|h" => \
$opt_help,
224 "version|v" => \
$opt_version,
225 "verbose!" => \
$opt_verbose,
226 "lines|l=i" => \
$opt_lines,
227 "ignore|i=s" => \
$opt_ignore_regex,
228 "recursive|r" => \
$opt_recursive,
229 "check|c=s" => \
$opt_check_regex,
230 "copyright" => \
$opt_copyright,
231 "machine|m" => \
$opt_machine,
232 "noconf" => \
$opt_noconf,
233 "no-conf" => \
$opt_noconf,
235 or die "Usage: $progname [options] filelist\nRun $progname --help for more details\n";
237 $opt_lines = $def_lines if $opt_lines !~ /^[1-9][0-9]*$/;
238 $opt_ignore_regex = $default_ignore_regex if ! length $opt_ignore_regex;
239 $opt_check_regex = $default_check_regex if ! length $opt_check_regex;
242 fatal
"--no-conf is only acceptable as the first command-line option!";
244 if ($opt_help) { help
(); exit 0; }
245 if ($opt_version) { version
(); exit 0; }
247 die "Usage: $progname [options] filelist\nRun $progname --help for more details\n" unless @ARGV;
249 $opt_lines = $def_lines if not defined $opt_lines;
253 my $files_count = @ARGV;
255 push @find_args, qw(-maxdepth 1) unless $opt_recursive;
256 push @find_args, qw(-follow -type f -print);
259 my $file = shift @ARGV;
262 open FIND
, '-|', 'find', $file, @find_args
263 or die "$progname: couldn't exec find: $!\n";
267 next unless m
%$opt_check_regex%;
270 push @files, $_ unless m
%$opt_ignore_regex%;
274 next unless ($files_count == 1) or $file =~ m
%$opt_check_regex%;
275 push @files, $file unless $file =~ m
%$opt_ignore_regex%;
280 my $file = shift @files;
287 open (F
, "<$file") or die "Unable to access $file\n";
289 last if ($. > $opt_lines);
291 $copyright_match = parse_copyright
($_);
292 if ($copyright_match) {
293 $copyrights{lc("$copyright_match")} = "$copyright_match";
298 $copyright = join(" / ", values %copyrights);
300 print qq(----- $file header
-----\n$content----- end header
-----\n\n)
303 # Remove Fortran comments
304 $content =~ s/^[cC] //gm;
305 $content =~ tr/\t\r\n/ /;
306 # Remove C / C++ comments
307 $content =~ s
#(\*/|/[/*])##g;
308 $content =~ tr
% A
-Za
-z
.,@
;0-9\
(\
)/-%%cd;
311 $license = parselicense
($content);
313 print "$file\t$license";
314 print "\t" . ($copyright or "*No copyright*") if $opt_copyright;
318 print "*No copyright* " unless $copyright;
319 print $license . "\n";
320 print " [Copyright: " . $copyright . "]\n"
321 if $copyright and $opt_copyright;
322 print "\n" if $opt_copyright;
326 sub parse_copyright
($) {
330 my $copyright_indicator_regex = '
331 (?:copyright # The full word
332 |copr\. # Legally-valid abbreviation
333 |\x{00a9} # Unicode character COPYRIGHT SIGN
334 |\xc2\xa9 # Unicode copyright sign encoded in iso8859
335 |\(c\) # Legally-null representation of sign
337 my $copyright_disindicator_regex = '
338 \b(?:info(?:rmation)? # Discussing copyright information
339 |notice # Discussing the notice
340 |and|or # Part of a sentence
343 if (m
%$copyright_indicator_regex(?
::\s
*|\s
+)(\S
.*)$%ix) {
346 # Ignore lines matching "see foo for copyright information" etc.
347 if ($match !~ m
%^\s
*$copyright_disindicator_regex%ix) {
349 $match =~ s/([,.])?\s*$//;
350 $match =~ s/$copyright_indicator_regex//igx;
352 $match =~ s/\s{2,}/ /g;
363 Usage: $progname [options] filename [filename ...]
365 --help, -h Display this message
366 --version, -v Display version and copyright info
367 --no-conf, --noconf Don't read devscripts config files; must be
368 the first option given
369 --verbose Display the header of each file before its
371 --lines, -l Specify how many lines of the file header
372 should be parsed for license information
373 (Default: $def_lines)
374 --check, -c Specify a pattern indicating which files should
376 (Default: '$default_check_regex')
377 --machine, -m Display in a machine readable way (good for awk)
378 --recursive, -r Add the contents of directories recursively
379 --copyright Also display the file's copyright
380 --ignore, -i Specify that files / directories matching the
381 regular expression should be ignored when
383 (Default: '$default_ignore_regex')
385 Default settings modified by devscripts configuration files:
392 This is $progname, from the Debian devscripts package, version ###VERSION###
393 Copyright (C) 2007, 2008 by Adam D. Barratt <adam\@adam-barratt.org.uk>; based
394 on a script of the same name from the KDE SDK by <dfaure\@kde.org>.
396 This program comes with ABSOLUTELY NO WARRANTY.
397 You are free to redistribute this code under the terms of the
398 GNU General Public License, version 2, or (at your option) any
403 sub parselicense
($) {
404 my ($licensetext) = @_;
410 if ($licensetext =~ /version ([^, ]+?)[.,]? (?:\(?only\)?.? )?(?:of the GNU (Affero )?(Lesser |Library )?General Public License )?(as )?published by the Free Software Foundation/i or
411 $licensetext =~ /GNU (?:Affero )?(?:Lesser |Library )?General Public License (?:as )?published by the Free Software Foundation; version ([^, ]+?)[.,]? /i) {
414 } elsif ($licensetext =~ /GNU (?:Affero )?(?:Lesser |Library )?General Public License, version (\d+(?:\.\d+)?)[ \.]/) {
416 } elsif ($licensetext =~ /either version ([^ ]+)(?: of the License)?, or \(at your option\) any later version/) {
417 $gplver = " (v$1 or later)";
420 if ($licensetext =~ /(?:675 Mass Ave|59 Temple Place|51 Franklin Steet|02139|02111-1307)/i) {
421 $extrainfo = " (with incorrect FSF address)$extrainfo";
424 if ($licensetext =~ /permission (?:is (also granted|given))? to link (the code of )?this program with (any edition of )?(Qt|the Qt library)/i) {
425 $extrainfo = " (with Qt exception)$extrainfo"
428 if ($licensetext =~ /(All changes made in this file will be lost|DO NOT (EDIT|delete this file)|Generated (automatically|by|from)|generated.*file)/i) {
429 $license = "GENERATED FILE";
432 if ($licensetext =~ /is (free software.? you can redistribute it and\/or modify it
|licensed
) under the terms of
(version
[^ ]+ of
)?the
(GNU
(Library
|Lesser
)General Public License
|LGPL
)/i
) {
433 $license = "LGPL$gplver$extrainfo $license";
436 if ($licensetext =~ /is free software.? you can redistribute it and\/or modify it under the terms of the
(GNU Affero General Public License
|AGPL
)/i
) {
437 $license = "AGPL$gplver$extrainfo $license";
440 if ($licensetext =~ /is free software.? you (can|may) redistribute it and\/or modify it under the terms of
(?
:version
[^ ]+ (?
:\
(?only\
)?
)?of
)?the GNU General Public License
/i
) {
441 $license = "GPL$gplver$extrainfo $license";
444 if ($licensetext =~ /is distributed under the terms of the GNU General Public License,/
445 and length $gplver) {
446 $license = "GPL$gplver$extrainfo $license";
449 if ($licensetext =~ /is distributed.*terms.*GPL/) {
450 $license = "GPL (unversioned/unknown version) $license";
453 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/) {
454 $license = "QPL (part of Qt) $license";
455 } elsif ($licensetext =~ /may be distributed under the terms of the Q Public License as defined/) {
456 $license = "QPL $license";
459 if ($licensetext =~ /opensource\.org\/licenses\
/mit-license\.php/) {
460 $license = "MIT/X11 (BSD like) $license";
461 } 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
)/) {
462 $license = "MIT/X11 (BSD like) $license";
463 } 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/) {
464 $license = "MIT/X11 (BSD like) $license";
467 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
/) {
468 $license = "ISC $license";
471 if ($licensetext =~ /THIS SOFTWARE IS PROVIDED .*AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY/) {
472 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) {
473 $license = "BSD (4 clause) $license";
474 } elsif ($licensetext =~ /(The name .*? may not|Neither the names? .*? nor the names of (its|their) contributors may) be used to endorse or promote products derived from this software/i) {
475 $license = "BSD (3 clause) $license";
476 } elsif ($licensetext =~ /Redistributions of source code must retain the above copyright notice/i) {
477 $license = "BSD (2 clause) $license";
479 $license = "BSD $license";
483 if ($licensetext =~ /Mozilla Public License Version ([^ ]+)/) {
484 $license = "MPL (v$1) $license";
487 if ($licensetext =~ /Released under the terms of the Artistic License ([^ ]+)/) {
488 $license = "Artistic (v$1) $license";
491 if ($licensetext =~ /is free software under the Artistic [Ll]icense/) {
492 $license = "Artistic $license";
495 if ($licensetext =~ /This program is free software; you can redistribute it and\/or modify it under the same terms as Perl itself
/) {
496 $license = "Perl $license";
499 if ($licensetext =~ /under the Apache License, Version ([^ ]+)/) {
500 $license = "Apache (v$1) $license";
503 if ($licensetext =~ /(THE BEER-WARE LICENSE)/i) {
504 $license = "Beerware $license";
507 if ($licensetext =~ /This source file is subject to version ([^ ]+) of the PHP license/) {
508 $license = "PHP (v$1) $license";
511 if ($licensetext =~ /under the terms of the CeCILL /) {
512 $license = "CeCILL $license";
515 if ($licensetext =~ /under the terms of the CeCILL-([^ ]+) /) {
516 $license = "CeCILL-$1 $license";
519 if ($licensetext =~ /under the SGI Free Software License B/) {
520 $license = "SGI Free Software License B $license";
523 if ($licensetext =~ /is in the public domain/i) {
524 $license = "Public domain $license";
527 if ($licensetext =~ /terms of the Common Development and Distribution License(, Version ([^(]+))? \(the License\)/) {
528 $license = "CDDL " . ($1 ?
"(v$2) " : '') . $license;
531 if ($licensetext =~ /Microsoft Permissive License \(Ms-PL\)/) {
532 $license = "Ms-PL $license";
535 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
536 $licensetext =~ /Boost Software License([ ,-]+Version ([^ ]+)?(\.))/i) {
537 $license = "BSL " . ($1 ?
"(v$2) " : '') . $license;
540 if ($licensetext =~ /PYTHON SOFTWARE FOUNDATION LICENSE (VERSION ([^ ]+))/i) {
541 $license = "PSF " . ($1 ?
"(v$2) " : '') . $license;
544 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
545 $licensetext =~ /see copyright notice in zlib\.h/) {
546 $license = "zlib/libpng $license";
547 } elsif ($licensetext =~ /This code is released under the libpng license/) {
548 $license = "libpng $license";
551 if ($licensetext =~ /Do What The Fuck You Want To Public License, Version ([^, ]+)/i) {
552 $license = "WTFPL (v$1) $license";
555 if ($licensetext =~ /Do what The Fuck You Want To Public License/i) {
556 $license = "WTFPL $license";
559 if ($licensetext =~ /(License WTFPL|Under (the|a) WTFPL)/i) {
560 $license = "WTFPL $license";
563 $license = "UNKNOWN" if (!length($license));
565 # Remove trailing spaces.
566 $license =~ s/\s+$//;
572 my ($pack,$file,$line);
573 ($pack,$file,$line) = caller();
574 (my $msg = "$progname: fatal error at line $line:\n@_\n") =~ tr/\0//d;