blob: 88bbd555c3952a64037f738d231b1e2e4f647a7a [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
satorux3d23ca02015-02-10 08:04:5116from yes_no import YesNo
[email protected]50e5a3d2010-08-26 00:23:2617
18
benwells5c5d6f92015-07-15 06:08:0219def IsInclude(line):
20 """Returns True if the line is an #include/#import/import line."""
21 return any([line.startswith('#include '), line.startswith('#import '),
22 line.startswith('import ')])
23
24
25def IncludeCompareKey(line, for_blink):
[email protected]50e5a3d2010-08-26 00:23:2626 """Sorting comparator key used for comparing two #include lines.
benwells5c5d6f92015-07-15 06:08:0227
28 Returns an integer, optionally followed by a string. The integer is used
29 for coarse sorting of different categories of headers, and the string is
30 used for fine sorting of headers within categeries.
[email protected]50e5a3d2010-08-26 00:23:2631 """
[email protected]5650b4a42014-04-09 00:52:1532 for prefix in ('#include ', '#import ', 'import '):
[email protected]50e5a3d2010-08-26 00:23:2633 if line.startswith(prefix):
[email protected]d5c49322011-05-19 20:08:5734 line = line[len(prefix):]
35 break
[email protected]51e3da52011-05-20 01:53:0636
benwells5c5d6f92015-07-15 06:08:0237 if for_blink:
38 # Blink likes to have its "config.h" include first.
39 if line.startswith('"config.h"'):
40 return '0'
41
42 # Blink sorts system headers after others. This is handled by sorting
43 # alphabetically so no need to do anything tricky.
44 return '1' + line
45
[email protected]51e3da52011-05-20 01:53:0646 # 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
thestigc5d32952014-08-25 22:32:3553 if line.startswith('<ole2.h>'): # Must be before e.g. intshcut.h
54 return '1' + line
[email protected]51e3da52011-05-20 01:53:0655 if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h
[email protected]d5d71052013-02-25 21:01:3556 return '1' + line
[email protected]51e3da52011-05-20 01:53:0657
[email protected]08079092012-01-05 18:24:3858 # C++ system headers should come after C system headers.
59 if line.startswith('<'):
60 if line.find('.h>') != -1:
[email protected]2661bb92013-03-13 21:24:4061 return '2' + line.lower()
[email protected]08079092012-01-05 18:24:3862 else:
[email protected]2661bb92013-03-13 21:24:4063 return '3' + line.lower()
[email protected]08079092012-01-05 18:24:3864
65 return '4' + line
[email protected]50e5a3d2010-08-26 00:23:2666
67
benwells5c5d6f92015-07-15 06:08:0268def SortHeader(infile, outfile, for_blink):
[email protected]50e5a3d2010-08-26 00:23:2669 """Sorts the headers in infile, writing the sorted file to outfile."""
benwells5c5d6f92015-07-15 06:08:0270 def CompareKey(line):
71 return IncludeCompareKey(line, for_blink)
72
[email protected]50e5a3d2010-08-26 00:23:2673 for line in infile:
74 if IsInclude(line):
75 headerblock = []
76 while IsInclude(line):
[email protected]1590a8e2013-06-18 14:25:5877 infile_ended_on_include_line = False
[email protected]50e5a3d2010-08-26 00:23:2678 headerblock.append(line)
[email protected]1590a8e2013-06-18 14:25:5879 # Ensure we don't die due to trying to read beyond the end of the file.
80 try:
81 line = infile.next()
82 except StopIteration:
83 infile_ended_on_include_line = True
84 break
benwells5c5d6f92015-07-15 06:08:0285 for header in sorted(headerblock, key=CompareKey):
[email protected]50e5a3d2010-08-26 00:23:2686 outfile.write(header)
[email protected]1590a8e2013-06-18 14:25:5887 if infile_ended_on_include_line:
88 # We already wrote the last line above; exit to ensure it isn't written
89 # again.
90 return
[email protected]50e5a3d2010-08-26 00:23:2691 # Intentionally fall through, to write the line that caused
92 # the above while loop to exit.
93 outfile.write(line)
94
95
[email protected]1590a8e2013-06-18 14:25:5896def FixFileWithConfirmFunction(filename, confirm_function,
benwells5c5d6f92015-07-15 06:08:0297 perform_safety_checks, for_blink=False):
[email protected]18367222012-11-22 11:28:5798 """Creates a fixed version of the file, invokes |confirm_function|
99 to decide whether to use the new file, and cleans up.
100
101 |confirm_function| takes two parameters, the original filename and
102 the fixed-up filename, and returns True to use the fixed-up file,
103 false to not use it.
[email protected]1590a8e2013-06-18 14:25:58104
105 If |perform_safety_checks| is True, then the function checks whether it is
106 unsafe to reorder headers in this file and skips the reorder with a warning
107 message in that case.
[email protected]10ab0ed52011-11-01 11:46:52108 """
[email protected]1590a8e2013-06-18 14:25:58109 if perform_safety_checks and IsUnsafeToReorderHeaders(filename):
110 print ('Not reordering headers in %s as the script thinks that the '
111 'order of headers in this file is semantically significant.'
112 % (filename))
113 return
[email protected]10ab0ed52011-11-01 11:46:52114 fixfilename = filename + '.new'
[email protected]4a2a50cb2013-06-04 06:27:38115 infile = open(filename, 'rb')
116 outfile = open(fixfilename, 'wb')
benwells5c5d6f92015-07-15 06:08:02117 SortHeader(infile, outfile, for_blink)
[email protected]10ab0ed52011-11-01 11:46:52118 infile.close()
119 outfile.close() # Important so the below diff gets the updated contents.
120
121 try:
[email protected]18367222012-11-22 11:28:57122 if confirm_function(filename, fixfilename):
[email protected]4a2a50cb2013-06-04 06:27:38123 if sys.platform == 'win32':
124 os.unlink(filename)
[email protected]10ab0ed52011-11-01 11:46:52125 os.rename(fixfilename, filename)
126 finally:
127 try:
128 os.remove(fixfilename)
129 except OSError:
130 # If the file isn't there, we don't care.
131 pass
132
133
benwells5c5d6f92015-07-15 06:08:02134def DiffAndConfirm(filename, should_confirm, perform_safety_checks, for_blink):
[email protected]18367222012-11-22 11:28:57135 """Shows a diff of what the tool would change the file named
136 filename to. Shows a confirmation prompt if should_confirm is true.
137 Saves the resulting file if should_confirm is false or the user
138 answers Y to the confirmation prompt.
139 """
140 def ConfirmFunction(filename, fixfilename):
141 diff = os.system('diff -u %s %s' % (filename, fixfilename))
[email protected]4a2a50cb2013-06-04 06:27:38142 if sys.platform != 'win32':
143 diff >>= 8
144 if diff == 0: # Check exit code.
[email protected]18367222012-11-22 11:28:57145 print '%s: no change' % filename
146 return False
147
148 return (not should_confirm or YesNo('Use new file (y/N)?'))
149
benwells5c5d6f92015-07-15 06:08:02150 FixFileWithConfirmFunction(filename, ConfirmFunction, perform_safety_checks,
151 for_blink)
[email protected]18367222012-11-22 11:28:57152
[email protected]1590a8e2013-06-18 14:25:58153def IsUnsafeToReorderHeaders(filename):
154 # *_message_generator.cc is almost certainly a file that generates IPC
155 # definitions. Changes in include order in these files can result in them not
156 # building correctly.
157 if filename.find("message_generator.cc") != -1:
158 return True
159 return False
[email protected]18367222012-11-22 11:28:57160
[email protected]50e5a3d2010-08-26 00:23:26161def main():
162 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
[email protected]10ab0ed52011-11-01 11:46:52163 parser.add_option('-f', '--force', action='store_false', default=True,
164 dest='should_confirm',
165 help='Turn off confirmation prompt.')
[email protected]1590a8e2013-06-18 14:25:58166 parser.add_option('--no_safety_checks',
167 action='store_false', default=True,
168 dest='perform_safety_checks',
169 help='Do not perform the safety checks via which this '
170 'script refuses to operate on files for which it thinks '
171 'the include ordering is semantically significant.')
benwells5c5d6f92015-07-15 06:08:02172 parser.add_option('--for_blink', action='store_true', default=False,
173 dest='for_blink', help='Whether the blink header sorting '
174 'rules should be applied.')
[email protected]10ab0ed52011-11-01 11:46:52175 opts, filenames = parser.parse_args()
[email protected]50e5a3d2010-08-26 00:23:26176
[email protected]10ab0ed52011-11-01 11:46:52177 if len(filenames) < 1:
[email protected]50e5a3d2010-08-26 00:23:26178 parser.print_help()
[email protected]cb155a82011-11-29 17:25:34179 return 1
[email protected]50e5a3d2010-08-26 00:23:26180
[email protected]10ab0ed52011-11-01 11:46:52181 for filename in filenames:
benwells5c5d6f92015-07-15 06:08:02182 DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks,
183 opts.for_blink)
[email protected]50e5a3d2010-08-26 00:23:26184
185
186if __name__ == '__main__':
[email protected]cb155a82011-11-29 17:25:34187 sys.exit(main())