OLD | NEW |
---|---|
(Empty) | |
1 import re | |
Evan Martin
2010/07/22 00:44:54
Copyright header plz thx.
Nico
2010/07/26 18:54:58
Presubmit caught this before committing.
| |
2 import sys | |
3 | |
4 # A tool to add "#pragma once" lines to files that don't have it yet. | |
5 # Intended usage: | |
6 # find chrome -name '*.h' -exec python tools/pragmaonce/pragmaonce.py {} \; | |
Evan Martin
2010/07/22 00:44:54
nit: extra double space
Nico
2010/07/26 18:54:58
Done.
| |
7 | |
8 # Some files have absurdly long comments at the top | |
9 NUM_LINES_TO_SCAN_FOR_GUARD = 250 | |
10 | |
11 def main(filename): | |
12 f = open(filename) | |
13 lines = f.readlines() | |
14 f.close() | |
15 | |
16 index = -1 | |
17 for i in xrange(min(NUM_LINES_TO_SCAN_FOR_GUARD, len(lines) - 1)): | |
18 m1 = re.match(r'^#ifndef ([A-Z_]+)', lines[i]) | |
Mark Mentovai
2010/07/22 02:48:38
You should likely permit digits too.
Nico
2010/07/26 18:54:58
Since I don't have a "$" at the end, this happened
| |
19 m2 = re.match(r'^#define ([A-Z_]+)', lines[i + 1]) | |
20 if m1 and m2: | |
21 if m1.group(1) != m2.group(1): | |
22 print 'Skipping', filename, \ | |
23 ': Broken include guard (%s, %s)' % (m1.group(1), m2.group(1)) | |
24 index = i + 2 | |
25 break | |
26 | |
27 if index == -1: | |
28 print 'Skipping', filename, ': no include guard found' | |
29 return | |
30 | |
31 if index < len(lines) and re.match(r'#pragma once', lines[index]): | |
32 # The pragma is already there. | |
33 return | |
34 | |
35 lines.insert(index, "#pragma once\n") | |
36 | |
37 f = open(filename, 'w') | |
38 f.write(''.join(lines)) | |
39 f.close() | |
40 | |
41 if __name__ == '__main__': | |
42 if len(sys.argv) != 2: | |
43 print >>sys.stderr, "Usage: %s inputfile" % sys.argv[0] | |
44 sys.exit(1) | |
45 main(sys.argv[1]) | |
OLD | NEW |