Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: tools/pragmaonce/pragmaonce.py

Issue 8678023: Fix python scripts in src/tools/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/playback_benchmark/run.py ('k') | tools/python/google/gethash_timer.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 #!/usr/bin/env python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 4 # found in the LICENSE file.
4 5
6 """Tool to add "#pragma once" lines to files that don't have it yet.
7
8 Usage:
9 find chrome -name '*.h' -exec tools/pragmaonce/pragmaonce.py {} \;
10 """
11
5 import re 12 import re
6 import sys 13 import sys
7 14
8 # A tool to add "#pragma once" lines to files that don't have it yet.
9 # Intended usage:
10 # find chrome -name '*.h' -exec python tools/pragmaonce/pragmaonce.py {} \;
11 15
12 # Some files have absurdly long comments at the top 16 # Some files have absurdly long comments at the top
13 NUM_LINES_TO_SCAN_FOR_GUARD = 250 17 NUM_LINES_TO_SCAN_FOR_GUARD = 250
14 18
15 def main(filename): 19 def main(filename):
16 f = open(filename) 20 f = open(filename)
17 lines = f.readlines() 21 lines = f.readlines()
18 f.close() 22 f.close()
19 23
20 index = -1 24 index = -1
21 for i in xrange(min(NUM_LINES_TO_SCAN_FOR_GUARD, len(lines) - 1)): 25 for i in xrange(min(NUM_LINES_TO_SCAN_FOR_GUARD, len(lines) - 1)):
22 m1 = re.match(r'^#ifndef ([A-Z_0-9]+)$', lines[i]) 26 m1 = re.match(r'^#ifndef ([A-Z_0-9]+)$', lines[i])
23 m2 = re.match(r'^#define ([A-Z_0-9]+)$', lines[i + 1]) 27 m2 = re.match(r'^#define ([A-Z_0-9]+)$', lines[i + 1])
24 if m1 and m2: 28 if m1 and m2:
25 if m1.group(1) != m2.group(1): 29 if m1.group(1) != m2.group(1):
26 print 'Skipping', filename, \ 30 print 'Skipping', filename, \
27 ': Broken include guard (%s, %s)' % (m1.group(1), m2.group(1)) 31 ': Broken include guard (%s, %s)' % (m1.group(1), m2.group(1))
28 index = i + 2 32 index = i + 2
29 break 33 break
30 34
31 if index == -1: 35 if index == -1:
32 print 'Skipping', filename, ': no include guard found' 36 print 'Skipping', filename, ': no include guard found'
33 return 37 return
34 38
35 if index < len(lines) and re.match(r'#pragma once', lines[index]): 39 if index < len(lines) and re.match(r'#pragma once', lines[index]):
36 # The pragma is already there. 40 # The pragma is already there.
37 return 41 return 0
38 42
39 lines.insert(index, "#pragma once\n") 43 lines.insert(index, "#pragma once\n")
40 44
41 f = open(filename, 'w') 45 f = open(filename, 'w')
42 f.write(''.join(lines)) 46 f.write(''.join(lines))
43 f.close() 47 f.close()
48 return 0
44 49
45 if __name__ == '__main__': 50 if __name__ == '__main__':
46 if len(sys.argv) != 2: 51 if len(sys.argv) != 2:
47 print >>sys.stderr, "Usage: %s inputfile" % sys.argv[0] 52 print >>sys.stderr, "Usage: %s inputfile" % sys.argv[0]
48 sys.exit(1) 53 sys.exit(1)
49 main(sys.argv[1]) 54 sys.exit(main(sys.argv[1]))
OLDNEW
« no previous file with comments | « tools/playback_benchmark/run.py ('k') | tools/python/google/gethash_timer.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698