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])
932
933
def parent_directories(filename):
933
934
"""Return the list of parent directories, deepest first.
935
936
For example, parent_directories("a/b/c") -> ["a/b", "a"].
961
962
# NB: This docstring is just an example, not a doctest, because doctest
962
963
# currently can't cope with the use of lazy imports in this namespace --
965
966
# This currently doesn't report the failure at the time it occurs, because
966
967
# they tend to happen very early in startup when we can't check config
967
968
# files etc, and also we want to report all failures but not spam the user
1233
1234
# but for now, we haven't optimized...
1234
1235
return [canonical_relpath(base, p) for p in paths]
1238
def decode_filename(filename):
1239
"""Decode the filename using the filesystem encoding
1241
If it is unicode, it is returned.
1242
Otherwise it is decoded from the the filesystem's encoding. If decoding
1243
fails, a errors.BadFilenameEncoding exception is raised.
1245
if type(filename) is unicode:
1248
return filename.decode(_fs_enc)
1249
except UnicodeDecodeError:
1250
raise errors.BadFilenameEncoding(filename, _fs_enc)
1236
1253
def safe_unicode(unicode_or_utf8_string):
1237
1254
"""Coerce unicode_or_utf8_string into unicode.
1321
1338
def normalizes_filenames():
1322
1339
"""Return True if this platform normalizes unicode filenames.
1324
Mac OSX does, Windows/Linux do not.
1326
1343
return _platform_normalizes_filenames
1332
1349
On platforms where the system normalizes filenames (Mac OSX),
1333
1350
you can access a file by any path which will normalize correctly.
1334
1351
On platforms where the system does not normalize filenames
1335
(Windows, Linux), you have to access a file by its exact path.
1352
(everything else), you have to access a file by its exact path.
1337
1354
Internally, bzr only supports NFC normalization, since that is
1338
1355
the standard for XML documents.
2021
2042
def send_all(sock, bytes, report_activity=None):
2022
2043
"""Send all bytes on a socket.
2024
2045
Breaks large blocks in smaller chunks to avoid buffering limitations on
2025
2046
some platforms, and catches EINTR which may be thrown if the send is
2026
2047
interrupted by a signal.
2028
2049
This is preferred to socket.sendall(), because it avoids portability bugs
2029
2050
and provides activity reporting.
2031
2052
:param report_activity: Call this as bytes are read, see
2032
2053
Transport._report_activity
2122
2143
def until_no_eintr(f, *a, **kw):
2123
2144
"""Run f(*a, **kw), retrying if an EINTR error occurs.
2125
2146
WARNING: you must be certain that it is safe to retry the call repeatedly
2126
2147
if EINTR does occur. This is typically only true for low-level operations
2127
2148
like os.read. If in any doubt, don't use this.
2166
@deprecated_function(deprecated_in((2, 2, 0)))
2145
2167
def re_compile_checked(re_string, flags=0, where=""):
2146
2168
"""Return a compiled re, or raise a sensible error.
2157
2179
re_obj = re.compile(re_string, flags)
2158
2180
re_obj.search("")
2182
except errors.InvalidPattern, e:
2162
2184
where = ' in ' + where
2163
2185
# despite the name 'error' is a type
2164
raise errors.BzrCommandError('Invalid regular expression%s: %r: %s'
2165
% (where, re_string, e))
2186
raise errors.BzrCommandError('Invalid regular expression%s: %s'
2168
2190
if sys.platform == "win32":
2258
2280
if sys.platform == 'win32':
2259
2281
def open_file(filename, mode='r', bufsize=-1):
2260
2282
"""This function is used to override the ``open`` builtin.
2262
2284
But it uses O_NOINHERIT flag so the file handle is not inherited by
2263
2285
child processes. Deleting or renaming a closed file opened with this
2264
2286
function is not blocking child processes.