26
27
lazy_import(globals(), """
27
28
from datetime import datetime
30
from ntpath import (abspath as _nt_abspath,
32
normpath as _nt_normpath,
33
realpath as _nt_realpath,
34
splitdrive as _nt_splitdrive,
31
# We need to import both shutil and rmtree as we export the later on posix
32
# and need the former on windows
34
from shutil import rmtree
37
# We need to import both tempfile and mkdtemp as we export the later on posix
38
# and need the former on windows
40
from tempfile import mkdtemp
44
from tempfile import (
43
49
from bzrlib import (
298
304
running python.exe under cmd.exe return capital C:\\
299
305
running win32 python inside a cygwin shell returns lowercase c:\\
301
drive, path = ntpath.splitdrive(path)
307
drive, path = _nt_splitdrive(path)
302
308
return drive.upper() + path
305
311
def _win32_abspath(path):
306
# Real ntpath.abspath doesn't have a problem with a unicode cwd
307
return _win32_fixdrive(ntpath.abspath(unicode(path)).replace('\\', '/'))
312
# Real _nt_abspath doesn't have a problem with a unicode cwd
313
return _win32_fixdrive(_nt_abspath(unicode(path)).replace('\\', '/'))
310
316
def _win98_abspath(path):
321
327
# /path => C:/path
322
328
path = unicode(path)
323
329
# check for absolute path
324
drive = ntpath.splitdrive(path)[0]
330
drive = _nt_splitdrive(path)[0]
325
331
if drive == '' and path[:2] not in('//','\\\\'):
326
332
cwd = os.getcwdu()
327
333
# we cannot simply os.path.join cwd and path
328
334
# because os.path.join('C:','/path') produce '/path'
329
335
# and this is incorrect
330
336
if path[:1] in ('/','\\'):
331
cwd = ntpath.splitdrive(cwd)[0]
337
cwd = _nt_splitdrive(cwd)[0]
333
339
path = cwd + '\\' + path
334
return _win32_fixdrive(ntpath.normpath(path).replace('\\', '/'))
340
return _win32_fixdrive(_nt_normpath(path).replace('\\', '/'))
337
343
def _win32_realpath(path):
338
# Real ntpath.realpath doesn't have a problem with a unicode cwd
339
return _win32_fixdrive(ntpath.realpath(unicode(path)).replace('\\', '/'))
344
# Real _nt_realpath doesn't have a problem with a unicode cwd
345
return _win32_fixdrive(_nt_realpath(unicode(path)).replace('\\', '/'))
342
348
def _win32_pathjoin(*args):
343
return ntpath.join(*args).replace('\\', '/')
349
return _nt_join(*args).replace('\\', '/')
346
352
def _win32_normpath(path):
347
return _win32_fixdrive(ntpath.normpath(unicode(path)).replace('\\', '/'))
353
return _win32_fixdrive(_nt_normpath(unicode(path)).replace('\\', '/'))
350
356
def _win32_getcwd():
389
395
basename = os.path.basename
390
396
split = os.path.split
391
397
splitext = os.path.splitext
392
# These were already lazily imported into local scope
398
# These were already imported into local scope
393
399
# mkdtemp = tempfile.mkdtemp
394
400
# rmtree = shutil.rmtree
448
454
On my standard US Windows XP, the preferred encoding is
449
455
cp1252, but the console is cp437
451
:param trace: If True trace the selected encoding via mutter().
453
457
from bzrlib.trace import mutter
454
458
output_encoding = getattr(sys.stdout, 'encoding', None)
456
460
input_encoding = getattr(sys.stdin, 'encoding', None)
457
461
if not input_encoding:
458
462
output_encoding = get_user_encoding()
460
mutter('encoding stdout as osutils.get_user_encoding() %r',
463
mutter('encoding stdout as osutils.get_user_encoding() %r',
463
466
output_encoding = input_encoding
465
mutter('encoding stdout as sys.stdin encoding %r',
467
mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
469
mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
469
mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
470
470
if output_encoding == 'cp0':
471
471
# invalid encoding (cp0 means 'no codepage' on Windows)
472
472
output_encoding = get_user_encoding()
474
mutter('cp0 is invalid encoding.'
473
mutter('cp0 is invalid encoding.'
475
474
' encoding stdout as osutils.get_user_encoding() %r',
512
511
"""True if f is a regular file."""
514
return stat.S_ISREG(os.lstat(f)[stat.ST_MODE])
513
return S_ISREG(os.lstat(f)[ST_MODE])
519
518
"""True if f is a symlink."""
521
return stat.S_ISLNK(os.lstat(f)[stat.ST_MODE])
520
return S_ISLNK(os.lstat(f)[ST_MODE])
933
932
def parent_directories(filename):
934
933
"""Return the list of parent directories, deepest first.
936
935
For example, parent_directories("a/b/c") -> ["a/b", "a"].
962
961
# NB: This docstring is just an example, not a doctest, because doctest
963
962
# currently can't cope with the use of lazy imports in this namespace --
966
965
# This currently doesn't report the failure at the time it occurs, because
967
966
# they tend to happen very early in startup when we can't check config
968
967
# files etc, and also we want to report all failures but not spam the user
969
968
# with 10 warnings.
969
from bzrlib import trace
970
970
exception_str = str(exception)
971
971
if exception_str not in _extension_load_failures:
972
972
trace.mutter("failed to load compiled extension: %s" % exception_str)
1233
1233
# but for now, we haven't optimized...
1234
1234
return [canonical_relpath(base, p) for p in paths]
1237
def decode_filename(filename):
1238
"""Decode the filename using the filesystem encoding
1240
If it is unicode, it is returned.
1241
Otherwise it is decoded from the the filesystem's encoding. If decoding
1242
fails, a errors.BadFilenameEncoding exception is raised.
1244
if type(filename) is unicode:
1247
return filename.decode(_fs_enc)
1248
except UnicodeDecodeError:
1249
raise errors.BadFilenameEncoding(filename, _fs_enc)
1252
1236
def safe_unicode(unicode_or_utf8_string):
1253
1237
"""Coerce unicode_or_utf8_string into unicode.
1337
1321
def normalizes_filenames():
1338
1322
"""Return True if this platform normalizes unicode filenames.
1324
Mac OSX does, Windows/Linux do not.
1342
1326
return _platform_normalizes_filenames
1348
1332
On platforms where the system normalizes filenames (Mac OSX),
1349
1333
you can access a file by any path which will normalize correctly.
1350
1334
On platforms where the system does not normalize filenames
1351
(everything else), you have to access a file by its exact path.
1335
(Windows, Linux), you have to access a file by its exact path.
1353
1337
Internally, bzr only supports NFC normalization, since that is
1354
1338
the standard for XML documents.
1461
1445
# a similar effect.
1463
1447
# 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
width = int(os.environ['BZR_COLUMNS'])
1449
return int(os.environ['BZR_COLUMNS'])
1467
1450
except (KeyError, ValueError):
1469
if width is not None:
1475
1453
isatty = getattr(sys.stdout, 'isatty', None)
1476
1454
if isatty is None or not isatty():
1880
1858
s = os.stat(src)
1881
1859
chown(dst, s.st_uid, s.st_gid)
1882
1860
except OSError, 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()
1861
trace.warning("Unable to copy ownership from '%s' to '%s': IOError: %s." % (src, dst, e))
1889
1864
def path_prefix_key(path):
2001
1972
# data at once.
2002
1973
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)
2012
1975
def read_bytes_from_socket(sock, report_activity=None,
2013
1976
max_read_size=MAX_SOCKET_CHUNK):
2014
1977
"""Read up to max_read_size of bytes from sock and notify of progress.
2022
1985
bytes = sock.recv(max_read_size)
2023
1986
except socket.error, e:
2024
1987
eno = e.args[0]
2025
if eno in _end_of_stream_errors:
1988
if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
2026
1989
# The connection was closed by the other side. Callers expect
2027
1990
# an empty string to signal end-of-stream.
2058
2021
def send_all(sock, bytes, report_activity=None):
2059
2022
"""Send all bytes on a socket.
2061
2024
Breaks large blocks in smaller chunks to avoid buffering limitations on
2062
2025
some platforms, and catches EINTR which may be thrown if the send is
2063
2026
interrupted by a signal.
2065
2028
This is preferred to socket.sendall(), because it avoids portability bugs
2066
2029
and provides activity reporting.
2068
2031
:param report_activity: Call this as bytes are read, see
2069
2032
Transport._report_activity
2081
2044
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:
2107
2047
def dereference_path(path):
2108
2048
"""Determine the real path to a file.
2182
2122
def until_no_eintr(f, *a, **kw):
2183
2123
"""Run f(*a, **kw), retrying if an EINTR error occurs.
2185
2125
WARNING: you must be certain that it is safe to retry the call repeatedly
2186
2126
if EINTR does occur. This is typically only true for low-level operations
2187
2127
like os.read. If in any doubt, don't use this.
2205
@deprecated_function(deprecated_in((2, 2, 0)))
2206
2145
def re_compile_checked(re_string, flags=0, where=""):
2207
2146
"""Return a compiled re, or raise a sensible error.
2218
2157
re_obj = re.compile(re_string, flags)
2219
2158
re_obj.search("")
2221
except errors.InvalidPattern, e:
2223
2162
where = ' in ' + where
2224
2163
# despite the name 'error' is a type
2225
raise errors.BzrCommandError('Invalid regular expression%s: %s'
2164
raise errors.BzrCommandError('Invalid regular expression%s: %r: %s'
2165
% (where, re_string, e))
2229
2168
if sys.platform == "win32":
2319
2258
if sys.platform == 'win32':
2320
2259
def open_file(filename, mode='r', bufsize=-1):
2321
2260
"""This function is used to override the ``open`` builtin.
2323
2262
But it uses O_NOINHERIT flag so the file handle is not inherited by
2324
2263
child processes. Deleting or renaming a closed file opened with this
2325
2264
function is not blocking child processes.
2370
2309
raise errors.BzrError("Can't decode username as %s." % \
2372
2311
return username
2375
def available_backup_name(base, exists):
2376
"""Find a non-existing backup file name.
2378
This will *not* create anything, this only return a 'free' entry. This
2379
should be used for checking names in a directory below a locked
2380
tree/branch/repo to avoid race conditions. This is LBYL (Look Before You
2381
Leap) and generally discouraged.
2383
:param base: The base name.
2385
:param exists: A callable returning True if the path parameter exists.
2388
name = "%s.~%d~" % (base, counter)
2391
name = "%s.~%d~" % (base, counter)
2395
def set_fd_cloexec(fd):
2396
"""Set a Unix file descriptor's FD_CLOEXEC flag. Do nothing if platform
2397
support for this is not available.
2401
old = fcntl.fcntl(fd, fcntl.F_GETFD)
2402
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
2403
except (ImportError, AttributeError):
2404
# Either the fcntl module or specific constants are not present
2408
def find_executable_on_path(name):
2409
"""Finds an executable on the PATH.
2411
On Windows, this will try to append each extension in the PATHEXT
2412
environment variable to the name, if it cannot be found with the name
2415
:param name: The base name of the executable.
2416
:return: The path to the executable found or None.
2418
path = os.environ.get('PATH')
2421
path = path.split(os.pathsep)
2422
if sys.platform == 'win32':
2423
exts = os.environ.get('PATHEXT', '').split(os.pathsep)
2424
exts = [ext.lower() for ext in exts]
2425
base, ext = os.path.splitext(name)
2427
if ext.lower() not in exts:
2435
f = os.path.join(d, name) + ext
2436
if os.access(f, os.X_OK):