File: tabBrake.pl

package info (click to toggle)
arb 6.0.6-8
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 66,204 kB
  • sloc: ansic: 394,911; cpp: 250,290; makefile: 19,644; sh: 15,879; perl: 10,473; fortran: 6,019; ruby: 683; xml: 503; python: 53; awk: 32
file content (297 lines) | stat: -rwxr-xr-x 9,807 bytes parent folder | download | duplicates (6)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/perl
#
# checks files for TABs and raises error if TABs found
# (TABs are evil, cause they have no default width)
#
# As well checks for older ARB compile logs and removes them


use strict;
use warnings;

my $forceAll  = 0; # 1 -> force scan of all files if no stamp, 0 -> assume all ok, scan only new files
my $defsStart = 15; # lineno of start of definition

my $checkForOldLogs = 1; # whether to check for old logfile and delete them

my %scan_extension = map { $_ => 1; } (
                                       'c', 'h',
                                       'cxx', 'hxx',
                                       'cpp', 'hpp',
                                       'f', 'inc',
                                       'm4', 'status', 'in',
                                       'txt', 'doc', 'README', 'readme',
                                       'sh',
                                       'pl', 'pm', 'PL', 'cgi',
                                       'java',
                                       'head', 'header', 'default', 'footer',
                                       'makefile',
                                       'template',
                                       'dat',
                                       'dtd', 'xsl',
                                       'eft', 'ift', 'ift2', 'amc',
                                       'sellst',
                                       'mask',
                                       'aisc', 'pa',
                                       'source', 'menu',
                                       'mat', 'conf',
                                      );

my %auto_determine = map { $_ => 1; } (
                                       'x', 'jar',
                                      );

my %ignore_extension = map { $_ => 1; } (
                                         'a', 'o', 'so',
                                         'class',
                                         'html', 'bitmap', 'ps', 'icon', 'hlp', 'help', 
                                         'gif', 'png', 'fig', 'vfont', 'SAVE',
                                         'tgz', 'gz',
                                         'lst', 'depends',
                                         'md5', 'bs', 'xs',
                                         'exists', 'ignore', '3pm',
                                         'arb', 'seq', 'genmenu',
                                         'init', 'cvsignore', 'log', 'am', 'org',
                                         'last_gcc', 'last_compiler', 'csv',
                                         'b', 'n', 'helpfiles', 
                                         'ms', 'gon', 'pep', 'bla', 'tct', 'comm',
                                         'before', 'v1', 'data', 'pdb', 'embl', 'xpm', 'big',
                                         '2_manual_postscript', '2_manual_text',
                                         'stamp',
                                        );

my %ignored_subdirs = map { $_ => 1; } (
                                        'GDE',
                                        'READSEQ',
                                        'HEADERLIBS',
                                        'patches',
                                        'UNIT_TESTER/flags',
                                        'UNIT_TESTER/logs',
                                        'UNIT_TESTER/run',
                                        'UNIT_TESTER/sockets',
                                        'UNIT_TESTER/tests',
                                        'UNIT_TESTER/valgrind',
                                        'bin',
                                       );

my %ignored_relpath = map { $_ => 1; } (
                                        'NAMES_COM/names_client.h',
                                        'NAMES_COM/names_server.h',
                                        'PROBE_COM/PT_com.h',
                                        'PROBE_COM/PT_server.h',
                                       );

my $tab_count        = 0;
my $files_newer_than = 0;

sub getModtime($) {
  my ($file) = @_;
  my @st = stat($file);
  if (not @st) { die "can't stat '$file' ($!)"; }
  return $st[9];
}


sub scan_for_tabs($) {
  my ($file) = @_;
  my $has_tabs = `grep -m 1 -n -H '\t' $file`;

  # print "$file:0: has_tabs='$has_tabs'\n";

  if ($has_tabs ne '') {
    my $pos = "$file:0:";
    if ($has_tabs =~ /^[^:]+:[0-9]+:/) {
      $pos = $&;
    }
    print "$pos contains tabs. Tabs are not allowed.\n";
    $tab_count++;
    if ($tab_count>10) { die "Further scan skipped.\n"; }
  }
}

