72
71
from bzrlib import symbol_versioning
75
# Cross platform wall-clock time functionality with decent resolution.
76
# On Linux ``time.clock`` returns only CPU time. On Windows, ``time.time()``
77
# only has a resolution of ~15ms. Note that ``time.clock()`` is not
78
# synchronized with ``time.time()``, this is only meant to be used to find
79
# delta times by subtracting from another call to this function.
80
timer_func = time.time
81
if sys.platform == 'win32':
82
timer_func = time.clock
84
74
# On win32, O_BINARY is used to indicate the file should
85
75
# be opened in binary mode, rather than text mode.
86
76
# On other platforms, O_BINARY doesn't exist, because
1352
1342
"""Return terminal width.
1354
1344
None is returned if the width can't established precisely.
1357
- if BZR_COLUMNS is set, returns its value
1358
- if there is no controlling terminal, returns None
1359
- if COLUMNS is set, returns its value,
1361
From there, we need to query the OS to get the size of the controlling
1365
- get termios.TIOCGWINSZ
1366
- if an error occurs or a negative value is obtained, returns None
1370
- win32utils.get_console_size() decides,
1371
- returns None on error (provided default value)
1374
1347
# If BZR_COLUMNS is set, take it, user is always right
1382
1355
# Don't guess, setting BZR_COLUMNS is the recommended way to override.
1385
# If COLUMNS is set, take it, the terminal knows better (even inside a
1386
# given terminal, the application can decide to set COLUMNS to a lower
1387
# value (splitted screen) or a bigger value (scroll bars))
1389
return int(os.environ['COLUMNS'])
1390
except (KeyError, ValueError):
1393
width, height = _terminal_size(None, None)
1395
# Consider invalid values as meaning no width
1401
def _win32_terminal_size(width, height):
1402
width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
1403
return width, height
1406
def _ioctl_terminal_size(width, height):
1358
if sys.platform == 'win32':
1359
return win32utils.get_console_size(defaultx=None)[0]
1408
1362
import struct, fcntl, termios
1409
1363
s = struct.pack('HHHH', 0, 0, 0, 0)
1410
1364
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1411
height, width = struct.unpack('HHHH', x)[0:2]
1365
width = struct.unpack('HHHH', x)[1]
1412
1366
except (IOError, AttributeError):
1414
return width, height
1416
_terminal_size = None
1417
"""Returns the terminal size as (width, height).
1419
:param width: Default value for width.
1420
:param height: Default value for height.
1422
This is defined specifically for each OS and query the size of the controlling
1423
terminal. If any error occurs, the provided default values should be returned.
1425
if sys.platform == 'win32':
1426
_terminal_size = _win32_terminal_size
1428
_terminal_size = _ioctl_terminal_size
1431
def _terminal_size_changed(signum, frame):
1432
"""Set COLUMNS upon receiving a SIGnal for WINdow size CHange."""
1433
width, height = _terminal_size(None, None)
1434
if width is not None:
1435
os.environ['COLUMNS'] = str(width)
1437
if sys.platform == 'win32':
1438
# Martin (gz) mentioned WINDOW_BUFFER_SIZE_RECORD from ReadConsoleInput but
1439
# I've no idea how to plug that in the current design -- vila 20091216
1442
signal.signal(signal.SIGWINCH, _terminal_size_changed)
1367
# If COLUMNS is set, take it
1369
return int(os.environ['COLUMNS'])
1370
except (KeyError, ValueError):
1374
# Consider invalid values as meaning no width
1445
1380
def supports_executable():
2088
2023
_cached_concurrency = concurrency
2089
2024
return concurrency
2092
class UnicodeOrBytesToBytesWriter(codecs.StreamWriter):
2093
"""A stream writer that doesn't decode str arguments."""
2095
def __init__(self, encode, stream, errors='strict'):
2096
codecs.StreamWriter.__init__(self, stream, errors)
2097
self.encode = encode
2099
def write(self, object):
2100
if type(object) is str:
2101
self.stream.write(object)
2103
data, _ = self.encode(object, self.errors)
2104
self.stream.write(data)