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 """ |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 comments = [] | 42 comments = [] |
43 sources = [] | 43 sources = [] |
44 for line in original_lines: | 44 for line in original_lines: |
45 if re.search(COMMENT_PATTERN, line): | 45 if re.search(COMMENT_PATTERN, line): |
46 comments.append(line) | 46 comments.append(line) |
47 elif re.search(SOURCE_PATTERN, line): | 47 elif re.search(SOURCE_PATTERN, line): |
48 # Associate the line with the preceeding comments. | 48 # Associate the line with the preceeding comments. |
49 sources.append([line, comments]) | 49 sources.append([line, comments]) |
50 comments = [] | 50 comments = [] |
51 else: | 51 else: |
52 if comments: | 52 # |sources| should be flushed first, to handle comments at the end of a |
53 output_lines.extend(comments) | 53 # source list correctly. |
54 comments = [] | |
55 if sources: | 54 if sources: |
56 for source_line, source_comments in sorted(sources): | 55 for source_line, source_comments in sorted(sources): |
57 output_lines.extend(source_comments) | 56 output_lines.extend(source_comments) |
58 output_lines.append(source_line) | 57 output_lines.append(source_line) |
59 sources = [] | 58 sources = [] |
| 59 if comments: |
| 60 output_lines.extend(comments) |
| 61 comments = [] |
60 output_lines.append(line) | 62 output_lines.append(line) |
61 return output_lines | 63 return output_lines |
62 | 64 |
63 | 65 |
64 def ProcessFile(filename, should_confirm): | 66 def ProcessFile(filename, should_confirm): |
65 """Process the input file and rewrite if needed. | 67 """Process the input file and rewrite if needed. |
66 | 68 |
67 Args: | 69 Args: |
68 filename: Path to the input file. | 70 filename: Path to the input file. |
69 should_confirm: If true, diff and confirmation prompt are shown. | 71 should_confirm: If true, diff and confirmation prompt are shown. |
(...skipping 29 matching lines...) Expand all Loading... |
99 if len(filenames) < 1: | 101 if len(filenames) < 1: |
100 parser.print_help() | 102 parser.print_help() |
101 return 1 | 103 return 1 |
102 | 104 |
103 for filename in filenames: | 105 for filename in filenames: |
104 ProcessFile(filename, opts.should_confirm) | 106 ProcessFile(filename, opts.should_confirm) |
105 | 107 |
106 | 108 |
107 if __name__ == '__main__': | 109 if __name__ == '__main__': |
108 sys.exit(main()) | 110 sys.exit(main()) |
OLD | NEW |