OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 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 """Given a GYP/GN filename as an argument, sort C++ source files in that file. | 6 """Given a GYP/GN filename, sort C-ish source files in that file. |
7 | 7 |
8 Shows a diff and prompts for confirmation before doing the deed. | 8 Shows a diff and prompts for confirmation before doing the deed. |
9 Works great with tools/git/for-all-touched-files.py. | 9 Works great with tools/git/for-all-touched-files.py. |
10 """ | 10 """ |
11 | 11 |
12 import difflib | 12 import difflib |
13 import optparse | 13 import optparse |
14 import re | 14 import re |
15 import sys | 15 import sys |
16 | 16 |
17 from yes_no import YesNo | 17 from yes_no import YesNo |
18 | 18 |
19 | 19 |
20 def SortSources(original_lines): | 20 def SortSources(original_lines): |
21 """Sort source file names in |original_lines|. | 21 """Sort source file names in |original_lines|. |
22 | 22 |
23 Args: | 23 Args: |
24 original_lines: Lines of the original content as a list of strings. | 24 original_lines: Lines of the original content as a list of strings. |
25 | 25 |
26 Returns: | 26 Returns: |
27 Lines of the sorted content as a list of strings. | 27 Lines of the sorted content as a list of strings. |
28 | 28 |
29 The algorithm is fairly naive. The code tries to find a list of C++ source | 29 The algorithm is fairly naive. The code tries to find a list of C-ish source |
30 file names by a simple regex, then sort them. The code does not try to | 30 file names by a simple regex, then sort them. The code does not try to |
31 understand the syntax of the build files, hence there are many cases that | 31 understand the syntax of the build files, hence there are many cases that |
32 the code cannot handle correctly (ex. comments within a list of source file | 32 the code cannot handle correctly (ex. comments within a list of source file |
33 names). | 33 names). |
34 """ | 34 """ |
35 | 35 |
36 output_lines = [] | 36 output_lines = [] |
37 sources = [] | 37 sources = [] |
38 for line in original_lines: | 38 for line in original_lines: |
39 if re.search(r'^\s+[\'"].*\.(c|cc|cpp|h)[\'"],$', line): | 39 if re.search(r'^\s+[\'"].*\.(c|cc|cpp|h|mm|rc)[\'"],$', line): |
40 sources.append(line) | 40 sources.append(line) |
41 else: | 41 else: |
42 if sources: | 42 if sources: |
43 output_lines.extend(sorted(sources)) | 43 output_lines.extend(sorted(sources)) |
44 sources = [] | 44 sources = [] |
45 output_lines.append(line) | 45 output_lines.append(line) |
46 return output_lines | 46 return output_lines |
47 | 47 |
48 | 48 |
49 def ProcessFile(filename, should_confirm): | 49 def ProcessFile(filename, should_confirm): |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 if len(filenames) < 1: | 84 if len(filenames) < 1: |
85 parser.print_help() | 85 parser.print_help() |
86 return 1 | 86 return 1 |
87 | 87 |
88 for filename in filenames: | 88 for filename in filenames: |
89 ProcessFile(filename, opts.should_confirm) | 89 ProcessFile(filename, opts.should_confirm) |
90 | 90 |
91 | 91 |
92 if __name__ == '__main__': | 92 if __name__ == '__main__': |
93 sys.exit(main()) | 93 sys.exit(main()) |
OLD | NEW |