19
19
from cStringIO import StringIO
21
from ntpath import (abspath as _nt_abspath,
23
normpath as _nt_normpath,
24
realpath as _nt_realpath,
22
27
from os import listdir
36
from ntpath import (abspath as _nt_abspath,
38
normpath as _nt_normpath,
39
realpath as _nt_realpath,
43
44
from bzrlib.errors import (BzrError,
204
205
_fs_enc = sys.getfilesystemencoding()
205
206
def _posix_abspath(path):
206
return os.path.abspath(path.encode(_fs_enc)).decode(_fs_enc)
207
# jam 20060426 This is another possibility which mimics
208
# os.path.abspath, only uses unicode characters instead
209
# if not os.path.isabs(path):
210
# return os.path.join(os.getcwdu(), path)
207
# jam 20060426 rather than encoding to fsencoding
208
# copy posixpath.abspath, but use os.getcwdu instead
209
if not posixpath.isabs(path):
210
path = posixpath.join(getcwd(), path)
211
return posixpath.normpath(path)
214
214
def _posix_realpath(path):
215
return os.path.realpath(path.encode(_fs_enc)).decode(_fs_enc)
215
return posixpath.realpath(path.encode(_fs_enc)).decode(_fs_enc)
218
218
def _win32_abspath(path):
219
return _nt_abspath(path.encode(_fs_enc)).decode(_fs_enc).replace('\\', '/')
219
# Real _nt_abspath doesn't have a problem with a unicode cwd
220
return _nt_abspath(unicode(path)).replace('\\', '/')
222
223
def _win32_realpath(path):
223
return _nt_realpath(path.encode(_fs_enc)).decode(_fs_enc).replace('\\', '/')
224
# Real _nt_realpath doesn't have a problem with a unicode cwd
225
return _nt_realpath(unicode(path)).replace('\\', '/')
226
228
def _win32_pathjoin(*args):
288
290
return shutil.rmtree(path, ignore_errors, onerror)
293
def get_terminal_encoding():
294
"""Find the best encoding for printing to the screen.
296
This attempts to check both sys.stdout and sys.stdin to see
297
what encoding they are in, and if that fails it falls back to
298
bzrlib.user_encoding.
299
The problem is that on Windows, locale.getpreferredencoding()
300
is not the same encoding as that used by the console:
301
http://mail.python.org/pipermail/python-list/2003-May/162357.html
303
On my standard US Windows XP, the preferred encoding is
304
cp1252, but the console is cp437
306
output_encoding = getattr(sys.stdout, 'encoding', None)
307
if not output_encoding:
308
input_encoding = getattr(sys.stdin, 'encoding', None)
309
if not input_encoding:
310
output_encoding = bzrlib.user_encoding
311
mutter('encoding stdout as bzrlib.user_encoding %r', output_encoding)
313
output_encoding = input_encoding
314
mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
316
mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
317
return output_encoding
291
320
def normalizepath(f):
292
321
if hasattr(os.path, 'realpath'):
846
875
for dir in reversed(dirblock):
847
876
if dir[2] == _directory:
848
877
pending.append(dir)
880
def path_prefix_key(path):
881
"""Generate a prefix-order path key for path.
883
This can be used to sort paths in the same way that walkdirs does.
885
return (dirname(path) , path)
888
def compare_paths_prefix_order(path_a, path_b):
889
"""Compare path_a and path_b to generate the same order walkdirs uses."""
890
key_a = path_prefix_key(path_a)
891
key_b = path_prefix_key(path_b)
892
return cmp(key_a, key_b)