258
260
return _win32_fixdrive(_nt_abspath(unicode(path)).replace('\\', '/'))
263
def _win98_abspath(path):
264
"""Return the absolute version of a path.
265
Windows 98 safe implementation (python reimplementation
266
of Win32 API function GetFullPathNameW)
271
# \\HOST\path => //HOST/path
272
# //HOST/path => //HOST/path
273
# path => C:/cwd/path
276
# check for absolute path
277
drive = _nt_splitdrive(path)[0]
278
if drive == '' and path[:2] not in('//','\\\\'):
280
# we cannot simply os.path.join cwd and path
281
# because os.path.join('C:','/path') produce '/path'
282
# and this is incorrect
283
if path[:1] in ('/','\\'):
284
cwd = _nt_splitdrive(cwd)[0]
286
path = cwd + '\\' + path
287
return _win32_fixdrive(_nt_normpath(path).replace('\\', '/'))
289
if win32utils.winver == 'Windows 98':
290
_win32_abspath = _win98_abspath
261
293
def _win32_realpath(path):
262
294
# Real _nt_realpath doesn't have a problem with a unicode cwd
263
295
return _win32_fixdrive(_nt_realpath(unicode(path)).replace('\\', '/'))
782
814
# 2) Isn't one of ' \t\r\n' which are characters we sometimes use as
784
816
# 3) '\xa0' isn't unicode safe since it is >128.
785
# So we are following textwrap's example and hard-coding our own.
786
# We probably could ignore \v and \f, too.
787
for ch in u' \t\n\r\v\f':
818
# This should *not* be a unicode set of characters in case the source
819
# string is not a Unicode string. We can auto-up-cast the characters since
820
# they are ascii, but we don't want to auto-up-cast the string in case it
822
for ch in ' \t\n\r\v\f':
850
885
raise errors.BzrBadParameterNotUnicode(unicode_or_utf8_string)
888
def safe_utf8(unicode_or_utf8_string):
889
"""Coerce unicode_or_utf8_string to a utf8 string.
891
If it is a str, it is returned.
892
If it is Unicode, it is encoded into a utf-8 string.
894
if isinstance(unicode_or_utf8_string, str):
895
# TODO: jam 20070209 This is overkill, and probably has an impact on
896
# performance if we are dealing with lots of apis that want a
899
# Make sure it is a valid utf-8 string
900
unicode_or_utf8_string.decode('utf-8')
901
except UnicodeDecodeError:
902
raise errors.BzrBadParameterNotUnicode(unicode_or_utf8_string)
903
return unicode_or_utf8_string
904
return unicode_or_utf8_string.encode('utf-8')
907
def safe_revision_id(unicode_or_utf8_string):
908
"""Revision ids should now be utf8, but at one point they were unicode.
910
This is the same as safe_utf8, except it uses the cached encode functions
911
to save a little bit of performance.
913
if unicode_or_utf8_string is None:
915
if isinstance(unicode_or_utf8_string, str):
916
# TODO: jam 20070209 Eventually just remove this check.
918
utf8_str = cache_utf8.get_cached_utf8(unicode_or_utf8_string)
919
except UnicodeDecodeError:
920
raise errors.BzrBadParameterNotUnicode(unicode_or_utf8_string)
922
return cache_utf8.encode(unicode_or_utf8_string)
853
925
_platform_normalizes_filenames = False
854
926
if sys.platform == 'darwin':
855
927
_platform_normalizes_filenames = True
897
969
def terminal_width():
898
970
"""Return estimated terminal width."""
899
971
if sys.platform == 'win32':
900
import bzrlib.win32console
901
return bzrlib.win32console.get_console_size()[0]
972
return win32utils.get_console_size()[0]
904
975
import struct, fcntl, termios