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, |