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
96
96
user_encoding = get_user_encoding()
97
97
return [a.decode(user_encoding) for a in sys.argv[1:]]
98
98
except UnicodeDecodeError:
99
raise errors.BzrError(("Parameter '%r' is unsupported by the current "
99
raise errors.BzrError("Parameter %r encoding is unsupported by %s "
100
"application locale." % (a, user_encoding))
103
103
def make_readonly(filename):
967
981
# they tend to happen very early in startup when we can't check config
968
982
# files etc, and also we want to report all failures but not spam the user
969
983
# with 10 warnings.
970
from bzrlib import trace
971
984
exception_str = str(exception)
972
985
if exception_str not in _extension_load_failures:
973
986
trace.mutter("failed to load compiled extension: %s" % exception_str)
1462
1475
# a similar effect.
1464
1477
# If BZR_COLUMNS is set, take it, user is always right
1478
# Except if they specified 0 in which case, impose no limit here
1466
return int(os.environ['BZR_COLUMNS'])
1480
width = int(os.environ['BZR_COLUMNS'])
1467
1481
except (KeyError, ValueError):
1483
if width is not None:
1470
1489
isatty = getattr(sys.stdout, 'isatty', None)
1471
1490
if isatty is None or not isatty():
1875
1894
s = os.stat(src)
1876
1895
chown(dst, s.st_uid, s.st_gid)
1877
1896
except OSError, e:
1878
trace.warning("Unable to copy ownership from '%s' to '%s': IOError: %s." % (src, dst, e))
1898
'Unable to copy ownership from "%s" to "%s". '
1899
'You may want to set it manually.', src, dst)
1900
trace.log_exception_quietly()
1881
1903
def path_prefix_key(path):
1993
2015
# data at once.
1994
2016
MAX_SOCKET_CHUNK = 64 * 1024
2018
_end_of_stream_errors = [errno.ECONNRESET]
2019
for _eno in ['WSAECONNRESET', 'WSAECONNABORTED']:
2020
_eno = getattr(errno, _eno, None)
2021
if _eno is not None:
2022
_end_of_stream_errors.append(_eno)
1996
2026
def read_bytes_from_socket(sock, report_activity=None,
1997
2027
max_read_size=MAX_SOCKET_CHUNK):
1998
2028
"""Read up to max_read_size of bytes from sock and notify of progress.
2006
2036
bytes = sock.recv(max_read_size)
2007
2037
except socket.error, e:
2008
2038
eno = e.args[0]
2009
if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
2039
if eno in _end_of_stream_errors:
2010
2040
# The connection was closed by the other side. Callers expect
2011
2041
# an empty string to signal end-of-stream.
2227
2257
termios.tcsetattr(fd, termios.TCSADRAIN, settings)
2231
2260
if sys.platform == 'linux2':
2232
2261
def _local_concurrency():
2234
prefix = 'processor'
2235
for line in file('/proc/cpuinfo', 'rb'):
2236
if line.startswith(prefix):
2237
concurrency = int(line[line.find(':')+1:]) + 1
2263
return os.sysconf('SC_NPROCESSORS_ONLN')
2264
except (ValueError, OSError, AttributeError):
2239
2266
elif sys.platform == 'darwin':
2240
2267
def _local_concurrency():
2241
2268
return subprocess.Popen(['sysctl', '-n', 'hw.availcpu'],
2242
2269
stdout=subprocess.PIPE).communicate()[0]
2243
elif sys.platform[0:7] == 'freebsd':
2270
elif "bsd" in sys.platform:
2244
2271
def _local_concurrency():
2245
2272
return subprocess.Popen(['sysctl', '-n', 'hw.ncpu'],
2246
2273
stdout=subprocess.PIPE).communicate()[0]
2274
2301
concurrency = os.environ.get('BZR_CONCURRENCY', None)
2275
2302
if concurrency is None:
2277
concurrency = _local_concurrency()
2278
except (OSError, IOError):
2304
import multiprocessing
2306
# multiprocessing is only available on Python >= 2.6
2308
concurrency = _local_concurrency()
2309
except (OSError, IOError):
2312
concurrency = multiprocessing.cpu_count()
2281
2314
concurrency = int(concurrency)
2282
2315
except (TypeError, ValueError):
2375
2408
name = "%s.~%d~" % (base, counter)
2412
def set_fd_cloexec(fd):
2413
"""Set a Unix file descriptor's FD_CLOEXEC flag. Do nothing if platform
2414
support for this is not available.
2418
old = fcntl.fcntl(fd, fcntl.F_GETFD)
2419
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
2420
except (ImportError, AttributeError):
2421
# Either the fcntl module or specific constants are not present
2425
def find_executable_on_path(name):
2426
"""Finds an executable on the PATH.
2428
On Windows, this will try to append each extension in the PATHEXT
2429
environment variable to the name, if it cannot be found with the name
2432
:param name: The base name of the executable.
2433
:return: The path to the executable found or None.
2435
path = os.environ.get('PATH')
2438
path = path.split(os.pathsep)
2439
if sys.platform == 'win32':
2440
exts = os.environ.get('PATHEXT', '').split(os.pathsep)
2441
exts = [ext.lower() for ext in exts]
2442
base, ext = os.path.splitext(name)
2444
if ext.lower() not in exts:
2452
f = os.path.join(d, name) + ext
2453
if os.access(f, os.X_OK):