/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: John Arbash Meinel
  • Date: 2009-12-21 17:24:22 UTC
  • mfrom: (4913 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4916.
  • Revision ID: john@arbash-meinel.com-20091221172422-0zy2v8x3fhcdc8c0
Merge bzr.dev 4913, to put NEWS in its place.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
from shutil import (
40
40
    rmtree,
41
41
    )
 
42
import signal
42
43
import subprocess
43
44
import tempfile
44
45
from tempfile import (
71
72
from bzrlib import symbol_versioning
72
73
 
73
74
 
 
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
 
83
 
74
84
# On win32, O_BINARY is used to indicate the file should
75
85
# be opened in binary mode, rather than text mode.
76
86
# On other platforms, O_BINARY doesn't exist, because
1342
1352
    """Return terminal width.
1343
1353
 
1344
1354
    None is returned if the width can't established precisely.
 
1355
 
 
1356
    The rules are:
 
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,
 
1360
 
 
1361
    From there, we need to query the OS to get the size of the controlling
 
1362
    terminal.
 
1363
 
 
1364
    Unices:
 
1365
    - get termios.TIOCGWINSZ
 
1366
    - if an error occurs or a negative value is obtained, returns None
 
1367
 
 
1368
    Windows:
 
1369
    
 
1370
    - win32utils.get_console_size() decides,
 
1371
    - returns None on error (provided default value)
1345
1372
    """
1346
1373
 
1347
1374
    # If BZR_COLUMNS is set, take it, user is always right
1355
1382
        # Don't guess, setting BZR_COLUMNS is the recommended way to override.
1356
1383
        return None
1357
1384
 
1358
 
    if sys.platform == 'win32':
1359
 
        return win32utils.get_console_size(defaultx=None)[0]
1360
 
 
 
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))
 
1388
    try:
 
1389
        return int(os.environ['COLUMNS'])
 
1390
    except (KeyError, ValueError):
 
1391
        pass
 
1392
 
 
1393
    width, height = _terminal_size(None, None)
 
1394
    if width <= 0:
 
1395
        # Consider invalid values as meaning no width
 
1396
        return None
 
1397
 
 
1398
    return width
 
1399
 
 
1400
 
 
1401
def _win32_terminal_size(width, height):
 
1402
    width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
 
1403
    return width, height
 
1404
 
 
1405
 
 
1406
def _ioctl_terminal_size(width, height):
1361
1407
    try:
1362
1408
        import struct, fcntl, termios
1363
1409
        s = struct.pack('HHHH', 0, 0, 0, 0)
1364
1410
        x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1365
 
        width = struct.unpack('HHHH', x)[1]
 
1411
        height, width = struct.unpack('HHHH', x)[0:2]
1366
1412
    except (IOError, AttributeError):
1367
 
        # If COLUMNS is set, take it
1368
 
        try:
1369
 
            return int(os.environ['COLUMNS'])
1370
 
        except (KeyError, ValueError):
1371
 
            return None
1372
 
 
1373
 
    if width <= 0:
1374
 
        # Consider invalid values as meaning no width
1375
 
        return None
1376
 
 
1377
 
    return width
 
1413
        pass
 
1414
    return width, height
 
1415
 
 
1416
_terminal_size = None
 
1417
"""Returns the terminal size as (width, height).
 
1418
 
 
1419
:param width: Default value for width.
 
1420
:param height: Default value for height.
 
1421
 
 
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.
 
1424
"""
 
1425
if sys.platform == 'win32':
 
1426
    _terminal_size = _win32_terminal_size
 
1427
else:
 
1428
    _terminal_size = _ioctl_terminal_size
 
1429
 
 
1430
 
 
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)
 
1436
 
 
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
 
1440
    pass
 
1441
else:
 
1442
    signal.signal(signal.SIGWINCH, _terminal_size_changed)
1378
1443
 
1379
1444
 
1380
1445
def supports_executable():