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

Unified Diff: tools/sort-headers.py

Issue 11412006: Automatically sort headers in files affected by move_source_file.py (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Improve comments. Created 8 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 side-by-side diff with in-line comments
Download patch
« tools/git/move_source_file.py ('K') | « tools/git/move_source_file.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 a786822fdedbd1e2fa239c56029a095b80795e13..501f92cbd34ca1b22e3d6289f1451d6607cca901 100755
--- a/tools/sort-headers.py
+++ b/tools/sort-headers.py
@@ -81,11 +81,13 @@ 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.
+def FixFileWithConfirmFunction(filename, confirm_function):
+ """Creates a fixed version of the file, invokes |confirm_function|
+ to decide whether to use the new file, and cleans up.
+
+ |confirm_function| takes two parameters, the original filename and
+ the fixed-up filename, and returns True to use the fixed-up file,
+ false to not use it.
"""
fixfilename = filename + '.new'
infile = open(filename, 'r')
@@ -95,12 +97,7 @@ def DiffAndConfirm(filename, should_confirm):
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)?'):
+ if confirm_function(filename, fixfilename):
os.rename(fixfilename, filename)
finally:
try:
@@ -110,6 +107,23 @@ def DiffAndConfirm(filename, should_confirm):
pass
+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.
+ """
+ def ConfirmFunction(filename, fixfilename):
+ diff = os.system('diff -u %s %s' % (filename, fixfilename))
+ if diff >> 8 == 0: # Check exit code.
+ print '%s: no change' % filename
+ return False
+
+ return (not should_confirm or YesNo('Use new file (y/N)?'))
+
+ FixFileWithConfirmFunction(filename, ConfirmFunction)
+
+
def main():
parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
parser.add_option('-f', '--force', action='store_false', default=True,
« tools/git/move_source_file.py ('K') | « tools/git/move_source_file.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698