Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: tools/sort-headers.py

Issue 1841863002: Update monet. (Closed) Base URL: https://github.com/domokit/monet.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/run_hooks.py ('k') | tools/sort_sources.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
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.
5
6 """Given a filename as an argument, sort the #include/#imports in that file.
7
8 Shows a diff and prompts for confirmation before doing the deed.
9 Works great with tools/git/for-all-touched-files.py.
10 """
11
12 import optparse
13 import os
14 import sys
15
16
17 def 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/
22 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)
36 print ch
37 return ch in ('Y', 'y')
38
39
40 def IncludeCompareKey(line):
41 """Sorting comparator key used for comparing two #include lines.
42 Returns the filename without the #include/#import/import prefix.
43 """
44 for prefix in ('#include ', '#import ', 'import '):
45 if line.startswith(prefix):
46 line = line[len(prefix):]
47 break
48
49 # In sky, config.h must always be first to defined HAVE, USE, etc.
50 if line.startswith('"sky/engine/config.h"'):
51 return '0'
52
53 # The win32 api has all sorts of implicit include order dependencies :-/
54 # Give a few headers special sort keys that make sure they appear before all
55 # other headers.
56 if line.startswith('<windows.h>'): # Must be before e.g. shellapi.h
57 return '0'
58 if line.startswith('<atlbase.h>'): # Must be before atlapp.h.
59 return '1' + line
60 if line.startswith('<ole2.h>'): # Must be before e.g. intshcut.h
61 return '1' + line
62 if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h
63 return '1' + line
64
65 # C++ system headers should come after C system headers.
66 if line.startswith('<'):
67 if line.find('.h>') != -1:
68 return '2' + line.lower()
69 else:
70 return '3' + line.lower()
71
72 return '4' + line
73
74
75 def IsInclude(line):
76 """Returns True if the line is an #include/#import/import line."""
77 return any([line.startswith('#include '), line.startswith('#import '),
78 line.startswith('import ')])
79
80
81 def SortHeader(infile, outfile):
82 """Sorts the headers in infile, writing the sorted file to outfile."""
83 for line in infile:
84 if IsInclude(line):
85 headerblock = []
86 while IsInclude(line):
87 infile_ended_on_include_line = False
88 headerblock.append(line)
89 # Ensure we don't die due to trying to read beyond the end of the file.
90 try:
91 line = infile.next()
92 except StopIteration:
93 infile_ended_on_include_line = True
94 break
95 for header in sorted(headerblock, key=IncludeCompareKey):
96 outfile.write(header)
97 if infile_ended_on_include_line:
98 # We already wrote the last line above; exit to ensure it isn't written
99 # again.
100 return
101 # Intentionally fall through, to write the line that caused
102 # the above while loop to exit.
103 outfile.write(line)
104
105
106 def FixFileWithConfirmFunction(filename, confirm_function,
107 perform_safety_checks):
108 """Creates a fixed version of the file, invokes |confirm_function|
109 to decide whether to use the new file, and cleans up.
110
111 |confirm_function| takes two parameters, the original filename and
112 the fixed-up filename, and returns True to use the fixed-up file,
113 false to not use it.
114
115 If |perform_safety_checks| is True, then the function checks whether it is
116 unsafe to reorder headers in this file and skips the reorder with a warning
117 message in that case.
118 """
119 if perform_safety_checks and IsUnsafeToReorderHeaders(filename):
120 print ('Not reordering headers in %s as the script thinks that the '
121 'order of headers in this file is semantically significant.'
122 % (filename))
123 return
124 fixfilename = filename + '.new'
125 infile = open(filename, 'rb')
126 outfile = open(fixfilename, 'wb')
127 SortHeader(infile, outfile)
128 infile.close()
129 outfile.close() # Important so the below diff gets the updated contents.
130
131 try:
132 if confirm_function(filename, fixfilename):
133 if sys.platform == 'win32':
134 os.unlink(filename)
135 os.rename(fixfilename, filename)
136 finally:
137 try:
138 os.remove(fixfilename)
139 except OSError:
140 # If the file isn't there, we don't care.
141 pass
142
143
144 def DiffAndConfirm(filename, should_confirm, perform_safety_checks):
145 """Shows a diff of what the tool would change the file named
146 filename to. Shows a confirmation prompt if should_confirm is true.
147 Saves the resulting file if should_confirm is false or the user
148 answers Y to the confirmation prompt.
149 """
150 def ConfirmFunction(filename, fixfilename):
151 diff = os.system('diff -u %s %s' % (filename, fixfilename))
152 if sys.platform != 'win32':
153 diff >>= 8
154 if diff == 0: # Check exit code.
155 print '%s: no change' % filename
156 return False
157
158 return (not should_confirm or YesNo('Use new file (y/N)?'))
159
160 FixFileWithConfirmFunction(filename, ConfirmFunction, perform_safety_checks)
161
162 def IsUnsafeToReorderHeaders(filename):
163 # *_message_generator.cc is almost certainly a file that generates IPC
164 # definitions. Changes in include order in these files can result in them not
165 # building correctly.
166 if filename.find("message_generator.cc") != -1:
167 return True
168 return False
169
170 def main():
171 parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
172 parser.add_option('-f', '--force', action='store_false', default=True,
173 dest='should_confirm',
174 help='Turn off confirmation prompt.')
175 parser.add_option('--no_safety_checks',
176 action='store_false', default=True,
177 dest='perform_safety_checks',
178 help='Do not perform the safety checks via which this '
179 'script refuses to operate on files for which it thinks '
180 'the include ordering is semantically significant.')
181 opts, filenames = parser.parse_args()
182
183 if len(filenames) < 1:
184 parser.print_help()
185 return 1
186
187 for filename in filenames:
188 DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks)
189
190
191 if __name__ == '__main__':
192 sys.exit(main())
OLDNEW
« no previous file with comments | « tools/run_hooks.py ('k') | tools/sort_sources.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698