sub recurse_dirs($$);
sub recurse_dirs($$) {
  my ($dir,$basedir) = @_;

  my @subdirs = ();
  my $subdir  = '';
  if ($dir ne $basedir) {
    $subdir = substr($dir,length($basedir)+1);
    if (defined $ignored_subdirs{$subdir}) {
      # print "Ignoring '$subdir' (dir='$dir')\n";
      return;
    }
  }

  opendir(DIR, $dir) || die "can't read directory '$dir' (Reason: $!)";
  foreach (readdir(DIR)) {
    if ($_ ne '.' and $_ ne '..') {
      my $full = $dir.'/'.$_;
      my $rel  = $subdir.'/'.$_;
      if (-l $full) {
        # print "$full:0: link -- skipped\n";
      }
      elsif (-d $full) {
        if ($_ ne 'CVS' and
            $_ ne '.svn' and
            $_ ne 'ARB_SOURCE_DOC' and
            $_ ne 'old_help' and
            $_ ne 'PERL2ARB' and
            $_ ne 'GENC' and $_ ne 'GENH'
           ) {
          push @subdirs, $full;
        }
      }
      elsif (-f $full) {
        my $scan      = -1;
        my $determine = 0;
        my $modtime   = getModtime($full);

        if ($modtime<$files_newer_than) {
          # file was created before last run of tabBrake.pl
          $scan = 0;

          # check if it's a log from an aborted compile
          if (/^[^.]+\.([0-9]+)\.log$/o) {
            if ($checkForOldLogs and $modtime<($files_newer_than-3*60)) {
              print "Old log file: $full -- removing\n";
              unlink($full) || print "$full:0: can't unlink (Reason: $!)\n";
            }
          }
        }
        elsif (defined $ignored_relpath{$rel}) {
          # print "$full:0: excluded by relpath '$rel'\n";
          $scan = 0;
        }
        else {
          if (/\.([^.]+)$/) {   # file with extension
            my $ext = $1;
            if (exists $ignore_extension{$ext}) {
              # print "$full:0: excluded by extension '$ext'\n";
              $scan = 0;
            }
            elsif (exists $scan_extension{$ext}) {
              # print "$full:0: scan!\n";
              $scan = 1;
            }
            elsif (exists $auto_determine{$ext}) {
              # print "$full:0: scan!\n";
              $determine = 1;
            }
            else {
              if ($ext =~ /^[0-9]+$/ 
                 ) {
                $scan = 0;
              }
              elsif ($full =~ /util\/config\./ or
                     $full =~ /lib\/config\./
                    ) {
                $scan = 1;
              }
              else {
                                # print "$full:0: a '$ext' file\n";
              }
            }
          }
          else {
            if (/Makefile/ or
                /ChangeLog/
               ) {
              $scan = 0;
            }
            else {
              $determine = 1;
            }
          }

          if ($determine==1) {
            $scan==-1 || die "logic error";
            my $file_says = `file $full`;
            if ($file_says =~ /^[^:]+:[ ]*(.*)$/) {
              $file_says = $1;
            }
            else {
              die "Can't parse output from 'file'-command";
            }

            if ($file_says =~ /executable/ or
                $file_says eq 'empty' or
                $file_says eq 'data' or
                $file_says eq 'IFF data' or
                $file_says =~ /archive.*data/
               ) {
              $scan = 0;
            }
            elsif ($file_says =~ /shell.*script/ or
                   $file_says =~ /ASCII.*text/ or 
                   $file_says =~ /ISO.*text/ or
                   $file_says =~ /perl.*script/ 
                  ) {
              $scan = 1;
            }
            else {
              print "$full:0: file_says='$file_says'\n";
            }
          }
        }
        if ($scan==-1) {
          if (/^#.*#$/) {
            print "$full:1: a lost file?\n";
          }
          else {
            # die "Don't know what to do with '$full'";
          }
        }
        elsif ($scan==1) {
          scan_for_tabs($full);
          # print "$full:0: scanning..\n";
        }
      }
    }
  }
  closedir(DIR);

  foreach (@subdirs) {
    recurse_dirs($_,$basedir);
  }
}

# --------------------------------------------------------------------------------

print "--- TAB brake ---\n";

my $ARBHOME = $ENV{'ARBHOME'};

if (not defined $ARBHOME) {
  die "'ARBHOME' not defined";
}

my $time_stamp = $ARBHOME.'/SOURCE_TOOLS/stamp.tabBrake';
my $exitcode   = 0;
if (-f $time_stamp) {
  $files_newer_than = getModtime($time_stamp);
  print "Checking files newer than ".localtime($files_newer_than)."\n";
}
else {
  if ($forceAll==1) {
    print "Initial call - checking all files\n";
    $files_newer_than = 0;
  }
  else {
    print "Initial call - assuming everything is TAB-free\n";
    $files_newer_than = time;
    $checkForOldLogs  = 0; # do not check for old logs (sometimes fails on fresh checkouts, e.g. in jenkins build server)
  }
}

if ($exitcode==0) {
  `touch $time_stamp`;          # mark time of last try
  # i.e. user inserts TAB to file -> fail once
}
recurse_dirs($ARBHOME,$ARBHOME);
if ($tab_count>0) {
  $exitcode = 1;
  print $ARBHOME.'/SOURCE_TOOLS/tabBrake.pl:'.$defsStart.': Warning: what may contain TABs is defined here'."\n";
}

exit($exitcode);