/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

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
from bzrlib import symbol_versioning
72
72
 
73
73
 
 
74
# Cross platform wall-clock time functionality with decent resolution.
 
75
# On Linux ``time.clock`` returns only CPU time. On Windows, ``time.time()``
 
76
# only has a resolution of ~15ms. Note that ``time.clock()`` is not
 
77
# synchronized with ``time.time()``, this is only meant to be used to find
 
78
# delta times by subtracting from another call to this function.
 
79
timer_func = time.time
 
80
if sys.platform == 'win32':
 
81
    timer_func = time.clock
 
82
 
74
83
# On win32, O_BINARY is used to indicate the file should
75
84
# be opened in binary mode, rather than text mode.
76
85
# On other platforms, O_BINARY doesn't exist, because
224
233
    else:
225
234
        file_existed = True
226
235
 
 
236
    failure_exc = None
227
237
    success = False
228
238
    try:
229
239
        try:
235
245
            # source and target may be aliases of each other (e.g. on a
236
246
            # case-insensitive filesystem), so we may have accidentally renamed
237
247
            # source by when we tried to rename target
238
 
            if not (file_existed and e.errno in (None, errno.ENOENT)):
239
 
                raise
 
248
            failure_exc = sys.exc_info()
 
249
            if (file_existed and e.errno in (None, errno.ENOENT)
 
250
                and old.lower() == new.lower()):
 
251
                # source and target are the same file on a case-insensitive
 
252
                # filesystem, so we don't generate an exception
 
253
                failure_exc = None
240
254
    finally:
241
255
        if file_existed:
242
256
            # If the file used to exist, rename it back into place
245
259
                unlink_func(tmp_name)
246
260
            else:
247
261
                rename_func(tmp_name, new)
 
262
    if failure_exc is not None:
 
263
        raise failure_exc[0], failure_exc[1], failure_exc[2]
248
264
 
249
265
 
250
266
# In Python 2.4.2 and older, os.path.abspath and os.path.realpath
688
704
    return offset.days * 86400 + offset.seconds
689
705
 
690
706
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
 
707
_default_format_by_weekday_num = [wd + " %Y-%m-%d %H:%M:%S" for wd in weekdays]
 
708
 
691
709
 
692
710
def format_date(t, offset=0, timezone='original', date_fmt=None,
693
711
                show_offset=True):
707
725
    date_str = time.strftime(date_fmt, tt)
708
726
    return date_str + offset_str
709
727
 
 
728
 
 
729
# Cache of formatted offset strings
 
730
_offset_cache = {}
 
731
 
 
732
 
 
733
def format_date_with_offset_in_original_timezone(t, offset=0,
 
734
    _cache=_offset_cache):
 
735
    """Return a formatted date string in the original timezone.
 
736
 
 
737
    This routine may be faster then format_date.
 
738
 
 
739
    :param t: Seconds since the epoch.
 
740
    :param offset: Timezone offset in seconds east of utc.
 
741
    """
 
742
    if offset is None:
 
743
        offset = 0
 
744
    tt = time.gmtime(t + offset)
 
745
    date_fmt = _default_format_by_weekday_num[tt[6]]
 
746
    date_str = time.strftime(date_fmt, tt)
 
747
    offset_str = _cache.get(offset, None)
 
748
    if offset_str is None:
 
749
        offset_str = ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)
 
750
        _cache[offset] = offset_str
 
751
    return date_str + offset_str
 
752
 
 
753
 
710
754
def format_local_date(t, offset=0, timezone='original', date_fmt=None,
711
755
                      show_offset=True):
712
756
    """Return an unicode date string formatted according to the current locale.
726
770
        date_str = date_str.decode(get_user_encoding(), 'replace')
727
771
    return date_str + offset_str
728
772
 
 
773
 
729
774
def _format_date(t, offset, timezone, date_fmt, show_offset):
730
775
    if timezone == 'utc':
731
776
        tt = time.gmtime(t)
1294
1339
    normalized_filename = _inaccessible_normalized_filename
1295
1340
 
1296
1341
 
 
1342
default_terminal_width = 80
 
1343
"""The default terminal width for ttys.
 
1344
 
 
1345
This is defined so that higher levels can share a common fallback value when
 
1346
terminal_width() returns None.
 
1347
"""
 
1348
 
 
1349
 
1297
1350
def terminal_width():
1298
 
    """Return estimated terminal width."""
1299
 
    if sys.platform == 'win32':
1300
 
        return win32utils.get_console_size()[0]
1301
 
    width = 0
 
1351
    """Return terminal width.
 
