blob: b1575b3822b871ce831428f5dd842399aa1b62ba [file] [log] [blame]
[email protected]cb155a82011-11-29 17:25:341#!/usr/bin/env python
[email protected]08079092012-01-05 18:24:382# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]377bf4a2011-05-19 20:17:113# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
[email protected]50e5a3d2010-08-26 00:23:265
6"""Given a filename as an argument, sort the #include/#imports in that file.
7
8Shows a diff and prompts for confirmation before doing the deed.
[email protected]10ab0ed52011-11-01 11:46:529Works great with tools/git/for-all-touched-files.py.
[email protected]50e5a3d2010-08-26 00:23:2610"""
11
12import optparse
13import os
14import sys
15import termios
16import tty
17
[email protected]cb155a82011-11-29 17:25:3418
[email protected]50e5a3d2010-08-26 00:23:2619def 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
36def IncludeCompareKey(line):
37 """Sorting comparator key used for comparing two #include lines.
38 Returns the filename without the #include/#import prefix.
39 """
[email protected]12ea4192012-05-29 15:13:4040 line = line.lower()
[email protected]50e5a3d2010-08-26 00:23:2641 for prefix in ('#include ', '#import '):
42 if line.startswith(prefix):
[email protected]d5c49322011-05-19 20:08:5743 line = line[len(prefix):]
44 break
[email protected]51e3da52011-05-20 01:53:0645
46 # The win32 api has all sorts of implicit include order dependencies :-/
47 # Give a few headers special sort keys that make sure they appear before all
48 # other headers.
49 if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h
50 return '0'
[email protected]d5d71052013-02-25 21:01:3551 if line.startswith('<atlbase.h>'): # Must be before atlapp.h.
52 return '1' + line
[email protected]51e3da52011-05-20 01:53:0653 if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h
[email protected]d5d71052013-02-25 21:01:3554 return '1' + line
[email protected]51e3da52011-05-20 01:53:0655
[email protected]08079092012-01-05 18:24:3856 # C++ system headers should come after C system headers.
57 if line.startswith('<'):
58 if line.find('.h>') != -1:
59 return '2' + line
60 else:
61 return '3' + line
62
63 return '4' + line
[email protected]50e5a3d2010-08-26 00:23:2664
65
66def IsInclude(line):
67 """Returns True if the line is an #include/#import line."""
68 return line.startswith('#include ') or line.startswith('#import ')
69
70
71def SortHeader(infile, outfile):
72 """Sorts the headers in infile, writing the sorted file to outfile."""
73 for line in infile:
74 if IsInclude(line):
75 headerblock = []
76 while IsInclude(line):
77 headerblock.append(line)
78 line = infile.next()
79 for header in sorted(headerblock, key=IncludeCompareKey):
80 outfile.write(header)
81 # Intentionally fall through, to write the line that caused
82 # the above while loop to exit.
83 outfile.write(line)
84
85
[email protected]18367222012-11-22 11:28:5786def FixFileWithConfirmFunction(filename, confirm_function):
87 """Creates a fixed version of the file, invokes |confirm_function|
88 to decide whether to use the new file, and cleans up.
89
90 |confirm_function| takes two parameters, the original filename and
91 the fixed-up filename, and returns True to use the fixed-up file,
92 false to not use it.
[email protected]10ab0ed52011-11-01 11:46:5293 """
94 fixfilename = filename + '.new'
95 infile = open(filename, 'r')
96 outfile = open(fixfilename, 'w')
97 SortHeader(infile, outfile)
98 infile.close()
99 outfile.close() # Important so the below diff gets the updated contents.
100
101 try:
[email protected]18367222012-11-22 11:28:57102 if confirm_function(filename, fixfilename):
[email protected]10ab0ed52011-11-01 11:46:52103 os.rename(fixfilename, filename)
104 finally:
105 try:
106 os.remove(fixfilename)
107 except OSError:
108 # If the file isn't there, we don't care.
109 pass
110
111
[email protected]18367222012-11-22 11:28:57112def DiffAndConfirm(filename, should_confirm):
113 """Shows a diff of what the tool would change the file named
114 filename to. Shows a confirmation prompt if should_confirm is true.
115 Saves the resulting file if should_confirm is false or the user
116 answers Y to the confirmation prompt.
117 """
118 def ConfirmFunction(filename, fixfilename):
119 diff = os.system('diff -u %s %s' % (filename, fixfilename))
120 if diff >> 8 == 0: # Check exit code.
121 print '%s: no change' % filename
122 return False
123
124 return (not should_confirm or YesNo('Use new file (y/N)?'))
125
126 FixFileWithConfirmFunction(filename, ConfirmFunction)
127
128
[email protected]50e5a3d2010-08-26 00:23:26129def main():
130 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
[email protected]10ab0ed52011-11-01 11:46:52131 parser.add_option('-f', '--force', action='store_false', default=True,
132 dest='should_confirm',
133 help='Turn off confirmation prompt.')
134 opts, filenames = parser.parse_args()
[email protected]50e5a3d2010-08-26 00:23:26135
[email protected]10ab0ed52011-11-01 11:46:52136 if len(filenames) < 1:
[email protected]50e5a3d2010-08-26 00:23:26137 parser.print_help()
[email protected]cb155a82011-11-29 17:25:34138 return 1
[email protected]50e5a3d2010-08-26 00:23:26139
[email protected]10ab0ed52011-11-01 11:46:52140 for filename in filenames:
141 DiffAndConfirm(filename, opts.should_confirm)
[email protected]50e5a3d2010-08-26 00:23:26142
143
144if __name__ == '__main__':
[email protected]cb155a82011-11-29 17:25:34145 sys.exit(main())