~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Lukáš Lalinský
  • Date: 2007-12-17 17:28:25 UTC
  • mfrom: (3120 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3123.
  • Revision ID: lalinsky@gmail.com-20071217172825-tr3pqm1mhvs3gwnn
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
234
234
 
235
235
    success = False
236
236
    try:
237
 
        # This may throw an exception, in which case success will
238
 
        # not be set.
239
 
        rename_func(old, new)
240
 
        success = True
 
237
        try:
 
238
            # This may throw an exception, in which case success will
 
239
            # not be set.
 
240
            rename_func(old, new)
 
241
            success = True
 
242
        except (IOError, OSError), e:
 
243
            # source and target may be aliases of each other (e.g. on a
 
244
            # case-insensitive filesystem), so we may have accidentally renamed
 
245
            # source by when we tried to rename target
 
246
            if not (file_existed and e.errno in (None, errno.ENOENT)):
 
247
                raise
241
248
    finally:
242
249
        if file_existed:
243
250
            # If the file used to exist, rename it back into place
1399
1406
        b += new
1400
1407
    return b
1401
1408
 
 
1409
 
1402
1410
def dereference_path(path):
1403
1411
    """Determine the real path to a file.
1404
1412
 
1416
1424
def supports_mapi():
1417
1425
    """Return True if we can use MAPI to launch a mail client."""
1418
1426
    return sys.platform == "win32"
 
1427
 
 
1428
 
 
1429
def resource_string(package, resource_name):
 
1430
    """Load a resource from a package and return it as a string.
 
1431
 
 
1432
    Note: Only packages that start with bzrlib are currently supported.
 
1433
 
 
1434
    This is designed to be a lightweight implementation of resource
 
1435
    loading in a way which is API compatible with the same API from
 
1436
    pkg_resources. See
 
1437
    http://peak.telecommunity.com/DevCenter/PkgResources#basic-resource-access.
 
1438
    If and when pkg_resources becomes a standard library, this routine
 
1439
    can delegate to it.
 
1440
    """
 
1441
    # Check package name is within bzrlib
 
1442
    if package == "bzrlib":
 
1443
        resource_relpath = resource_name
 
1444
    elif package.startswith("bzrlib."):
 
1445
        package = package[len("bzrlib."):].replace('.', os.sep)
 
1446
        resource_relpath = pathjoin(package, resource_name)
 
1447
    else:
 
1448
        raise errors.BzrError('resource package %s not in bzrlib' % package)
 
1449
 
 
1450
    # Map the resource to a file and read its contents
 
1451
    base = dirname(bzrlib.__file__)
 
1452
    if getattr(sys, 'frozen', None):    # bzr.exe
 
1453
        base = abspath(pathjoin(base, '..', '..'))
 
1454
    filename = pathjoin(base, resource_relpath)
 
1455
    return open(filename, 'rU').read()