19
19
from cStringIO import StringIO
21
from ntpath import (abspath as _nt_abspath,
23
normpath as _nt_normpath,
24
realpath as _nt_realpath,
25
splitdrive as _nt_splitdrive,
22
28
from os import listdir
36
from ntpath import (abspath as _nt_abspath,
38
normpath as _nt_normpath,
39
realpath as _nt_realpath,
43
45
from bzrlib.errors import (BzrError,
49
from bzrlib.symbol_versioning import *
51
from bzrlib.symbol_versioning import (deprecated_function,
50
53
from bzrlib.trace import mutter
51
import bzrlib.win32console
54
56
def make_readonly(filename):
204
206
_fs_enc = sys.getfilesystemencoding()
205
207
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)
208
# jam 20060426 rather than encoding to fsencoding
209
# copy posixpath.abspath, but use os.getcwdu instead
210
if not posixpath.isabs(path):
211
path = posixpath.join(getcwd(), path)
212
return posixpath.normpath(path)
214
215
def _posix_realpath(path):
215
return os.path.realpath(path.encode(_fs_enc)).decode(_fs_enc)
216
return posixpath.realpath(path.encode(_fs_enc)).decode(_fs_enc)
219
def _win32_fixdrive(path):
220
"""Force drive letters to be consistent.
222
win32 is inconsistent whether it returns lower or upper case
223
and even if it was consistent the user might type the other
224
so we force it to uppercase
225
running python.exe under cmd.exe return capital C:\\
226
running win32 python inside a cygwin shell returns lowercase c:\\
228
drive, path = _nt_splitdrive(path)
229
return drive.upper() + path
218
232
def _win32_abspath(path):
219
return _nt_abspath(path.encode(_fs_enc)).decode(_fs_enc).replace('\\', '/')
233
# Real _nt_abspath doesn't have a problem with a unicode cwd
234
return _win32_fixdrive(_nt_abspath(unicode(path)).replace('\\', '/'))
222
237
def _win32_realpath(path):
223
return _nt_realpath(path.encode(_fs_enc)).decode(_fs_enc).replace('\\', '/')
238
# Real _nt_realpath doesn't have a problem with a unicode cwd
239
return _win32_fixdrive(_nt_realpath(unicode(path)).replace('\\', '/'))
226
242
def _win32_pathjoin(*args):
230
246
def _win32_normpath(path):
231
return _nt_normpath(path).replace('\\', '/')
247
return _win32_fixdrive(_nt_normpath(unicode(path)).replace('\\', '/'))
234
250
def _win32_getcwd():
235
return os.getcwdu().replace('\\', '/')
251
return _win32_fixdrive(os.getcwdu().replace('\\', '/'))
238
254
def _win32_mkdtemp(*args, **kwargs):
239
return tempfile.mkdtemp(*args, **kwargs).replace('\\', '/')
255
return _win32_fixdrive(tempfile.mkdtemp(*args, **kwargs).replace('\\', '/'))
242
258
def _win32_rename(old, new):
288
304
return shutil.rmtree(path, ignore_errors, onerror)
307
def get_terminal_encoding():
308
"""Find the best encoding for printing to the screen.
310
This attempts to check both sys.stdout and sys.stdin to see
311
what encoding they are in, and if that fails it falls back to
312
bzrlib.user_encoding.
313
The problem is that on Windows, locale.getpreferredencoding()
314
is not the same encoding as that used by the console:
315
http://mail.python.org/pipermail/python-list/2003-May/162357.html
317
On my standard US Windows XP, the preferred encoding is
318
cp1252, but the console is cp437
320
output_encoding = getattr(sys.stdout, 'encoding', None)
321
if not output_encoding:
322
input_encoding = getattr(sys.stdin, 'encoding', None)
323
if not input_encoding:
324
output_encoding = bzrlib.user_encoding
325
mutter('encoding stdout as bzrlib.user_encoding %r', output_encoding)
327
output_encoding = input_encoding
328
mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
330
mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
331
return output_encoding
291
334
def normalizepath(f):
292
335
if hasattr(os.path, 'realpath'):