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
4 # found in the LICENSE file.
6 """Given a filename as an argument, sort the #include/#imports in that file.
8 Shows a diff and prompts for confirmation before doing the deed.
9 Works great with tools/git/for-all-touched-files.py.
18 """Prompts with a yes/no question, returns True if yes."""
21 # http://code.activestate.com/recipes/134892/
22 if sys
.platform
== 'win32':
28 fd
= sys
.stdin
.fileno()
29 old_settings
= termios
.tcgetattr(fd
)
32 tty
.setraw(sys
.stdin
.fileno())
33 ch
= sys
.stdin
.read(1)
35 termios
.tcsetattr(fd
, termios
.TCSADRAIN
, old_settings
)
37 return ch
in ('Y', 'y')
40 def IncludeCompareKey(line
):
41 """Sorting comparator key used for comparing two #include lines.
42 Returns the filename without the #include/#import/import prefix.
44 for prefix
in ('#include ', '#import ', 'import '):
45 if line
.startswith(prefix
):
46 line
= line
[len(prefix
):]
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
52 if line
.startswith('<windows.h>'): # Must be before e.g. shellapi.h
54 if line
.startswith('<atlbase.h>'): # Must be before atlapp.h.
56 if line
.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h
59 # C++ system headers should come after C system headers.
60 if line
.startswith('<'):
61 if line
.find('.h>') != -1:
62 return '2' + line
.lower()
64 return '3' + line
.lower()
70 """Returns True if the line is an #include/#import/import line."""
71 return any([line
.startswith('#include '), line
.startswith('#import '),
72 line
.startswith('import ')])
75 def SortHeader(infile
, outfile
):
76 """Sorts the headers in infile, writing the sorted file to outfile."""
80 while IsInclude(line
):
81 infile_ended_on_include_line
= False
82 headerblock
.append(line
)
83 # Ensure we don't die due to trying to read beyond the end of the file.
87 infile_ended_on_include_line
= True
89 for header
in sorted(headerblock
, key
=IncludeCompareKey
):
91 if infile_ended_on_include_line
:
92 # We already wrote the last line above; exit to ensure it isn't written
95 # Intentionally fall through, to write the line that caused
96 # the above while loop to exit.
100 def FixFileWithConfirmFunction(filename
, confirm_function
,
101 perform_safety_checks
):
102 """Creates a fixed version of the file, invokes |confirm_function|
103 to decide whether to use the new file, and cleans up.
105 |confirm_function| takes two parameters, the original filename and
106 the fixed-up filename, and returns True to use the fixed-up file,
109 If |perform_safety_checks| is True, then the function checks whether it is
110 unsafe to reorder headers in this file and skips the reorder with a warning
111 message in that case.
113 if perform_safety_checks
and IsUnsafeToReorderHeaders(filename
):
114 print ('Not reordering headers in %s as the script thinks that the '
115 'order of headers in this file is semantically significant.'
118 fixfilename
= filename
+ '.new'
119 infile
= open(filename
, 'rb')
120 outfile
= open(fixfilename
, 'wb')
121 SortHeader(infile
, outfile
)
123 outfile
.close() # Important so the below diff gets the updated contents.
126 if confirm_function(filename
, fixfilename
):
127 if sys
.platform
== 'win32':
129 os
.rename(fixfilename
, filename
)
132 os
.remove(fixfilename
)
134 # If the file isn't there, we don't care.
138 def DiffAndConfirm(filename
, should_confirm
, perform_safety_checks
):
139 """Shows a diff of what the tool would change the file named
140 filename to. Shows a confirmation prompt if should_confirm is true.
141 Saves the resulting file if should_confirm is false or the user
142 answers Y to the confirmation prompt.
144 def ConfirmFunction(filename
, fixfilename
):
145 diff
= os
.system('diff -u %s %s' % (filename
, fixfilename
))
146 if sys
.platform
!= 'win32':
148 if diff
== 0: # Check exit code.
149 print '%s: no change' % filename
152 return (not should_confirm
or YesNo('Use new file (y/N)?'))
154 FixFileWithConfirmFunction(filename
, ConfirmFunction
, perform_safety_checks
)
156 def IsUnsafeToReorderHeaders(filename
):
157 # *_message_generator.cc is almost certainly a file that generates IPC
158 # definitions. Changes in include order in these files can result in them not
159 # building correctly.
160 if filename
.find("message_generator.cc") != -1:
165 parser
= optparse
.OptionParser(usage
='%prog filename1 filename2 ...')
166 parser
.add_option('-f', '--force', action
='store_false', default
=True,
167 dest
='should_confirm',
168 help='Turn off confirmation prompt.')
169 parser
.add_option('--no_safety_checks',
170 action
='store_false', default
=True,
171 dest
='perform_safety_checks',
172 help='Do not perform the safety checks via which this '
173 'script refuses to operate on files for which it thinks '
174 'the include ordering is semantically significant.')
175 opts
, filenames
= parser
.parse_args()
177 if len(filenames
) < 1:
181 for filename
in filenames
:
182 DiffAndConfirm(filename
, opts
.should_confirm
, opts
.perform_safety_checks
)
185 if __name__
== '__main__':