Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: tools/sort_sources.py

Issue 917083002: Sort GYP files under base using tools/sort_sources.py (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix the bug Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« base/base.gypi ('K') | « base/base.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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())
OLDNEW
« base/base.gypi ('K') | « base/base.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698