copyright.pl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # SPDX-License-Identifier: curl
  23. #
  24. ###########################################################################
  25. #
  26. # Invoke script in the root of the git checkout. Scans all files in git unless
  27. # given a specific single file.
  28. #
  29. # Usage: copyright.pl [file]
  30. #
  31. my %skips;
  32. # file names
  33. my %skiplist = (
  34. # REUSE-specific file
  35. ".reuse/dep5" => "<built-in>",
  36. # License texts
  37. "LICENSES/BSD-3-Clause.txt" => "<built-in>",
  38. "LICENSES/BSD-4-Clause-UC.txt" => "<built-in>",
  39. "LICENSES/ISC.txt" => "<built-in>",
  40. "LICENSES/curl.txt" => "<built-in>",
  41. "COPYING" => "<built-in>",
  42. "docs/GPLv3.txt" => "<built-in>",
  43. );
  44. sub scanfile {
  45. my ($f) = @_;
  46. my $line=1;
  47. my $found = 0;
  48. open(F, "<$f") || return -1;
  49. while (<F>) {
  50. chomp;
  51. my $l = $_;
  52. # check for a copyright statement and save the years
  53. if($l =~ /.* ?copyright .* (\d\d\d\d|)/i) {
  54. my $count = 0;
  55. while($l =~ /([\d]{4})/g) {
  56. push @copyright, {
  57. year => $1,
  58. line => $line,
  59. col => index($l, $1),
  60. code => $l
  61. };
  62. $count++;
  63. }
  64. if(!$count) {
  65. # year-less
  66. push @copyright, {
  67. year => -1,
  68. line => $line,
  69. col => index($l, $1),
  70. code => $l
  71. };
  72. $count++;
  73. }
  74. $found = $count;
  75. }
  76. if($l =~ /SPDX-License-Identifier:/) {
  77. $spdx = 1;
  78. }
  79. # allow within the first 100 lines
  80. if(++$line > 100) {
  81. last;
  82. }
  83. }
  84. close(F);
  85. return $found;
  86. }
  87. sub checkfile {
  88. my ($file, $skipped, $pattern) = @_;
  89. $spdx = 0;
  90. my $found = scanfile($file);
  91. if($found < 1) {
  92. if($skipped) {
  93. # just move on
  94. $skips{$pattern}++;
  95. return 0;
  96. }
  97. if(!$found) {
  98. print "$file:1: missing copyright range\n";
  99. return 2;
  100. }
  101. # this means the file couldn't open - it might not exist, consider
  102. # that fine
  103. return 1;
  104. }
  105. if(!$spdx) {
  106. if($skipped) {
  107. # move on
  108. $skips{$pattern}++;
  109. return 0;
  110. }
  111. print "$file:1: missing SPDX-License-Identifier\n";
  112. return 2;
  113. }
  114. if($skipped) {
  115. print "$file:1: ignored superfluously by $pattern\n" if($verbose);
  116. $superf{$pattern}++;
  117. }
  118. return 1;
  119. }
  120. sub dep5 {
  121. my ($file) = @_;
  122. my @files;
  123. my $copy;
  124. open(F, "<$file") || die "can't open $file";
  125. my $line = 0;
  126. while(<F>) {
  127. $line++;
  128. if(/^Files: (.*)/i) {
  129. push @files, `git ls-files $1`;
  130. }
  131. elsif(/^Copyright: (.*)/i) {
  132. $copy = $1;
  133. }
  134. elsif(/^License: (.*)/i) {
  135. my $license = $1;
  136. for my $f (@files) {
  137. chomp $f;
  138. if($f =~ /\.gitignore\z/) {
  139. # ignore .gitignore
  140. }
  141. else {
  142. if($skiplist{$f}) {
  143. print STDERR "$f already skipped at $skiplist{$f}\n";
  144. }
  145. $skiplist{$f} = "dep5:$line";
  146. }
  147. }
  148. undef @files;
  149. }
  150. }
  151. close(F);
  152. }
  153. dep5(".reuse/dep5");
  154. my $checkall = 0;
  155. my @all;
  156. my $verbose;
  157. if($ARGV[0] eq "-v") {
  158. $verbose = 1;
  159. shift @ARGV;
  160. }
  161. if($ARGV[0]) {
  162. push @all, @ARGV;
  163. }
  164. else {
  165. @all = `git ls-files`;
  166. $checkall = 1;
  167. }
  168. for my $f (@all) {
  169. chomp $f;
  170. my $skipped = 0;
  171. my $miss;
  172. my $wro;
  173. my $pattern;
  174. if($skiplist{$f}) {
  175. $pattern = $skip;
  176. $skiplisted++;
  177. $skipped = 1;
  178. $skip{$f}++;
  179. }
  180. my $r = checkfile($f, $skipped, $pattern);
  181. $mis=1 if($r == 2);
  182. $wro=1 if(!$r);
  183. if(!$skipped) {
  184. $missing += $mis;
  185. $wrong += $wro;
  186. }
  187. }
  188. if($verbose) {
  189. print STDERR "$missing files have no copyright\n" if($missing);
  190. print STDERR "$wrong files have wrong copyright year\n" if ($wrong);
  191. print STDERR "$skiplisted files are skipped\n" if ($skiplisted);
  192. for my $s (@skiplist) {
  193. if(!$skips{$s}) {
  194. printf ("Never skipped pattern: %s\n", $s);
  195. }
  196. if($superf{$s}) {
  197. printf ("%s was skipped superfluously %u times and legitimately %u times\n",
  198. $s, $superf{$s}, $skips{$s});
  199. }
  200. }
  201. }
  202. if($checkall) {
  203. for(keys %skiplist) {
  204. if(!$skip{$_}) {
  205. printf STDERR "$_ is marked for SKIP but is missing!\n";
  206. }
  207. }
  208. }
  209. exit 1 if($missing || $wrong);