copyright.pl 6.1 KB

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