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 | 16 from yes_no import YesNo |
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 | 17 |
39 | 18 |
40 def IncludeCompareKey(line): | 19 def IncludeCompareKey(line): |
41 """Sorting comparator key used for comparing two #include lines. | 20 """Sorting comparator key used for comparing two #include lines. |
42 Returns the filename without the #include/#import/import prefix. | 21 Returns the filename without the #include/#import/import prefix. |
43 """ | 22 """ |
44 for prefix in ('#include ', '#import ', 'import '): | 23 for prefix in ('#include ', '#import ', 'import '): |
45 if line.startswith(prefix): | 24 if line.startswith(prefix): |
46 line = line[len(prefix):] | 25 line = line[len(prefix):] |
47 break | 26 break |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 if len(filenames) < 1: | 158 if len(filenames) < 1: |
180 parser.print_help() | 159 parser.print_help() |
181 return 1 | 160 return 1 |
182 | 161 |
183 for filename in filenames: | 162 for filename in filenames: |
184 DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks) | 163 DiffAndConfirm(filename, opts.should_confirm, opts.perform_safety_checks) |
185 | 164 |
186 | 165 |
187 if __name__ == '__main__': | 166 if __name__ == '__main__': |
188 sys.exit(main()) | 167 sys.exit(main()) |
OLD | NEW |