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, sort C-ish 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 SUFFIXES = ['c', 'cc', 'cpp', 'h', 'mm', 'rc', 'rc.version', 'ico', 'def', |
| 20 'release'] |
| 21 PATTERN = re.compile('^\s+[\'"].*\.(%s)[\'"],$' % |
| 22 '|'.join([re.escape(x) for x in SUFFIXES])) |
19 | 23 |
20 def SortSources(original_lines): | 24 def SortSources(original_lines): |
21 """Sort source file names in |original_lines|. | 25 """Sort source file names in |original_lines|. |
22 | 26 |
23 Args: | 27 Args: |
24 original_lines: Lines of the original content as a list of strings. | 28 original_lines: Lines of the original content as a list of strings. |
25 | 29 |
26 Returns: | 30 Returns: |
27 Lines of the sorted content as a list of strings. | 31 Lines of the sorted content as a list of strings. |
28 | 32 |
29 The algorithm is fairly naive. The code tries to find a list of C-ish source | 33 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 | 34 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 | 35 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 | 36 the code cannot handle correctly (ex. comments within a list of source file |
33 names). | 37 names). |
34 """ | 38 """ |
35 | 39 |
36 output_lines = [] | 40 output_lines = [] |
37 sources = [] | 41 sources = [] |
38 for line in original_lines: | 42 for line in original_lines: |
39 if re.search(r'^\s+[\'"].*\.(c|cc|cpp|h|mm|rc)[\'"],$', line): | 43 if re.search(PATTERN, line): |
40 sources.append(line) | 44 sources.append(line) |
41 else: | 45 else: |
42 if sources: | 46 if sources: |
43 output_lines.extend(sorted(sources)) | 47 output_lines.extend(sorted(sources)) |
44 sources = [] | 48 sources = [] |
45 output_lines.append(line) | 49 output_lines.append(line) |
46 return output_lines | 50 return output_lines |
47 | 51 |
48 | 52 |
49 def ProcessFile(filename, should_confirm): | 53 def ProcessFile(filename, should_confirm): |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 if len(filenames) < 1: | 88 if len(filenames) < 1: |
85 parser.print_help() | 89 parser.print_help() |
86 return 1 | 90 return 1 |
87 | 91 |
88 for filename in filenames: | 92 for filename in filenames: |
89 ProcessFile(filename, opts.should_confirm) | 93 ProcessFile(filename, opts.should_confirm) |
90 | 94 |
91 | 95 |
92 if __name__ == '__main__': | 96 if __name__ == '__main__': |
93 sys.exit(main()) | 97 sys.exit(main()) |
OLD | NEW |