234
236
# source and target may be aliases of each other (e.g. on a
235
237
# case-insensitive filesystem), so we may have accidentally renamed
236
238
# source by when we tried to rename target
237
if not (file_existed and e.errno in (None, errno.ENOENT)):
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
241
247
# If the file used to exist, rename it back into place
706
716
date_str = time.strftime(date_fmt, tt)
707
717
return date_str + offset_str
720
# Cache of formatted offset strings
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.
728
This routine may be faster then format_date.
730
:param t: Seconds since the epoch.
731
:param offset: Timezone offset in seconds east of utc.
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
709
745
def format_local_date(t, offset=0, timezone='original', date_fmt=None,
710
746
show_offset=True):
711
747
"""Return an unicode date string formatted according to the current locale.
921
_extension_load_failures = []
924
def failed_to_load_extension(exception):
925
"""Handle failing to load a binary extension.
927
This should be called from the ImportError block guarding the attempt to
928
import the native extension. If this function returns, the pure-Python
929
implementation should be loaded instead::
932
>>> import bzrlib._fictional_extension_pyx
933
>>> except ImportError, e:
934
>>> bzrlib.osutils.failed_to_load_extension(e)
935
>>> import bzrlib._fictional_extension_py
937
# NB: This docstring is just an example, not a doctest, because doctest
938
# currently can't cope with the use of lazy imports in this namespace --
941
# This currently doesn't report the failure at the time it occurs, because
942
# they tend to happen very early in startup when we can't check config
943
# files etc, and also we want to report all failures but not spam the user
945
from bzrlib import trace
946
exception_str = str(exception)
947
if exception_str not in _extension_load_failures:
948
trace.mutter("failed to load compiled extension: %s" % exception_str)
949
_extension_load_failures.append(exception_str)
952
def report_extension_load_failures():
953
if not _extension_load_failures:
955
from bzrlib.config import GlobalConfig
956
if GlobalConfig().get_user_option_as_bool('ignore_missing_extensions'):
958
# the warnings framework should by default show this only once
959
from bzrlib.trace import warning
961
"bzr: warning: some compiled extensions could not be loaded; "
962
"see <https://answers.launchpad.net/bzr/+faq/703>")
963
# we no longer show the specific missing extensions here, because it makes
964
# the message too long and scary - see
965
# https://bugs.launchpad.net/bzr/+bug/430529
885
969
from bzrlib._chunks_to_lines_pyx import chunks_to_lines
970
except ImportError, e:
971
failed_to_load_extension(e)
887
972
from bzrlib._chunks_to_lines_py import chunks_to_lines
1245
1330
normalized_filename = _inaccessible_normalized_filename
1333
default_terminal_width = 80
1334
"""The default terminal width for ttys.
1336
This is defined so that higher levels can share a common fallback value when
1337
terminal_width() returns None.
1248
1341
def terminal_width():
1249
"""Return estimated terminal width."""
1250
if sys.platform == 'win32':
1251
return win32utils.get_console_size()[0]
1342
"""Return terminal width.
1344
None is returned if the width can't established precisely.
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,
1351
From there, we need to query the OS to get the size of the controlling
1355
- get termios.TIOCGWINSZ
1356
- if an error occurs or a negative value is obtained, returns None
1360
- win32utils.get_console_size() decides,
1361
- returns None on error (provided default value)
1364
# If BZR_COLUMNS is set, take it, user is always right
1366
return int(os.environ['BZR_COLUMNS'])
1367
except (KeyError, ValueError):
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.
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))
1379
return int(os.environ['COLUMNS'])
1380
except (KeyError, ValueError):
1383
width, height = _terminal_size(None, None)
1385
# Consider invalid values as meaning no width
1391
def _win32_terminal_size(width, height):
1392
width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
1393
return width, height
1396
def _ioctl_terminal_size(width, height):
1254
1398
import struct, fcntl, termios
1255
1399
s = struct.pack('HHHH', 0, 0, 0, 0)
1256
1400
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1257
width = struct.unpack('HHHH', x)[1]
1401
height, width = struct.unpack('HHHH', x)[0:2]
1402
except (IOError, AttributeError):
1262
width = int(os.environ['COLUMNS'])
1404
return width, height
1406
_terminal_size = None
1407
"""Returns the terminal size as (width, height).
1409
:param width: Default value for width.
1410
:param height: Default value for height.
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.
1415
if sys.platform == 'win32':
1416
_terminal_size = _win32_terminal_size
1418
_terminal_size = _ioctl_terminal_size
1271
1421
def supports_executable():
1893
2046
anything goes wrong.
1895
2048
global _cached_local_concurrency
1896
2050
if _cached_local_concurrency is not None and use_cache:
1897
2051
return _cached_local_concurrency
1900
concurrency = _local_concurrency()
1901
except (OSError, IOError):
2053
concurrency = os.environ.get('BZR_CONCURRENCY', None)
2054
if concurrency is None:
2056
concurrency = _local_concurrency()
2057
except (OSError, IOError):
1904
2060
concurrency = int(concurrency)
1905
2061
except (TypeError, ValueError):