blob: 42a8be8e52b705b03388ee5ac5ee1c21bf64fdac [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
[email protected]50e5a3d2010-08-26 00:23:2615
[email protected]cb155a82011-11-29 17:25:3416
[email protected]50e5a3d2010-08-26 00:23:2617def YesNo(prompt):
18 """Prompts with a yes/no question, returns True if yes."""
19 print prompt,
20 sys.stdout.flush()
21 # http://code.activestate.com/recipes/134892/
[email protected]4a2a50cb2013-06-04 06:27:3822 if sys.platform == 'win32':
23 import msvcrt
24 ch = msvcrt.getch()
25 else:
26 import termios
27 import tty
28 fd = sys.stdin.fileno()
29 old_settings = termios.tcgetattr(fd)
30 ch = 'n'
31 try:
32 tty.setraw(sys.stdin.fileno())
33 ch = sys.stdin.read(1)
34 finally:
35 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
[email protected]50e5a3d2010-08-26 00:23:2636 print ch
37 return ch in ('Y', 'y')
38
39
40def IncludeCompareKey(line):
41 """Sorting comparator key used for comparing two #include lines.
42 Returns the filename without the #include/#import prefix.
43 """
44 for prefix in ('#include ', '#import '):
45 if line.startswith(prefix):
[email protected]d5c49322011-05-19 20:08:5746 line = line[len(prefix):]
47 break
[email protected]51e3da52011-05-20 01:53:0648
49 # The win32 api has all sorts of implicit include order dependencies :-/
50 # Give a few headers special sort keys that make sure they appear before all
51 # other headers.
52 if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h
53 return '0'
[email protected]d5d71052013-02-25 21:01:3554 if line.startswith('<atlbase.h>'): # Must be before atlapp.h.
55 return '1' + line
[email protected]51e3da52011-05-20 01:53:0656 if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h
[email protected]d5d71052013-02-25 21:01:3557 return '1' + line
[email protected]51e3da52011-05-20 01:53:0658
[email protected]08079092012-01-05 18:24:3859 # C++ system headers should come after C system headers.
60 if line.startswith('<'):
61 if line.find('.h>') != -1:
[email protected]2661bb92013-03-13 21:24:4062 return '2' + line.lower()
[email protected]08079092012-01-05 18:24:3863 else:
[email protected]2661bb92013-03-13 21:24:4064 return '3' + line.lower()
[email protected]08079092012-01-05 18:24:3865
66 return '4' + line
[email protected]50e5a3d2010-08-26 00:23:2667
68
69def IsInclude(line):
70 """Returns True if the line is an #include/#import line."""
71 return line.startswith('#include ') or line.startswith('#import ')
72
73
74def SortHeader(infile, outfile):
75 """Sorts the headers in infile, writing the sorted file to outfile."""
76 for line in infile:
77 if IsInclude(line):
78 headerblock = []
79 while IsInclude(line):
[email protected]1590a8e2013-06-18 14:25:5880 infile_ended_on_include_line = False
[email protected]50e5a3d2010-08-26 00:23:2681 headerblock.append(line)
[email protected]1590a8e2013-06-18 14:25:5882 # Ensure we don't die due to trying to read beyond the end of the file.
83 try:
84 line = infile.next()
85 except StopIteration:
86 infile_ended_on_include_line = True
87 break
[email protected]50e5a3d2010-08-26 00:23:2688 for header in sorted(headerblock, key=IncludeCompareKey):
89 outfile.write(header)
[email protected]1590a8e2013-06-18 14:25:5890 if infile_ended_on_include_line:
91 # We already wrote the last line above; exit to ensure it isn't written
92 # again.
93 return
[email protected]50e5a3d2010-08-26 00:23:2694 # Intentionally fall through, to write the line that caused
95 # the above while loop to exit.
96 outfile.write(line)
97
98
[email protected]1590a8e2013-06-18 14:25:5899def FixFileWithConfirmFunction(filename, confirm_function,
100 perform_safety_checks):
[email protected]18367222012-11-22 11:28:57101 """Creates a fixed version of the file, invokes |confirm_function|
102 to decide whether to use the new file, and cleans up.
103
104 |confirm_function| takes two parameters, the original filename and
105 the fixed-up filename, and returns True to use the fixed-up file,
106 false to not use it.
[email protected]1590a8e2013-06-18 14:25:58107
108 If |perform_safety_checks| is True, then the function checks whether it is
109 unsafe to reorder headers in this file and skips the reorder with a warning
110 message in that case.
[email protected]10ab0ed52011-11-01 11:46:52111 """
[email protected]1590a8e2013-06-18 14:25:58112 if perform_safety_checks and IsUnsafeToReorderHeaders(filename):
113 print ('Not reordering headers in %s as the script thinks that the '
114 'order of headers in this file is semantically significant.'
115 % (filename))
116 return
[email protected]10ab0ed52011-11-01 11:46:52117 fixfilename = filename + '.new'
[email protected]4a2a50cb2013-06-04 06:27:38118 infile = open(filename, 'rb')
119 outfile = open(fixfilename, 'wb')
[email protected]10ab0ed52011-11-01 11:46:52120 SortHeader(infile, outfile)
121 infile.close()
122 outfile.close() # Important so the below diff gets the updated contents.
123
124 try:
[email protected]18367222012-11-22 11:28:57125 if confirm_function(filename, fixfilename):
[email protected]4a2a50cb2013-06-04 06:27:38126 if sys.platform == 'win32':
127 os.unlink(filename)
[email protected]10ab0ed52011-11-01 11:46:52128 os.rename(fixfilename, filename)
129 finally:
130 try:
131 os.remove(fixfilename)
132 except OSError:
133 # If the file isn't there, we don't care.
134 pass
135
136
[email protected]1590a8e2013-06-18 14:25:58137def DiffAndConfirm(filename, should_confirm, perform_safety_checks):
[email protected]18367222012-11-22 11:28:57138 """Shows a diff of what the tool would change the file named
139 filename to. Shows a confirmation prompt if should_confirm is true.
140 Saves the resulting file if should_confirm is false or the user
141 answers Y to the confirmation prompt.
142 """
143 def ConfirmFunction(filename, fixfilename):
144 diff = os.system('diff -u %s %s' % (filename, fixfilename))
[email protected]4a2a50cb2013-06-04 06:27:38145 if sys.platform != 'win32':
146 diff >>= 8
147 if diff == 0: # Check exit code.
[email protected]18367222012-11-22 11:28:57148 print '%s: no change' % filename
149 return False
150
151 return (not should_confirm or YesNo('Use new file (y/N)?'))
152
[email protected]1590a8e2013-06-18 14:25:58153 FixFileWithConfirmFunction(filename, ConfirmFunction, perform_safety_checks)
[email protected]18367222012-11-22 11:28:57154
[email protected]1590a8e2013-06-18 14:25:58155def IsUnsafeToReorderHeaders(filename):
156 # *_message_generator.cc is almost certainly a file that generates IPC
157 # definitions. Changes in include order in these files can result in them not
158 # building correctly.
159 if filename.find("message_generator.cc") != -1:
160 return True
161 return False
[email protected]18367222012-11-22 11:28:57162
[email protected]50e5a3d2010-08-26 00:23:26163def main():
164 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
[email protected]10ab0ed52011-11-01 11:46:52165 parser.add_option('-f', '--force', action='store_false', default=True,
166 dest='should_confirm',
167 help='Turn off confirmation prompt.')
[email protected]1590a8e2013-06-18 14:25:58168 parser.add_option('--no_safety_checks',
169 action='store_false', default=True,
170 dest='perform_safety_checks',
171 help='Do not perform the safety checks via which this '
172 'script refuses to operate on files for which it thinks '
173 'the include ordering is semantically significant.')
[email protected]10ab0ed52011-11-01 11:46:52174 opts, filenames = parser.parse_args()
[email protected]50e5a3d2010-08-26 00:23:26175
[email protected]10ab0ed52011-11-01 11:46:52176 if len(filenames) < 1:
[email protected]50e5a3d2010-08-26 00:23:26177 parser.print_help()
[email protected]cb155a82011-11-29 17:25:34178 return 1
[email protected]50e5a3d2010-08-26 00:23:26179
[email protected]10ab0ed52011-11-01 11:46:52180 for filename in filenames:
[email protected]1590a8e2013-06-18 14:25:58181 DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks)
[email protected]50e5a3d2010-08-26 00:23:26182
183
184if __name__ == '__main__':
[email protected]cb155a82011-11-29 17:25:34185 sys.exit(main())