| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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. |
| 9 Works great with tools/git/for-all-touched-files.py. | 9 Works great with tools/git/for-all-touched-files.py. |
| 10 """ | 10 """ |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 while IsInclude(line): | 74 while IsInclude(line): |
| 75 headerblock.append(line) | 75 headerblock.append(line) |
| 76 line = infile.next() | 76 line = infile.next() |
| 77 for header in sorted(headerblock, key=IncludeCompareKey): | 77 for header in sorted(headerblock, key=IncludeCompareKey): |
| 78 outfile.write(header) | 78 outfile.write(header) |
| 79 # Intentionally fall through, to write the line that caused | 79 # Intentionally fall through, to write the line that caused |
| 80 # the above while loop to exit. | 80 # the above while loop to exit. |
| 81 outfile.write(line) | 81 outfile.write(line) |
| 82 | 82 |
| 83 | 83 |
| 84 def DiffAndConfirm(filename, should_confirm): | 84 def FixFileWithConfirmFunction(filename, confirm_function): |
| 85 """Shows a diff of what the tool would change the file named | 85 """Creates a fixed version of the file, invokes |confirm_function| |
| 86 filename to. Shows a confirmation prompt if should_confirm is true. | 86 to decide whether to use the new file, and cleans up. |
| 87 Saves the resulting file if should_confirm is false or the user | 87 |
| 88 answers Y to the confirmation prompt. | 88 |confirm_function| takes two parameters, the original filename and |
| 89 the fixed-up filename, and returns True to use the fixed-up file, |
| 90 false to not use it. |
| 89 """ | 91 """ |
| 90 fixfilename = filename + '.new' | 92 fixfilename = filename + '.new' |
| 91 infile = open(filename, 'r') | 93 infile = open(filename, 'r') |
| 92 outfile = open(fixfilename, 'w') | 94 outfile = open(fixfilename, 'w') |
| 93 SortHeader(infile, outfile) | 95 SortHeader(infile, outfile) |
| 94 infile.close() | 96 infile.close() |
| 95 outfile.close() # Important so the below diff gets the updated contents. | 97 outfile.close() # Important so the below diff gets the updated contents. |
| 96 | 98 |
| 97 try: | 99 try: |
| 98 diff = os.system('diff -u %s %s' % (filename, fixfilename)) | 100 if confirm_function(filename, fixfilename): |
| 99 if diff >> 8 == 0: # Check exit code. | |
| 100 print '%s: no change' % filename | |
| 101 return | |
| 102 | |
| 103 if not should_confirm or YesNo('Use new file (y/N)?'): | |
| 104 os.rename(fixfilename, filename) | 101 os.rename(fixfilename, filename) |
| 105 finally: | 102 finally: |
| 106 try: | 103 try: |
| 107 os.remove(fixfilename) | 104 os.remove(fixfilename) |
| 108 except OSError: | 105 except OSError: |
| 109 # If the file isn't there, we don't care. | 106 # If the file isn't there, we don't care. |
| 110 pass | 107 pass |
| 111 | 108 |
| 112 | 109 |
| 110 def DiffAndConfirm(filename, should_confirm): |
| 111 """Shows a diff of what the tool would change the file named |
| 112 filename to. Shows a confirmation prompt if should_confirm is true. |
| 113 Saves the resulting file if should_confirm is false or the user |
| 114 answers Y to the confirmation prompt. |
| 115 """ |
| 116 def ConfirmFunction(filename, fixfilename): |
| 117 diff = os.system('diff -u %s %s' % (filename, fixfilename)) |
| 118 if diff >> 8 == 0: # Check exit code. |
| 119 print '%s: no change' % filename |
| 120 return False |
| 121 |
| 122 return (not should_confirm or YesNo('Use new file (y/N)?')) |
| 123 |
| 124 FixFileWithConfirmFunction(filename, ConfirmFunction) |
| 125 |
| 126 |
| 113 def main(): | 127 def main(): |
| 114 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...') | 128 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...') |
| 115 parser.add_option('-f', '--force', action='store_false', default=True, | 129 parser.add_option('-f', '--force', action='store_false', default=True, |
| 116 dest='should_confirm', | 130 dest='should_confirm', |
| 117 help='Turn off confirmation prompt.') | 131 help='Turn off confirmation prompt.') |
| 118 opts, filenames = parser.parse_args() | 132 opts, filenames = parser.parse_args() |
| 119 | 133 |
| 120 if len(filenames) < 1: | 134 if len(filenames) < 1: |
| 121 parser.print_help() | 135 parser.print_help() |
| 122 return 1 | 136 return 1 |
| 123 | 137 |
| 124 for filename in filenames: | 138 for filename in filenames: |
| 125 DiffAndConfirm(filename, opts.should_confirm) | 139 DiffAndConfirm(filename, opts.should_confirm) |
| 126 | 140 |
| 127 | 141 |
| 128 if __name__ == '__main__': | 142 if __name__ == '__main__': |
| 129 sys.exit(main()) | 143 sys.exit(main()) |
| OLD | NEW |