27
26
lazy_import(globals(), """
28
27
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
44
from tempfile import (
40
from tempfile import mkdtemp
49
43
from bzrlib import (
304
298
running python.exe under cmd.exe return capital C:\\
305
299
running win32 python inside a cygwin shell returns lowercase c:\\
307
drive, path = _nt_splitdrive(path)
301
drive, path = ntpath.splitdrive(path)
308
302
return drive.upper() + path
311
305
def _win32_abspath(path):
312
# Real _nt_abspath doesn't have a problem with a unicode cwd
313
return _win32_fixdrive(_nt_abspath(unicode(path)).replace('\\', '/'))
306
# Real ntpath.abspath doesn't have a problem with a unicode cwd
307
return _win32_fixdrive(ntpath.abspath(unicode(path)).replace('\\', '/'))
316
310
def _win98_abspath(path):
327
321
# /path => C:/path
328
322
path = unicode(path)
329
323
# check for absolute path
330
drive = _nt_splitdrive(path)[0]
324
drive = ntpath.splitdrive(path)[0]
331
325
if drive == '' and path[:2] not in('//','\\\\'):
332
326
cwd = os.getcwdu()
333
327
# we cannot simply os.path.join cwd and path
334
328
# because os.path.join('C:','/path') produce '/path'
335
329
# and this is incorrect
336
330
if path[:1] in ('/','\\'):
337
cwd = _nt_splitdrive(cwd)[0]
331
cwd = ntpath.splitdrive(cwd)[0]
339
333
path = cwd + '\\' + path
340
return _win32_fixdrive(_nt_normpath(path).replace('\\', '/'))
334
return _win32_fixdrive(ntpath.normpath(path).replace('\\', '/'))
343
337
def _win32_realpath(path):
344
# Real _nt_realpath doesn't have a problem with a unicode cwd
345
return _win32_fixdrive(_nt_realpath(unicode(path)).replace('\\', '/'))
338
# Real ntpath.realpath doesn't have a problem with a unicode cwd
339
return _win32_fixdrive(ntpath.realpath(unicode(path)).replace('\\', '/'))
348
342
def _win32_pathjoin(*args):
349
return _nt_join(*args).replace('\\', '/')
343
return ntpath.join(*args).replace('\\', '/')
352
346
def _win32_normpath(path):
353
return _win32_fixdrive(_nt_normpath(unicode(path)).replace('\\', '/'))
347
return _win32_fixdrive(ntpath.normpath(unicode(path)).replace('\\', '/'))
356
350
def _win32_getcwd():
395
389
basename = os.path.basename
396
390
split = os.path.split
397
391
splitext = os.path.splitext
398
# These were already imported into local scope
392
# These were already lazily imported into local scope
399
393
# mkdtemp = tempfile.mkdtemp
400
394
# rmtree = shutil.rmtree
454
448
On my standard US Windows XP, the preferred encoding is
455
449
cp1252, but the console is cp437
451
:param trace: If True trace the selected encoding via mutter().
457
453
from bzrlib.trace import mutter
458
454
output_encoding = getattr(sys.stdout, 'encoding', None)
460
456
input_encoding = getattr(sys.stdin, 'encoding', None)
461
457
if not input_encoding:
462
458
output_encoding = get_user_encoding()
463
mutter('encoding stdout as osutils.get_user_encoding() %r',
460
mutter('encoding stdout as osutils.get_user_encoding() %r',
466
463
output_encoding = input_encoding
467
mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
465
mutter('encoding stdout as sys.stdin encoding %r',
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()
473
mutter('cp0 is invalid encoding.'
474
mutter('cp0 is invalid encoding.'
474
475
' encoding stdout as osutils.get_user_encoding() %r',
511
512
"""True if f is a regular file."""
513
return S_ISREG(os.lstat(f)[ST_MODE])
514
return stat.S_ISREG(os.lstat(f)[stat.ST_MODE])
518
519
"""True if f is a symlink."""
520
return S_ISLNK(os.lstat(f)[ST_MODE])
521
return stat.S_ISLNK(os.lstat(f)[stat.ST_MODE])
2060
2065
report_activity(sent, 'write')
2068
def connect_socket(address):
2069
# Slight variation of the socket.create_connection() function (provided by
2070
# python-2.6) that can fail if getaddrinfo returns an empty list. We also
2071
# provide it for previous python versions. Also, we don't use the timeout
2072
# parameter (provided by the python implementation) so we don't implement
2074
err = socket.error('getaddrinfo returns an empty list')
2075
host, port = address
2076
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
2077
af, socktype, proto, canonname, sa = res
2080
sock = socket.socket(af, socktype, proto)
2084
except socket.error, err:
2085
# 'err' is now the most recent error
2086
if sock is not None:
2063
2091
def dereference_path(path):
2064
2092
"""Determine the real path to a file.
2189
@deprecated_function(deprecated_in((2, 2, 0)))
2161
2190
def re_compile_checked(re_string, flags=0, where=""):
2162
2191
"""Return a compiled re, or raise a sensible error.
2173
2202
re_obj = re.compile(re_string, flags)
2174
2203
re_obj.search("")
2205
except errors.InvalidPattern, e:
2178
2207
where = ' in ' + where
2179
2208
# despite the name 'error' is a type
2180
raise errors.BzrCommandError('Invalid regular expression%s: %r: %s'
2181
% (where, re_string, e))
2209
raise errors.BzrCommandError('Invalid regular expression%s: %s'
2184
2213
if sys.platform == "win32":