copywrite.pl 5.4 KB

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