| 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 | 6 """Moves C++ files to a new location, updating any include paths that |
| 7 point to them, and re-ordering headers as needed. If multiple source | 7 point to them, and re-ordering headers as needed. If multiple source |
| 8 files are specified, the destination must be a directory (and must end | 8 files are specified, the destination must be a directory (and must end |
| 9 in a slash). Updates include guards in moved header files. Assumes | 9 in a slash). Updates include guards in moved header files. Assumes |
| 10 Chromium coding style. | 10 Chromium coding style. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 | 38 |
| 39 def MakeDestinationPath(from_path, to_path): | 39 def MakeDestinationPath(from_path, to_path): |
| 40 """Given the from and to paths, return a correct destination path. | 40 """Given the from and to paths, return a correct destination path. |
| 41 | 41 |
| 42 The initial destination path may either a full path or a directory, | 42 The initial destination path may either a full path or a directory, |
| 43 in which case the path must end with /. Also does basic sanity | 43 in which case the path must end with /. Also does basic sanity |
| 44 checks. | 44 checks. |
| 45 """ | 45 """ |
| 46 if os.path.splitext(from_path)[1] not in HANDLED_EXTENSIONS: | 46 if os.path.splitext(from_path)[1] not in HANDLED_EXTENSIONS: |
| 47 raise Exception('Only intended to move individual source files.') | 47 raise Exception('Only intended to move individual source files. (%s)' % |
| 48 from_path) |
| 48 dest_extension = os.path.splitext(to_path)[1] | 49 dest_extension = os.path.splitext(to_path)[1] |
| 49 if dest_extension not in HANDLED_EXTENSIONS: | 50 if dest_extension not in HANDLED_EXTENSIONS: |
| 50 if to_path.endswith('/') or to_path.endswith('\\'): | 51 if to_path.endswith('/') or to_path.endswith('\\'): |
| 51 to_path += os.path.basename(from_path) | 52 to_path += os.path.basename(from_path) |
| 52 else: | 53 else: |
| 53 raise Exception('Destination must be either full path or end with /.') | 54 raise Exception('Destination must be either full path or end with /.') |
| 54 return to_path | 55 return to_path |
| 55 | 56 |
| 56 | 57 |
| 57 def MoveFile(from_path, to_path): | 58 def MoveFile(from_path, to_path): |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 old_guard = MakeIncludeGuardName(old_path) | 130 old_guard = MakeIncludeGuardName(old_path) |
| 130 new_guard = MakeIncludeGuardName(new_path) | 131 new_guard = MakeIncludeGuardName(new_path) |
| 131 | 132 |
| 132 with open(new_path) as f: | 133 with open(new_path) as f: |
| 133 contents = f.read() | 134 contents = f.read() |
| 134 | 135 |
| 135 new_contents = contents.replace(old_guard, new_guard) | 136 new_contents = contents.replace(old_guard, new_guard) |
| 136 # The file should now have three instances of the new guard: two at the top | 137 # The file should now have three instances of the new guard: two at the top |
| 137 # of the file plus one at the bottom for the comment on the #endif. | 138 # of the file plus one at the bottom for the comment on the #endif. |
| 138 if new_contents.count(new_guard) != 3: | 139 if new_contents.count(new_guard) != 3: |
| 139 print ('WARNING: Could not not successfully update include guard; perhaps ' | 140 print ('WARNING: Could not successfully update include guard; perhaps ' |
| 140 'old guard is not per style guide? You will have to update the ' | 141 'old guard is not per style guide? You will have to update the ' |
| 141 'include guard manually.') | 142 'include guard manually. (%s)' % old_path) |
| 142 | 143 |
| 143 with open(new_path, 'w') as f: | 144 with open(new_path, 'w') as f: |
| 144 f.write(new_contents) | 145 f.write(new_contents) |
| 145 | 146 |
| 146 | 147 |
| 147 def main(): | 148 def main(): |
| 148 if not os.path.isdir('.git'): | 149 if not os.path.isdir('.git'): |
| 149 print 'Fatal: You must run from the root of a git checkout.' | 150 print 'Fatal: You must run from the root of a git checkout.' |
| 150 return 1 | 151 return 1 |
| 151 args = sys.argv[1:] | 152 args = sys.argv[1:] |
| (...skipping 15 matching lines...) Expand all Loading... |
| 167 for from_path in args[:len(args)-1]: | 168 for from_path in args[:len(args)-1]: |
| 168 to_path = MakeDestinationPath(from_path, args[-1]) | 169 to_path = MakeDestinationPath(from_path, args[-1]) |
| 169 if not already_moved: | 170 if not already_moved: |
| 170 MoveFile(from_path, to_path) | 171 MoveFile(from_path, to_path) |
| 171 UpdatePostMove(from_path, to_path) | 172 UpdatePostMove(from_path, to_path) |
| 172 return 0 | 173 return 0 |
| 173 | 174 |
| 174 | 175 |
| 175 if __name__ == '__main__': | 176 if __name__ == '__main__': |
| 176 sys.exit(main()) | 177 sys.exit(main()) |
| OLD | NEW |