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
4 # found in the LICENSE file.
6 """Create files with copyright boilerplate and header include guards.
8 Usage: tools/boilerplate.py path/to/file.{h,cc}
11 from datetime import date
17 'Copyright %d The Chromium Authors. All rights reserved.' %
19 'Use of this source code is governed by a BSD-style license that can be',
20 'found in the LICENSE file.'
23 EXTENSIONS_TO_COMMENTS = {
31 def _GetHeader(filename):
32 _, ext = os.path.splitext(filename)
34 comment = EXTENSIONS_TO_COMMENTS[ext] + ' '
35 return '\n'.join([comment + line for line in LINES])
38 def _CppHeader(filename):
39 guard = filename.replace('/', '_').replace('.', '_').upper() + '_'
50 def _CppImplementation(filename):
51 base, _ = os.path.splitext(filename)
52 include = '#include "' + base + '.h"'
53 return '\n'.join(['', include])
56 def _CreateFile(filename):
57 contents = _GetHeader(filename) + '\n'
59 if filename.endswith('.h'):
60 contents += _CppHeader(filename)
61 elif filename.endswith('.cc') or filename.endswith('.mm'):
62 contents += _CppImplementation(filename)
64 fd = open(filename, 'w')
72 print >> sys.stderr, 'Usage: boilerplate.py path/to/file.h path/to/file.cc'
75 # Perform checks first so that the entire operation is atomic.
77 _, ext = os.path.splitext(f)
78 if not ext[1:] in EXTENSIONS_TO_COMMENTS:
79 print >> sys.stderr, 'Unknown file type for %s' % f
83 print >> sys.stderr, 'A file at path %s already exists' % f
90 if __name__ == '__main__':