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

Side by Side Diff: tools/sort_sources.py

Issue 888873003: tools/sort_sources.py: sort C++ source file names in build files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « tools/sort-headers.py ('k') | tools/yes_no.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
Daniel Erat 2015/02/09 16:30:01 nit: 2015
satorux1 2015/02/10 06:55:04 Done.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Given a GYP/GN filename as an argument, sort C++ source files in that file.
7
8 Shows a diff and prompts for confirmation before doing the deed.
9 Works great with tools/git/for-all-touched-files.py.
10 """
11
12 import difflib
13 import optparse
14 import re
15 import sys
16
17 from yes_no import YesNo
18
19
20 def SortSources(original_lines):
21 """Sort source file names in |original_lines|.
22
23 Args:
24 original_lines: Lines of the original content as a list of strings.
25
26 Returns:
27 Lines of the sorted content as a list of strings.
28
29 The algorithm is fairly naive. The code tries to find a list of C++ source
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
32 the code cannot handle correctly (ex. comments within a list of source file
33 names).
34 """
35
36 output_lines = []
37 sources = []
38 for line in original_lines:
39 if re.search(r'^ +[\'"].*\.(c|cc|cpp|h)[\'"],$', line):
Daniel Erat 2015/02/09 16:30:01 nit: not sure if it's worth doing, but you could a
satorux1 2015/02/10 06:55:04 Done.
40 sources.append(line)
41 else:
42 if sources:
43 output_lines.extend(sorted(sources))
44 sources = []
45 output_lines.append(line)
46 return output_lines
47
48
49 def ProcessFile(filename, should_confirm):
50 """Process the input file and rewrite if needed.
51
52 Args:
53 filename: Path to the input file.
54 should_confirm: If true, diff and confirmation prompt are shown.
55 """
56
57 original_lines = []
58 with open(filename, 'r') as input_file:
59 for line in input_file:
60 original_lines.append(line)
61
62 new_lines = SortSources(original_lines)
63 if original_lines == new_lines:
64 print '%s: no change' % filename
65 return
66
67 if should_confirm:
68 diff = difflib.unified_diff(original_lines, new_lines)
69 sys.stdout.writelines(diff)
70 if not YesNo('Use new file (y/N)'):
71 return
72
73 with open(filename, 'w') as output_file:
74 output_file.writelines(new_lines)
75
76
77 def main():
78 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
79 parser.add_option('-f', '--force', action='store_false', default=True,
80 dest='should_confirm',
81 help='Turn off confirmation prompt.')
82 opts, filenames = parser.parse_args()
83
84 if len(filenames) < 1:
85 parser.print_help()
86 return 1
87
88 for filename in filenames:
89 ProcessFile(filename, opts.should_confirm)
90
91
92 if __name__ == '__main__':
93 sys.exit(main())
OLDNEW
« no previous file with comments | « tools/sort-headers.py ('k') | tools/yes_no.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698