[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame^] | 1 | #!/usr/bin/env python |
[email protected] | 377bf4a | 2011-05-19 20:17:11 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 5 | |
| 6 | """Given a filename as an argument, sort the #include/#imports in that file. |
| 7 | |
| 8 | Shows a diff and prompts for confirmation before doing the deed. |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 9 | Works great with tools/git/for-all-touched-files.py. |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 10 | """ |
| 11 | |
| 12 | import optparse |
| 13 | import os |
| 14 | import sys |
| 15 | import termios |
| 16 | import tty |
| 17 | |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame^] | 18 | |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 19 | def YesNo(prompt): |
| 20 | """Prompts with a yes/no question, returns True if yes.""" |
| 21 | print prompt, |
| 22 | sys.stdout.flush() |
| 23 | # http://code.activestate.com/recipes/134892/ |
| 24 | fd = sys.stdin.fileno() |
| 25 | old_settings = termios.tcgetattr(fd) |
| 26 | ch = 'n' |
| 27 | try: |
| 28 | tty.setraw(sys.stdin.fileno()) |
| 29 | ch = sys.stdin.read(1) |
| 30 | finally: |
| 31 | termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) |
| 32 | print ch |
| 33 | return ch in ('Y', 'y') |
| 34 | |
| 35 | |
| 36 | def IncludeCompareKey(line): |
| 37 | """Sorting comparator key used for comparing two #include lines. |
| 38 | Returns the filename without the #include/#import prefix. |
| 39 | """ |
| 40 | for prefix in ('#include ', '#import '): |
| 41 | if line.startswith(prefix): |
[email protected] | d5c4932 | 2011-05-19 20:08:57 | [diff] [blame] | 42 | line = line[len(prefix):] |
| 43 | break |
[email protected] | 51e3da5 | 2011-05-20 01:53:06 | [diff] [blame] | 44 | |
| 45 | # The win32 api has all sorts of implicit include order dependencies :-/ |
| 46 | # Give a few headers special sort keys that make sure they appear before all |
| 47 | # other headers. |
| 48 | if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h |
| 49 | return '0' |
| 50 | if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h |
| 51 | return '1' |
| 52 | |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 53 | return line |
| 54 | |
| 55 | |
| 56 | def IsInclude(line): |
| 57 | """Returns True if the line is an #include/#import line.""" |
| 58 | return line.startswith('#include ') or line.startswith('#import ') |
| 59 | |
| 60 | |
| 61 | def SortHeader(infile, outfile): |
| 62 | """Sorts the headers in infile, writing the sorted file to outfile.""" |
| 63 | for line in infile: |
| 64 | if IsInclude(line): |
| 65 | headerblock = [] |
| 66 | while IsInclude(line): |
| 67 | headerblock.append(line) |
| 68 | line = infile.next() |
| 69 | for header in sorted(headerblock, key=IncludeCompareKey): |
| 70 | outfile.write(header) |
| 71 | # Intentionally fall through, to write the line that caused |
| 72 | # the above while loop to exit. |
| 73 | outfile.write(line) |
| 74 | |
| 75 | |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 76 | def DiffAndConfirm(filename, should_confirm): |
| 77 | """Shows a diff of what the tool would change the file named |
| 78 | filename to. Shows a confirmation prompt if should_confirm is true. |
| 79 | Saves the resulting file if should_confirm is false or the user |
| 80 | answers Y to the confirmation prompt. |
| 81 | """ |
| 82 | fixfilename = filename + '.new' |
| 83 | infile = open(filename, 'r') |
| 84 | outfile = open(fixfilename, 'w') |
| 85 | SortHeader(infile, outfile) |
| 86 | infile.close() |
| 87 | outfile.close() # Important so the below diff gets the updated contents. |
| 88 | |
| 89 | try: |
| 90 | diff = os.system('diff -u %s %s' % (filename, fixfilename)) |
| 91 | if diff >> 8 == 0: # Check exit code. |
| 92 | print '%s: no change' % filename |
| 93 | return |
| 94 | |
| 95 | if not should_confirm or YesNo('Use new file (y/N)?'): |
| 96 | os.rename(fixfilename, filename) |
| 97 | finally: |
| 98 | try: |
| 99 | os.remove(fixfilename) |
| 100 | except OSError: |
| 101 | # If the file isn't there, we don't care. |
| 102 | pass |
| 103 | |
| 104 | |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 105 | def main(): |
| 106 | parser = optparse.OptionParser(usage='%prog filename1 filename2 ...') |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 107 | parser.add_option('-f', '--force', action='store_false', default=True, |
| 108 | dest='should_confirm', |
| 109 | help='Turn off confirmation prompt.') |
| 110 | opts, filenames = parser.parse_args() |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 111 | |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 112 | if len(filenames) < 1: |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 113 | parser.print_help() |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame^] | 114 | return 1 |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 115 | |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 116 | for filename in filenames: |
| 117 | DiffAndConfirm(filename, opts.should_confirm) |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 118 | |
| 119 | |
| 120 | if __name__ == '__main__': |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame^] | 121 | sys.exit(main()) |