[PDNCF] Python 3.12 compatibility
[platform/framework/web/chromium-efl.git] / tools / boilerplate.py
1 #!/usr/bin/env python3
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.
5
6 """Create files with copyright boilerplate and header include guards.
7
8 Usage: tools/boilerplate.py path/to/file.{h,cc}
9 """
10
11 from __future__ import print_function, unicode_literals
12
13 from datetime import date
14 import io
15 import os
16 import os.path
17 import sys
18
19 LINES = [
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.'
23 ]
24
25 NO_COMPILE_LINES = [
26     'This is a "No Compile Test" suite.',
27     'https://dev.chromium.org/developers/testing/no-compile-tests'
28 ]
29
30 EXTENSIONS_TO_COMMENTS = {
31     'cc': '//',
32     'gn': '#',
33     'gni': '#',
34     'h': '//',
35     'js': '//',
36     'mm': '//',
37     'mojom': '//',
38     'nc': '//',
39     'proto': '//',
40     'py': '#',
41     'swift': '//',
42     'ts': '//',
43     'typemap': '#',
44 }
45
46
47 def _GetHeaderImpl(filename, lines):
48   _, ext = os.path.splitext(filename)
49   ext = ext[1:]
50   comment = EXTENSIONS_TO_COMMENTS[ext] + ' '
51   return '\n'.join([comment + line for line in lines])
52
53
54 def _GetHeader(filename):
55   return _GetHeaderImpl(filename, LINES)
56
57
58 def _GetNoCompileHeader(filename):
59   assert (filename.endswith(".nc"))
60   return '\n' + _GetHeaderImpl(filename, NO_COMPILE_LINES)
61
62
63 def _CppHeader(filename):
64   guard = filename.upper() + '_'
65   for char in '/\\.+':
66     guard = guard.replace(char, '_')
67   return '\n'.join([
68     '',
69     '#ifndef ' + guard,
70     '#define ' + guard,
71     '',
72     '#endif  // ' + guard,
73     ''
74   ])
75
76
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):]
84   return filename
85
86
87 def _RemoveTestSuffix(filename):
88   base, _ = os.path.splitext(filename)
89   suffixes = [ '_test', '_unittest', '_browsertest' ]
90   for suffix in suffixes:
91     l = len(suffix)
92     if base[-l:] == suffix:
93       return base[:-l]
94   return base
95
96
97 def _IsIOSFile(filename):
98   if os.path.splitext(os.path.basename(filename))[0].endswith('_ios'):
99     return True
100   if 'ios' in filename.split(os.path.sep):
101     return True
102   return False
103
104
105 def _FilePathSlashesToCpp(filename):
106   return filename.replace('\\', '/')
107
108
109 def _CppImplementation(filename):
110   return '\n#include "' + _FilePathSlashesToCpp(_RemoveTestSuffix(filename)) \
111     + '.h"\n'
112
113
114 def _ObjCppImplementation(filename):
115   return '\n#import "' + _FilePathSlashesToCpp(_RemoveTestSuffix(filename)) \
116     + '.h"\n'
117
118
119 def _CreateFile(filename):
120   filename = _RemoveCurrentDirectoryPrefix(filename)
121
122   contents = _GetHeader(filename) + '\n'
123
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)
133
134   with io.open(filename, mode='w', newline='\n') as fd:
135     fd.write(contents)
136
137
138 def Main():
139   files = sys.argv[1:]
140   if len(files) < 1:
141     print(
142         'Usage: boilerplate.py path/to/file.h path/to/file.cc', file=sys.stderr)
143     return 1
144
145   # Perform checks first so that the entire operation is atomic.
146   for f in files:
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)
150       return 2
151
152     if os.path.exists(f):
153       print('A file at path %s already exists' % f, file=sys.stderr)
154       return 2
155
156   for f in files:
157     _CreateFile(f)
158
159
160 if __name__ == '__main__':
161   sys.exit(Main())