Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2016 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 import os | |
| 7 import sys | |
| 8 from third_party import colorama | |
| 9 | |
| 10 IS_TTY = None | |
| 11 OUT_TYPE = 'unknown' | |
| 12 | |
| 13 def init(): | |
| 14 should_wrap = True | |
| 15 global IS_TTY, OUT_TYPE | |
| 16 IS_TTY = sys.stdout.isatty() | |
| 17 if IS_TTY: | |
| 18 OUT_TYPE = 'console' | |
|
iannucci
2016/04/02 06:08:20
yay, we detected it via the normal mechanism! This
brucedawson
2016/04/02 20:42:50
Can you make this explanation and others comments
iannucci
2016/04/02 23:45:34
Yeah should have done that in the first place :).
| |
| 19 elif sys.platform.startswith('win'): | |
| 20 OUT_TYPE = 'pipe' | |
|
iannucci
2016/04/02 06:08:21
Assume it's a pipe
| |
| 21 | |
| 22 import msvcrt | |
| 23 import ctypes | |
| 24 h = msvcrt.get_osfhandle(sys.stdout.fileno()) | |
|
iannucci
2016/04/02 06:08:20
h is the win32 HANDLE for stdout.
| |
| 25 ftype = ctypes.windll.kernel32.GetFileType(h) | |
| 26 if ftype == 2: # FILE_TYPE_CHAR | |
|
iannucci
2016/04/02 06:08:20
This happens when were running in cmd but via some
| |
| 27 OUT_TYPE = 'console (cmd via msys)' | |
| 28 IS_TTY = True | |
| 29 elif ftype == 3: # FILE_TYPE_PIPE | |
|
iannucci
2016/04/02 06:08:21
This happens if we're running under bash in intera
| |
| 30 OUT_TYPE = 'pipe' | |
| 31 def NT_SUCCESS(status): | |
| 32 if status >= 0 and status <= 0x3FFFFFFF: | |
| 33 return True | |
| 34 if status >= 0x40000000 and status <= 0x7FFFFFFF: | |
| 35 return True | |
| 36 return False | |
| 37 | |
| 38 class UNICODE_STRING(ctypes.Structure): | |
| 39 _fields_ = [('Length', ctypes.c_ushort), | |
| 40 ('MaximumLength', ctypes.c_ushort), | |
| 41 ('Buffer', ctypes.c_wchar_p)] | |
| 42 | |
| 43 class OBJECT_NAME_INFORMATION(ctypes.Structure): | |
| 44 _fields_ = [('Name', UNICODE_STRING), | |
| 45 ('NameBuffer', ctypes.c_wchar_p)] | |
|
iannucci
2016/04/02 06:08:21
These are all defined in the MSDN documentation un
| |
| 46 | |
| 47 # 1: ObjectNameInformation | |
| 48 buf = ctypes.create_string_buffer('\0', 1024) | |
| 49 if NT_SUCCESS(ctypes.windll.ntdll.NtQueryObject(h, 1, buf, len(buf)-2, | |
|
iannucci
2016/04/02 06:08:21
Ask NT what the name of thing attached to the stdo
| |
| 50 None)): | |
| 51 # MSYS console, but not an MSYS pipe | |
| 52 out = OBJECT_NAME_INFORMATION.from_buffer(buf) | |
| 53 name = out.Name.Buffer.split('\\')[-1] | |
| 54 IS_TTY = name.startswith('msys-') and '-pty' in name | |
|
iannucci
2016/04/02 06:08:21
The full name is something like "\Device\NamedPipe
| |
| 55 should_wrap = False | |
|
iannucci
2016/04/02 06:08:21
either way, we don't need to do the weird win32 sy
| |
| 56 if IS_TTY: | |
| 57 OUT_TYPE = 'console (msys)' | |
| 58 else: | |
|
iannucci
2016/04/02 06:08:20
Not on windows, so isatty is reliable.
| |
| 59 OUT_TYPE = 'pipe' | |
| 60 | |
| 61 colorama.init(wrap=should_wrap) | |
| 62 | |
| 63 if __name__ == '__main__': | |
| 64 init() | |
| 65 print 'IS_TTY:', IS_TTY | |
| 66 print 'OUT_TYPE:', OUT_TYPE | |
| OLD | NEW |