~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Andrew Bennetts
  • Date: 2010-03-26 04:47:45 UTC
  • mfrom: (5116 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5117.
  • Revision ID: andrew.bennetts@canonical.com-20100326044745-ubvt5tmse1a17s1f
MergeĀ lp:bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
from bzrlib import (
52
52
    cache_utf8,
53
53
    errors,
 
54
    trace,
54
55
    win32utils,
55
56
    )
56
 
from bzrlib.smart import medium
57
 
from bzrlib.tests import test_smart_transport
58
57
""")
59
58
 
60
59
from bzrlib.symbol_versioning import (
1812
1811
            real_handlers[kind](abspath, relpath)
1813
1812
 
1814
1813
 
 
1814
def copy_ownership(dst, src=None):
 
1815
    """Copy usr/grp ownership from src file/dir to dst file/dir.
 
1816
 
 
1817
    If src is None, the containing directory is used as source. If chown
 
1818
    fails, the error is ignored and a warning is printed.
 
1819
    """
 
1820
    chown = getattr(os, 'chown', None)
 
1821
    if chown is None:
 
1822
        return
 
1823
 
 
1824
    if src == None:
 
1825
        src = os.path.dirname(dst)
 
1826
        if src == '':
 
1827
            src = '.'
 
1828
 
 
1829
    try:
 
1830
        s = os.stat(src)
 
1831
        chown(dst, s.st_uid, s.st_gid)
 
1832
    except OSError, e:
 
1833
        trace.warning("Unable to copy ownership from '%s' to '%s': IOError: %s." % (src, dst, e))
 
1834
 
 
1835
 
 
1836
def mkdir_with_ownership(path, ownership_src=None):
 
1837
    """Create the directory 'path' with specified ownership.
 
1838
 
 
1839
    If ownership_src is given, copies (chown) usr/grp ownership
 
1840
    from 'ownership_src' to 'path'. If ownership_src is None, use the
 
1841
    containing dir ownership.
 
1842
    """
 
1843
    os.mkdir(path)
 
1844
    copy_ownership(path, ownership_src)
 
1845
 
 
1846
 
 
1847
def open_with_ownership(filename, mode='r', bufsize=-1, ownership_src=None):
 
1848
    """Open the file 'filename' with the specified ownership.
 
1849
 
 
1850
    If ownership_src is specified, copy usr/grp ownership from ownership_src
 
1851
    to filename. If ownership_src is None, copy ownership from containing
 
1852
    directory.
 
1853
    Returns the opened file object.
 
1854
    """
 
1855
    f = open(filename, mode, bufsize)
 
1856
    copy_ownership(filename, ownership_src)
 
1857
    return f
 
1858
 
 
1859
 
1815
1860
def path_prefix_key(path):
1816
1861
    """Generate a prefix-order path key for path.
1817
1862