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