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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 if os.path.isdir(to_path): | 58 if os.path.isdir(to_path): |
59 to_path = os.path.join(to_path, os.path.basename(from_path)) | 59 to_path = os.path.join(to_path, os.path.basename(from_path)) |
60 else: | 60 else: |
61 dest_extension = os.path.splitext(to_path)[1] | 61 dest_extension = os.path.splitext(to_path)[1] |
62 if dest_extension not in HANDLED_EXTENSIONS: | 62 if dest_extension not in HANDLED_EXTENSIONS: |
63 raise Exception('Destination must be either a full path with ' | 63 raise Exception('Destination must be either a full path with ' |
64 'a recognized extension or a directory.') | 64 'a recognized extension or a directory.') |
65 return to_path | 65 return to_path |
66 | 66 |
67 | 67 |
68 def UpdateIncludePathForBlink(path): | |
69 """Updates the path of a moved file to what it would be when used in an | |
Matt Giuca
2015/07/13 06:25:46
Docstrings should have a one-line paragraph at the
benwells
2015/07/13 06:32:13
Done.
| |
70 include statement in blink. As blink has its 'public' and 'Source' folders | |
Matt Giuca
2015/07/13 06:25:46
Nit: Capital 'B'.
benwells
2015/07/13 06:32:13
Done.
| |
71 in the include search path, these prefixes of file paths are not included | |
72 in include statements. | |
73 """ | |
74 for prefix in ('public/', 'Source/'): | |
75 if path.startswith(prefix): | |
76 return path[len(prefix):] | |
77 | |
78 return path | |
79 | |
80 | |
68 def MoveFile(from_path, to_path): | 81 def MoveFile(from_path, to_path): |
69 """Performs a git mv command to move a file from |from_path| to |to_path|. | 82 """Performs a git mv command to move a file from |from_path| to |to_path|. |
70 """ | 83 """ |
71 if not os.system('git mv %s %s' % (from_path, to_path)) == 0: | 84 if not os.system('git mv %s %s' % (from_path, to_path)) == 0: |
72 raise Exception('Fatal: Failed to run git mv command.') | 85 raise Exception('Fatal: Failed to run git mv command.') |
73 | 86 |
74 | 87 |
75 def UpdatePostMove(from_path, to_path): | 88 def UpdatePostMove(from_path, to_path, in_blink): |
76 """Given a file that has moved from |from_path| to |to_path|, | 89 """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 | 90 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 | 91 updates all references to the file in other source files. Also tries |
79 to update references in .gyp(i) files using a heuristic. | 92 to update references in .gyp(i) files using a heuristic. |
80 """ | 93 """ |
81 # Include paths always use forward slashes. | 94 # Include paths always use forward slashes. |
82 from_path = from_path.replace('\\', '/') | 95 from_path = from_path.replace('\\', '/') |
83 to_path = to_path.replace('\\', '/') | 96 to_path = to_path.replace('\\', '/') |
84 | 97 |
85 if os.path.splitext(from_path)[1] in ['.h', '.hh']: | 98 if os.path.splitext(from_path)[1] in ['.h', '.hh']: |
86 UpdateIncludeGuard(from_path, to_path) | 99 UpdateIncludeGuard(from_path, to_path) |
87 | 100 |
101 from_include_path = from_path | |
102 to_include_path = to_path | |
103 if in_blink: | |
104 from_include_path = UpdateIncludePathForBlink(from_include_path) | |
105 to_include_path = UpdateIncludePathForBlink(to_include_path) | |
106 | |
88 # Update include/import references. | 107 # Update include/import references. |
89 files_with_changed_includes = mffr.MultiFileFindReplace( | 108 files_with_changed_includes = mffr.MultiFileFindReplace( |
90 r'(#(include|import)\s*["<])%s([>"])' % re.escape(from_path), | 109 r'(#(include|import)\s*["<])%s([>"])' % re.escape(from_include_path), |
91 r'\1%s\3' % to_path, | 110 r'\1%s\3' % to_include_path, |
92 ['*.cc', '*.h', '*.m', '*.mm', '*.cpp']) | 111 ['*.cc', '*.h', '*.m', '*.mm', '*.cpp']) |
93 | 112 |
94 # Reorder headers in files that changed. | 113 # Reorder headers in files that changed. |
95 for changed_file in files_with_changed_includes: | 114 for changed_file in files_with_changed_includes: |
96 def AlwaysConfirm(a, b): return True | 115 def AlwaysConfirm(a, b): return True |
97 sort_headers.FixFileWithConfirmFunction(changed_file, AlwaysConfirm, True) | 116 sort_headers.FixFileWithConfirmFunction(changed_file, AlwaysConfirm, True, |
117 in_blink) | |
98 | 118 |
99 # Update comments; only supports // comments, which are primarily | 119 # Update comments; only supports // comments, which are primarily |
100 # used in our code. | 120 # used in our code. |
101 # | 121 # |
102 # This work takes a bit of time. If this script starts feeling too | 122 # 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 | 123 # slow, one good way to speed it up is to make the comment handling |
104 # optional under a flag. | 124 # optional under a flag. |
105 mffr.MultiFileFindReplace( | 125 mffr.MultiFileFindReplace( |
106 r'(//.*)%s' % re.escape(from_path), | 126 r'(//.*)%s' % re.escape(from_path), |
107 r'\1%s' % to_path, | 127 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) | 208 'include guard manually. (%s)' % new_path) |
189 | 209 |
190 with open(new_path, 'w') as f: | 210 with open(new_path, 'w') as f: |
191 f.write(new_contents) | 211 f.write(new_contents) |
192 | 212 |
193 def main(): | 213 def main(): |
194 if not os.path.isdir('.git'): | 214 if not os.path.isdir('.git'): |
195 print 'Fatal: You must run from the root of a git checkout.' | 215 print 'Fatal: You must run from the root of a git checkout.' |
196 return 1 | 216 return 1 |
197 | 217 |
218 in_blink = os.getcwd().endswith("third_party/WebKit") | |
219 | |
198 parser = optparse.OptionParser(usage='%prog FROM_PATH... TO_PATH') | 220 parser = optparse.OptionParser(usage='%prog FROM_PATH... TO_PATH') |
199 parser.add_option('--already_moved', action='store_true', | 221 parser.add_option('--already_moved', action='store_true', |
200 dest='already_moved', | 222 dest='already_moved', |
201 help='Causes the script to skip moving the file.') | 223 help='Causes the script to skip moving the file.') |
202 parser.add_option('--no_error_for_non_source_file', action='store_false', | 224 parser.add_option('--no_error_for_non_source_file', action='store_false', |
203 default='True', | 225 default='True', |
204 dest='error_for_non_source_file', | 226 dest='error_for_non_source_file', |
205 help='Causes the script to simply print a warning on ' | 227 help='Causes the script to simply print a warning on ' |
206 'encountering a non-source file rather than raising an ' | 228 'encountering a non-source file rather than raising an ' |
207 'error.') | 229 'error.') |
(...skipping 12 matching lines...) Expand all Loading... | |
220 parser.print_help() | 242 parser.print_help() |
221 return 1 | 243 return 1 |
222 | 244 |
223 for from_path in from_paths: | 245 for from_path in from_paths: |
224 if not opts.error_for_non_source_file and not IsHandledFile(from_path): | 246 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) | 247 print '%s does not appear to be a source file, skipping' % (from_path) |
226 continue | 248 continue |
227 to_path = MakeDestinationPath(from_path, orig_to_path) | 249 to_path = MakeDestinationPath(from_path, orig_to_path) |
228 if not opts.already_moved: | 250 if not opts.already_moved: |
229 MoveFile(from_path, to_path) | 251 MoveFile(from_path, to_path) |
230 UpdatePostMove(from_path, to_path) | 252 UpdatePostMove(from_path, to_path, in_blink) |
231 return 0 | 253 return 0 |
232 | 254 |
233 | 255 |
234 if __name__ == '__main__': | 256 if __name__ == '__main__': |
235 sys.exit(main()) | 257 sys.exit(main()) |
OLD | NEW |