171
245
rename_func(tmp_name, new)
173
# Default is to just use the python builtins
174
abspath = os.path.abspath
175
realpath = os.path.realpath
248
# In Python 2.4.2 and older, os.path.abspath and os.path.realpath
249
# choke on a Unicode string containing a relative path if
250
# os.getcwd() returns a non-sys.getdefaultencoding()-encoded
252
_fs_enc = sys.getfilesystemencoding() or 'utf-8'
253
def _posix_abspath(path):
254
# jam 20060426 rather than encoding to fsencoding
255
# copy posixpath.abspath, but use os.getcwdu instead
256
if not posixpath.isabs(path):
257
path = posixpath.join(getcwd(), path)
258
return posixpath.normpath(path)
261
def _posix_realpath(path):
262
return posixpath.realpath(path.encode(_fs_enc)).decode(_fs_enc)
265
def _win32_fixdrive(path):
266
"""Force drive letters to be consistent.
268
win32 is inconsistent whether it returns lower or upper case
269
and even if it was consistent the user might type the other
270
so we force it to uppercase
271
running python.exe under cmd.exe return capital C:\\
272
running win32 python inside a cygwin shell returns lowercase c:\\
274
drive, path = _nt_splitdrive(path)
275
return drive.upper() + path
278
def _win32_abspath(path):
279
# Real _nt_abspath doesn't have a problem with a unicode cwd
280
return _win32_fixdrive(_nt_abspath(unicode(path)).replace('\\', '/'))
283
def _win98_abspath(path):
284
"""Return the absolute version of a path.
285
Windows 98 safe implementation (python reimplementation
286
of Win32 API function GetFullPathNameW)
291
# \\HOST\path => //HOST/path
292
# //HOST/path => //HOST/path
293
# path => C:/cwd/path
296
# check for absolute path
297
drive = _nt_splitdrive(path)[0]
298
if drive == '' and path[:2] not in('//','\\\\'):
300
# we cannot simply os.path.join cwd and path
301
# because os.path.join('C:','/path') produce '/path'
302
# and this is incorrect
303
if path[:1] in ('/','\\'):
304
cwd = _nt_splitdrive(cwd)[0]
306
path = cwd + '\\' + path
307
return _win32_fixdrive(_nt_normpath(path).replace('\\', '/'))
310
def _win32_realpath(path):
311
# Real _nt_realpath doesn't have a problem with a unicode cwd
312
return _win32_fixdrive(_nt_realpath(unicode(path)).replace('\\', '/'))
315
def _win32_pathjoin(*args):
316
return _nt_join(*args).replace('\\', '/')
319
def _win32_normpath(path):
320
return _win32_fixdrive(_nt_normpath(unicode(path)).replace('\\', '/'))
324
return _win32_fixdrive(os.getcwdu().replace('\\', '/'))
327
def _win32_mkdtemp(*args, **kwargs):
328
return _win32_fixdrive(tempfile.mkdtemp(*args, **kwargs).replace('\\', '/'))
331
def _win32_rename(old, new):
332
"""We expect to be able to atomically replace 'new' with old.
334
On win32, if new exists, it must be moved out of the way first,
338
fancy_rename(old, new, rename_func=os.rename, unlink_func=os.unlink)
340
if e.errno in (errno.EPERM, errno.EACCES, errno.EBUSY, errno.EINVAL):
341
# If we try to rename a non-existant file onto cwd, we get
342
# EPERM or EACCES instead of ENOENT, this will raise ENOENT
343
# if the old path doesn't exist, sometimes we get EACCES
344
# On Linux, we seem to get EBUSY, on Mac we get EINVAL
350
return unicodedata.normalize('NFC', os.getcwdu())
353
# Default is to just use the python builtins, but these can be rebound on
354
# particular platforms.
355
abspath = _posix_abspath
356
realpath = _posix_realpath
176
357
pathjoin = os.path.join
177
358
normpath = os.path.normpath
178
359
getcwd = os.getcwdu
179
mkdtemp = tempfile.mkdtemp
180
360
rename = os.rename
181
361
dirname = os.path.dirname
182
362
basename = os.path.basename
184
if os.name == "posix":
185
# In Python 2.4.2 and older, os.path.abspath and os.path.realpath
186
# choke on a Unicode string containing a relative path if
187
# os.getcwd() returns a non-sys.getdefaultencoding()-encoded
189
_fs_enc = sys.getfilesystemencoding()
191
return os.path.abspath(path.encode(_fs_enc)).decode(_fs_enc)
194
return os.path.realpath(path.encode(_fs_enc)).decode(_fs_enc)
363
split = os.path.split
364
splitext = os.path.splitext
365
# These were already imported into local scope
366
# mkdtemp = tempfile.mkdtemp
367
# rmtree = shutil.rmtree
369
MIN_ABS_PATHLENGTH = 1
196
372
if sys.platform == 'win32':
197
# We need to use the Unicode-aware os.path.abspath and
198
# os.path.realpath on Windows systems.
200
return os.path.abspath(path).replace('\\', '/')
203
return os.path.realpath(path).replace('\\', '/')
206
return os.path.join(*args).replace('\\', '/')
209
return os.path.normpath(path).replace('\\', '/')
212
return os.getcwdu().replace('\\', '/')
214
def mkdtemp(*args, **kwargs):
215
return tempfile.mkdtemp(*args, **kwargs).replace('\\', '/')
217
def rename(old, new):
218
fancy_rename(old, new, rename_func=os.rename, unlink_func=os.unlink)
373
if win32utils.winver == 'Windows 98':
374
abspath = _win98_abspath
376
abspath = _win32_abspath
377
realpath = _win32_realpath
378
pathjoin = _win32_pathjoin
379
normpath = _win32_normpath
380
getcwd = _win32_getcwd
381
mkdtemp = _win32_mkdtemp
382
rename = _win32_rename
384
MIN_ABS_PATHLENGTH = 3
386
def _win32_delete_readonly(function, path, excinfo):
387
"""Error handler for shutil.rmtree function [for win32]
388
Helps to remove files and dirs marked as read-only.
390
exception = excinfo[1]
391
if function in (os.remove, os.rmdir) \
392
and isinstance(exception, OSError) \
393
and exception.errno == errno.EACCES:
399
def rmtree(path, ignore_errors=False, onerror=_win32_delete_readonly):
400
"""Replacer for shutil.rmtree: could remove readonly dirs/files"""
401
return shutil.rmtree(path, ignore_errors, onerror)
403
f = win32utils.get_unicode_argv # special function or None
407
elif sys.platform == 'darwin':
411
def get_terminal_encoding():
412
"""Find the best encoding for printing to the screen.
414
This attempts to check both sys.stdout and sys.stdin to see
415
what encoding they are in, and if that fails it falls back to
416
osutils.get_user_encoding().
417
The problem is that on Windows, locale.getpreferredencoding()
418
is not the same encoding as that used by the console:
419
http://mail.python.org/pipermail/python-list/2003-May/162357.html
421
On my standard US Windows XP, the preferred encoding is
422
cp1252, but the console is cp437
424
from bzrlib.trace import mutter
425
output_encoding = getattr(sys.stdout, 'encoding', None)
426
if not output_encoding:
427
input_encoding = getattr(sys.stdin, 'encoding', None)
428
if not input_encoding:
429
output_encoding = get_user_encoding()
430
mutter('encoding stdout as osutils.get_user_encoding() %r',
433
output_encoding = input_encoding
434
mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
436
mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
437
if output_encoding == 'cp0':
438
# invalid encoding (cp0 means 'no codepage' on Windows)
439
output_encoding = get_user_encoding()
440
mutter('cp0 is invalid encoding.'
441
' encoding stdout as osutils.get_user_encoding() %r',
445
codecs.lookup(output_encoding)
447
sys.stderr.write('bzr: warning:'
448
' unknown terminal encoding %s.\n'
449
' Using encoding %s instead.\n'
450
% (output_encoding, get_user_encoding())
452
output_encoding = get_user_encoding()
454
return output_encoding
221
457
def normalizepath(f):
222
if hasattr(os.path, 'realpath'):
458
if getattr(os.path, 'realpath', None) is not None:
1026
def _cicp_canonical_relpath(base, path):
1027
"""Return the canonical path relative to base.
1029
Like relpath, but on case-insensitive-case-preserving file-systems, this
1030
will return the relpath as stored on the file-system rather than in the
1031
case specified in the input string, for all existing portions of the path.
1033
This will cause O(N) behaviour if called for every path in a tree; if you
1034
have a number of paths to convert, you should use canonical_relpaths().
1036
# TODO: it should be possible to optimize this for Windows by using the
1037
# win32 API FindFiles function to look for the specified name - but using
1038
# os.listdir() still gives us the correct, platform agnostic semantics in
1041
rel = relpath(base, path)
1042
# '.' will have been turned into ''
1046
abs_base = abspath(base)
1048
_listdir = os.listdir
1050
# use an explicit iterator so we can easily consume the rest on early exit.
1051
bit_iter = iter(rel.split('/'))
1052
for bit in bit_iter:
1054
for look in _listdir(current):
1055
if lbit == look.lower():
1056
current = pathjoin(current, look)
1059
# got to the end, nothing matched, so we just return the
1060
# non-existing bits as they were specified (the filename may be
1061
# the target of a move, for example).
1062
current = pathjoin(current, bit, *list(bit_iter))
1064
return current[len(abs_base)+1:]
1066
# XXX - TODO - we need better detection/integration of case-insensitive
1067
# file-systems; Linux often sees FAT32 devices (or NFS-mounted OSX
1068
# filesystems), for example, so could probably benefit from the same basic
1069
# support there. For now though, only Windows and OSX get that support, and
1070
# they get it for *all* file-systems!
1071
if sys.platform in ('win32', 'darwin'):
1072
canonical_relpath = _cicp_canonical_relpath
1074
canonical_relpath = relpath
1076
def canonical_relpaths(base, paths):
1077
"""Create an iterable to canonicalize a sequence of relative paths.
1079
The intent is for this implementation to use a cache, vastly speeding
1080
up multiple transformations in the same directory.
1082
# but for now, we haven't optimized...
1083
return [canonical_relpath(base, p) for p in paths]
625
1085
def safe_unicode(unicode_or_utf8_string):
626
1086
"""Coerce unicode_or_utf8_string into unicode.
628
1088
If it is unicode, it is returned.
629
Otherwise it is decoded from utf-8. If a decoding error
630
occurs, it is wrapped as a If the decoding fails, the exception is wrapped
631
as a BzrBadParameter exception.
1089
Otherwise it is decoded from utf-8. If decoding fails, the exception is
1090
wrapped in a BzrBadParameterNotUnicode exception.
633
1092
if isinstance(unicode_or_utf8_string, unicode):
634
1093
return unicode_or_utf8_string
636
1095
return unicode_or_utf8_string.decode('utf8')
637
1096
except UnicodeDecodeError:
638
raise BzrBadParameterNotUnicode(unicode_or_utf8_string)
1097
raise errors.BzrBadParameterNotUnicode(unicode_or_utf8_string)
1100
def safe_utf8(unicode_or_utf8_string):
1101
"""Coerce unicode_or_utf8_string to a utf8 string.
1103
If it is a str, it is returned.
1104
If it is Unicode, it is encoded into a utf-8 string.
1106
if isinstance(unicode_or_utf8_string, str):
1107
# TODO: jam 20070209 This is overkill, and probably has an impact on
1108
# performance if we are dealing with lots of apis that want a
1111
# Make sure it is a valid utf-8 string
1112
unicode_or_utf8_string.decode('utf-8')
1113
except UnicodeDecodeError:
1114
raise errors.BzrBadParameterNotUnicode(unicode_or_utf8_string)
1115
return unicode_or_utf8_string
1116
return unicode_or_utf8_string.encode('utf-8')
1119
_revision_id_warning = ('Unicode revision ids were deprecated in bzr 0.15.'
1120
' Revision id generators should be creating utf8'
1124
def safe_revision_id(unicode_or_utf8_string, warn=True):
1125
"""Revision ids should now be utf8, but at one point they were unicode.
1127
:param unicode_or_utf8_string: A possibly Unicode revision_id. (can also be
1129
:param warn: Functions that are sanitizing user data can set warn=False
1130
:return: None or a utf8 revision id.
1132
if (unicode_or_utf8_string is None
1133
or unicode_or_utf8_string.__class__ == str):
1134
return unicode_or_utf8_string
1136
symbol_versioning.warn(_revision_id_warning, DeprecationWarning,
1138
return cache_utf8.encode(unicode_or_utf8_string)
1141
_file_id_warning = ('Unicode file ids were deprecated in bzr 0.15. File id'
1142
' generators should be creating utf8 file ids.')
1145
def safe_file_id(unicode_or_utf8_string, warn=True):
1146
"""File ids should now be utf8, but at one point they were unicode.
1148
This is the same as safe_utf8, except it uses the cached encode functions
1149
to save a little bit of performance.
1151
:param unicode_or_utf8_string: A possibly Unicode file_id. (can also be
1153
:param warn: Functions that are sanitizing user data can set warn=False
1154
:return: None or a utf8 file id.
1156
if (unicode_or_utf8_string is None
1157
or unicode_or_utf8_string.__class__ == str):
1158
return unicode_or_utf8_string
1160
symbol_versioning.warn(_file_id_warning, DeprecationWarning,
1162
return cache_utf8.encode(unicode_or_utf8_string)
1165
_platform_normalizes_filenames = False
1166
if sys.platform == 'darwin':
1167
_platform_normalizes_filenames = True
1170
def normalizes_filenames():
1171
"""Return True if this platform normalizes unicode filenames.
1173
Mac OSX does, Windows/Linux do not.
1175
return _platform_normalizes_filenames
1178
def _accessible_normalized_filename(path):
1179
"""Get the unicode normalized path, and if you can access the file.
1181
On platforms where the system normalizes filenames (Mac OSX),
1182
you can access a file by any path which will normalize correctly.
1183
On platforms where the system does not normalize filenames
1184
(Windows, Linux), you have to access a file by its exact path.
1186
Internally, bzr only supports NFC normalization, since that is
1187
the standard for XML documents.
1189
So return the normalized path, and a flag indicating if the file
1190
can be accessed by that path.
1193
return unicodedata.normalize('NFC', unicode(path)), True
1196
def _inaccessible_normalized_filename(path):
1197
__doc__ = _accessible_normalized_filename.__doc__
1199
normalized = unicodedata.normalize('NFC', unicode(path))
1200
return normalized, normalized == path
1203
if _platform_normalizes_filenames:
1204
normalized_filename = _accessible_normalized_filename
1206
normalized_filename = _inaccessible_normalized_filename
641
1209
def terminal_width():
642
1210
"""Return estimated terminal width."""
644
# TODO: Do something smart on Windows?
646
# TODO: Is there anything that gets a better update when the window
647
# is resized while the program is running? We could use the Python termcap
1211
if sys.platform == 'win32':
1212
return win32utils.get_console_size()[0]
650
return int(os.environ['COLUMNS'])
651
except (IndexError, KeyError, ValueError):
1215
import struct, fcntl, termios
1216
s = struct.pack('HHHH', 0, 0, 0, 0)
1217
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1218
width = struct.unpack('HHHH', x)[1]
1223
width = int(os.environ['COLUMNS'])
654
1232
def supports_executable():
655
1233
return sys.platform != "win32"
1236
def supports_posix_readonly():
1237
"""Return True if 'readonly' has POSIX semantics, False otherwise.
1239
Notably, a win32 readonly file cannot be deleted, unlike POSIX where the
1240
directory controls creation/deletion, etc.
1242
And under win32, readonly means that the directory itself cannot be
1243
deleted. The contents of a readonly directory can be changed, unlike POSIX
1244
where files in readonly directories cannot be added, deleted or renamed.
1246
return sys.platform != "win32"
1249
def set_or_unset_env(env_variable, value):
1250
"""Modify the environment, setting or removing the env_variable.
1252
:param env_variable: The environment variable in question
1253
:param value: The value to set the environment to. If None, then
1254
the variable will be removed.
1255
:return: The original value of the environment variable.
1257
orig_val = os.environ.get(env_variable)
1259
if orig_val is not None:
1260
del os.environ[env_variable]
1262
if isinstance(value, unicode):
1263
value = value.encode(get_user_encoding())
1264
os.environ[env_variable] = value
1268
_validWin32PathRE = re.compile(r'^([A-Za-z]:[/\\])?[^:<>*"?\|]*$')
1271
def check_legal_path(path):
1272
"""Check whether the supplied path is legal.
1273
This is only required on Windows, so we don't test on other platforms
1276
if sys.platform != "win32":
1278
if _validWin32PathRE.match(path) is None:
1279
raise errors.IllegalPath(path)
1282
_WIN32_ERROR_DIRECTORY = 267 # Similar to errno.ENOTDIR
1284
def _is_error_enotdir(e):
1285
"""Check if this exception represents ENOTDIR.
1287
Unfortunately, python is very inconsistent about the exception
1288
here. The cases are:
1289
1) Linux, Mac OSX all versions seem to set errno == ENOTDIR
1290
2) Windows, Python2.4, uses errno == ERROR_DIRECTORY (267)
1291
which is the windows error code.
1292
3) Windows, Python2.5 uses errno == EINVAL and
1293
winerror == ERROR_DIRECTORY
1295
:param e: An Exception object (expected to be OSError with an errno
1296
attribute, but we should be able to cope with anything)
1297
:return: True if this represents an ENOTDIR error. False otherwise.
1299
en = getattr(e, 'errno', None)
1300
if (en == errno.ENOTDIR
1301
or (sys.platform == 'win32'
1302
and (en == _WIN32_ERROR_DIRECTORY
1303
or (en == errno.EINVAL
1304
and getattr(e, 'winerror', None) == _WIN32_ERROR_DIRECTORY)
1310
def walkdirs(top, prefix=""):
1311
"""Yield data about all the directories in a tree.
1313
This yields all the data about the contents of a directory at a time.
1314
After each directory has been yielded, if the caller has mutated the list
1315
to exclude some directories, they are then not descended into.
1317
The data yielded is of the form:
1318
((directory-relpath, directory-path-from-top),
1319
[(relpath, basename, kind, lstat, path-from-top), ...]),
1320
- directory-relpath is the relative path of the directory being returned
1321
with respect to top. prefix is prepended to this.
1322
- directory-path-from-root is the path including top for this directory.
1323
It is suitable for use with os functions.
1324
- relpath is the relative path within the subtree being walked.
1325
- basename is the basename of the path
1326
- kind is the kind of the file now. If unknown then the file is not
1327
present within the tree - but it may be recorded as versioned. See
1329
- lstat is the stat data *if* the file was statted.
1330
- planned, not implemented:
1331
path_from_tree_root is the path from the root of the tree.
1333
:param prefix: Prefix the relpaths that are yielded with 'prefix'. This
1334
allows one to walk a subtree but get paths that are relative to a tree
1336
:return: an iterator over the dirs.
1338
#TODO there is a bit of a smell where the results of the directory-
1339
# summary in this, and the path from the root, may not agree
1340
# depending on top and prefix - i.e. ./foo and foo as a pair leads to
1341
# potentially confusing output. We should make this more robust - but
1342
# not at a speed cost. RBC 20060731
1344
_directory = _directory_kind
1345
_listdir = os.listdir
1346
_kind_from_mode = file_kind_from_stat_mode
1347
pending = [(safe_unicode(prefix), "", _directory, None, safe_unicode(top))]
1349
# 0 - relpath, 1- basename, 2- kind, 3- stat, 4-toppath
1350
relroot, _, _, _, top = pending.pop()
1352
relprefix = relroot + u'/'
1355
top_slash = top + u'/'
1358
append = dirblock.append
1360
names = sorted(_listdir(top))
1362
if not _is_error_enotdir(e):
1366
abspath = top_slash + name
1367
statvalue = _lstat(abspath)
1368
kind = _kind_from_mode(statvalue.st_mode)
1369
append((relprefix + name, name, kind, statvalue, abspath))
1370
yield (relroot, top), dirblock
1372
# push the user specified dirs from dirblock
1373
pending.extend(d for d in reversed(dirblock) if d[2] == _directory)
1376
class DirReader(object):
1377
"""An interface for reading directories."""
1379
def top_prefix_to_starting_dir(self, top, prefix=""):
1380
"""Converts top and prefix to a starting dir entry
1382
:param top: A utf8 path
1383
:param prefix: An optional utf8 path to prefix output relative paths
1385
:return: A tuple starting with prefix, and ending with the native
1388
raise NotImplementedError(self.top_prefix_to_starting_dir)
1390
def read_dir(self, prefix, top):
1391
"""Read a specific dir.
1393
:param prefix: A utf8 prefix to be preprended to the path basenames.
1394
:param top: A natively encoded path to read.
1395
:return: A list of the directories contents. Each item contains:
1396
(utf8_relpath, utf8_name, kind, lstatvalue, native_abspath)
1398
raise NotImplementedError(self.read_dir)
1401
_selected_dir_reader = None
1404
def _walkdirs_utf8(top, prefix=""):
1405
"""Yield data about all the directories in a tree.
1407
This yields the same information as walkdirs() only each entry is yielded
1408
in utf-8. On platforms which have a filesystem encoding of utf8 the paths
1409
are returned as exact byte-strings.
1411
:return: yields a tuple of (dir_info, [file_info])
1412
dir_info is (utf8_relpath, path-from-top)
1413
file_info is (utf8_relpath, utf8_name, kind, lstat, path-from-top)
1414
if top is an absolute path, path-from-top is also an absolute path.
1415
path-from-top might be unicode or utf8, but it is the correct path to
1416
pass to os functions to affect the file in question. (such as os.lstat)
1418
global _selected_dir_reader
1419
if _selected_dir_reader is None:
1420
fs_encoding = _fs_enc.upper()
1421
if sys.platform == "win32" and win32utils.winver == 'Windows NT':
1422
# Win98 doesn't have unicode apis like FindFirstFileW
1423
# TODO: We possibly could support Win98 by falling back to the
1424
# original FindFirstFile, and using TCHAR instead of WCHAR,
1425
# but that gets a bit tricky, and requires custom compiling
1428
from bzrlib._walkdirs_win32 import Win32ReadDir
1429
_selected_dir_reader = Win32ReadDir()
1432
elif fs_encoding in ('UTF-8', 'US-ASCII', 'ANSI_X3.4-1968'):
1433
# ANSI_X3.4-1968 is a form of ASCII
1435
from bzrlib._readdir_pyx import UTF8DirReader
1436
_selected_dir_reader = UTF8DirReader()
1440
if _selected_dir_reader is None:
1441
# Fallback to the python version
1442
_selected_dir_reader = UnicodeDirReader()
1444
# 0 - relpath, 1- basename, 2- kind, 3- stat, 4-toppath
1445
# But we don't actually uses 1-3 in pending, so set them to None
1446
pending = [[_selected_dir_reader.top_prefix_to_starting_dir(top, prefix)]]
1447
read_dir = _selected_dir_reader.read_dir
1448
_directory = _directory_kind
1450
relroot, _, _, _, top = pending[-1].pop()
1453
dirblock = sorted(read_dir(relroot, top))
1454
yield (relroot, top), dirblock
1455
# push the user specified dirs from dirblock
1456
next = [d for d in reversed(dirblock) if d[2] == _directory]
1458
pending.append(next)
1461
class UnicodeDirReader(DirReader):
1462
"""A dir reader for non-utf8 file systems, which transcodes."""
1464
__slots__ = ['_utf8_encode']
1467
self._utf8_encode = codecs.getencoder('utf8')
1469
def top_prefix_to_starting_dir(self, top, prefix=""):
1470
"""See DirReader.top_prefix_to_starting_dir."""
1471
return (safe_utf8(prefix), None, None, None, safe_unicode(top))
1473
def read_dir(self, prefix, top):
1474
"""Read a single directory from a non-utf8 file system.
1476
top, and the abspath element in the output are unicode, all other paths
1477
are utf8. Local disk IO is done via unicode calls to listdir etc.
1479
This is currently the fallback code path when the filesystem encoding is
1480
not UTF-8. It may be better to implement an alternative so that we can
1481
safely handle paths that are not properly decodable in the current
1484
See DirReader.read_dir for details.
1486
_utf8_encode = self._utf8_encode
1488
_listdir = os.listdir
1489
_kind_from_mode = file_kind_from_stat_mode
1492
relprefix = prefix + '/'
1495
top_slash = top + u'/'
1498
append = dirblock.append
1499
for name in sorted(_listdir(top)):
1501
name_utf8 = _utf8_encode(name)[0]
1502
except UnicodeDecodeError:
1503
raise errors.BadFilenameEncoding(
1504
_utf8_encode(relprefix)[0] + name, _fs_enc)
1505
abspath = top_slash + name
1506
statvalue = _lstat(abspath)
1507
kind = _kind_from_mode(statvalue.st_mode)
1508
append((relprefix + name_utf8, name_utf8, kind, statvalue, abspath))
1512
def copy_tree(from_path, to_path, handlers={}):
1513
"""Copy all of the entries in from_path into to_path.
1515
:param from_path: The base directory to copy.
1516
:param to_path: The target directory. If it does not exist, it will
1518
:param handlers: A dictionary of functions, which takes a source and
1519
destinations for files, directories, etc.
1520
It is keyed on the file kind, such as 'directory', 'symlink', or 'file'
1521
'file', 'directory', and 'symlink' should always exist.
1522
If they are missing, they will be replaced with 'os.mkdir()',
1523
'os.readlink() + os.symlink()', and 'shutil.copy2()', respectively.
1525
# Now, just copy the existing cached tree to the new location
1526
# We use a cheap trick here.
1527
# Absolute paths are prefixed with the first parameter
1528
# relative paths are prefixed with the second.
1529
# So we can get both the source and target returned
1530
# without any extra work.
1532
def copy_dir(source, dest):
1535
def copy_link(source, dest):
1536
"""Copy the contents of a symlink"""
1537
link_to = os.readlink(source)
1538
os.symlink(link_to, dest)
1540
real_handlers = {'file':shutil.copy2,
1541
'symlink':copy_link,
1542
'directory':copy_dir,
1544
real_handlers.update(handlers)
1546
if not os.path.exists(to_path):
1547
real_handlers['directory'](from_path, to_path)
1549
for dir_info, entries in walkdirs(from_path, prefix=to_path):
1550
for relpath, name, kind, st, abspath in entries:
1551
real_handlers[kind](abspath, relpath)
1554
def path_prefix_key(path):
1555
"""Generate a prefix-order path key for path.
1557
This can be used to sort paths in the same way that walkdirs does.
1559
return (dirname(path) , path)
1562
def compare_paths_prefix_order(path_a, path_b):
1563
"""Compare path_a and path_b to generate the same order walkdirs uses."""
1564
key_a = path_prefix_key(path_a)
1565
key_b = path_prefix_key(path_b)
1566
return cmp(key_a, key_b)
1569
_cached_user_encoding = None
1572
def get_user_encoding(use_cache=True):
1573
"""Find out what the preferred user encoding is.
1575
This is generally the encoding that is used for command line parameters
1576
and file contents. This may be different from the terminal encoding
1577
or the filesystem encoding.
1579
:param use_cache: Enable cache for detected encoding.
1580
(This parameter is turned on by default,
1581
and required only for selftesting)
1583
:return: A string defining the preferred user encoding
1585
global _cached_user_encoding
1586
if _cached_user_encoding is not None and use_cache:
1587
return _cached_user_encoding
1589
if sys.platform == 'darwin':
1590
# python locale.getpreferredencoding() always return
1591
# 'mac-roman' on darwin. That's a lie.
1592
sys.platform = 'posix'
1594
if os.environ.get('LANG', None) is None:
1595
# If LANG is not set, we end up with 'ascii', which is bad
1596
# ('mac-roman' is more than ascii), so we set a default which
1597
# will give us UTF-8 (which appears to work in all cases on
1598
# OSX). Users are still free to override LANG of course, as
1599
# long as it give us something meaningful. This work-around
1600
# *may* not be needed with python 3k and/or OSX 10.5, but will
1601
# work with them too -- vila 20080908
1602
os.environ['LANG'] = 'en_US.UTF-8'
1605
sys.platform = 'darwin'
1610
user_encoding = locale.getpreferredencoding()
1611
except locale.Error, e:
1612
sys.stderr.write('bzr: warning: %s\n'
1613
' Could not determine what text encoding to use.\n'
1614
' This error usually means your Python interpreter\n'
1615
' doesn\'t support the locale set by $LANG (%s)\n'
1616
" Continuing with ascii encoding.\n"
1617
% (e, os.environ.get('LANG')))
1618
user_encoding = 'ascii'
1620
# Windows returns 'cp0' to indicate there is no code page. So we'll just
1621
# treat that as ASCII, and not support printing unicode characters to the
1624
# For python scripts run under vim, we get '', so also treat that as ASCII
1625
if user_encoding in (None, 'cp0', ''):
1626
user_encoding = 'ascii'
1630
codecs.lookup(user_encoding)
1632
sys.stderr.write('bzr: warning:'
1633
' unknown encoding %s.'
1634
' Continuing with ascii encoding.\n'
1637
user_encoding = 'ascii'
1640
_cached_user_encoding = user_encoding
1642
return user_encoding
1645
def get_host_name():
1646
"""Return the current unicode host name.
1648
This is meant to be used in place of socket.gethostname() because that
1649
behaves inconsistently on different platforms.
1651
if sys.platform == "win32":
1653
return win32utils.get_host_name()
1656
return socket.gethostname().decode(get_user_encoding())
1659
def recv_all(socket, bytes):
1660
"""Receive an exact number of bytes.
1662
Regular Socket.recv() may return less than the requested number of bytes,
1663
dependning on what's in the OS buffer. MSG_WAITALL is not available
1664
on all platforms, but this should work everywhere. This will return
1665
less than the requested amount if the remote end closes.
1667
This isn't optimized and is intended mostly for use in testing.
1670
while len(b) < bytes:
1671
new = until_no_eintr(socket.recv, bytes - len(b))
1678
def send_all(socket, bytes, report_activity=None):
1679
"""Send all bytes on a socket.
1681
Regular socket.sendall() can give socket error 10053 on Windows. This
1682
implementation sends no more than 64k at a time, which avoids this problem.
1684
:param report_activity: Call this as bytes are read, see
1685
Transport._report_activity
1688
for pos in xrange(0, len(bytes), chunk_size):
1689
block = bytes[pos:pos+chunk_size]
1690
if report_activity is not None:
1691
report_activity(len(block), 'write')
1692
until_no_eintr(socket.sendall, block)
1695
def dereference_path(path):
1696
"""Determine the real path to a file.
1698
All parent elements are dereferenced. But the file itself is not
1700
:param path: The original path. May be absolute or relative.
1701
:return: the real path *to* the file
1703
parent, base = os.path.split(path)
1704
# The pathjoin for '.' is a workaround for Python bug #1213894.
1705
# (initial path components aren't dereferenced)
1706
return pathjoin(realpath(pathjoin('.', parent)), base)
1709
def supports_mapi():
1710
"""Return True if we can use MAPI to launch a mail client."""
1711
return sys.platform == "win32"
1714
def resource_string(package, resource_name):
1715
"""Load a resource from a package and return it as a string.
1717
Note: Only packages that start with bzrlib are currently supported.
1719
This is designed to be a lightweight implementation of resource
1720
loading in a way which is API compatible with the same API from
1722
http://peak.telecommunity.com/DevCenter/PkgResources#basic-resource-access.
1723
If and when pkg_resources becomes a standard library, this routine
1726
# Check package name is within bzrlib
1727
if package == "bzrlib":
1728
resource_relpath = resource_name
1729
elif package.startswith("bzrlib."):
1730
package = package[len("bzrlib."):].replace('.', os.sep)
1731
resource_relpath = pathjoin(package, resource_name)
1733
raise errors.BzrError('resource package %s not in bzrlib' % package)
1735
# Map the resource to a file and read its contents
1736
base = dirname(bzrlib.__file__)
1737
if getattr(sys, 'frozen', None): # bzr.exe
1738
base = abspath(pathjoin(base, '..', '..'))
1739
filename = pathjoin(base, resource_relpath)
1740
return open(filename, 'rU').read()
1743
def file_kind_from_stat_mode_thunk(mode):
1744
global file_kind_from_stat_mode
1745
if file_kind_from_stat_mode is file_kind_from_stat_mode_thunk:
1747
from bzrlib._readdir_pyx import UTF8DirReader
1748
file_kind_from_stat_mode = UTF8DirReader().kind_from_mode
1750
from bzrlib._readdir_py import (
1751
_kind_from_mode as file_kind_from_stat_mode
1753
return file_kind_from_stat_mode(mode)
1754
file_kind_from_stat_mode = file_kind_from_stat_mode_thunk
1757
def file_kind(f, _lstat=os.lstat):
1759
return file_kind_from_stat_mode(_lstat(f).st_mode)
1761
if getattr(e, 'errno', None) in (errno.ENOENT, errno.ENOTDIR):
1762
raise errors.NoSuchFile(f)
1766
def until_no_eintr(f, *a, **kw):
1767
"""Run f(*a, **kw), retrying if an EINTR error occurs."""
1768
# Borrowed from Twisted's twisted.python.util.untilConcludes function.
1772
except (IOError, OSError), e:
1773
if e.errno == errno.EINTR:
1777
def re_compile_checked(re_string, flags=0, where=""):
1778
"""Return a compiled re, or raise a sensible error.
1780
This should only be used when compiling user-supplied REs.
1782
:param re_string: Text form of regular expression.
1783
:param flags: eg re.IGNORECASE
1784
:param where: Message explaining to the user the context where
1785
it occurred, eg 'log search filter'.
1787
# from https://bugs.launchpad.net/bzr/+bug/251352
1789
re_obj = re.compile(re_string, flags)
1794
where = ' in ' + where
1795
# despite the name 'error' is a type
1796
raise errors.BzrCommandError('Invalid regular expression%s: %r: %s'
1797
% (where, re_string, e))
1800
if sys.platform == "win32":
1803
return msvcrt.getch()
1808
fd = sys.stdin.fileno()
1809
settings = termios.tcgetattr(fd)
1812
ch = sys.stdin.read(1)
1814
termios.tcsetattr(fd, termios.TCSADRAIN, settings)