/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

merge bzr.dev into cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
224
224
    else:
225
225
        file_existed = True
226
226
 
 
227
    failure_exc = None
227
228
    success = False
228
229
    try:
229
230
        try:
235
236
            # source and target may be aliases of each other (e.g. on a
236
237
            # case-insensitive filesystem), so we may have accidentally renamed
237
238
            # source by when we tried to rename target
238
 
            if not (file_existed and e.errno in (None, errno.ENOENT)):
239
 
                raise
 
239
            failure_exc = sys.exc_info()
 
240
            if (file_existed and e.errno in (None, errno.ENOENT)
 
241
                and old.lower() == new.lower()):
 
242
                # source and target are the same file on a case-insensitive
 
243
                # filesystem, so we don't generate an exception
 
244
                failure_exc = None
240
245
    finally:
241
246
        if file_existed:
242
247
            # If the file used to exist, rename it back into place
245
250
                unlink_func(tmp_name)
246
251
            else:
247
252
                rename_func(tmp_name, new)
 
253
    if failure_exc is not None:
 
254
        raise failure_exc[0], failure_exc[1], failure_exc[2]
248
255
 
249
256
 
250
257
# In Python 2.4.2 and older, os.path.abspath and os.path.realpath
688
695
    return offset.days * 86400 + offset.seconds
689
696
 
690
697
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
 
698
_default_format_by_weekday_num = [wd + " %Y-%m-%d %H:%M:%S" for wd in weekdays]
 
699
 
691
700
 
692
701
def format_date(t, offset=0, timezone='original', date_fmt=None,
693
702
                show_offset=True):
707
716
    date_str = time.strftime(date_fmt, tt)
708
717
    return date_str + offset_str
709
718
 
 
719
 
 
720
# Cache of formatted offset strings
 
721
_offset_cache = {}
 
722
 
 
723
 
 
724
def format_date_with_offset_in_original_timezone(t, offset=0,
 
725
    _cache=_offset_cache):
 
726
    """Return a formatted date string in the original timezone.
 
727
 
 
728
    This routine may be faster then format_date.
 
729
 
 
730
    :param t: Seconds since the epoch.
 
731
    :param offset: Timezone offset in seconds east of utc.
 
732
    """
 
733
    if offset is None:
 
734
        offset = 0
 
735
    tt = time.gmtime(t + offset)
 
736
    date_fmt = _default_format_by_weekday_num[tt[6]]
 
737
    date_str = time.strftime(date_fmt, tt)
 
738
    offset_str = _cache.get(offset, None)
 
739
    if offset_str is None:
 
740
        offset_str = ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)
 
741
        _cache[offset] = offset_str
 
742
    return date_str + offset_str
 
743
 
 
744
 
710
745
def format_local_date(t, offset=0, timezone='original', date_fmt=None,
711
746
                      show_offset=True):
712
747
    """Return an unicode date string formatted according to the current locale.
726
761
        date_str = date_str.decode(get_user_encoding(), 'replace')
727
762
    return date_str + offset_str
728
763
 
 
764
 
729
765
def _format_date(t, offset, timezone, date_fmt, show_offset):
730
766
    if timezone == 'utc':
731
767
        tt = time.gmtime(t)
1294
1330
    normalized_filename = _inaccessible_normalized_filename
1295
1331
 
1296
1332
 
 
1333
default_terminal_width = 80
 
1334
"""The default terminal width for ttys.
 
1335
 
 
1336
This is defined so that higher levels can share a common fallback value when
 
1337
terminal_width() returns None.
 
1338
"""
 
1339
 
 
1340
 
1297
1341
def terminal_width():
1298
 
    """Return estimated terminal width."""
1299
 
    if sys.platform == 'win32':
1300
 
        return win32utils.get_console_size()[0]
1301
 
    width = 0
 
1342
    """Return terminal width.
 
1343
 
 
1344
    None is returned if the width can't established precisely.
 
