2 # Copyright 2014 The Chromium Authors
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 __future__ import print_function, unicode_literals
13 from datetime import date
20 f'Copyright {date.today().year} The Chromium Authors',
21 'Use of this source code is governed by a BSD-style license that can be',
22 'found in the LICENSE file.'
26 'This is a "No Compile Test" suite.',
27 'https://dev.chromium.org/developers/testing/no-compile-tests'
30 EXTENSIONS_TO_COMMENTS = {
47 def _GetHeaderImpl(filename, lines):
48 _, ext = os.path.splitext(filename)
50 comment = EXTENSIONS_TO_COMMENTS[ext] + ' '
51 return '\n'.join([comment + line for line in lines])
54 def _GetHeader(filename):
55 return _GetHeaderImpl(filename, LINES)
58 def _GetNoCompileHeader(filename):
59 assert (filename.endswith(".nc"))
60 return '\n' + _GetHeaderImpl(filename, NO_COMPILE_LINES)
63 def _CppHeader(filename):
64 guard = filename.upper() + '_'
66 guard = guard.replace(char, '_')
77 def _RemoveCurrentDirectoryPrefix(filename):
78 current_dir_prefixes = [os.curdir + os.sep]
79 if os.altsep is not None:
80 current_dir_prefixes.append(os.curdir + os.altsep)
81 for prefix in current_dir_prefixes:
82 if filename.startswith(prefix):
83 return filename[len(prefix):]
87 def _RemoveTestSuffix(filename):
88 base, _ = os.path.splitext(filename)
89 suffixes = [ '_test', '_unittest', '_browsertest' ]
90 for suffix in suffixes:
92 if base[-l:] == suffix:
97 def _IsIOSFile(filename):
98 if os.path.splitext(os.path.basename(filename))[0].endswith('_ios'):
100 if 'ios' in filename.split(os.path.sep):
105 def _FilePathSlashesToCpp(filename):
106 return filename.replace('\\', '/')
109 def _CppImplementation(filename):
110 return '\n#include "' + _FilePathSlashesToCpp(_RemoveTestSuffix(filename)) \
114 def _ObjCppImplementation(filename):
115 return '\n#import "' + _FilePathSlashesToCpp(_RemoveTestSuffix(filename)) \
119 def _CreateFile(filename):
120 filename = _RemoveCurrentDirectoryPrefix(filename)
122 contents = _GetHeader(filename) + '\n'
124 if filename.endswith('.h'):
125 contents += _CppHeader(filename)
126 elif filename.endswith('.cc'):
127 contents += _CppImplementation(filename)
128 elif filename.endswith('.nc'):
129 contents += _GetNoCompileHeader(filename) + '\n'
130 contents += _CppImplementation(filename)
131 elif filename.endswith('.mm'):
132 contents += _ObjCppImplementation(filename)
134 with io.open(filename, mode='w', newline='\n') as fd:
142 'Usage: boilerplate.py path/to/file.h path/to/file.cc', file=sys.stderr)
145 # Perform checks first so that the entire operation is atomic.
147 _, ext = os.path.splitext(f)
148 if not ext[1:] in EXTENSIONS_TO_COMMENTS:
149 print('Unknown file type for %s' % f, file=sys.stderr)
152 if os.path.exists(f):
153 print('A file at path %s already exists' % f, file=sys.stderr)
160 if __name__ == '__main__':