~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Neil Martinsen-Burrell
  • Date: 2009-12-11 13:44:05 UTC
  • mto: This revision was merged to the branch mainline in revision 4916.
  • Revision ID: nmb@wartburg.edu-20091211134405-36njvdrqhco95s79
switch should use directory services when creating a branch

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
43
42
import subprocess
44
43
import tempfile
45
44
from tempfile import (
72
71
from bzrlib import symbol_versioning
73
72
 
74
73
 
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
 
 
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.
1353
1343
 
1354
1344
    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)
1372
1345
    """
1373
1346
 
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.
1383
1356
        return None
1384
1357
 
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):
 
1358
    if sys.platform == 'win32':
 
1359
        return win32utils.get_console_size(defaultx=None)[0]
 
1360
 
1407
1361
    try:
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):
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)
 
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
1443
1378
 
1444
1379
 
1445
1380
def supports_executable():
2087
2022
    if use_cache:
2088
2023
        _cached_concurrency = concurrency
2089
2024
    return concurrency
2090
 
 
2091
 
 
2092
 
class UnicodeOrBytesToBytesWriter(codecs.StreamWriter):
2093
 
    """A stream writer that doesn't decode str arguments."""
2094
 
 
2095
 
    def __init__(self, encode, stream, errors='strict'):
2096
 
        codecs.StreamWriter.__init__(self, stream, errors)
2097
 
        self.encode = encode
2098
 
 
2099
 
    def write(self, object):
2100
 
        if type(object) is str:
2101
 
            self.stream.write(object)
2102
 
        else:
2103
 
            data, _ = self.encode(object, self.errors)
2104
 
            self.stream.write(data)