/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: Aaron Bentley
  • Date: 2009-12-07 21:46:28 UTC
  • mfrom: (4871 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4873.
  • Revision ID: aaron@aaronbentley.com-20091207214628-yifwgux0fn4x3bo4
Merge bzr.dev into merge-i-lock.

Show diffs side-by-side

added added

removed removed

Lines of Context:
695
695
    return offset.days * 86400 + offset.seconds
696
696
 
697
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
 
698
700
 
699
701
def format_date(t, offset=0, timezone='original', date_fmt=None,
700
702
                show_offset=True):
714
716
    date_str = time.strftime(date_fmt, tt)
715
717
    return date_str + offset_str
716
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
 
717
745
def format_local_date(t, offset=0, timezone='original', date_fmt=None,
718
746
                      show_offset=True):
719
747
    """Return an unicode date string formatted according to the current locale.
733
761
        date_str = date_str.decode(get_user_encoding(), 'replace')
734
762
    return date_str + offset_str
735
763
 
 
764
 
736
765
def _format_date(t, offset, timezone, date_fmt, show_offset):
737
766
    if timezone == 'utc':
738
767
        tt = time.gmtime(t)
1301
1330
    normalized_filename = _inaccessible_normalized_filename
1302
1331
 
1303
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
 
1304
1341
def terminal_width():
1305
 
    """Return estimated terminal width."""
 
1342
    """Return terminal width.
 
1343
 
 
1344
    None is returned if the width can't established precisely.
 
1345
    """
 
1346
 
 
1347
    # If BZR_COLUMNS is set, take it, user is always right
 
1348
    try:
 
1349
        return int(os.environ['BZR_COLUMNS'])
 
1350
    except (KeyError, ValueError):
 
1351
        pass
 
1352
 
 
1353
    isatty = getattr(sys.stdout, 'isatty', None)
 
1354
    if  isatty is None or not isatty():
 
1355
        # Don't guess, setting BZR_COLUMNS is the recommended way to override.
 
1356
        return None
 
1357
 
1306
1358
    if sys.platform == 'win32':
1307
 
        return win32utils.get_console_size()[0]
1308
 
    width = 0
 
1359
        return win32utils.get_console_size(defaultx=None)[0]
 
1360
 
1309
1361
    try:
1310
1362
        import struct, fcntl, termios
1311
1363
        s = struct.pack('HHHH', 0, 0, 0, 0)
1312
1364
        x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1313
1365
        width = struct.unpack('HHHH', x)[1]
1314
 
    except IOError:
1315
 
        pass
1316
 
    if width <= 0:
 
1366
    except (IOError, AttributeError):
 
1367
        # If COLUMNS is set, take it
1317
1368
        try:
1318
 
            width = int(os.environ['COLUMNS'])
1319
 
        except:
1320
 
            pass
 
1369
            return int(os.environ['COLUMNS'])
 
1370
        except (KeyError, ValueError):
 
1371
            return None
 
1372
 
1321
1373
    if width <= 0:
1322
 
        width = 80
 
1374
        # Consider invalid values as meaning no width
 
1375
        return None
1323
1376
 
1324
1377
    return width
1325
1378
 
1952
2005
    anything goes wrong.
1953
2006
    """
1954
2007
    global _cached_local_concurrency
 
2008
 
1955
2009
    if _cached_local_concurrency is not None and use_cache:
1956
2010
        return _cached_local_concurrency
1957
2011
 
1958
 
    try:
1959
 
        concurrency = _local_concurrency()
1960
 
    except (OSError, IOError):
1961
 
        concurrency = None
 
2012
    concurrency = os.environ.get('BZR_CONCURRENCY', None)
 
2013
    if concurrency is None:
 
2014
        try:
 
2015
            concurrency = _local_concurrency()
 
2016
        except (OSError, IOError):
 
2017
            pass
1962
2018
    try:
1963
2019
        concurrency = int(concurrency)
1964
2020
    except (TypeError, ValueError):