2
0

copyright.pl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 1998 - 2020, 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.haxx.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 @whitelist=(
  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', # whereever they are
  36. '.gitattributes', # whereever 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. '^winbuild/BUILD.WINDOWS.txt$', # instructions
  47. '\/readme',
  48. '^.github/', # github instruction files
  49. # docs/ files we're okay with without copyright
  50. 'INSTALL.cmake',
  51. 'TheArtOfHttpScripting',
  52. 'page-footer',
  53. 'curl_multi_socket_all.3',
  54. 'curl_strnequal.3',
  55. 'symbols-in-versions',
  56. # macos-framework files
  57. '^lib\/libcurl.plist',
  58. '^lib\/libcurl.vers.in',
  59. # symbian build files we know little about
  60. '^packages\/Symbian\/bwins\/libcurlu.def',
  61. '^packages\/Symbian\/eabi\/libcurlu.def',
  62. '^packages\/Symbian\/group\/bld.inf',
  63. '^packages\/Symbian\/group\/curl.iby',
  64. '^packages\/Symbian\/group\/curl.mmp',
  65. '^packages\/Symbian\/group\/curl.pkg',
  66. '^packages\/Symbian\/group\/libcurl.iby',
  67. '^packages\/Symbian\/group\/libcurl.mmp',
  68. '^packages\/Symbian\/group\/libcurl.pkg',
  69. # vms files
  70. '^packages\/vms\/build_vms.com',
  71. '^packages\/vms\/curl_release_note_start.txt',
  72. '^packages\/vms\/curlmsg.sdl',
  73. '^packages\/vms\/macro32_exactcase.patch',
  74. # XML junk
  75. '^projects\/wolfssl_override.props',
  76. # macos framework generated files
  77. '^src\/macos\/curl.mcp.xml.sit.hqx',
  78. '^src\/macos\/src\/curl_GUSIConfig.cpp',
  79. );
  80. sub scanfile {
  81. my ($f) = @_;
  82. my $line=1;
  83. my $found = 0;
  84. open(F, "<$f") ||
  85. print ERROR "can't open $f\n";
  86. while (<F>) {
  87. chomp;
  88. my $l = $_;
  89. # check for a copyright statement and save the years
  90. if($l =~ /.* +copyright .* *\d\d\d\d/i) {
  91. while($l =~ /([\d]{4})/g) {
  92. push @copyright, {
  93. year => $1,
  94. line => $line,
  95. col => index($l, $1),
  96. code => $l
  97. };
  98. $found++;
  99. }
  100. }
  101. # allow within the first 100 lines
  102. if(++$line > 100) {
  103. last;
  104. }
  105. }
  106. close(F);
  107. return $found;
  108. }
  109. sub checkfile {
  110. my ($file) = @_;
  111. my $fine = 0;
  112. @copyright=();
  113. my $found = scanfile($file);
  114. if(!$found) {
  115. print "$file: missing copyright range\n";
  116. return 2;
  117. }
  118. my $commityear = undef;
  119. @copyright = sort {$$b{year} cmp $$a{year}} @copyright;
  120. # if the file is modified, assume commit year this year
  121. if(`git status -s -- $file` =~ /^ [MARCU]/) {
  122. $commityear = (localtime(time))[5] + 1900;
  123. }
  124. else {
  125. # min-parents=1 to ignore wrong initial commit in truncated repos
  126. my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- $file`;
  127. if($grl) {
  128. chomp $grl;
  129. $commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
  130. }
  131. }
  132. if(defined($commityear) && scalar(@copyright) &&
  133. $copyright[0]{year} != $commityear) {
  134. print "$file: copyright year out of date, should be $commityear, " .
  135. "is $copyright[0]{year}\n";
  136. }
  137. else {
  138. $fine = 1;
  139. }
  140. return $fine;
  141. }
  142. my @all;
  143. if($ARGV[0]) {
  144. push @all, $ARGV[0];
  145. }
  146. else {
  147. @all = `git ls-files`;
  148. }
  149. for my $f (@all) {
  150. chomp $f;
  151. my $skipped = 0;
  152. for my $skip (@whitelist) {
  153. #print "$f matches $skip ?\n";
  154. if($f =~ /$skip/) {
  155. $whitelisted++;
  156. $skipped = 1;
  157. #print "$f: SKIPPED ($skip)\n";
  158. last;
  159. }
  160. }
  161. if(!$skipped) {
  162. my $r = checkfile($f);
  163. $missing++ if($r == 2);
  164. $wrong++ if(!$r);
  165. }
  166. }
  167. print STDERR "$missing files have no copyright\n" if($missing);
  168. print STDERR "$wrong files have wrong copyright year\n" if ($wrong);
  169. print STDERR "$whitelisted files are whitelisted\n" if ($whitelisted);
  170. exit 1 if($missing || $wrong);