2091
2065
report_activity(sent, 'write')
2094
def connect_socket(address):
2095
# Slight variation of the socket.create_connection() function (provided by
2096
# python-2.6) that can fail if getaddrinfo returns an empty list. We also
2097
# provide it for previous python versions. Also, we don't use the timeout
2098
# parameter (provided by the python implementation) so we don't implement
2100
err = socket.error('getaddrinfo returns an empty list')
2101
host, port = address
2102
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
2103
af, socktype, proto, canonname, sa = res
2106
sock = socket.socket(af, socktype, proto)
2110
except socket.error, err:
2111
# 'err' is now the most recent error
2112
if sock is not None:
2117
2068
def dereference_path(path):
2118
2069
"""Determine the real path to a file.
2179
2130
return file_kind_from_stat_mode(mode)
2180
2131
file_kind_from_stat_mode = file_kind_from_stat_mode_thunk
2182
def file_stat(f, _lstat=os.lstat):
2134
def file_kind(f, _lstat=os.lstat):
2136
return file_kind_from_stat_mode(_lstat(f).st_mode)
2186
2137
except OSError, e:
2187
2138
if getattr(e, 'errno', None) in (errno.ENOENT, errno.ENOTDIR):
2188
2139
raise errors.NoSuchFile(f)
2191
def file_kind(f, _lstat=os.lstat):
2192
stat_value = file_stat(f, _lstat)
2193
return file_kind_from_stat_mode(stat_value.st_mode)
2195
2143
def until_no_eintr(f, *a, **kw):
2196
2144
"""Run f(*a, **kw), retrying if an EINTR error occurs.
2256
2204
termios.tcsetattr(fd, termios.TCSADRAIN, settings)
2259
if sys.platform.startswith('linux'):
2208
if sys.platform == 'linux2':
2260
2209
def _local_concurrency():
2262
return os.sysconf('SC_NPROCESSORS_ONLN')
2263
except (ValueError, OSError, AttributeError):
2211
prefix = 'processor'
2212
for line in file('/proc/cpuinfo', 'rb'):
2213
if line.startswith(prefix):
2214
concurrency = int(line[line.find(':')+1:]) + 1
2265
2216
elif sys.platform == 'darwin':
2266
2217
def _local_concurrency():
2267
2218
return subprocess.Popen(['sysctl', '-n', 'hw.availcpu'],
2268
2219
stdout=subprocess.PIPE).communicate()[0]
2269
elif "bsd" in sys.platform:
2220
elif sys.platform[0:7] == 'freebsd':
2270
2221
def _local_concurrency():
2271
2222
return subprocess.Popen(['sysctl', '-n', 'hw.ncpu'],
2272
2223
stdout=subprocess.PIPE).communicate()[0]
2300
2251
concurrency = os.environ.get('BZR_CONCURRENCY', None)
2301
2252
if concurrency is None:
2303
import multiprocessing
2305
# multiprocessing is only available on Python >= 2.6
2307
concurrency = _local_concurrency()
2308
except (OSError, IOError):
2311
concurrency = multiprocessing.cpu_count()
2254
concurrency = _local_concurrency()
2255
except (OSError, IOError):
2313
2258
concurrency = int(concurrency)
2314
2259
except (TypeError, ValueError):
2385
2330
except UnicodeDecodeError:
2386
2331
raise errors.BzrError("Can't decode username as %s." % \
2388
except ImportError, e:
2389
if sys.platform != 'win32':
2391
if str(e) != 'No module named pwd':
2393
# https://bugs.launchpad.net/bzr/+bug/660174
2394
# getpass.getuser() is unable to return username on Windows
2395
# if there is no USERNAME environment variable set.
2396
# That could be true if bzr is running as a service,
2397
# e.g. running `bzr serve` as a service on Windows.
2398
# We should not fail with traceback in this case.
2399
username = u'UNKNOWN'
2400
2333
return username
2403
def available_backup_name(base, exists):
2404
"""Find a non-existing backup file name.
2406
This will *not* create anything, this only return a 'free' entry. This
2407
should be used for checking names in a directory below a locked
2408
tree/branch/repo to avoid race conditions. This is LBYL (Look Before You
2409
Leap) and generally discouraged.
2411
:param base: The base name.
2413
:param exists: A callable returning True if the path parameter exists.
2416
name = "%s.~%d~" % (base, counter)
2419
name = "%s.~%d~" % (base, counter)
2423
def set_fd_cloexec(fd):
2424
"""Set a Unix file descriptor's FD_CLOEXEC flag. Do nothing if platform
2425
support for this is not available.
2429
old = fcntl.fcntl(fd, fcntl.F_GETFD)
2430
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
2431
except (ImportError, AttributeError):
2432
# Either the fcntl module or specific constants are not present
2436
def find_executable_on_path(name):
2437
"""Finds an executable on the PATH.
2439
On Windows, this will try to append each extension in the PATHEXT
2440
environment variable to the name, if it cannot be found with the name
2443
:param name: The base name of the executable.
2444
:return: The path to the executable found or None.
2446
path = os.environ.get('PATH')
2449
path = path.split(os.pathsep)
2450
if sys.platform == 'win32':
2451
exts = os.environ.get('PATHEXT', '').split(os.pathsep)
2452
exts = [ext.lower() for ext in exts]
2453
base, ext = os.path.splitext(name)
2455
if ext.lower() not in exts:
2463
f = os.path.join(d, name) + ext
2464
if os.access(f, os.X_OK):
2469
def _posix_is_local_pid_dead(pid):
2470
"""True if pid doesn't correspond to live process on this machine"""
2472
# Special meaning of unix kill: just check if it's there.
2475
if e.errno == errno.ESRCH:
2476
# On this machine, and really not found: as sure as we can be
2479
elif e.errno == errno.EPERM:
2480
# exists, though not ours
2483
mutter("os.kill(%d, 0) failed: %s" % (pid, e))
2484
# Don't really know.
2487
# Exists and our process: not dead.
2490
if sys.platform == "win32":
2491
is_local_pid_dead = win32utils.is_local_pid_dead
2493
is_local_pid_dead = _posix_is_local_pid_dead
2496
def fdatasync(fileno):
2497
"""Flush file contents to disk if possible.
2499
:param fileno: Integer OS file handle.
2500
:raises TransportNotPossible: If flushing to disk is not possible.
2502
fn = getattr(os, 'fdatasync', getattr(os, 'fsync', None))