copyright.pl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. ###########################################################################
  23. #
  24. # Invoke script in the root of the git checkout. Scans all files in git unless
  25. # given a specific single file.
  26. #
  27. # Usage: copyright.pl [file]
  28. #
  29. # regexes of files to not scan
  30. my @skiplist=(
  31. '^tests\/data\/test(\d+)$', # test case data
  32. '^docs\/cmdline-opts\/[a-z]+(.*)\.d$', # curl.1 pieces
  33. '(\/|^)[A-Z0-9_.-]+$', # all uppercase file name, possibly with dot and dash
  34. '(\/|^)[A-Z0-9_-]+\.md$', # all uppercase file name with .md extension
  35. '.gitignore', # wherever they are
  36. '.gitattributes', # wherever they are
  37. '^tests/certs/.*', # generated certs
  38. '^tests/stunnel.pem', # generated cert
  39. '^tests/valgrind.supp', # valgrind suppressions
  40. '^projects/Windows/.*.dsw$', # generated MSVC file
  41. '^projects/Windows/.*.sln$', # generated MSVC file
  42. '^projects/Windows/.*.tmpl$', # generated MSVC file
  43. '^projects/Windows/.*.vcxproj.filters$', # generated MSVC file
  44. '^m4/ax_compile_check_sizeof.m4$', # imported, leave be
  45. '^.mailmap', # git control file
  46. '\/readme',
  47. '^.github/', # github instruction files
  48. '^.dcignore', # deepcode.ai instruction file
  49. '^.lift/', # muse-CI control files
  50. "buildconf", # its nothing to copyright
  51. # docs/ files we're okay with without copyright
  52. 'INSTALL.cmake',
  53. 'TheArtOfHttpScripting',
  54. 'page-footer',
  55. 'curl_multi_socket_all.3',
  56. 'curl_strnequal.3',
  57. 'symbols-in-versions',
  58. 'options-in-versions',
  59. # macos-framework files
  60. '^lib\/libcurl.plist.in',
  61. '^lib\/libcurl.vers.in',
  62. # vms files
  63. '^packages\/vms\/build_vms.com',
  64. '^packages\/vms\/curl_release_note_start.txt',
  65. '^packages\/vms\/curlmsg.sdl',
  66. '^packages\/vms\/macro32_exactcase.patch',
  67. # XML junk
  68. '^projects\/wolfssl_override.props',
  69. # macos framework generated files
  70. '^src\/macos\/curl.mcp.xml.sit.hqx',
  71. '^src\/macos\/src\/curl_GUSIConfig.cpp',
  72. # checksrc control files
  73. '\.checksrc$',
  74. # an empty control file
  75. "^zuul.d/playbooks/.zuul.ignore",
  76. # markdown linkchecker config
  77. "mlc_config.json",
  78. );
  79. sub scanfile {
  80. my ($f) = @_;
  81. my $line=1;
  82. my $found = 0;
  83. open(F, "<$f") || return -1;
  84. while (<F>) {
  85. chomp;
  86. my $l = $_;
  87. # check for a copyright statement and save the years
  88. if($l =~ /.* +copyright .* *\d\d\d\d/i) {
  89. while($l =~ /([\d]{4})/g) {
  90. push @copyright, {
  91. year => $1,
  92. line => $line,
  93. col => index($l, $1),
  94. code => $l
  95. };
  96. $found++;
  97. }
  98. }
  99. # allow within the first 100 lines
  100. if(++$line > 100) {
  101. last;
  102. }
  103. }
  104. close(F);
  105. return $found;
  106. }
  107. sub checkfile {
  108. my ($file) = @_;
  109. my $fine = 0;
  110. @copyright=();
  111. my $found = scanfile($file);
  112. if($found < 1) {
  113. if(!$found) {
  114. print "$file:1: missing copyright range\n";
  115. return 2;
  116. }
  117. # this means the file couldn't open - it might not exist, consider
  118. # that fine
  119. return 1;
  120. }
  121. my $commityear = undef;
  122. @copyright = sort {$$b{year} cmp $$a{year}} @copyright;
  123. # if the file is modified, assume commit year this year
  124. if(`git status -s -- $file` =~ /^ [MARCU]/) {
  125. $commityear = (localtime(time))[5] + 1900;
  126. }
  127. else {
  128. # min-parents=1 to ignore wrong initial commit in truncated repos
  129. my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- $file`;
  130. if($grl) {
  131. chomp $grl;
  132. $commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
  133. }
  134. }
  135. if(defined($commityear) && scalar(@copyright) &&
  136. $copyright[0]{year} != $commityear) {
  137. printf "$file:%d: copyright year out of date, should be $commityear, " .
  138. "is $copyright[0]{year}\n",
  139. $copyright[0]{line};
  140. }
  141. else {
  142. $fine = 1;
  143. }
  144. return $fine;
  145. }
  146. my @all;
  147. my $verbose;
  148. if($ARGV[0] eq "-v") {
  149. $verbose = 1;
  150. shift @ARGV;
  151. }
  152. if($ARGV[0]) {
  153. push @all, @ARGV;
  154. }
  155. else {
  156. @all = `git ls-files`;
  157. }
  158. for my $f (@all) {
  159. chomp $f;
  160. my $skipped = 0;
  161. for my $skip (@skiplist) {
  162. #print "$f matches $skip ?\n";
  163. if($f =~ /$skip/) {
  164. $skiplisted++;
  165. $skipped = 1;
  166. #print "$f: SKIPPED ($skip)\n";
  167. last;
  168. }
  169. }
  170. if(!$skipped) {
  171. my $r = checkfile($f);
  172. $missing++ if($r == 2);
  173. $wrong++ if(!$r);
  174. }
  175. }
  176. if($verbose) {
  177. print STDERR "$missing files have no copyright\n" if($missing);
  178. print STDERR "$wrong files have wrong copyright year\n" if ($wrong);
  179. print STDERR "$skiplisted files are skipped\n" if ($skiplisted);
  180. }
  181. exit 1 if($missing || $wrong);