[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | 0807909 | 2012-01-05 18:24:38 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 377bf4a | 2011-05-19 20:17:11 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 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. |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 9 | Works great with tools/git/for-all-touched-files.py. |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 10 | """ |
| 11 | |
| 12 | import optparse |
| 13 | import os |
| 14 | import sys |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 15 | |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame] | 16 | |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 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/ |
[email protected] | 4a2a50cb | 2013-06-04 06:27:38 | [diff] [blame] | 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) |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 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. |
[email protected] | 5650b4a4 | 2014-04-09 00:52:15 | [diff] [blame^] | 42 | Returns the filename without the #include/#import/import prefix. |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 43 | """ |
[email protected] | 5650b4a4 | 2014-04-09 00:52:15 | [diff] [blame^] | 44 | for prefix in ('#include ', '#import ', 'import '): |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 45 | if line.startswith(prefix): |
[email protected] | d5c4932 | 2011-05-19 20:08:57 | [diff] [blame] | 46 | line = line[len(prefix):] |
| 47 | break |
[email protected] | 51e3da5 | 2011-05-20 01:53:06 | [diff] [blame] | 48 | |
| 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] | d5d7105 | 2013-02-25 21:01:35 | [diff] [blame] | 54 | if line.startswith('<atlbase.h>'): # Must be before atlapp.h. |
| 55 | return '1' + line |
[email protected] | 51e3da5 | 2011-05-20 01:53:06 | [diff] [blame] | 56 | if line.startswith('<unknwn.h>'): # Must be before e.g. intshcut.h |
[email protected] | d5d7105 | 2013-02-25 21:01:35 | [diff] [blame] | 57 | return '1' + line |
[email protected] | 51e3da5 | 2011-05-20 01:53:06 | [diff] [blame] | 58 | |
[email protected] | 0807909 | 2012-01-05 18:24:38 | [diff] [blame] | 59 | # C++ system headers should come after C system headers. |
| 60 | if line.startswith('<'): |
| 61 | if line.find('.h>') != -1: |
[email protected] | 2661bb9 | 2013-03-13 21:24:40 | [diff] [blame] | 62 | return '2' + line.lower() |
[email protected] | 0807909 | 2012-01-05 18:24:38 | [diff] [blame] | 63 | else: |
[email protected] | 2661bb9 | 2013-03-13 21:24:40 | [diff] [blame] | 64 | return '3' + line.lower() |
[email protected] | 0807909 | 2012-01-05 18:24:38 | [diff] [blame] | 65 | |
| 66 | return '4' + line |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 67 | |
| 68 | |
| 69 | def IsInclude(line): |
[email protected] | 5650b4a4 | 2014-04-09 00:52:15 | [diff] [blame^] | 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 ')]) |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 73 | |
| 74 | |
| 75 | def SortHeader(infile, outfile): |
| 76 | """Sorts the headers in infile, writing the sorted file to outfile.""" |
| 77 | for line in infile: |
| 78 | if IsInclude(line): |
| 79 | headerblock = [] |
| 80 | while IsInclude(line): |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 81 | infile_ended_on_include_line = False |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 82 | headerblock.append(line) |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 83 | # Ensure we don't die due to trying to read beyond the end of the file. |
| 84 | try: |
| 85 | line = infile.next() |
| 86 | except StopIteration: |
| 87 | infile_ended_on_include_line = True |
| 88 | break |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 89 | for header in sorted(headerblock, key=IncludeCompareKey): |
| 90 | outfile.write(header) |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 91 | if infile_ended_on_include_line: |
| 92 | # We already wrote the last line above; exit to ensure it isn't written |
| 93 | # again. |
| 94 | return |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 95 | # Intentionally fall through, to write the line that caused |
| 96 | # the above while loop to exit. |
| 97 | outfile.write(line) |
| 98 | |
| 99 | |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 100 | def FixFileWithConfirmFunction(filename, confirm_function, |
| 101 | perform_safety_checks): |
[email protected] | 1836722 | 2012-11-22 11:28:57 | [diff] [blame] | 102 | """Creates a fixed version of the file, invokes |confirm_function| |
| 103 | to decide whether to use the new file, and cleans up. |
| 104 | |
| 105 | |confirm_function| takes two parameters, the original filename and |
| 106 | the fixed-up filename, and returns True to use the fixed-up file, |
| 107 | false to not use it. |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 108 | |
| 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. |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 112 | """ |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 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.' |
| 116 | % (filename)) |
| 117 | return |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 118 | fixfilename = filename + '.new' |
[email protected] | 4a2a50cb | 2013-06-04 06:27:38 | [diff] [blame] | 119 | infile = open(filename, 'rb') |
| 120 | outfile = open(fixfilename, 'wb') |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 121 | SortHeader(infile, outfile) |
| 122 | infile.close() |
| 123 | outfile.close() # Important so the below diff gets the updated contents. |
| 124 | |
| 125 | try: |
[email protected] | 1836722 | 2012-11-22 11:28:57 | [diff] [blame] | 126 | if confirm_function(filename, fixfilename): |
[email protected] | 4a2a50cb | 2013-06-04 06:27:38 | [diff] [blame] | 127 | if sys.platform == 'win32': |
| 128 | os.unlink(filename) |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 129 | os.rename(fixfilename, filename) |
| 130 | finally: |
| 131 | try: |
| 132 | os.remove(fixfilename) |
| 133 | except OSError: |
| 134 | # If the file isn't there, we don't care. |
| 135 | pass |
| 136 | |
| 137 | |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 138 | def DiffAndConfirm(filename, should_confirm, perform_safety_checks): |
[email protected] | 1836722 | 2012-11-22 11:28:57 | [diff] [blame] | 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. |
| 143 | """ |
| 144 | def ConfirmFunction(filename, fixfilename): |
| 145 | diff = os.system('diff -u %s %s' % (filename, fixfilename)) |
[email protected] | 4a2a50cb | 2013-06-04 06:27:38 | [diff] [blame] | 146 | if sys.platform != 'win32': |
| 147 | diff >>= 8 |
| 148 | if diff == 0: # Check exit code. |
[email protected] | 1836722 | 2012-11-22 11:28:57 | [diff] [blame] | 149 | print '%s: no change' % filename |
| 150 | return False |
| 151 | |
| 152 | return (not should_confirm or YesNo('Use new file (y/N)?')) |
| 153 | |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 154 | FixFileWithConfirmFunction(filename, ConfirmFunction, perform_safety_checks) |
[email protected] | 1836722 | 2012-11-22 11:28:57 | [diff] [blame] | 155 | |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 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: |
| 161 | return True |
| 162 | return False |
[email protected] | 1836722 | 2012-11-22 11:28:57 | [diff] [blame] | 163 | |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 164 | def main(): |
| 165 | parser = optparse.OptionParser(usage='%prog filename1 filename2 ...') |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 166 | parser.add_option('-f', '--force', action='store_false', default=True, |
| 167 | dest='should_confirm', |
| 168 | help='Turn off confirmation prompt.') |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 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.') |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 175 | opts, filenames = parser.parse_args() |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 176 | |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 177 | if len(filenames) < 1: |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 178 | parser.print_help() |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame] | 179 | return 1 |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 180 | |
[email protected] | 10ab0ed5 | 2011-11-01 11:46:52 | [diff] [blame] | 181 | for filename in filenames: |
[email protected] | 1590a8e | 2013-06-18 14:25:58 | [diff] [blame] | 182 | DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks) |
[email protected] | 50e5a3d | 2010-08-26 00:23:26 | [diff] [blame] | 183 | |
| 184 | |
| 185 | if __name__ == '__main__': |
[email protected] | cb155a8 | 2011-11-29 17:25:34 | [diff] [blame] | 186 | sys.exit(main()) |