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. );
  43. sub scanfile {
  44. my ($f) = @_;
  45. my $line=1;
  46. my $found = 0;
  47. open(F, "<$f") || return -1;
  48. while (<F>) {
  49. chomp;
  50. my $l = $_;
  51. # check for a copyright statement and save the years
  52. if($l =~ /.* ?copyright .* (\d\d\d\d|)/i) {
  53. my $count = 0;
  54. while($l =~ /([\d]{4})/g) {
  55. push @copyright, {
  56. year => $1,
  57. line => $line,
  58. col => index($l, $1),
  59. code => $l
  60. };
  61. $count++;
  62. }
  63. if(!$count) {
  64. # year-less
  65. push @copyright, {
  66. year => -1,
  67. line => $line,
  68. col => index($l, $1),
  69. code => $l
  70. };
  71. $count++;
  72. }
  73. $found = $count;
  74. }
  75. if($l =~ /SPDX-License-Identifier:/) {
  76. $spdx = 1;
  77. }
  78. # allow within the first 100 lines
  79. if(++$line > 100) {
  80. last;
  81. }
  82. }
  83. close(F);
  84. return $found;
  85. }
  86. sub checkfile {
  87. my ($file, $skipped, $pattern) = @_;
  88. $spdx = 0;
  89. my $found = scanfile($file);
  90. if($found < 1) {
  91. if($skipped) {
  92. # just move on
  93. $skips{$pattern}++;
  94. return 0;
  95. }
  96. if(!$found) {
  97. print "$file:1: missing copyright range\n";
  98. return 2;
  99. }
  100. # this means the file couldn't open - it might not exist, consider
  101. # that fine
  102. return 1;
  103. }
  104. if(!$spdx) {
  105. if($skipped) {
  106. # move on
  107. $skips{$pattern}++;
  108. return 0;
  109. }
  110. print "$file:1: missing SPDX-License-Identifier\n";
  111. return 2;
  112. }
  113. if($skipped) {
  114. print "$file:1: ignored superfluously by $pattern\n" if($verbose);
  115. $superf{$pattern}++;
  116. }
  117. return 1;
  118. }
  119. sub dep5 {
  120. my ($file) = @_;
  121. my @files;
  122. my $copy;
  123. open(F, "<$file") || die "can't open $file";
  124. my $line = 0;
  125. while(<F>) {
  126. $line++;
  127. if(/^Files: (.*)/i) {
  128. push @files, `git ls-files $1`;
  129. }
  130. elsif(/^Copyright: (.*)/i) {
  131. $copy = $1;
  132. }
  133. elsif(/^License: (.*)/i) {
  134. my $license = $1;
  135. for my $f (@files) {
  136. chomp $f;
  137. if($f =~ /\.gitignore\z/) {
  138. # ignore .gitignore
  139. }
  140. else {
  141. if($skiplist{$f}) {
  142. print STDERR "$f already skipped at $skiplist{$f}\n";
  143. }
  144. $skiplist{$f} = "dep5:$line";
  145. }
  146. }
  147. undef @files;
  148. }
  149. }
  150. close(F);
  151. }
  152. dep5(".reuse/dep5");
  153. my $checkall = 0;
  154. my @all;
  155. my $verbose;
  156. if($ARGV[0] eq "-v") {
  157. $verbose = 1;
  158. shift @ARGV;
  159. }
  160. if($ARGV[0]) {
  161. push @all, @ARGV;
  162. }
  163. else {
  164. @all = `git ls-files`;
  165. $checkall = 1;
  166. }
  167. for my $f (@all) {
  168. chomp $f;
  169. my $skipped = 0;
  170. my $miss;
  171. my $wro;
  172. my $pattern;
  173. if($skiplist{$f}) {
  174. $pattern = $skip;
  175. $skiplisted++;
  176. $skipped = 1;
  177. $skip{$f}++;
  178. }
  179. my $r = checkfile($f, $skipped, $pattern);
  180. $mis=1 if($r == 2);
  181. $wro=1 if(!$r);
  182. if(!$skipped) {
  183. $missing += $mis;
  184. $wrong += $wro;
  185. }
  186. }
  187. if($verbose) {
  188. print STDERR "$missing files have no copyright\n" if($missing);
  189. print STDERR "$wrong files have wrong copyright year\n" if ($wrong);
  190. print STDERR "$skiplisted files are skipped\n" if ($skiplisted);
  191. for my $s (@skiplist) {
  192. if(!$skips{$s}) {
  193. printf ("Never skipped pattern: %s\n", $s);
  194. }
  195. if($superf{$s}) {
  196. printf ("%s was skipped superfluously %u times and legitimately %u times\n",
  197. $s, $superf{$s}, $skips{$s});
  198. }
  199. }
  200. }
  201. if($checkall) {
  202. for(keys %skiplist) {
  203. if(!$skip{$_}) {
  204. printf STDERR "$_ is marked for SKIP but is missing!\n";
  205. }
  206. }
  207. }
  208. exit 1 if($missing || $wrong);