Chromium Code Reviews| Index: tools/sort-headers.py |
| diff --git a/tools/sort-headers.py b/tools/sort-headers.py |
| index e51343ca861c54405e8c673e2985d075ff0e6480..2718d5dae0ebd70e6cfc5583e3f096e467876433 100755 |
| --- a/tools/sort-headers.py |
| +++ b/tools/sort-headers.py |
| @@ -71,36 +71,43 @@ def SortHeader(infile, outfile): |
| outfile.write(line) |
| +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.
|
| + 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__': |