1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005-2011 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
967
967
# they tend to happen very early in startup when we can't check config
968
968
# files etc, and also we want to report all failures but not spam the user
969
969
# with 10 warnings.
970
from bzrlib import trace
971
970
exception_str = str(exception)
972
971
if exception_str not in _extension_load_failures:
973
972
trace.mutter("failed to load compiled extension: %s" % exception_str)
1462
1461
# a similar effect.
1464
1463
# If BZR_COLUMNS is set, take it, user is always right
1464
# Except if they specified 0 in which case, impose no limit here
1466
return int(os.environ['BZR_COLUMNS'])
1466
width = int(os.environ['BZR_COLUMNS'])
1467
1467
except (KeyError, ValueError):
1469
if width is not None:
1470
1475
isatty = getattr(sys.stdout, 'isatty', None)
1471
1476
if isatty is None or not isatty():
1875
1880
s = os.stat(src)
1876
1881
chown(dst, s.st_uid, s.st_gid)
1877
1882
except OSError, e:
1878
trace.warning("Unable to copy ownership from '%s' to '%s': IOError: %s." % (src, dst, e))
1884
'Unable to copy ownership from "%s" to "%s". '
1885
'You may want to set it manually.', src, dst)
1886
trace.log_exception_quietly()
1881
1889
def path_prefix_key(path):
1993
2001
# data at once.
1994
2002
MAX_SOCKET_CHUNK = 64 * 1024
2004
_end_of_stream_errors = [errno.ECONNRESET]
2005
for _eno in ['WSAECONNRESET', 'WSAECONNABORTED']:
2006
_eno = getattr(errno, _eno, None)
2007
if _eno is not None:
2008
_end_of_stream_errors.append(_eno)
1996
2012
def read_bytes_from_socket(sock, report_activity=None,
1997
2013
max_read_size=MAX_SOCKET_CHUNK):
1998
2014
"""Read up to max_read_size of bytes from sock and notify of progress.
2006
2022
bytes = sock.recv(max_read_size)
2007
2023
except socket.error, e:
2008
2024
eno = e.args[0]
2009
if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
2025
if eno in _end_of_stream_errors:
2010
2026
# The connection was closed by the other side. Callers expect
2011
2027
# an empty string to signal end-of-stream.
2065
2081
report_activity(sent, 'write')
2084
def connect_socket(address):
2085
# Slight variation of the socket.create_connection() function (provided by
2086
# python-2.6) that can fail if getaddrinfo returns an empty list. We also
2087
# provide it for previous python versions. Also, we don't use the timeout
2088
# parameter (provided by the python implementation) so we don't implement
2090
err = socket.error('getaddrinfo returns an empty list')
2091
host, port = address
2092
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
2093
af, socktype, proto, canonname, sa = res
2096
sock = socket.socket(af, socktype, proto)
2100
except socket.error, err:
2101
# 'err' is now the most recent error
2102
if sock is not None:
2068
2107
def dereference_path(path):
2069
2108
"""Determine the real path to a file.
2330
2369
except UnicodeDecodeError:
2331
2370
raise errors.BzrError("Can't decode username as %s." % \
2372
except ImportError, e:
2373
if sys.platform != 'win32':
2375
if str(e) != 'No module named pwd':
2377
# https://bugs.launchpad.net/bzr/+bug/660174
2378
# getpass.getuser() is unable to return username on Windows
2379
# if there is no USERNAME environment variable set.
2380
# That could be true if bzr is running as a service,
2381
# e.g. running `bzr serve` as a service on Windows.
2382
# We should not fail with traceback in this case.
2383
username = u'UNKNOWN'
2333
2384
return username
2387
def available_backup_name(base, exists):
2388
"""Find a non-existing backup file name.
2390
This will *not* create anything, this only return a 'free' entry. This
2391
should be used for checking names in a directory below a locked
2392
tree/branch/repo to avoid race conditions. This is LBYL (Look Before You
2393
Leap) and generally discouraged.
2395
:param base: The base name.
2397
:param exists: A callable returning True if the path parameter exists.
2400
name = "%s.~%d~" % (base, counter)
2403
name = "%s.~%d~" % (base, counter)
2407
def set_fd_cloexec(fd):
2408
"""Set a Unix file descriptor's FD_CLOEXEC flag. Do nothing if platform
2409
support for this is not available.
2413
old = fcntl.fcntl(fd, fcntl.F_GETFD)
2414
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
2415
except (ImportError, AttributeError):
2416
# Either the fcntl module or specific constants are not present