[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """Given a filename as an argument, sort the #include/#imports in that file. |
| 4 | |
| 5 | Shows a diff and prompts for confirmation before doing the deed. |
| 6 | """ |
| 7 | |
| 8 | import optparse |
| 9 | import os |
| 10 | import sys |
| 11 | import termios |
| 12 | import tty |
| 13 | |
| 14 | def YesNo(prompt): |
| 15 | """Prompts with a yes/no question, returns True if yes.""" |
| 16 | print prompt, |
| 17 | sys.stdout.flush() |
| 18 | # http://code.activestate.com/recipes/134892/ |
| 19 | fd = sys.stdin.fileno() |
| 20 | old_settings = termios.tcgetattr(fd) |
| 21 | ch = 'n' |
| 22 | try: |
| 23 | tty.setraw(sys.stdin.fileno()) |
| 24 | ch = sys.stdin.read(1) |
| 25 | finally: |
| 26 | termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) |
| 27 | print ch |
| 28 | return ch in ('Y', 'y') |
| 29 | |
| 30 | |
| 31 | def IncludeCompareKey(line): |
| 32 | """Sorting comparator key used for comparing two #include lines. |
| 33 | Returns the filename without the #include/#import prefix. |
| 34 | """ |
| 35 | for prefix in ('#include ', '#import '): |
| 36 | if line.startswith(prefix): |
| 37 | return line[len(prefix):] |
| 38 | return line |
| 39 | |
| 40 | |
| 41 | def IsInclude(line): |
| 42 | """Returns True if the line is an #include/#import line.""" |
| 43 | return line.startswith('#include ') or line.startswith('#import ') |
| 44 | |
| 45 | |
| 46 | def SortHeader(infile, outfile): |
| 47 | """Sorts the headers in infile, writing the sorted file to outfile.""" |
| 48 | for line in infile: |
| 49 | if IsInclude(line): |
| 50 | headerblock = [] |
| 51 | while IsInclude(line): |
| 52 | headerblock.append(line) |
| 53 | line = infile.next() |
| 54 | for header in sorted(headerblock, key=IncludeCompareKey): |
| 55 | outfile.write(header) |
| 56 | # Intentionally fall through, to write the line that caused |
| 57 | # the above while loop to exit. |
| 58 | outfile.write(line) |
| 59 | |
| 60 | |
| 61 | def main(): |
| 62 | parser = optparse.OptionParser(usage='%prog filename1 filename2 ...') |
| 63 | opts, args = parser.parse_args() |
| 64 | |
| 65 | if len(args) < 1: |
| 66 | parser.print_help() |
| 67 | sys.exit(1) |
| 68 | |
| 69 | for filename in args: |
| 70 | fixfilename = filename + '.new' |
| 71 | infile = open(filename, 'r') |
| 72 | outfile = open(fixfilename, 'w') |
| 73 | SortHeader(infile, outfile) |
| 74 | infile.close() |
| 75 | outfile.close() # Important so the below diff gets the updated contents. |
| 76 | |
| 77 | try: |
| 78 | diff = os.system('diff -u %s %s' % (filename, fixfilename)) |
| 79 | if diff >> 8 == 0: # Check exit code. |
| 80 | print '%s: no change' % filename |
| 81 | continue |
| 82 | |
| 83 | if YesNo('Use new file (y/N)?'): |
| 84 | os.rename(fixfilename, filename) |
| 85 | finally: |
| 86 | try: |
| 87 | os.remove(fixfilename) |
| 88 | except OSError: |
| 89 | # If the file isn't there, we don't care. |
| 90 | pass |
| 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | main() |