copyright.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 1998 - 2021, 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. '^.muse/', # 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',
  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. );
  75. sub scanfile {
  76. my ($f) = @_;
  77. my $line=1;
  78. my $found = 0;
  79. open(F, "<$f") ||
  80. print ERROR "can't open $f\n";
  81. while (<F>) {
  82. chomp;
  83. my $l = $_;
  84. # check for a copyright statement and save the years
  85. if($l =~ /.* +copyright .* *\d\d\d\d/i) {
  86. while($l =~ /([\d]{4})/g) {
  87. push @copyright, {
  88. year => $1,
  89. line => $line,
  90. col => index($l, $1),
  91. code => $l
  92. };
  93. $found++;
  94. }
  95. }
  96. # allow within the first 100 lines
  97. if(++$line > 100) {
  98. last;
  99. }
  100. }
  101. close(F);
  102. return $found;
  103. }
  104. sub checkfile {
  105. my ($file) = @_;
  106. my $fine = 0;
  107. @copyright=();
  108. my $found = scanfile($file);
  109. if(!$found) {
  110. print "$file:1: missing copyright range\n";
  111. return 2;
  112. }
  113. my $commityear = undef;
  114. @copyright = sort {$$b{year} cmp $$a{year}} @copyright;
  115. # if the file is modified, assume commit year this year
  116. if(`git status -s -- $file` =~ /^ [MARCU]/) {
  117. $commityear = (localtime(time))[5] + 1900;
  118. }
  119. else {
  120. # min-parents=1 to ignore wrong initial commit in truncated repos
  121. my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- $file`;
  122. if($grl) {
  123. chomp $grl;
  124. $commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
  125. }
  126. }
  127. if(defined($commityear) && scalar(@copyright) &&
  128. $copyright[0]{year} != $commityear) {
  129. printf "$file:%d: copyright year out of date, should be $commityear, " .
  130. "is $copyright[0]{year}\n",
  131. $copyright[0]{line};
  132. }
  133. else {
  134. $fine = 1;
  135. }
  136. return $fine;
  137. }
  138. my @all;
  139. if($ARGV[0]) {
  140. push @all, $ARGV[0];
  141. }
  142. else {
  143. @all = `git ls-files`;
  144. }
  145. for my $f (@all) {
  146. chomp $f;
  147. my $skipped = 0;
  148. for my $skip (@skiplist) {
  149. #print "$f matches $skip ?\n";
  150. if($f =~ /$skip/) {
  151. $skiplisted++;
  152. $skipped = 1;
  153. #print "$f: SKIPPED ($skip)\n";
  154. last;
  155. }
  156. }
  157. if(!$skipped) {
  158. my $r = checkfile($f);
  159. $missing++ if($r == 2);
  160. $wrong++ if(!$r);
  161. }
  162. }
  163. print STDERR "$missing files have no copyright\n" if($missing);
  164. print STDERR "$wrong files have wrong copyright year\n" if ($wrong);
  165. print STDERR "$skiplisted files are skipped\n" if ($skiplisted);
  166. exit 1 if($missing || $wrong);