copyright.pl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. my @all = `git ls-files $1`;
  129. if(!$all[0]) {
  130. print STDERR "$1 matches no files\n";
  131. }
  132. else {
  133. push @files, @all;
  134. }
  135. }
  136. elsif(/^Copyright: (.*)/i) {
  137. $copy = $1;
  138. }
  139. elsif(/^License: (.*)/i) {
  140. my $license = $1;
  141. for my $f (@files) {
  142. chomp $f;
  143. if($f =~ /\.gitignore\z/) {
  144. # ignore .gitignore
  145. }
  146. else {
  147. if($skiplist{$f}) {
  148. print STDERR "$f already skipped at $skiplist{$f}\n";
  149. }
  150. $skiplist{$f} = "dep5:$line";
  151. }
  152. }
  153. undef @files;
  154. }
  155. }
  156. close(F);
  157. }
  158. dep5(".reuse/dep5");
  159. my $checkall = 0;
  160. my @all;
  161. my $verbose;
  162. if($ARGV[0] eq "-v") {
  163. $verbose = 1;
  164. shift @ARGV;
  165. }
  166. if($ARGV[0]) {
  167. push @all, @ARGV;
  168. }
  169. else {
  170. @all = `git ls-files`;
  171. $checkall = 1;
  172. }
  173. for my $f (@all) {
  174. chomp $f;
  175. my $skipped = 0;
  176. my $miss;
  177. my $wro;
  178. my $pattern;
  179. if($skiplist{$f}) {
  180. $pattern = $skip;
  181. $skiplisted++;
  182. $skipped = 1;
  183. $skip{$f}++;
  184. }
  185. my $r = checkfile($f, $skipped, $pattern);
  186. $mis=1 if($r == 2);
  187. $wro=1 if(!$r);
  188. if(!$skipped) {
  189. $missing += $mis;
  190. $wrong += $wro;
  191. }
  192. }
  193. if($verbose) {
  194. print STDERR "$missing files have no copyright\n" if($missing);
  195. print STDERR "$wrong files have wrong copyright year\n" if ($wrong);
  196. print STDERR "$skiplisted files are skipped\n" if ($skiplisted);
  197. for my $s (@skiplist) {
  198. if(!$skips{$s}) {
  199. printf ("Never skipped pattern: %s\n", $s);
  200. }
  201. if($superf{$s}) {
  202. printf ("%s was skipped superfluously %u times and legitimately %u times\n",
  203. $s, $superf{$s}, $skips{$s});
  204. }
  205. }
  206. }
  207. if($checkall) {
  208. for(keys %skiplist) {
  209. if(!$skip{$_}) {
  210. printf STDERR "$_ is marked for SKIP but is missing!\n";
  211. }
  212. }
  213. }
  214. exit 1 if($missing || $wrong);