Jeroen Dhollander | 8254062 | 2022-03-31 15:27:23 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Avi Drissman | 01528bb4 | 2022-09-08 15:21:46 | [diff] [blame] | 2 | # Copyright 2014 The Chromium Authors |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 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 | |
Jeffrey Young | f16b207 | 2021-09-15 21:43:24 | [diff] [blame] | 11 | from __future__import print_function, unicode_literals |
Raul Tambre | 57e09d6 | 2019-09-22 17:18:52 | [diff] [blame] | 12 | |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 13 | from datetimeimport date |
Jeffrey Young | f16b207 | 2021-09-15 21:43:24 | [diff] [blame] | 14 | import io |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 15 | import os |
| 16 | import os.path |
| 17 | import sys |
| 18 | |
| 19 | LINES=[ |
Avi Drissman | 01528bb4 | 2022-09-08 15:21:46 | [diff] [blame] | 20 | f'Copyright {date.today().year} The Chromium Authors', |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 21 | 'Use of this source code is governed by a BSD-style license that can be', |
| 22 | 'found in the LICENSE file.' |
| 23 | ] |
| 24 | |
Jan Wilken Dörrie | c6601a53 | 2021-01-05 16:24:54 | [diff] [blame] | 25 | NO_COMPILE_LINES=[ |
| 26 | 'This is a "No Compile Test" suite.', |
| 27 | 'https://dev.chromium.org/developers/testing/no-compile-tests' |
| 28 | ] |
| 29 | |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 30 | EXTENSIONS_TO_COMMENTS={ |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 31 | 'cc':'//', |
sdefresne | 5feb0b4 | 2016-03-15 11:03:40 | [diff] [blame] | 32 | 'gn':'#', |
| 33 | 'gni':'#', |
Sylvain Defresne | 5548a45 | 2023-02-21 18:59:48 | [diff] [blame] | 34 | 'h':'//', |
| 35 | 'js':'//', |
| 36 | 'mm':'//', |
Tanmoy Mollik | 0bf3efe | 2019-05-10 09:23:12 | [diff] [blame] | 37 | 'mojom':'//', |
Sylvain Defresne | 5548a45 | 2023-02-21 18:59:48 | [diff] [blame] | 38 | 'nc':'//', |
| 39 | 'proto':'//', |
| 40 | 'py':'#', |
| 41 | 'swift':'//', |
Mike Dougherty | f69ec2a | 2022-05-12 04:12:53 | [diff] [blame] | 42 | 'ts':'//', |
Tanmoy Mollik | 0bf3efe | 2019-05-10 09:23:12 | [diff] [blame] | 43 | 'typemap':'#', |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 44 | } |
| 45 | |
Jan Wilken Dörrie | c6601a53 | 2021-01-05 16:24:54 | [diff] [blame] | 46 | |
| 47 | def_GetHeaderImpl(filename, lines): |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 48 | _, ext= os.path.splitext(filename) |
| 49 | ext= ext[1:] |
| 50 | comment= EXTENSIONS_TO_COMMENTS[ext]+' ' |
Jan Wilken Dörrie | c6601a53 | 2021-01-05 16:24:54 | [diff] [blame] | 51 | return'\n'.join([comment+ linefor linein 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) |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 61 | |
| 62 | |
| 63 | def_CppHeader(filename): |
rohitrao | 786f9438 | 2016-11-16 02:47:15 | [diff] [blame] | 64 | guard= filename.upper()+'_' |
emx | 359564e | 2017-04-28 18:17:15 | [diff] [blame] | 65 | for charin'/\\.+': |
marq | 070cc78 | 2016-11-15 16:19:25 | [diff] [blame] | 66 | guard= guard.replace(char,'_') |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 67 | return'\n'.join([ |
| 68 | '', |
| 69 | '#ifndef '+ guard, |
| 70 | '#define '+ guard, |
| 71 | '', |
| 72 | '#endif // '+ guard, |
| 73 | '' |
| 74 | ]) |
| 75 | |
| 76 | |
Jesse McKenna | 79d284b8 | 2020-04-23 14:51:50 | [diff] [blame] | 77 | def_RemoveCurrentDirectoryPrefix(filename): |
| 78 | current_dir_prefixes=[os.curdir+ os.sep] |
| 79 | if os.altsepisnotNone: |
| 80 | current_dir_prefixes.append(os.curdir+ os.altsep) |
| 81 | for prefixin current_dir_prefixes: |
| 82 | if filename.startswith(prefix): |
| 83 | return filename[len(prefix):] |
| 84 | return filename |
| 85 | |
| 86 | |
bnc | 70c50537 | 2016-12-14 16:28:14 | [diff] [blame] | 87 | def_RemoveTestSuffix(filename): |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 88 | base, _= os.path.splitext(filename) |
bnc | 70c50537 | 2016-12-14 16:28:14 | [diff] [blame] | 89 | suffixes=['_test','_unittest','_browsertest'] |
| 90 | for suffixin suffixes: |
| 91 | l= len(suffix) |
| 92 | if base[-l:]== suffix: |
| 93 | return base[:-l] |
| 94 | return base |
| 95 | |
sdefresne | d6844b4a | 2017-03-07 01:11:20 | [diff] [blame] | 96 | |
| 97 | def_IsIOSFile(filename): |
| 98 | if os.path.splitext(os.path.basename(filename))[0].endswith('_ios'): |
| 99 | returnTrue |
| 100 | if'ios'in filename.split(os.path.sep): |
| 101 | returnTrue |
| 102 | returnFalse |
| 103 | |
| 104 | |
emx | 359564e | 2017-04-28 18:17:15 | [diff] [blame] | 105 | def_FilePathSlashesToCpp(filename): |
| 106 | return filename.replace('\\','/') |
| 107 | |
| 108 | |
bnc | 70c50537 | 2016-12-14 16:28:14 | [diff] [blame] | 109 | def_CppImplementation(filename): |
emx | 359564e | 2017-04-28 18:17:15 | [diff] [blame] | 110 | return'\n#include "'+_FilePathSlashesToCpp(_RemoveTestSuffix(filename)) \ |
| 111 | +'.h"\n' |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 112 | |
| 113 | |
kkhorimoto | cc826ad | 2016-02-11 20:17:46 | [diff] [blame] | 114 | def_ObjCppImplementation(filename): |
Avi Drissman | 3d243a4 | 2023-08-01 16:53:59 | [diff] [blame] | 115 | return'\n#import "'+_FilePathSlashesToCpp(_RemoveTestSuffix(filename)) \ |
| 116 | +'.h"\n' |
kkhorimoto | cc826ad | 2016-02-11 20:17:46 | [diff] [blame] | 117 | |
| 118 | |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 119 | def_CreateFile(filename): |
Jesse McKenna | 79d284b8 | 2020-04-23 14:51:50 | [diff] [blame] | 120 | filename=_RemoveCurrentDirectoryPrefix(filename) |
| 121 | |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 122 | contents=_GetHeader(filename)+'\n' |
| 123 | |
| 124 | if filename.endswith('.h'): |
| 125 | contents+=_CppHeader(filename) |
kkhorimoto | cc826ad | 2016-02-11 20:17:46 | [diff] [blame] | 126 | elif filename.endswith('.cc'): |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 127 | contents+=_CppImplementation(filename) |
Jan Wilken Dörrie | c6601a53 | 2021-01-05 16:24:54 | [diff] [blame] | 128 | elif filename.endswith('.nc'): |
| 129 | contents+=_GetNoCompileHeader(filename)+'\n' |
| 130 | contents+=_CppImplementation(filename) |
kkhorimoto | cc826ad | 2016-02-11 20:17:46 | [diff] [blame] | 131 | elif filename.endswith('.mm'): |
| 132 | contents+=_ObjCppImplementation(filename) |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 133 | |
Jeffrey Young | f16b207 | 2021-09-15 21:43:24 | [diff] [blame] | 134 | with io.open(filename, mode='w', newline='\n')as fd: |
| 135 | fd.write(contents) |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 136 | |
| 137 | |
Ryan Tarpine | 9d34209 | 2024-02-22 00:40:28 | [diff] [blame] | 138 | # A file is safe to overwrite if it's an empty file we can write to. |
| 139 | def_IsSafeToOverwrite(path): |
| 140 | return os.path.isfile(path)and os.path.getsize(path)==0and os.access( |
| 141 | path, os.W_OK) |
| 142 | |
| 143 | |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 144 | defMain(): |
| 145 | files= sys.argv[1:] |
| 146 | if len(files)<1: |
Raul Tambre | 57e09d6 | 2019-09-22 17:18:52 | [diff] [blame] | 147 | print( |
| 148 | 'Usage: boilerplate.py path/to/file.h path/to/file.cc', file=sys.stderr) |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 149 | return1 |
| 150 | |
| 151 | # Perform checks first so that the entire operation is atomic. |
| 152 | for fin files: |
| 153 | _, ext= os.path.splitext(f) |
| 154 | ifnot ext[1:]in EXTENSIONS_TO_COMMENTS: |
Raul Tambre | 57e09d6 | 2019-09-22 17:18:52 | [diff] [blame] | 155 | print('Unknown file type for %s'% f, file=sys.stderr) |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 156 | return2 |
| 157 | |
Ryan Tarpine | 9d34209 | 2024-02-22 00:40:28 | [diff] [blame] | 158 | if os.path.exists(f)andnot_IsSafeToOverwrite(f): |
Raul Tambre | 57e09d6 | 2019-09-22 17:18:52 | [diff] [blame] | 159 | print('A file at path %s already exists'% f, file=sys.stderr) |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 160 | return2 |
| 161 | |
rsesek@chromium.org | fb9d58c | 2014-03-20 17:43:10 | [diff] [blame] | 162 | for fin files: |
| 163 | _CreateFile(f) |
| 164 | |
| 165 | |
| 166 | if __name__=='__main__': |
| 167 | sys.exit(Main()) |