1345
 
 
1346
    The rules are:
 
1347
    - if BZR_COLUMNS is set, returns its value
 
1348
    - if there is no controlling terminal, returns None
 
1349
    - if COLUMNS is set, returns its value,
 
1350
 
 
1351
    From there, we need to query the OS to get the size of the controlling
 
1352
    terminal.
 
1353
 
 
1354
    Unices:
 
1355
    - get termios.TIOCGWINSZ
 
1356
    - if an error occurs or a negative value is obtained, returns None
 
1357
 
 
1358
    Windows:
 
1359
    
 
1360
    - win32utils.get_console_size() decides,
 
1361
    - returns None on error (provided default value)
 
1362
    """
 
1363
 
 
1364
    # If BZR_COLUMNS is set, take it, user is always right
 
1365
    try:
 
1366
        return int(os.environ['BZR_COLUMNS'])
 
1367
    except (KeyError, ValueError):
 
1368
        pass
 
1369
 
 
1370
    isatty = getattr(sys.stdout, 'isatty', None)
 
1371
    if  isatty is None or not isatty():
 
1372
        # Don't guess, setting BZR_COLUMNS is the recommended way to override.
 
1373
        return None
 
1374
 
 
1375
    # If COLUMNS is set, take it, the terminal knows better (even inside a
 
1376
    # given terminal, the application can decide to set COLUMNS to a lower
 
1377
    # value (splitted screen) or a bigger value (scroll bars))
 
1378
    try:
 
1379
        return int(os.environ['COLUMNS'])
 
1380
    except (KeyError, ValueError):
 
1381
        pass
 
1382
 
 
1383
    width, height = _terminal_size(None, None)
 
1384
    if width <= 0:
 
1385
        # Consider invalid values as meaning no width
 
1386
        return None
 
1387
 
 
1388
    return width
 
1389
 
 
1390
 
 
1391
def _win32_terminal_size(width, height):
 
1392
    width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
 
1393
    return width, height
 
1394
 
 
1395
 
 
1396
def _ioctl_terminal_size(width, height):
1302
1397
    try:
1303
1398
        import struct, fcntl, termios
1304
1399
        s = struct.pack('HHHH', 0, 0, 0, 0)
1305
1400
        x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1306
 
        width = struct.unpack('HHHH', x)[1]
1307
 
    except IOError:
 
1401
        height, width = struct.unpack('HHHH', x)[0:2]
 
1402
    except (IOError, AttributeError):
1308
1403
        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
 
1404
    return width, height
 
1405
 
 
1406
_terminal_size = None
 
1407
"""Returns the terminal size as (width, height).
 
1408
 
 
1409
:param width: Default value for width.
 
1410
:param height: Default value for height.
 
1411
 
 
1412
This is defined specifically for each OS and query the size of the controlling
 
1413
terminal. If any error occurs, the provided default values should be returned.
 
1414
"""
 
1415
if sys.platform == 'win32':
 
1416
    _terminal_size = _win32_terminal_size
 
1417
else:
 
1418
    _terminal_size = _ioctl_terminal_size
1318
1419
 
1319
1420
 
1320
1421
def supports_executable():
1945
2046
    anything goes wrong.
1946
2047
    """
1947
2048
    global _cached_local_concurrency
 
2049
 
1948
2050
    if _cached_local_concurrency is not None and use_cache:
1949
2051
        return _cached_local_concurrency
1950
2052
 
1951
 
    try:
1952
 
        concurrency = _local_concurrency()
1953
 
    except (OSError, IOError):
1954
 
        concurrency = None
 
2053
    concurrency = os.environ.get('BZR_CONCURRENCY', None)
 
2054
    if concurrency is None:
 
2055
        try:
 
2056
            concurrency = _local_concurrency()
 
2057
        except (OSError, IOError):
 
2058
            pass
1955
2059
    try:
1956
2060
        concurrency = int(concurrency)
1957
2061
    except (TypeError, ValueError):