OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 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 """Wrapper script to help run clang tools across Chromium code. | 6 """Wrapper script to help run clang tools across Chromium code. |
7 | 7 |
8 How to use this tool: | 8 How to use this tool: |
9 If you want to run the tool across all Chromium code: | 9 If you want to run the tool across all Chromium code: |
10 run_tool.py <tool> <path/to/compiledb> | 10 run_tool.py <tool> <path/to/compiledb> |
(...skipping 21 matching lines...) Expand all Loading... |
32 ==== END EDITS ==== | 32 ==== END EDITS ==== |
33 | 33 |
34 Any generated edits are applied once the clang tool has finished running | 34 Any generated edits are applied once the clang tool has finished running |
35 across Chromium, regardless of whether some instances failed or not. | 35 across Chromium, regardless of whether some instances failed or not. |
36 """ | 36 """ |
37 | 37 |
38 import collections | 38 import collections |
39 import functools | 39 import functools |
40 import multiprocessing | 40 import multiprocessing |
41 import os.path | 41 import os.path |
| 42 import pipes |
42 import subprocess | 43 import subprocess |
43 import sys | 44 import sys |
44 | 45 |
45 | 46 |
46 Edit = collections.namedtuple( | 47 Edit = collections.namedtuple( |
47 'Edit', ('edit_type', 'offset', 'length', 'replacement')) | 48 'Edit', ('edit_type', 'offset', 'length', 'replacement')) |
48 | 49 |
49 | 50 |
50 def _GetFilesFromGit(paths = None): | 51 def _GetFilesFromGit(paths = None): |
51 """Gets the list of files in the git repository. | 52 """Gets the list of files in the git repository. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 continue | 209 continue |
209 last_edit = edit | 210 last_edit = edit |
210 contents[edit.offset:edit.offset + edit.length] = edit.replacement | 211 contents[edit.offset:edit.offset + edit.length] = edit.replacement |
211 if not edit.replacement: | 212 if not edit.replacement: |
212 _ExtendDeletionIfElementIsInList(contents, edit.offset) | 213 _ExtendDeletionIfElementIsInList(contents, edit.offset) |
213 edit_count += 1 | 214 edit_count += 1 |
214 f.seek(0) | 215 f.seek(0) |
215 f.truncate() | 216 f.truncate() |
216 f.write(contents) | 217 f.write(contents) |
217 if clang_format_diff_path: | 218 if clang_format_diff_path: |
| 219 # TODO(dcheng): python3.3 exposes this publicly as shlex.quote, but Chrome |
| 220 # uses python2.7. Use the deprecated interface until Chrome uses a newer |
| 221 # Python. |
218 if subprocess.call('git diff -U0 %s | python %s -i -p1 -style=file ' % ( | 222 if subprocess.call('git diff -U0 %s | python %s -i -p1 -style=file ' % ( |
219 k, clang_format_diff_path), shell=True) != 0: | 223 pipes.quote(k), clang_format_diff_path), shell=True) != 0: |
220 print 'clang-format failed for %s' % k | 224 print 'clang-format failed for %s' % k |
221 print 'Applied %d edits to %d files' % (edit_count, len(edits)) | 225 print 'Applied %d edits to %d files' % (edit_count, len(edits)) |
222 | 226 |
223 | 227 |
224 _WHITESPACE_BYTES = frozenset((ord('\t'), ord('\n'), ord('\r'), ord(' '))) | 228 _WHITESPACE_BYTES = frozenset((ord('\t'), ord('\n'), ord('\r'), ord(' '))) |
225 | 229 |
226 | 230 |
227 def _ExtendDeletionIfElementIsInList(contents, offset): | 231 def _ExtendDeletionIfElementIsInList(contents, offset): |
228 """Extends the range of a deletion if the deleted element was part of a list. | 232 """Extends the range of a deletion if the deleted element was part of a list. |
229 | 233 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 _ApplyEdits({k : v for k, v in dispatcher.edits.iteritems() | 299 _ApplyEdits({k : v for k, v in dispatcher.edits.iteritems() |
296 if k in filenames}, | 300 if k in filenames}, |
297 clang_format_diff_path) | 301 clang_format_diff_path) |
298 if dispatcher.failed_count != 0: | 302 if dispatcher.failed_count != 0: |
299 return 2 | 303 return 2 |
300 return 0 | 304 return 0 |
301 | 305 |
302 | 306 |
303 if __name__ == '__main__': | 307 if __name__ == '__main__': |
304 sys.exit(main(sys.argv[1:])) | 308 sys.exit(main(sys.argv[1:])) |
OLD | NEW |