OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Given a filename as an argument, sort the #include/#imports in that file. | 6 """Given a filename as an argument, sort the #include/#imports in that file. |
7 | 7 |
8 Shows a diff and prompts for confirmation before doing the deed. | 8 Shows a diff and prompts for confirmation before doing the deed. |
9 Works great with tools/git/for-all-touched-files.py. | 9 Works great with tools/git/for-all-touched-files.py. |
10 """ | 10 """ |
11 | 11 |
12 import optparse | 12 import optparse |
13 import os | 13 import os |
14 import sys | 14 import sys |
15 | 15 |
16 from yes_no import YesNo | 16 from yes_no import YesNo |
17 | 17 |
18 | 18 |
19 def IncludeCompareKey(line): | 19 def 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 | |
25 def IncludeCompareKey(line, for_blink): | |
20 """Sorting comparator key used for comparing two #include lines. | 26 """Sorting comparator key used for comparing two #include lines. |
21 Returns the filename without the #include/#import/import prefix. | 27 Returns the filename without the #include/#import/import prefix. |
22 """ | 28 """ |
23 for prefix in ('#include ', '#import ', 'import '): | 29 for prefix in ('#include ', '#import ', 'import '): |
24 if line.startswith(prefix): | 30 if line.startswith(prefix): |
25 line = line[len(prefix):] | 31 line = line[len(prefix):] |
26 break | 32 break |
27 | 33 |
34 if for_blink: | |
35 # Blink likes to have its "config.h" include first. | |
36 if line.startswith('"config.h"'): | |
37 return '0' | |
38 | |
39 # Blink sorts system headers after others. This is handled by sorting | |
40 # alphabetically so no need to do anything tricky. | |
41 return '1' + line | |
42 | |
28 # The win32 api has all sorts of implicit include order dependencies :-/ | 43 # The win32 api has all sorts of implicit include order dependencies :-/ |
29 # Give a few headers special sort keys that make sure they appear before all | 44 # Give a few headers special sort keys that make sure they appear before all |
30 # other headers. | 45 # other headers. |
31 if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h | 46 if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h |
32 return '0' | 47 return '0' |
33 if line.startswith('<atlbase.h>'): # Must be before atlapp.h. | 48 if line.startswith('<atlbase.h>'): # Must be before atlapp.h. |
34 return '1' + line | 49 return '1' + line |
35 if line.startswith('<ole2.h>'): # Must be before e.g. intshcut.h | 50 if line.startswith('<ole2.h>'): # Must be before e.g. intshcut.h |
36 return '1' + line | 51 return '1' + line |
37 if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h | 52 if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h |
38 return '1' + line | 53 return '1' + line |
39 | 54 |
40 # C++ system headers should come after C system headers. | 55 # C++ system headers should come after C system headers. |
41 if line.startswith('<'): | 56 if line.startswith('<'): |
42 if line.find('.h>') != -1: | 57 if line.find('.h>') != -1: |
43 return '2' + line.lower() | 58 return '2' + line.lower() |
44 else: | 59 else: |
45 return '3' + line.lower() | 60 return '3' + line.lower() |
46 | 61 |
47 return '4' + line | 62 return '4' + line |
48 | 63 |
49 | 64 |
50 def IsInclude(line): | 65 def SortHeader(infile, outfile, for_blink): |
51 """Returns True if the line is an #include/#import/import line.""" | 66 def CompareKey(line): |
52 return any([line.startswith('#include '), line.startswith('#import '), | 67 return IncludeCompareKey(line, for_blink) |
53 line.startswith('import ')]) | |
54 | 68 |
55 | |
56 def SortHeader(infile, outfile): | |
57 """Sorts the headers in infile, writing the sorted file to outfile.""" | 69 """Sorts the headers in infile, writing the sorted file to outfile.""" |
Matt Giuca
2015/07/13 06:25:46
Docstring should go before the nested functions (w
benwells
2015/07/13 06:32:13
Done.
| |
58 for line in infile: | 70 for line in infile: |
59 if IsInclude(line): | 71 if IsInclude(line): |
60 headerblock = [] | 72 headerblock = [] |
61 while IsInclude(line): | 73 while IsInclude(line): |
62 infile_ended_on_include_line = False | 74 infile_ended_on_include_line = False |
63 headerblock.append(line) | 75 headerblock.append(line) |
64 # Ensure we don't die due to trying to read beyond the end of the file. | 76 # Ensure we don't die due to trying to read beyond the end of the file. |
65 try: | 77 try: |
66 line = infile.next() | 78 line = infile.next() |
67 except StopIteration: | 79 except StopIteration: |
68 infile_ended_on_include_line = True | 80 infile_ended_on_include_line = True |
69 break | 81 break |
70 for header in sorted(headerblock, key=IncludeCompareKey): | 82 for header in sorted(headerblock, key=CompareKey): |
71 outfile.write(header) | 83 outfile.write(header) |
72 if infile_ended_on_include_line: | 84 if infile_ended_on_include_line: |
73 # We already wrote the last line above; exit to ensure it isn't written | 85 # We already wrote the last line above; exit to ensure it isn't written |
74 # again. | 86 # again. |
75 return | 87 return |
76 # Intentionally fall through, to write the line that caused | 88 # Intentionally fall through, to write the line that caused |
77 # the above while loop to exit. | 89 # the above while loop to exit. |
78 outfile.write(line) | 90 outfile.write(line) |
79 | 91 |
80 | 92 |
81 def FixFileWithConfirmFunction(filename, confirm_function, | 93 def FixFileWithConfirmFunction(filename, confirm_function, |
82 perform_safety_checks): | 94 perform_safety_checks, for_blink=False): |
83 """Creates a fixed version of the file, invokes |confirm_function| | 95 """Creates a fixed version of the file, invokes |confirm_function| |
84 to decide whether to use the new file, and cleans up. | 96 to decide whether to use the new file, and cleans up. |
85 | 97 |
86 |confirm_function| takes two parameters, the original filename and | 98 |confirm_function| takes two parameters, the original filename and |
87 the fixed-up filename, and returns True to use the fixed-up file, | 99 the fixed-up filename, and returns True to use the fixed-up file, |
88 false to not use it. | 100 false to not use it. |
89 | 101 |
90 If |perform_safety_checks| is True, then the function checks whether it is | 102 If |perform_safety_checks| is True, then the function checks whether it is |
91 unsafe to reorder headers in this file and skips the reorder with a warning | 103 unsafe to reorder headers in this file and skips the reorder with a warning |
92 message in that case. | 104 message in that case. |
93 """ | 105 """ |
94 if perform_safety_checks and IsUnsafeToReorderHeaders(filename): | 106 if perform_safety_checks and IsUnsafeToReorderHeaders(filename): |
95 print ('Not reordering headers in %s as the script thinks that the ' | 107 print ('Not reordering headers in %s as the script thinks that the ' |
96 'order of headers in this file is semantically significant.' | 108 'order of headers in this file is semantically significant.' |
97 % (filename)) | 109 % (filename)) |
98 return | 110 return |
99 fixfilename = filename + '.new' | 111 fixfilename = filename + '.new' |
100 infile = open(filename, 'rb') | 112 infile = open(filename, 'rb') |
101 outfile = open(fixfilename, 'wb') | 113 outfile = open(fixfilename, 'wb') |
102 SortHeader(infile, outfile) | 114 SortHeader(infile, outfile, for_blink) |
103 infile.close() | 115 infile.close() |
104 outfile.close() # Important so the below diff gets the updated contents. | 116 outfile.close() # Important so the below diff gets the updated contents. |
105 | 117 |
106 try: | 118 try: |
107 if confirm_function(filename, fixfilename): | 119 if confirm_function(filename, fixfilename): |
108 if sys.platform == 'win32': | 120 if sys.platform == 'win32': |
109 os.unlink(filename) | 121 os.unlink(filename) |
110 os.rename(fixfilename, filename) | 122 os.rename(fixfilename, filename) |
111 finally: | 123 finally: |
112 try: | 124 try: |
113 os.remove(fixfilename) | 125 os.remove(fixfilename) |
114 except OSError: | 126 except OSError: |
115 # If the file isn't there, we don't care. | 127 # If the file isn't there, we don't care. |
116 pass | 128 pass |
117 | 129 |
118 | 130 |
119 def DiffAndConfirm(filename, should_confirm, perform_safety_checks): | 131 def DiffAndConfirm(filename, should_confirm, perform_safety_checks, for_blink): |
120 """Shows a diff of what the tool would change the file named | 132 """Shows a diff of what the tool would change the file named |
121 filename to. Shows a confirmation prompt if should_confirm is true. | 133 filename to. Shows a confirmation prompt if should_confirm is true. |
122 Saves the resulting file if should_confirm is false or the user | 134 Saves the resulting file if should_confirm is false or the user |
123 answers Y to the confirmation prompt. | 135 answers Y to the confirmation prompt. |
124 """ | 136 """ |
125 def ConfirmFunction(filename, fixfilename): | 137 def ConfirmFunction(filename, fixfilename): |
126 diff = os.system('diff -u %s %s' % (filename, fixfilename)) | 138 diff = os.system('diff -u %s %s' % (filename, fixfilename)) |
127 if sys.platform != 'win32': | 139 if sys.platform != 'win32': |
128 diff >>= 8 | 140 diff >>= 8 |
129 if diff == 0: # Check exit code. | 141 if diff == 0: # Check exit code. |
130 print '%s: no change' % filename | 142 print '%s: no change' % filename |
131 return False | 143 return False |
132 | 144 |
133 return (not should_confirm or YesNo('Use new file (y/N)?')) | 145 return (not should_confirm or YesNo('Use new file (y/N)?')) |
134 | 146 |
135 FixFileWithConfirmFunction(filename, ConfirmFunction, perform_safety_checks) | 147 FixFileWithConfirmFunction(filename, ConfirmFunction, perform_safety_checks, |
148 for_blink) | |
136 | 149 |
137 def IsUnsafeToReorderHeaders(filename): | 150 def IsUnsafeToReorderHeaders(filename): |
138 # *_message_generator.cc is almost certainly a file that generates IPC | 151 # *_message_generator.cc is almost certainly a file that generates IPC |
139 # definitions. Changes in include order in these files can result in them not | 152 # definitions. Changes in include order in these files can result in them not |
140 # building correctly. | 153 # building correctly. |
141 if filename.find("message_generator.cc") != -1: | 154 if filename.find("message_generator.cc") != -1: |
142 return True | 155 return True |
143 return False | 156 return False |
144 | 157 |
145 def main(): | 158 def main(): |
146 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...') | 159 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...') |
147 parser.add_option('-f', '--force', action='store_false', default=True, | 160 parser.add_option('-f', '--force', action='store_false', default=True, |
148 dest='should_confirm', | 161 dest='should_confirm', |
149 help='Turn off confirmation prompt.') | 162 help='Turn off confirmation prompt.') |
150 parser.add_option('--no_safety_checks', | 163 parser.add_option('--no_safety_checks', |
151 action='store_false', default=True, | 164 action='store_false', default=True, |
152 dest='perform_safety_checks', | 165 dest='perform_safety_checks', |
153 help='Do not perform the safety checks via which this ' | 166 help='Do not perform the safety checks via which this ' |
154 'script refuses to operate on files for which it thinks ' | 167 'script refuses to operate on files for which it thinks ' |
155 'the include ordering is semantically significant.') | 168 'the include ordering is semantically significant.') |
169 parser.add_option('--for_blink', action='store_true', default=False, | |
170 dest='for_blink', help='Whether the blink header sorting ' | |
171 'rules should be applied.') | |
156 opts, filenames = parser.parse_args() | 172 opts, filenames = parser.parse_args() |
157 | 173 |
158 if len(filenames) < 1: | 174 if len(filenames) < 1: |
159 parser.print_help() | 175 parser.print_help() |
160 return 1 | 176 return 1 |
161 | 177 |
162 for filename in filenames: | 178 for filename in filenames: |
163 DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks) | 179 DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks, |
180 opts.for_blink) | |
164 | 181 |
165 | 182 |
166 if __name__ == '__main__': | 183 if __name__ == '__main__': |
167 sys.exit(main()) | 184 sys.exit(main()) |
OLD | NEW |