71
71
from bzrlib import symbol_versioning
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
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
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)):
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
242
256
# If the file used to exist, rename it back into place
707
725
date_str = time.strftime(date_fmt, tt)
708
726
return date_str + offset_str
729
# Cache of formatted offset strings
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.
737
This routine may be faster then format_date.
739
:param t: Seconds since the epoch.
740
:param offset: Timezone offset in seconds east of utc.
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
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.
1294
1339
normalized_filename = _inaccessible_normalized_filename
1342
default_terminal_width = 80
1343
"""The default terminal width for ttys.
1345
This is defined so that higher levels can share a common fallback value when
1346
terminal_width() returns None.
1297
1350
def terminal_width():
1298
"""Return estimated terminal width."""
1299
if sys.platform == 'win32':
1300
return win32utils.get_console_size()[0]
1351
"""Return terminal width.
1353
None is returned if the width can't established precisely.
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,
1360
From there, we need to query the OS to get the size of the controlling
1364
- get termios.TIOCGWINSZ
1365
- if an error occurs or a negative value is obtained, returns None
1369
- win32utils.get_console_size() decides,
1370
- returns None on error (provided default value)
1373
# If BZR_COLUMNS is set, take it, user is always right
1375
return int(os.environ['BZR_COLUMNS'])
1376
except (KeyError, ValueError):
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.
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))
1388
return int(os.environ['COLUMNS'])
1389
except (KeyError, ValueError):
1392
width, height = _terminal_size(None, None)
1394
# Consider invalid values as meaning no width
1400
def _win32_terminal_size(width, height):
1401
width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
1402
return width, height
1405
def _ioctl_terminal_size(width, height):
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]
1410
height, width = struct.unpack('HHHH', x)[0:2]
1411
except (IOError, AttributeError):
1311
width = int(os.environ['COLUMNS'])
1413
return width, height
1415
_terminal_size = None
1416
"""Returns the terminal size as (width, height).
1418
:param width: Default value for width.
1419
:param height: Default value for height.
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.
1424
if sys.platform == 'win32':
1425
_terminal_size = _win32_terminal_size
1427
_terminal_size = _ioctl_terminal_size
1320
1430
def supports_executable():
1945
2055
anything goes wrong.
1947
2057
global _cached_local_concurrency
1948
2059
if _cached_local_concurrency is not None and use_cache:
1949
2060
return _cached_local_concurrency
1952
concurrency = _local_concurrency()
1953
except (OSError, IOError):
2062
concurrency = os.environ.get('BZR_CONCURRENCY', None)
2063
if concurrency is None:
2065
concurrency = _local_concurrency()
2066
except (OSError, IOError):
1956
2069
concurrency = int(concurrency)
1957
2070
except (TypeError, ValueError):