329
def _posix_path_from_environ(key):
330
"""Get unicode path from `key` in environment or None if not present
332
Note that posix systems use arbitrary byte strings for filesystem objects,
333
so a path that raises BadFilenameEncoding here may still be accessible.
335
val = os.environ.get(key, None)
339
return val.decode(_fs_enc)
340
except UnicodeDecodeError:
341
# GZ 2011-12-12:Ideally want to include `key` in the exception message
342
raise errors.BadFilenameEncoding(val, _fs_enc)
345
def _posix_getuser_unicode():
346
"""Get username from environment or password database as unicode"""
347
name = getpass.getuser()
348
user_encoding = get_user_encoding()
350
return name.decode(user_encoding)
351
except UnicodeDecodeError:
352
raise errors.BzrError("Encoding of username %r is unsupported by %s "
353
"application locale." % (name, user_encoding))
324
356
def _win32_fixdrive(path):
325
357
"""Force drive letters to be consistent.
913
949
return os.fstat(f.fileno())[stat.ST_SIZE]
916
# Define rand_bytes based on platform.
918
# Python 2.4 and later have os.urandom,
919
# but it doesn't work on some arches
921
rand_bytes = os.urandom
922
except (NotImplementedError, AttributeError):
923
# If python doesn't have os.urandom, or it doesn't work,
924
# then try to first pull random data from /dev/urandom
952
# Alias os.urandom to support platforms (which?) without /dev/urandom and
953
# override if it doesn't work. Avoid checking on windows where there is
954
# significant initialisation cost that can be avoided for some bzr calls.
956
rand_bytes = os.urandom
958
if rand_bytes.__module__ != "nt":
926
rand_bytes = file('/dev/urandom', 'rb').read
927
# Otherwise, use this hack as a last resort
928
except (IOError, OSError):
961
except NotImplementedError:
929
962
# not well seeded, but better than nothing
930
963
def rand_bytes(n):
1949
1980
_cached_user_encoding = None
1952
def get_user_encoding(use_cache=True):
1983
def get_user_encoding(use_cache=DEPRECATED_PARAMETER):
1953
1984
"""Find out what the preferred user encoding is.
1955
1986
This is generally the encoding that is used for command line parameters
1956
1987
and file contents. This may be different from the terminal encoding
1957
1988
or the filesystem encoding.
1959
:param use_cache: Enable cache for detected encoding.
1960
(This parameter is turned on by default,
1961
and required only for selftesting)
1963
1990
:return: A string defining the preferred user encoding
1965
1992
global _cached_user_encoding
1966
if _cached_user_encoding is not None and use_cache:
1993
if deprecated_passed(use_cache):
1994
warn_deprecated("use_cache should only have been used for tests",
1995
DeprecationWarning, stacklevel=2)
1996
if _cached_user_encoding is not None:
1967
1997
return _cached_user_encoding
1969
if sys.platform == 'darwin':
1970
# python locale.getpreferredencoding() always return
1971
# 'mac-roman' on darwin. That's a lie.
1972
sys.platform = 'posix'
1974
if os.environ.get('LANG', None) is None:
1975
# If LANG is not set, we end up with 'ascii', which is bad
1976
# ('mac-roman' is more than ascii), so we set a default which
1977
# will give us UTF-8 (which appears to work in all cases on
1978
# OSX). Users are still free to override LANG of course, as
1979
# long as it give us something meaningful. This work-around
1980
# *may* not be needed with python 3k and/or OSX 10.5, but will
1981
# work with them too -- vila 20080908
1982
os.environ['LANG'] = 'en_US.UTF-8'
1985
sys.platform = 'darwin'
1999
if os.name == 'posix' and getattr(locale, 'CODESET', None) is not None:
2000
# Use the existing locale settings and call nl_langinfo directly
2001
# rather than going through getpreferredencoding. This avoids
2002
# <http://bugs.python.org/issue6202> on OSX Python 2.6 and the
2003
# possibility of the setlocale call throwing an error.
2004
user_encoding = locale.nl_langinfo(locale.CODESET)
2006
# GZ 2011-12-19: On windows could call GetACP directly instead.
2007
user_encoding = locale.getpreferredencoding(False)
1990
user_encoding = locale.getpreferredencoding()
1991
except locale.Error, e:
1992
sys.stderr.write('bzr: warning: %s\n'
1993
' Could not determine what text encoding to use.\n'
1994
' This error usually means your Python interpreter\n'
1995
' doesn\'t support the locale set by $LANG (%s)\n'
1996
" Continuing with ascii encoding.\n"
1997
% (e, os.environ.get('LANG')))
1998
user_encoding = 'ascii'
2000
# Windows returns 'cp0' to indicate there is no code page. So we'll just
2001
# treat that as ASCII, and not support printing unicode characters to the
2004
# For python scripts run under vim, we get '', so also treat that as ASCII
2005
if user_encoding in (None, 'cp0', ''):
2006
user_encoding = 'ascii'
2010
codecs.lookup(user_encoding)
2010
user_encoding = codecs.lookup(user_encoding).name
2012
if user_encoding not in ("", "cp0"):
2012
2013
sys.stderr.write('bzr: warning:'
2013
2014
' unknown encoding %s.'
2014
2015
' Continuing with ascii encoding.\n'
2015
2016
% user_encoding
2017
user_encoding = 'ascii'
2020
_cached_user_encoding = user_encoding
2018
user_encoding = 'ascii'
2020
# Get 'ascii' when setlocale has not been called or LANG=C or unset.
2021
if user_encoding == 'ascii':
2022
if sys.platform == 'darwin':
2023
# OSX is special-cased in Python to have a UTF-8 filesystem
2024
# encoding and previously had LANG set here if not present.
2025
user_encoding = 'utf-8'
2026
# GZ 2011-12-19: Maybe UTF-8 should be the default in this case
2027
# for some other posix platforms as well.
2029
_cached_user_encoding = user_encoding
2022
2030
return user_encoding
2026
2034
return get_terminal_encoding()
2029
_message_encoding = None
2032
def get_message_encoding():
2033
"""Return the encoding used for messages
2035
While the message encoding is a general setting it should usually only be
2036
needed for decoding system error strings such as from OSError instances.
2038
global _message_encoding
2039
if _message_encoding is None:
2040
if os.name == "posix":
2042
# This is a process-global setting that can change, but should in
2043
# general just get set once at process startup then be constant.
2044
_message_encoding = locale.getlocale(locale.LC_MESSAGES)[1]
2046
# On windows want the result of GetACP() which this boils down to.
2047
_message_encoding = get_user_encoding()
2048
return _message_encoding or "ascii"
2051
2037
def get_host_name():
2052
2038
"""Return the current unicode host name.
2434
2419
open_file = open
2437
def getuser_unicode():
2438
"""Return the username as unicode.
2441
user_encoding = get_user_encoding()
2442
username = getpass.getuser().decode(user_encoding)
2443
except UnicodeDecodeError:
2444
raise errors.BzrError("Can't decode username as %s." % \
2446
except ImportError, e:
2447
if sys.platform != 'win32':
2449
if str(e) != 'No module named pwd':
2451
# https://bugs.launchpad.net/bzr/+bug/660174
2452
# getpass.getuser() is unable to return username on Windows
2453
# if there is no USERNAME environment variable set.
2454
# That could be true if bzr is running as a service,
2455
# e.g. running `bzr serve` as a service on Windows.
2456
# We should not fail with traceback in this case.
2457
username = u'UNKNOWN'
2461
2422
def available_backup_name(base, exists):
2462
2423
"""Find a non-existing backup file name.