| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
use File::Basename; |
|---|
| 35 |
use Getopt::Std; |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
my $path; |
|---|
| 39 |
|
|---|
| 40 |
BEGIN { |
|---|
| 41 |
$path = (fileparse("$0"))[1]; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
use lib($path); |
|---|
| 45 |
use RecurseDir; |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 161 |
my $all_copyrights; |
|---|
| 162 |
foreach(@copyright) |
|---|
| 163 |
{ |
|---|
| 164 |
$all_copyrights .= $_; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 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 |
|
|---|
| 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 ) |
|---|
| 183 |
{ |
|---|
| 184 |
print "Replacing the copyright in $filename ...\n"; |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 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 |
|
|---|
| 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 |
} |
|---|