copywrite.pl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/perl
  2. ## Script to easily add missing copyright statements in
  3. ## GRASS source code files
  4. ## 2006, written by Schuyler Erle <schuyler , nocat net>
  5. ##
  6. ## COPYRIGHT: (C) 2006 by the GRASS Development Team
  7. ##
  8. ## This program is free software under the GNU General Public
  9. ## License (>=v2). Read the file COPYING that comes with GRASS
  10. ## for details.
  11. ##
  12. ## The script searches and opens sequentially all files
  13. ## which lack the word "copyright". Additionally the local
  14. ## ChangeLog is fetched and contributor names are extracted.
  15. ## Then a new header is inserted containing the GPL statement,
  16. ## purpose template and authors.
  17. ##
  18. ## Please run from top source code directory.
  19. ## For usage details, see below.
  20. #-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#
  21. #
  22. # edit this, and edit the bottom of the file after __END__
  23. # my $editor = "vi -o";
  24. #my $editor = "nedit";
  25. my $editor = "xemacs";
  26. # don't forget to get a copy of
  27. # http://www.red-bean.com/cvs2cl/cvs2cl.pl
  28. # and put it in the same directory as this script
  29. #
  30. # run this script as:
  31. # perl copywrite.pl contributors.csv
  32. # committer identities can be provided in a file on the commandline
  33. # the format of the file should be "username,identity", one per line
  34. # The contributors.csv is already in the main directory of the GRASS
  35. # source code, so it should be found.
  36. #
  37. ####
  38. # to work offline:
  39. # $ export PATH=$PATH:$(pwd)
  40. # $ find . -name main.c | while read i; do \
  41. # echo $i; (cd `dirname $i`; cvs2cl.pl -f ChangeLog.tmp main.c); done
  42. #
  43. # to clean up after:
  44. # $ find -name ChangeLog.tmp | xargs rm
  45. #
  46. # otherwise, this script will assume you are online and fetch
  47. # the ChangeLogs on the fly (and remove them automatically).
  48. #
  49. #-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#
  50. use File::Find;
  51. use File::Basename;
  52. use Cwd;
  53. use strict;
  54. use warnings;
  55. use File::Basename;
  56. my $ChangeLog = "ChangeLog.tmp";
  57. my $cvs2cl = getcwd . "/cvs2cl.pl -f $ChangeLog";
  58. my $template = do {local $/; <DATA>};
  59. my %identity;
  60. sub evaluate_file {
  61. my $name = do { no warnings; $File::Find::name };
  62. # only process main.c
  63. return unless $_ eq 'main.c';
  64. warn "+ Reading $name\n";
  65. # slurp main.c
  66. my $main_c = do {local $/; open F, $_ or die "reading $name: $!"; <F>};
  67. # continue if main.c is 25 lines or longer
  68. return unless ($main_c =~ y/\n/\n/) >= 25;
  69. # continue if main.c doesn't mention "copyright"
  70. return if $main_c =~ /\(?c\)?opyright/gios;
  71. # run cvs2cl
  72. warn "+ Checking CVS log $name\n";
  73. my $fetched_ChangeLog = 0;
  74. unless (-f $ChangeLog) {
  75. system "$cvs2cl $_" and die "$cvs2cl $_: $!";
  76. $fetched_ChangeLog++;
  77. }
  78. # get the list of contributors
  79. my (%username, %year, $user);
  80. my $original_author = "";
  81. {
  82. # Read whole paras at once
  83. local $/ = "";
  84. open LOG, $ChangeLog or die "$ChangeLog: $!";
  85. while (my $line = <LOG>) {
  86. # Hack out the pretty printing
  87. $line =~ s/\s+/ /gos;
  88. # And drop trailing spaces
  89. $line =~ s/\s+$//gos;
  90. # Match individual check ins
  91. if ($line =~ /^(\d{4})-\d\d-\d\d\s+\d\d:\d\d\s+(.+)$/gos) {
  92. $year{$1}++;
  93. $user = $2;
  94. $username{$user}++;
  95. }
  96. # Note when a user did the initial check in
  97. # if ($line =~ /^\s+\*\s+[^:]+:\s+(?:initial|new$)/gios) {
  98. # $original_author = $user;
  99. # }
  100. $original_author = $user;
  101. # Note when a contributor submitted code
  102. if ($line =~ /:\s+([^:<]+\s*<[^>]+>)\s*:/gos) {
  103. $username{$1}++;
  104. }
  105. }
  106. }
  107. # don't duplicate the original author
  108. delete $username{$original_author} if $original_author;
  109. #figure out module name
  110. my $fullname = $name;
  111. my $mymodule = basename(dirname($fullname));
  112. # append the list of contributors
  113. my @years = sort map {$_ + 0} keys %year;
  114. my @authors = sort keys %username;
  115. # map committers to identities
  116. @authors = map { exists( $identity{$_} ) ? $identity{$_} : $_ } @authors;
  117. $original_author = $identity{$original_author}
  118. if exists $identity{$original_author};
  119. # execute the template
  120. {
  121. local $" = ", ";
  122. $main_c = eval(
  123. "<<END_OF_TEMPLATE\n" . $template . "\nEND_OF_TEMPLATE" );
  124. }
  125. # write the new version of main.c
  126. warn "+ Rewriting $name\n";
  127. open F, ">$_" or die "writing $name: $!";
  128. print F $main_c;
  129. close F;
  130. # load it up in an editor for vetting
  131. system "$editor $_ $ChangeLog description.html" and die "$editor $_ $ChangeLog: $!";
  132. # delete the ChangeLog after *only* if this script created it
  133. unlink $ChangeLog if $fetched_ChangeLog;
  134. }
  135. # committer identities can be provided in a file on the commandline
  136. # the format of the file should be "username,identity", one per line
  137. #
  138. if ($ARGV[0]) {
  139. while (<>) {
  140. chomp;
  141. next unless /^[a-z]/ios;
  142. my ($user, $id) = split(",", $_, 2);
  143. $identity{$user} = $id;
  144. }
  145. }
  146. find( \&evaluate_file, "." );
  147. ### edit the template after __END__
  148. __END__
  149. /****************************************************************************
  150. *
  151. * MODULE: $mymodule
  152. * AUTHOR(S): $original_author (original contributor)
  153. * @authors
  154. * PURPOSE:
  155. * COPYRIGHT: (C) $years[0]-$years[-1] by the GRASS Development Team
  156. *
  157. * This program is free software under the GNU General Public
  158. * License (>=v2). Read the file COPYING that comes with GRASS
  159. * for details.
  160. *
  161. *****************************************************************************/
  162. $main_c