1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # checks files for TABs and raises error if TABs found |
---|
4 | # (TABs are evil, cause they have no default width) |
---|
5 | # |
---|
6 | # As well checks for older ARB compile logs and removes them |
---|
7 | |
---|
8 | |
---|
9 | use strict; |
---|
10 | use warnings; |
---|
11 | |
---|
12 | my $forceAll = 0; # 1 -> force scan of all files if no stamp, 0 -> assume all ok, scan only new files |
---|
13 | |
---|
14 | my %scan_extension = map { $_ => 1; } ( |
---|
15 | 'c', 'h', |
---|
16 | 'cxx', 'hxx', |
---|
17 | 'cpp', 'hpp', |
---|
18 | 'f', 'inc', |
---|
19 | 'm4', 'status', 'in', |
---|
20 | 'txt', 'doc', 'README', 'readme', |
---|
21 | 'sh', |
---|
22 | 'pl', 'pm', 'PL', 'cgi', |
---|
23 | 'java', |
---|
24 | 'head', 'header', 'default', 'footer', |
---|
25 | 'makefile', |
---|
26 | 'template', |
---|
27 | 'dat', |
---|
28 | 'dtd', 'xsl', |
---|
29 | 'eft', 'ift', 'ift2', 'amc', |
---|
30 | 'sellst', |
---|
31 | 'mask', |
---|
32 | 'aisc', 'pa', |
---|
33 | 'source', 'menu', |
---|
34 | 'mat', 'conf', |
---|
35 | ); |
---|
36 | |
---|
37 | my %auto_determine = map { $_ => 1; } ( |
---|
38 | 'x', 'jar', |
---|
39 | ); |
---|
40 | |
---|
41 | my %ignore_extension = map { $_ => 1; } ( |
---|
42 | 'a', 'o', 'so', |
---|
43 | 'class', |
---|
44 | 'html', 'bitmap', 'ps', 'icon', 'hlp', 'help', |
---|
45 | 'gif', 'png', 'fig', 'vfont', 'SAVE', |
---|
46 | 'tgz', 'gz', |
---|
47 | 'lst', 'depends', |
---|
48 | 'md5', 'bs', 'xs', |
---|
49 | 'exists', 'ignore', '3pm', |
---|
50 | 'arb', 'seq', 'genmenu', |
---|
51 | 'init', 'cvsignore', 'log', 'am', 'org', |
---|
52 | 'last_gcc', 'csv', |
---|
53 | 'b', 'n', 'helpfiles', |
---|
54 | 'ms', 'gon', 'pep', 'bla', 'tct', 'comm', |
---|
55 | 'before', 'v1', 'data', 'pdb', 'embl', 'xpm', 'big', |
---|
56 | '2_manual_postscript', '2_manual_text', |
---|
57 | 'stamp', |
---|
58 | ); |
---|
59 | |
---|
60 | my %ignored_subdirs = map { $_ => 1; } ( |
---|
61 | 'GDE/CLUSTAL', |
---|
62 | 'GDE/CLUSTALW', |
---|
63 | 'GDE/FASTDNAML', |
---|
64 | 'GDE/AxML', |
---|
65 | 'GDE/TREEPUZZLE', |
---|
66 | 'GDE/PHYML', |
---|
67 | 'GDE/RAxML', |
---|
68 | 'GDE/SUPPORT', |
---|
69 | 'GDE/PHYLIP', |
---|
70 | 'patches', |
---|
71 | 'bin', |
---|
72 | ); |
---|
73 | |
---|
74 | my $tab_count = 0; |
---|
75 | my $files_newer_than = 0; |
---|
76 | |
---|
77 | sub getModtime($) { |
---|
78 | my ($file) = @_; |
---|
79 | my @st = stat($file); |
---|
80 | if (not @st) { die "can't stat '$file' ($!)"; } |
---|
81 | return $st[9]; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | sub scan_for_tabs($) { |
---|
86 | my ($file) = @_; |
---|
87 | my $has_tabs = `grep -m 1 -n -H '\t' $file`; |
---|
88 | |
---|
89 | # print "$file:0: has_tabs='$has_tabs'\n"; |
---|
90 | |
---|
91 | if ($has_tabs ne '') { |
---|
92 | my $pos = "$file:0:"; |
---|
93 | if ($has_tabs =~ /^[^:]+:[0-9]+:/) { |
---|
94 | $pos = $&; |
---|
95 | } |
---|
96 | print "$pos contains tabs. Tabs are not allowed.\n"; |
---|
97 | $tab_count++; |
---|
98 | if ($tab_count>10) { die "Further scan skipped.\n"; } |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | sub recurse_dirs($$); |
---|
103 | sub recurse_dirs($$) { |
---|
104 | my ($dir,$basedir) = @_; |
---|
105 | |
---|
106 | my @subdirs = (); |
---|
107 | if ($dir ne $basedir) { |
---|
108 | my $subdir = substr($dir,length($basedir)+1); |
---|
109 | if (defined $ignored_subdirs{$subdir}) { |
---|
110 | # print "Ignoring '$subdir' (dir='$dir')\n"; |
---|
111 | return; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | opendir(DIR, $dir) || die "can't read directory '$dir' (Reason: $!)"; |
---|
116 | foreach (readdir(DIR)) { |
---|
117 | if ($_ ne '.' and $_ ne '..') { |
---|
118 | my $full = $dir.'/'.$_; |
---|
119 | if (-l $full) { |
---|
120 | # print "$full:0: link -- skipped\n"; |
---|
121 | } |
---|
122 | elsif (-d $full) { |
---|
123 | if ($_ ne 'CVS' and |
---|
124 | $_ ne '.svn' and |
---|
125 | $_ ne 'ARB_SOURCE_DOC' and |
---|
126 | $_ ne 'old_help' and |
---|
127 | $_ ne 'PERL2ARB' and |
---|
128 | $_ ne 'GENC' and $_ ne 'GENH' |
---|
129 | ) { |
---|
130 | push @subdirs, $full; |
---|
131 | } |
---|
132 | } |
---|
133 | elsif (-f $full) { |
---|
134 | my $scan = -1; |
---|
135 | my $determine = 0; |
---|
136 | my $modtime = getModtime($full); |
---|
137 | |
---|
138 | if ($modtime<$files_newer_than) { |
---|
139 | $scan = 0; |
---|
140 | # file was created before last compile start |
---|
141 | # check if it's a log from an aborted compile |
---|
142 | if (/^[^.]+\.([0-9]+)\.log$/o) { |
---|
143 | print "Old log file: $full -- removing\n"; |
---|
144 | unlink($full) || print "$full:0: can't unlink (Reason: $!)\n"; |
---|
145 | } |
---|
146 | } |
---|
147 | else { |
---|
148 | if (/\.([^.]+)$/) { # file with extension |
---|
149 | my $ext = $1; |
---|
150 | if (exists $ignore_extension{$ext}) { |
---|
151 | # print "$full:0: excluded by extension '$ext'\n"; |
---|
152 | $scan = 0; |
---|
153 | } |
---|
154 | elsif (exists $scan_extension{$ext}) { |
---|
155 | # print "$full:0: scan!\n"; |
---|
156 | $scan = 1; |
---|
157 | } |
---|
158 | elsif (exists $auto_determine{$ext}) { |
---|
159 | # print "$full:0: scan!\n"; |
---|
160 | $determine = 1; |
---|
161 | } |
---|
162 | else { |
---|
163 | if ($ext =~ /^[0-9]+$/ |
---|
164 | ) { |
---|
165 | $scan = 0; |
---|
166 | } |
---|
167 | elsif ($full =~ /util\/config\./ or |
---|
168 | $full =~ /lib\/config\./ |
---|
169 | ) { |
---|
170 | $scan = 1; |
---|
171 | } |
---|
172 | else { |
---|
173 | # print "$full:0: a '$ext' file\n"; |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | else { |
---|
178 | if (/Makefile/ or |
---|
179 | /ChangeLog/ |
---|
180 | ) { |
---|
181 | $scan = 0; |
---|
182 | } |
---|
183 | else { |
---|
184 | $determine = 1; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | if ($determine==1) { |
---|
189 | $scan==-1 || die "logic error"; |
---|
190 | my $file_says = `file $full`; |
---|
191 | if ($file_says =~ /^[^:]+:[ ]*(.*)$/) { |
---|
192 | $file_says = $1; |
---|
193 | } |
---|
194 | else { |
---|
195 | die "Can't parse output from 'file'-command"; |
---|
196 | } |
---|
197 | |
---|
198 | if ($file_says =~ /executable/ or |
---|
199 | $file_says eq 'empty' or |
---|
200 | $file_says eq 'data' or |
---|
201 | $file_says eq 'IFF data' or |
---|
202 | $file_says =~ /archive.*data/ |
---|
203 | ) { |
---|
204 | $scan = 0; |
---|
205 | } |
---|
206 | elsif ($file_says =~ /shell.*script/ or |
---|
207 | $file_says =~ /ASCII.*text/ or |
---|
208 | $file_says =~ /ISO.*text/ or |
---|
209 | $file_says =~ /perl.*script/ |
---|
210 | ) { |
---|
211 | $scan = 1; |
---|
212 | } |
---|
213 | else { |
---|
214 | print "$full:0: file_says='$file_says'\n"; |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | if ($scan==-1) { |
---|
219 | if (/^#.*#$/) { |
---|
220 | print "$full:1: a lost file?\n"; |
---|
221 | } |
---|
222 | else { |
---|
223 | # die "Don't know what to do with '$full'"; |
---|
224 | } |
---|
225 | } |
---|
226 | elsif ($scan==1) { |
---|
227 | scan_for_tabs($full); |
---|
228 | # print "$full:0: scanning..\n"; |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | closedir(DIR); |
---|
234 | |
---|
235 | foreach (@subdirs) { |
---|
236 | recurse_dirs($_,$basedir); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | # -------------------------------------------------------------------------------- |
---|
241 | |
---|
242 | print "--- TAB brake ---\n"; |
---|
243 | |
---|
244 | my $ARBHOME = $ENV{'ARBHOME'}; |
---|
245 | |
---|
246 | if (not defined $ARBHOME) { |
---|
247 | die "'ARBHOME' not defined"; |
---|
248 | } |
---|
249 | |
---|
250 | my $time_stamp = $ARBHOME.'/SOURCE_TOOLS/tabBrake.stamp'; |
---|
251 | my $exitcode = 0; |
---|
252 | if (-f $time_stamp) { |
---|
253 | $files_newer_than = getModtime($time_stamp); |
---|
254 | print "Checking files newer than ".localtime($files_newer_than)."\n"; |
---|
255 | } |
---|
256 | else { |
---|
257 | if ($forceAll==1) { |
---|
258 | print "Initial call - checking all files\n"; |
---|
259 | $files_newer_than = 0; |
---|
260 | } |
---|
261 | else { |
---|
262 | print "Initial call - assuming everything is TAB-free\n"; |
---|
263 | $files_newer_than = time; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | if ($exitcode==0) { |
---|
268 | `touch $time_stamp`; # mark time of last try |
---|
269 | # i.e. user inserts TAB to file -> fail once |
---|
270 | } |
---|
271 | recurse_dirs($ARBHOME,$ARBHOME); |
---|
272 | if ($tab_count>0) { $exitcode = 1; } |
---|
273 | |
---|
274 | exit($exitcode); |
---|