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

Side by Side Diff: tools/boilerplate.py

Issue 2846223002: Fixed some issues with boilerplate.py to make it work on Windows (Closed)
Patch Set: Created 3 years, 7 months 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved. 2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # 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
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Create files with copyright boilerplate and header include guards. 6 """Create files with copyright boilerplate and header include guards.
7 7
8 Usage: tools/boilerplate.py path/to/file.{h,cc} 8 Usage: tools/boilerplate.py path/to/file.{h,cc}
9 """ 9 """
10 10
(...skipping 21 matching lines...) Expand all
32 32
33 def _GetHeader(filename): 33 def _GetHeader(filename):
34 _, ext = os.path.splitext(filename) 34 _, ext = os.path.splitext(filename)
35 ext = ext[1:] 35 ext = ext[1:]
36 comment = EXTENSIONS_TO_COMMENTS[ext] + ' ' 36 comment = EXTENSIONS_TO_COMMENTS[ext] + ' '
37 return '\n'.join([comment + line for line in LINES]) 37 return '\n'.join([comment + line for line in LINES])
38 38
39 39
40 def _CppHeader(filename): 40 def _CppHeader(filename):
41 guard = filename.upper() + '_' 41 guard = filename.upper() + '_'
42 for char in '/.+': 42 for char in '/\\.+':
43 guard = guard.replace(char, '_') 43 guard = guard.replace(char, '_')
44 return '\n'.join([ 44 return '\n'.join([
45 '', 45 '',
46 '#ifndef ' + guard, 46 '#ifndef ' + guard,
47 '#define ' + guard, 47 '#define ' + guard,
48 '', 48 '',
49 '#endif // ' + guard, 49 '#endif // ' + guard,
50 '' 50 ''
51 ]) 51 ])
52 52
53 53
54 def _RemoveTestSuffix(filename): 54 def _RemoveTestSuffix(filename):
55 base, _ = os.path.splitext(filename) 55 base, _ = os.path.splitext(filename)
56 suffixes = [ '_test', '_unittest', '_browsertest' ] 56 suffixes = [ '_test', '_unittest', '_browsertest' ]
57 for suffix in suffixes: 57 for suffix in suffixes:
58 l = len(suffix) 58 l = len(suffix)
59 if base[-l:] == suffix: 59 if base[-l:] == suffix:
60 return base[:-l] 60 return base[:-l]
61 return base 61 return base
62 62
63 63
64 def _IsIOSFile(filename): 64 def _IsIOSFile(filename):
65 if os.path.splitext(os.path.basename(filename))[0].endswith('_ios'): 65 if os.path.splitext(os.path.basename(filename))[0].endswith('_ios'):
66 return True 66 return True
67 if 'ios' in filename.split(os.path.sep): 67 if 'ios' in filename.split(os.path.sep):
68 return True 68 return True
69 return False 69 return False
70 70
71 71
72 def _FilePathSlashesToCpp(filename):
73 return filename.replace('\\', '/')
74
75
72 def _CppImplementation(filename): 76 def _CppImplementation(filename):
73 return '\n#include "' + _RemoveTestSuffix(filename) + '.h"\n' 77 return '\n#include "' + _FilePathSlashesToCpp(_RemoveTestSuffix(filename)) \
78 + '.h"\n'
74 79
75 80
76 def _ObjCppImplementation(filename): 81 def _ObjCppImplementation(filename):
77 implementation = '\n#import "' + _RemoveTestSuffix(filename) + '.h"\n' 82 implementation = '\n#import "' + _RemoveTestSuffix(filename) + '.h"\n'
78 if not _IsIOSFile(filename): 83 if not _IsIOSFile(filename):
79 return implementation 84 return implementation
80 implementation += '\n' 85 implementation += '\n'
81 implementation += '#if !defined(__has_feature) || !__has_feature(objc_arc)\n' 86 implementation += '#if !defined(__has_feature) || !__has_feature(objc_arc)\n'
82 implementation += '#error "This file requires ARC support."\n' 87 implementation += '#error "This file requires ARC support."\n'
83 implementation += '#endif\n' 88 implementation += '#endif\n'
84 return implementation 89 return implementation
85 90
86 91
87 def _CreateFile(filename): 92 def _CreateFile(filename):
88 contents = _GetHeader(filename) + '\n' 93 contents = _GetHeader(filename) + '\n'
89 94
90 if filename.endswith('.h'): 95 if filename.endswith('.h'):
91 contents += _CppHeader(filename) 96 contents += _CppHeader(filename)
92 elif filename.endswith('.cc'): 97 elif filename.endswith('.cc'):
93 contents += _CppImplementation(filename) 98 contents += _CppImplementation(filename)
94 elif filename.endswith('.mm'): 99 elif filename.endswith('.mm'):
95 contents += _ObjCppImplementation(filename) 100 contents += _ObjCppImplementation(filename)
96 101
97 fd = open(filename, 'w') 102 fd = open(filename, 'wb')
98 fd.write(contents) 103 fd.write(contents)
99 fd.close() 104 fd.close()
100 105
101 106
102 def Main(): 107 def Main():
103 files = sys.argv[1:] 108 files = sys.argv[1:]
104 if len(files) < 1: 109 if len(files) < 1:
105 print >> sys.stderr, 'Usage: boilerplate.py path/to/file.h path/to/file.cc' 110 print >> sys.stderr, 'Usage: boilerplate.py path/to/file.h path/to/file.cc'
106 return 1 111 return 1
107 112
108 # Perform checks first so that the entire operation is atomic. 113 # Perform checks first so that the entire operation is atomic.
109 for f in files: 114 for f in files:
110 _, ext = os.path.splitext(f) 115 _, ext = os.path.splitext(f)
111 if not ext[1:] in EXTENSIONS_TO_COMMENTS: 116 if not ext[1:] in EXTENSIONS_TO_COMMENTS:
112 print >> sys.stderr, 'Unknown file type for %s' % f 117 print >> sys.stderr, 'Unknown file type for %s' % f
113 return 2 118 return 2
114 119
115 if os.path.exists(f): 120 if os.path.exists(f):
116 print >> sys.stderr, 'A file at path %s already exists' % f 121 print >> sys.stderr, 'A file at path %s already exists' % f
117 return 2 122 return 2
118 123
119 for f in files: 124 for f in files:
120 _CreateFile(f) 125 _CreateFile(f)
121 126
122 127
123 if __name__ == '__main__': 128 if __name__ == '__main__':
124 sys.exit(Main()) 129 sys.exit(Main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698