| 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', | 19 SUFFIXES = ['c', 'cc', 'cpp', 'h', 'mm', 'rc', 'rc.version', 'ico', 'def', |
| 20 'release'] | 20 'release'] |
| 21 PATTERN = re.compile('^\s+[\'"].*\.(%s)[\'"],$' % | 21 SOURCE_PATTERN = re.compile('^\s+[\'"].*\.(%s)[\'"],$' % |
| 22 '|'.join([re.escape(x) for x in SUFFIXES])) | 22 '|'.join([re.escape(x) for x in SUFFIXES])) |
| 23 COMMENT_PATTERN = re.compile('^\s+#') |
| 23 | 24 |
| 24 def SortSources(original_lines): | 25 def SortSources(original_lines): |
| 25 """Sort source file names in |original_lines|. | 26 """Sort source file names in |original_lines|. |
| 26 | 27 |
| 27 Args: | 28 Args: |
| 28 original_lines: Lines of the original content as a list of strings. | 29 original_lines: Lines of the original content as a list of strings. |
| 29 | 30 |
| 30 Returns: | 31 Returns: |
| 31 Lines of the sorted content as a list of strings. | 32 Lines of the sorted content as a list of strings. |
| 32 | 33 |
| 33 The algorithm is fairly naive. The code tries to find a list of C-ish source | 34 The algorithm is fairly naive. The code tries to find a list of C-ish |
| 34 file names by a simple regex, then sort them. The code does not try to | 35 source file names by a simple regex, then sort them. The code does not try |
| 35 understand the syntax of the build files, hence there are many cases that | 36 to understand the syntax of the build files, hence there are some cases |
| 36 the code cannot handle correctly (ex. comments within a list of source file | 37 that the code cannot handle correctly (ex. blank lines within a list of |
| 37 names). | 38 source file names). |
| 38 """ | 39 """ |
| 39 | 40 |
| 40 output_lines = [] | 41 output_lines = [] |
| 42 comments = [] |
| 41 sources = [] | 43 sources = [] |
| 42 for line in original_lines: | 44 for line in original_lines: |
| 43 if re.search(PATTERN, line): | 45 if re.search(COMMENT_PATTERN, line): |
| 44 sources.append(line) | 46 comments.append(line) |
| 47 elif re.search(SOURCE_PATTERN, line): |
| 48 # Associate the line with the preceeding comments. |
| 49 sources.append([line, comments]) |
| 50 comments = [] |
| 45 else: | 51 else: |
| 52 if comments: |
| 53 output_lines.extend(comments) |
| 54 comments = [] |
| 46 if sources: | 55 if sources: |
| 47 output_lines.extend(sorted(sources)) | 56 for source_line, source_comments in sorted(sources): |
| 57 output_lines.extend(source_comments) |
| 58 output_lines.append(source_line) |
| 48 sources = [] | 59 sources = [] |
| 49 output_lines.append(line) | 60 output_lines.append(line) |
| 50 return output_lines | 61 return output_lines |
| 51 | 62 |
| 52 | 63 |
| 53 def ProcessFile(filename, should_confirm): | 64 def ProcessFile(filename, should_confirm): |
| 54 """Process the input file and rewrite if needed. | 65 """Process the input file and rewrite if needed. |
| 55 | 66 |
| 56 Args: | 67 Args: |
| 57 filename: Path to the input file. | 68 filename: Path to the input file. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 88 if len(filenames) < 1: | 99 if len(filenames) < 1: |
| 89 parser.print_help() | 100 parser.print_help() |
| 90 return 1 | 101 return 1 |
| 91 | 102 |
| 92 for filename in filenames: | 103 for filename in filenames: |
| 93 ProcessFile(filename, opts.should_confirm) | 104 ProcessFile(filename, opts.should_confirm) |
| 94 | 105 |
| 95 | 106 |
| 96 if __name__ == '__main__': | 107 if __name__ == '__main__': |
| 97 sys.exit(main()) | 108 sys.exit(main()) |
| OLD | NEW |