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

Side by Side Diff: tools/sort-headers.py

Issue 8390055: Lets you run sort-headers.py on all files changed since some git branch. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address final review comments. Created 9 years, 1 month 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/git/for-all-touched-files.py ('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/python 1 #!/usr/bin/python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2011 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 filename as an argument, sort the #include/#imports in that file. 6 """Given a filename as an argument, sort the #include/#imports 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 """ 10 """
10 11
11 import optparse 12 import optparse
12 import os 13 import os
13 import sys 14 import sys
14 import termios 15 import termios
15 import tty 16 import tty
16 17
17 def YesNo(prompt): 18 def YesNo(prompt):
18 """Prompts with a yes/no question, returns True if yes.""" 19 """Prompts with a yes/no question, returns True if yes."""
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 while IsInclude(line): 65 while IsInclude(line):
65 headerblock.append(line) 66 headerblock.append(line)
66 line = infile.next() 67 line = infile.next()
67 for header in sorted(headerblock, key=IncludeCompareKey): 68 for header in sorted(headerblock, key=IncludeCompareKey):
68 outfile.write(header) 69 outfile.write(header)
69 # Intentionally fall through, to write the line that caused 70 # Intentionally fall through, to write the line that caused
70 # the above while loop to exit. 71 # the above while loop to exit.
71 outfile.write(line) 72 outfile.write(line)
72 73
73 74
75 def DiffAndConfirm(filename, should_confirm):
76 """Shows a diff of what the tool would change the file named
77 filename to. Shows a confirmation prompt if should_confirm is true.
78 Saves the resulting file if should_confirm is false or the user
79 answers Y to the confirmation prompt.
80 """
81 fixfilename = filename + '.new'
82 infile = open(filename, 'r')
83 outfile = open(fixfilename, 'w')
84 SortHeader(infile, outfile)
85 infile.close()
86 outfile.close() # Important so the below diff gets the updated contents.
87
88 try:
89 diff = os.system('diff -u %s %s' % (filename, fixfilename))
90 if diff >> 8 == 0: # Check exit code.
91 print '%s: no change' % filename
92 return
93
94 if not should_confirm or YesNo('Use new file (y/N)?'):
95 os.rename(fixfilename, filename)
96 finally:
97 try:
98 os.remove(fixfilename)
99 except OSError:
100 # If the file isn't there, we don't care.
101 pass
102
103
74 def main(): 104 def main():
75 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...') 105 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
76 opts, args = parser.parse_args() 106 parser.add_option('-f', '--force', action='store_false', default=True,
107 dest='should_confirm',
108 help='Turn off confirmation prompt.')
109 opts, filenames = parser.parse_args()
77 110
78 if len(args) < 1: 111 if len(filenames) < 1:
79 parser.print_help() 112 parser.print_help()
80 sys.exit(1) 113 sys.exit(1)
81 114
82 for filename in args: 115 for filename in filenames:
83 fixfilename = filename + '.new' 116 DiffAndConfirm(filename, opts.should_confirm)
84 infile = open(filename, 'r')
85 outfile = open(fixfilename, 'w')
86 SortHeader(infile, outfile)
87 infile.close()
88 outfile.close() # Important so the below diff gets the updated contents.
89
90 try:
91 diff = os.system('diff -u %s %s' % (filename, fixfilename))
92 if diff >> 8 == 0: # Check exit code.
93 print '%s: no change' % filename
94 continue
95
96 if YesNo('Use new file (y/N)?'):
97 os.rename(fixfilename, filename)
98 finally:
99 try:
100 os.remove(fixfilename)
101 except OSError:
102 # If the file isn't there, we don't care.
103 pass
104 117
105 118
106 if __name__ == '__main__': 119 if __name__ == '__main__':
107 main() 120 main()
OLDNEW
« no previous file with comments | « tools/git/for-all-touched-files.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698