root/juggler/branches/2.2/scripts/auto-copyright/auto-copyright.pl

Revision 5987, 6.4 kB (checked in by patrick, 7 years ago)

Use some path trickery of mine so that the path to the RecurseDir? module
is not hard-coded. This allows the script to be run from anywhere.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/perl
2
3 #
4 # Copyright (c) 2000 Patrick L. Hartling (original author)
5 # contributors:
6 #  - Kevin Meinert (command line args, external config data, working dir)
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 #    notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 #    notice, this list of conditions and the following disclaimer in the
15 #    documentation and/or other materials provided with the distribution.
16 # 3. The names of its contributors may not be used to endorse or promote
17 #    products derived from this software without specific prior written
18 #    permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
21 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
23 # NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #
31 #       $Id$
32 #
33
34 use File::Basename;
35 use Getopt::Std;
36
37 # Do this to include the path to the script in @INC.
38 my $path;
39
40 BEGIN {
41     $path = (fileparse("$0"))[1];
42 }
43
44 use lib($path);
45 use RecurseDir;
46
47 # get opts:
48 getopts('d:t:c:e:ha');
49
50 my $tags_file = "$opt_t";
51 my $copyright_file = "$opt_c";
52 my @extensions = split( /,/, "$opt_e" );
53 my $working_dir = "$opt_d" || ".";
54
55
56
57 if ( $opt_h )
58 {
59    helpText();
60    exit 1;
61 }
62
63 # execute the tags file, putting it's vars into scope.
64 if($tags_file)
65 {
66    unless ($return = do $tags_file)
67    {     
68        helpText();
69        warn "couldn't parse $tags_file: $@"         if $@;
70        warn "couldn't do $tags_file: $!"            unless defined $return;
71        warn "couldn't run $tags_file"               unless $return;
72    }
73 }
74
75 if (!open(TAGSFILE, "$tags_file"))
76 {
77    helpText();
78    print "ERROR: Must specify a valid tags file. i.e. -t tags.pl\n";
79    exit 1;
80 }
81 close(TAGSFILE);
82
83
84 $RecurseDir::print_cur_dir = 1;
85 $RecurseDir::pass_rec_func_cur_file = 1;
86
87 if (!open(COPYRIGHT, "$copyright_file"))
88 {
89    helpText();
90    print "ERROR: Must specify a valid copyright file. i.e. -c copyright_header_include.txt\n";
91    exit 1;
92 }
93 my(@copyright) = <COPYRIGHT>;
94 close(COPYRIGHT);
95
96
97 recurseDir( $working_dir );
98
99 exit 0;
100
101 sub helpText()
102 {
103     print "\n\n";
104     print "auto-copyright - by patrick hartling and kevin meinert\n";
105     print "\n";
106     print "Example Usage: \n";
107     print "       $0 -c copyrightheader.txt -t tagsfile.pl -e h,cpp -a\n";
108     print "             add or replace header to all files in current dir\n";
109     print "\n";
110     print "       $0 -c copyrightheader.txt -t tagsfile.pl -e h,cpp\n";
111     print "             replace headers (no add) to all files in current dir\n";
112     print "\n";
113     print "       $0 -h\n";
114     print "             gives this text\n";
115     print "\n";
116     print "Options:\n";
117     print "       -d <working dir> name of dir to start recursive processing\n";
118     print "       -e <ext1,ext2,..,extn> file extensions to process\n";
119     print "       -c <(c) header> name of file with copyright text\n";
120     print "       -t <tags.pl> name of perl script which defines 4\n";
121     print "                    variables as input to this script\n";
122     print "       -a causes the header to be added if old header is not found\n";
123     print "\n\n";
124 }
125
126 sub makeRegexSafe( $$ )
127 {
128    my $string = shift;
129    my $regex_safe_string = shift;
130    
131    $$regex_safe_string = $string;
132    $$regex_safe_string =~ s/\./\\./gs;
133    $$regex_safe_string =~ s/\*/\\*/gs;
134    $$regex_safe_string =~ s/\$/\\\$/gs;
135    $$regex_safe_string =~ s/\+/\\+/gs;
136    $$regex_safe_string =~ s/\?/\\?/gs;
137    $$regex_safe_string =~ s/\@/\\\@/gs;
138 }
139
140
141 sub recurseFunc {
142     my $filename = shift;
143
144     return unless checkName("$filename");
145
146     if ( ! open(INPUT, "$filename") )
147     {
148         warn "WARNING: Could not open $filename: $!\n";
149     }
150    
151     else
152     {
153         if ( ! open(OUTPUT, "> $filename.new") )
154          {
155             warn "WARNING: Could not create new file: $!\n";
156         }
157          
158          else
159          {
160             # gather every copyright together
161             my $all_copyrights;
162             foreach(@copyright)
163             {
164                $all_copyrights .= $_;
165             }           
166
167             # gather each line of the source file into one string so we can do a subst across multiple lines.
168             my $file_contents;
169             while(<INPUT>)
170             {
171                $file_contents .= $_;
172             }
173
174             my $copyrights_plus_delimiters = "$newbegintag\n$all_copyrights$newendtag\n";
175            
176             # convert tags for use in a regex
177             $begintag_regex_safe = $begintag;
178             $endtag_regex_safe = $endtag;
179             makeRegexSafe( $begintag, \$begintag_regex_safe );
180             makeRegexSafe( $endtag, \$endtag_regex_safe );
181            
182             if ( $file_contents =~ s/($begintag_regex_safe.*?$endtag_regex_safe)/$copyrights_plus_delimiters/s ) # not global, only first one...
183             {
184                 print "Replacing the copyright in $filename ...\n";
185                
186                 #print "old:\n";
187                 #print $1;
188                 
189                 #print "new:\n";
190                 #print $copyrights_plus_delimiters;
191             }
192
193             elsif ($opt_a)
194             {
195                       print "Adding copyright to $filename ...\n";
196                print OUTPUT "\n$copyrights_plus_delimiters";
197             }
198
199             else
200             {
201                print "$filename: No previous copyright, no replace.\n";
202             }
203
204             # put the new file_contents to the output file.
205             print OUTPUT $file_contents;
206
207             close(OUTPUT) or warn "WARNING: Could not save changes: $!\n";
208
209             unlink("$filename");
210             rename("$filename.new", "$filename");
211         }
212
213         close(INPUT);
214     }
215 }
216
217 sub checkName ($) {
218    my $filename = shift;
219
220    foreach (@extensions)
221    {
222       return 1 if $filename =~ /\.$_$/;
223    }
224    return 0;
225 }
Note: See TracBrowser for help on using the browser.