/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-19 10:59:35 UTC
  • mfrom: (6383 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6385.
  • Revision ID: jelmer@canonical.com-20111219105935-9hilixk0anu39v0f
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
 
64
64
 
65
65
import bzrlib
66
 
from bzrlib import symbol_versioning
 
66
from bzrlib import symbol_versioning, _fs_enc
67
67
 
68
68
 
69
69
# Cross platform wall-clock time functionality with decent resolution.
293
293
# choke on a Unicode string containing a relative path if
294
294
# os.getcwd() returns a non-sys.getdefaultencoding()-encoded
295
295
# string.
296
 
_fs_enc = sys.getfilesystemencoding() or 'utf-8'
297
296
def _posix_abspath(path):
298
297
    # jam 20060426 rather than encoding to fsencoding
299
298
    # copy posixpath.abspath, but use os.getcwdu instead
321
320
    return path
322
321
 
323
322
 
 
323
def _posix_path_from_environ(key):
 
324
    """Get unicode path from `key` in environment or None if not present
 
325
 
 
326
    Note that posix systems use arbitrary byte strings for filesystem objects,
 
327
    so a path that raises BadFilenameEncoding here may still be accessible.
 
328
    """
 
329
    val = os.environ.get(key, None)
 
330
    if val is None:
 
331
        return val
 
332
    try:
 
333
        return val.decode(_fs_enc)
 
334
    except UnicodeDecodeError:
 
335
        # GZ 2011-12-12:Ideally want to include `key` in the exception message
 
336
        raise errors.BadFilenameEncoding(val, _fs_enc)
 
337
 
 
338
 
 
339
def _posix_getuser_unicode():
 
340
    """Get username from environment or password database as unicode"""
 
341
    name = getpass.getuser()
 
342
    user_encoding = get_user_encoding()
 
343
    try:
 
344
        return name.decode(user_encoding)
 
345
    except UnicodeDecodeError:
 
346
        raise errors.BzrError("Encoding of username %r is unsupported by %s "
 
347
            "application locale." % (name, user_encoding))
 
348
 
 
349
 
324
350
def _win32_fixdrive(path):
325
351
    """Force drive letters to be consistent.
326
352
 
415
441
realpath = _posix_realpath
416
442
pathjoin = os.path.join
417
443
normpath = _posix_normpath
 
444
path_from_environ = _posix_path_from_environ
 
445
getuser_unicode = _posix_getuser_unicode
418
446
getcwd = os.getcwdu
419
447
rename = os.rename
420
448
dirname = os.path.dirname
476
504
    f = win32utils.get_unicode_argv     # special function or None
477
505
    if f is not None:
478
506
        get_unicode_argv = f
 
507
    path_from_environ = win32utils.get_environ_unicode
 
508
    getuser_unicode = win32utils.get_user_name
479
509
 
480
510
elif sys.platform == 'darwin':
481
511
    getcwd = _mac_getcwd
1771
1801
    """
1772
1802
    global _selected_dir_reader
1773
1803
    if _selected_dir_reader is None:
1774
 
        fs_encoding = _fs_enc.upper()
1775
1804
        if sys.platform == "win32" and win32utils.winver == 'Windows NT':
1776
1805
            # Win98 doesn't have unicode apis like FindFirstFileW
1777
1806
            # TODO: We possibly could support Win98 by falling back to the
1783
1812
                _selected_dir_reader = Win32ReadDir()
1784
1813
            except ImportError:
1785
1814
                pass
1786
 
        elif fs_encoding in ('UTF-8', 'US-ASCII', 'ANSI_X3.4-1968'):
1787
 
            # ANSI_X3.4-1968 is a form of ASCII
 
1815
        elif _fs_enc in ('utf-8', 'ascii'):
1788
1816
            try:
1789
1817
                from bzrlib._readdir_pyx import UTF8DirReader
1790
1818
                _selected_dir_reader = UTF8DirReader()
2297
2325
 
2298
2326
 
2299
2327
if sys.platform == "win32":
2300
 
    import msvcrt
2301
2328
    def getchar():
 
2329
        import msvcrt
2302
2330
        return msvcrt.getch()
2303
2331
else:
2304
 
    import tty
2305
 
    import termios
2306
2332
    def getchar():
 
2333
        import tty
 
2334
        import termios
2307
2335
        fd = sys.stdin.fileno()
2308
2336
        settings = termios.tcgetattr(fd)
2309
2337
        try:
2434
2462
    open_file = open
2435
2463
 
2436
2464
 
2437
 
def getuser_unicode():
2438
 
    """Return the username as unicode.
2439
 
    """
2440
 
    try:
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." % \
2445
 
                user_encoding)
2446
 
    except ImportError, e:
2447
 
        if sys.platform != 'win32':
2448
 
            raise
2449
 
        if str(e) != 'No module named pwd':
2450
 
            raise
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'
2458
 
    return username
2459
 
 
2460
 
 
2461
2465
def available_backup_name(base, exists):
2462
2466
    """Find a non-existing backup file name.
2463
2467