~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

(gz) Add path_from_environ and clean ups to osutils and win32utils (Martin
 Packman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
320
320
    return path
321
321
 
322
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
 
323
350
def _win32_fixdrive(path):
324
351
    """Force drive letters to be consistent.
325
352
 
414
441
realpath = _posix_realpath
415
442
pathjoin = os.path.join
416
443
normpath = _posix_normpath
 
444
path_from_environ = _posix_path_from_environ
 
445
getuser_unicode = _posix_getuser_unicode
417
446
getcwd = os.getcwdu
418
447
rename = os.rename
419
448
dirname = os.path.dirname
475
504
    f = win32utils.get_unicode_argv     # special function or None
476
505
    if f is not None:
477
506
        get_unicode_argv = f
 
507
    path_from_environ = win32utils.get_environ_unicode
 
508
    getuser_unicode = win32utils.get_user_name
478
509
 
479
510
elif sys.platform == 'darwin':
480
511
    getcwd = _mac_getcwd
2431
2462
    open_file = open
2432
2463
 
2433
2464
 
2434
 
def getuser_unicode():
2435
 
    """Return the username as unicode.
2436
 
    """
2437
 
    try:
2438
 
        user_encoding = get_user_encoding()
2439
 
        username = getpass.getuser().decode(user_encoding)
2440
 
    except UnicodeDecodeError:
2441
 
        raise errors.BzrError("Can't decode username as %s." % \
2442
 
                user_encoding)
2443
 
    except ImportError, e:
2444
 
        if sys.platform != 'win32':
2445
 
            raise
2446
 
        if str(e) != 'No module named pwd':
2447
 
            raise
2448
 
        # https://bugs.launchpad.net/bzr/+bug/660174
2449
 
        # getpass.getuser() is unable to return username on Windows
2450
 
        # if there is no USERNAME environment variable set.
2451
 
        # That could be true if bzr is running as a service,
2452
 
        # e.g. running `bzr serve` as a service on Windows.
2453
 
        # We should not fail with traceback in this case.
2454
 
        username = u'UNKNOWN'
2455
 
    return username
2456
 
 
2457
 
 
2458
2465
def available_backup_name(base, exists):
2459
2466
    """Find a non-existing backup file name.
2460
2467