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

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: Remove unneeded import. 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
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.
(unused - use chromium) 2011/10/31 18:19:56 Maybe add "Works great with tools/git/for-all-touc
Jói 2011/10/31 18:32:08 Done.
9 """ 9 """
10 10
11 import optparse 11 import optparse
12 import os 12 import os
13 import sys 13 import sys
14 import termios 14 import termios
15 import tty 15 import tty
16 16
17 def YesNo(prompt): 17 def YesNo(prompt):
18 """Prompts with a yes/no question, returns True if yes.""" 18 """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): 64 while IsInclude(line):
65 headerblock.append(line) 65 headerblock.append(line)
66 line = infile.next() 66 line = infile.next()
67 for header in sorted(headerblock, key=IncludeCompareKey): 67 for header in sorted(headerblock, key=IncludeCompareKey):
68 outfile.write(header) 68 outfile.write(header)
69 # Intentionally fall through, to write the line that caused 69 # Intentionally fall through, to write the line that caused
70 # the above while loop to exit. 70 # the above while loop to exit.
71 outfile.write(line) 71 outfile.write(line)
72 72
73 73
74 def DiffAndConfirm(filename, should_confirm):
(unused - use chromium) 2011/10/31 18:19:56 needs docstring
Jói 2011/10/31 18:32:08 Done.
75 fixfilename = filename + '.new'
76 infile = open(filename, 'r')
77 outfile = open(fixfilename, 'w')
78 SortHeader(infile, outfile)
79 infile.close()
80 outfile.close() # Important so the below diff gets the updated contents.
81
82 try:
83 diff = os.system('diff -u %s %s' % (filename, fixfilename))
84 if diff >> 8 == 0: # Check exit code.
85 print '%s: no change' % filename
86 return
87
88 if not should_confirm or YesNo('Use new file (y/N)?'):
89 os.rename(fixfilename, filename)
90 finally:
91 try:
92 os.remove(fixfilename)
93 except OSError:
94 # If the file isn't there, we don't care.
95 pass
96
97
74 def main(): 98 def main():
75 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...') 99 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
76 opts, args = parser.parse_args() 100 parser.add_option('-f', '--force', action='store_false', default=True,
101 dest='should_confirm',
102 help='Turn off confirmation prompt.')
103 opts, filenames = parser.parse_args()
77 104
78 if len(args) < 1: 105 if len(filenames) < 1:
79 parser.print_help() 106 parser.print_help()
80 sys.exit(1) 107 sys.exit(1)
81 108
82 for filename in args: 109 for filename in filenames:
83 fixfilename = filename + '.new' 110 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 111
105 112
106 if __name__ == '__main__': 113 if __name__ == '__main__':
107 main() 114 main()
OLDNEW
« tools/git/for-all-touched-files.py ('K') | « 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