OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """Moves C++ files to a new location, updating any include paths that point | 6 """Moves C++ files to a new location, updating any include paths that point |
7 to them, and re-ordering headers as needed. If multiple source files are | 7 to them, and re-ordering headers as needed. If multiple source files are |
8 specified, the destination must be a directory. Updates include guards in | 8 specified, the destination must be a directory. Updates include guards in |
9 moved header files. Assumes Chromium coding style. | 9 moved header files. Assumes Chromium coding style. |
10 | 10 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 return to_path | 65 return to_path |
66 | 66 |
67 | 67 |
68 def MoveFile(from_path, to_path): | 68 def MoveFile(from_path, to_path): |
69 """Performs a git mv command to move a file from |from_path| to |to_path|. | 69 """Performs a git mv command to move a file from |from_path| to |to_path|. |
70 """ | 70 """ |
71 if not os.system('git mv %s %s' % (from_path, to_path)) == 0: | 71 if not os.system('git mv %s %s' % (from_path, to_path)) == 0: |
72 raise Exception('Fatal: Failed to run git mv command.') | 72 raise Exception('Fatal: Failed to run git mv command.') |
73 | 73 |
74 | 74 |
75 def UpdatePostMove(from_path, to_path): | 75 def UpdatePostMove(from_path, to_path, in_blink): |
76 """Given a file that has moved from |from_path| to |to_path|, | 76 """Given a file that has moved from |from_path| to |to_path|, |
77 updates the moved file's include guard to match the new path and | 77 updates the moved file's include guard to match the new path and |
78 updates all references to the file in other source files. Also tries | 78 updates all references to the file in other source files. Also tries |
79 to update references in .gyp(i) files using a heuristic. | 79 to update references in .gyp(i) files using a heuristic. |
80 """ | 80 """ |
81 # Include paths always use forward slashes. | 81 # Include paths always use forward slashes. |
82 from_path = from_path.replace('\\', '/') | 82 from_path = from_path.replace('\\', '/') |
83 to_path = to_path.replace('\\', '/') | 83 to_path = to_path.replace('\\', '/') |
84 | 84 |
85 if os.path.splitext(from_path)[1] in ['.h', '.hh']: | 85 if os.path.splitext(from_path)[1] in ['.h', '.hh']: |
86 UpdateIncludeGuard(from_path, to_path) | 86 UpdateIncludeGuard(from_path, to_path) |
87 | 87 |
88 from_include_path = from_path | |
89 to_include_path = to_path | |
90 if in_blink: | |
91 def UpdateIncludePathForBlink(path): | |
Matt Giuca
2015/07/07 05:20:52
This should be moved to the top level. (Only make
benwells
2015/07/13 05:46:31
Done.
| |
92 for prefix in ['public/', 'Source/']: | |
Matt Giuca
2015/07/07 05:20:52
Use tuple not list ("(,)" not "[,]").
(Semantical
benwells
2015/07/13 05:46:31
Done.
| |
93 if path.startswith(prefix): | |
94 return path[len(prefix):] | |
Matt Giuca
2015/07/07 05:20:52
nit: Blank line after return.
benwells
2015/07/13 05:46:31
Done.
| |
95 return path | |
96 from_include_path = UpdateIncludePathForBlink(from_include_path) | |
97 to_include_path = UpdateIncludePathForBlink(to_include_path) | |
98 | |
88 # Update include/import references. | 99 # Update include/import references. |
89 files_with_changed_includes = mffr.MultiFileFindReplace( | 100 files_with_changed_includes = mffr.MultiFileFindReplace( |
90 r'(#(include|import)\s*["<])%s([>"])' % re.escape(from_path), | 101 r'(#(include|import)\s*["<])%s([>"])' % re.escape(from_include_path), |
91 r'\1%s\3' % to_path, | 102 r'\1%s\3' % to_include_path, |
92 ['*.cc', '*.h', '*.m', '*.mm', '*.cpp']) | 103 ['*.cc', '*.h', '*.m', '*.mm', '*.cpp']) |
93 | 104 |
94 # Reorder headers in files that changed. | 105 # Reorder headers in files that changed. |
95 for changed_file in files_with_changed_includes: | 106 for changed_file in files_with_changed_includes: |
96 def AlwaysConfirm(a, b): return True | 107 def AlwaysConfirm(a, b): return True |
97 sort_headers.FixFileWithConfirmFunction(changed_file, AlwaysConfirm, True) | 108 sort_headers.FixFileWithConfirmFunction(changed_file, AlwaysConfirm, True, |
109 in_blink) | |
98 | 110 |
99 # Update comments; only supports // comments, which are primarily | 111 # Update comments; only supports // comments, which are primarily |
100 # used in our code. | 112 # used in our code. |
101 # | 113 # |
102 # This work takes a bit of time. If this script starts feeling too | 114 # This work takes a bit of time. If this script starts feeling too |
103 # slow, one good way to speed it up is to make the comment handling | 115 # slow, one good way to speed it up is to make the comment handling |
104 # optional under a flag. | 116 # optional under a flag. |
105 mffr.MultiFileFindReplace( | 117 mffr.MultiFileFindReplace( |
106 r'(//.*)%s' % re.escape(from_path), | 118 r'(//.*)%s' % re.escape(from_path), |
107 r'\1%s' % to_path, | 119 r'\1%s' % to_path, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 'include guard manually. (%s)' % new_path) | 200 'include guard manually. (%s)' % new_path) |
189 | 201 |
190 with open(new_path, 'w') as f: | 202 with open(new_path, 'w') as f: |
191 f.write(new_contents) | 203 f.write(new_contents) |
192 | 204 |
193 def main(): | 205 def main(): |
194 if not os.path.isdir('.git'): | 206 if not os.path.isdir('.git'): |
195 print 'Fatal: You must run from the root of a git checkout.' | 207 print 'Fatal: You must run from the root of a git checkout.' |
196 return 1 | 208 return 1 |
197 | 209 |
210 in_blink = os.getcwd().endswith("third_party/WebKit") | |
Matt Giuca
2015/07/07 05:20:52
I'm concerned this will break when the repos merge
benwells
2015/07/13 05:46:31
Yep, it will break. I think it is unavoidable now,
| |
211 | |
198 parser = optparse.OptionParser(usage='%prog FROM_PATH... TO_PATH') | 212 parser = optparse.OptionParser(usage='%prog FROM_PATH... TO_PATH') |
199 parser.add_option('--already_moved', action='store_true', | 213 parser.add_option('--already_moved', action='store_true', |
200 dest='already_moved', | 214 dest='already_moved', |
201 help='Causes the script to skip moving the file.') | 215 help='Causes the script to skip moving the file.') |
202 parser.add_option('--no_error_for_non_source_file', action='store_false', | 216 parser.add_option('--no_error_for_non_source_file', action='store_false', |
203 default='True', | 217 default='True', |
204 dest='error_for_non_source_file', | 218 dest='error_for_non_source_file', |
205 help='Causes the script to simply print a warning on ' | 219 help='Causes the script to simply print a warning on ' |
206 'encountering a non-source file rather than raising an ' | 220 'encountering a non-source file rather than raising an ' |
207 'error.') | 221 'error.') |
(...skipping 12 matching lines...) Expand all Loading... | |
220 parser.print_help() | 234 parser.print_help() |
221 return 1 | 235 return 1 |
222 | 236 |
223 for from_path in from_paths: | 237 for from_path in from_paths: |
224 if not opts.error_for_non_source_file and not IsHandledFile(from_path): | 238 if not opts.error_for_non_source_file and not IsHandledFile(from_path): |
225 print '%s does not appear to be a source file, skipping' % (from_path) | 239 print '%s does not appear to be a source file, skipping' % (from_path) |
226 continue | 240 continue |
227 to_path = MakeDestinationPath(from_path, orig_to_path) | 241 to_path = MakeDestinationPath(from_path, orig_to_path) |
228 if not opts.already_moved: | 242 if not opts.already_moved: |
229 MoveFile(from_path, to_path) | 243 MoveFile(from_path, to_path) |
230 UpdatePostMove(from_path, to_path) | 244 UpdatePostMove(from_path, to_path, in_blink) |
231 return 0 | 245 return 0 |
232 | 246 |
233 | 247 |
234 if __name__ == '__main__': | 248 if __name__ == '__main__': |
235 sys.exit(main()) | 249 sys.exit(main()) |
OLD | NEW |