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

Unified 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, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/git/for-all-touched-files.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/sort-headers.py
diff --git a/tools/sort-headers.py b/tools/sort-headers.py
index e51343ca861c54405e8c673e2985d075ff0e6480..fb7dd25bd5513ffcad10ee06b90f12dc20f37a86 100755
--- a/tools/sort-headers.py
+++ b/tools/sort-headers.py
@@ -6,6 +6,7 @@
"""Given a filename as an argument, sort the #include/#imports in that file.
Shows a diff and prompts for confirmation before doing the deed.
+Works great with tools/git/for-all-touched-files.py.
"""
import optparse
@@ -71,36 +72,48 @@ def SortHeader(infile, outfile):
outfile.write(line)
+def DiffAndConfirm(filename, should_confirm):
+ """Shows a diff of what the tool would change the file named
+ filename to. Shows a confirmation prompt if should_confirm is true.
+ Saves the resulting file if should_confirm is false or the user
+ answers Y to the confirmation prompt.
+ """
+ fixfilename = filename + '.new'
+ infile = open(filename, 'r')
+ outfile = open(fixfilename, 'w')
+ SortHeader(infile, outfile)
+ infile.close()
+ outfile.close() # Important so the below diff gets the updated contents.
+
+ try:
+ diff = os.system('diff -u %s %s' % (filename, fixfilename))
+ if diff >> 8 == 0: # Check exit code.
+ print '%s: no change' % filename
+ return
+
+ if not should_confirm or YesNo('Use new file (y/N)?'):
+ os.rename(fixfilename, filename)
+ finally:
+ try:
+ os.remove(fixfilename)
+ except OSError:
+ # If the file isn't there, we don't care.
+ pass
+
+
def main():
parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
- opts, args = parser.parse_args()
+ parser.add_option('-f', '--force', action='store_false', default=True,
+ dest='should_confirm',
+ help='Turn off confirmation prompt.')
+ opts, filenames = parser.parse_args()
- if len(args) < 1:
+ if len(filenames) < 1:
parser.print_help()
sys.exit(1)
- for filename in args:
- fixfilename = filename + '.new'
- infile = open(filename, 'r')
- outfile = open(fixfilename, 'w')
- SortHeader(infile, outfile)
- infile.close()
- outfile.close() # Important so the below diff gets the updated contents.
-
- try:
- diff = os.system('diff -u %s %s' % (filename, fixfilename))
- if diff >> 8 == 0: # Check exit code.
- print '%s: no change' % filename
- continue
-
- if YesNo('Use new file (y/N)?'):
- os.rename(fixfilename, filename)
- finally:
- try:
- os.remove(fixfilename)
- except OSError:
- # If the file isn't there, we don't care.
- pass
+ for filename in filenames:
+ DiffAndConfirm(filename, opts.should_confirm)
if __name__ == '__main__':
« 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