1352
 
 
1353
    None is returned if the width can't established precisely.
 
1354
 
 
1355
    The rules are:
 
1356
    - if BZR_COLUMNS is set, returns its value
 
1357
    - if there is no controlling terminal, returns None
 
1358
    - if COLUMNS is set, returns its value,
 
1359
 
 
1360
    From there, we need to query the OS to get the size of the controlling
 
1361
    terminal.
 
1362
 
 
1363
    Unices:
 
1364
    - get termios.TIOCGWINSZ
 
1365
    - if an error occurs or a negative value is obtained, returns None
 
1366
 
 
1367
    Windows:
 
1368
    
 
1369
    - win32utils.get_console_size() decides,
 
1370
    - returns None on error (provided default value)
 
1371
    """
 
1372
 
 
1373
    # If BZR_COLUMNS is set, take it, user is always right
 
1374
    try:
 
1375
        return int(os.environ['BZR_COLUMNS'])
 
1376
    except (KeyError, ValueError):
 
1377
        pass
 
1378
 
 
1379
    isatty = getattr(sys.stdout, 'isatty', None)
 
1380
    if  isatty is None or not isatty():
 
1381
        # Don't guess, setting BZR_COLUMNS is the recommended way to override.
 
1382
        return None
 
1383
 
 
1384
    # If COLUMNS is set, take it, the terminal knows better (even inside a
 
1385
    # given terminal, the application can decide to set COLUMNS to a lower
 
1386
    # value (splitted screen) or a bigger value (scroll bars))
 
1387
    try:
 
1388
        return int(os.environ['COLUMNS'])
 
1389
    except (KeyError, ValueError):
 
1390
        pass
 
1391
 
 
1392
    width, height = _terminal_size(None, None)
 
1393
    if width <= 0:
 
1394
        # Consider invalid values as meaning no width
 
1395
        return None
 
1396
 
 
1397
    return width
 
1398
 
 
1399
 
 
1400
def _win32_terminal_size(width, height):
 
1401
    width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
 
1402
    return width, height
 
1403
 
 
1404
 
 
1405
def _ioctl_terminal_size(width, height):
1302
1406
    try:
1303
1407
        import struct, fcntl, termios
1304
1408
        s = struct.pack('HHHH', 0, 0, 0, 0)
1305
1409
        x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1306
 
        width = struct.unpack('HHHH', x)[1]
1307
 
    except IOError:
 
1410
        height, width = struct.unpack('HHHH', x)[0:2]
 
1411
    except (IOError, AttributeError):
1308
1412
        pass
1309
 
    if width <= 0:
1310
 
        try:
1311
 
            width = int(os.environ['COLUMNS'])
1312
 
        except:
1313
 
            pass
1314
 
    if width <= 0:
1315
 
        width = 80
1316
 
 
1317
 
    return width
 
1413
    return width, height
 
1414
 
 
1415
_terminal_size = None
 
1416
"""Returns the terminal size as (width, height).
 
1417
 
 
1418
:param width: Default value for width.
 
1419
:param height: Default value for height.
 
1420
 
 
1421
This is defined specifically for each OS and query the size of the controlling
 
1422
terminal. If any error occurs, the provided default values should be returned.
 
1423
"""
 
1424
if sys.platform == 'win32':
 
1425
    _terminal_size = _win32_terminal_size
 
1426
else:
 
1427
    _terminal_size = _ioctl_terminal_size
1318
1428
 
1319
1429
 
1320
1430
def supports_executable():
1945
2055
    anything goes wrong.
1946
2056
    """
1947
2057
    global _cached_local_concurrency
 
2058
 
1948
2059
    if _cached_local_concurrency is not None and use_cache:
1949
2060
        return _cached_local_concurrency
1950
2061
 
1951
 
    try:
1952
 
        concurrency = _local_concurrency()
1953
 
    except (OSError, IOError):
1954
 
        concurrency = None
 
2062
    concurrency = os.environ.get('BZR_CONCURRENCY', None)
 
2063
    if concurrency is None:
 
2064
        try:
 
2065
            concurrency = _local_concurrency()
 
2066
        except (OSError, IOError):
 
2067
            pass
1955
2068
    try:
1956
2069
        concurrency = int(concurrency)
1957
2070
    except (TypeError, ValueError):