~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2009-06-30 04:08:12 UTC
  • mfrom: (4440.1.3 bzr-trunk)
  • mto: This revision was merged to the branch mainline in revision 4491.
  • Revision ID: mbp@sourcefrog.net-20090630040812-c5070bmkzjo6546s
merge fix for forcing readonly deletion, and tweak

Show diffs side-by-side

added added

removed removed

Lines of Context:
927
927
        shutil.copyfile(src, dest)
928
928
 
929
929
 
930
 
# Look Before You Leap (LBYL) is appropriate here instead of Easier to Ask for
931
 
# Forgiveness than Permission (EAFP) because:
932
 
# - root can damage a solaris file system by using unlink,
933
 
# - unlink raises different exceptions on different OSes (linux: EISDIR, win32:
934
 
#   EACCES, OSX: EPERM) when invoked on a directory.
935
930
def delete_any(path):
936
 
    """Delete a file or directory."""
 
931
    """Delete a file, symlink or directory.  
 
932
    
 
933
    Will delete even if readonly.
 
934
    """
 
935
    try:
 
936
       _delete_file_or_dir(path)
 
937
    except (OSError, IOError), e:
 
938
        if e.errno in (errno.EPERM, errno.EACCES):
 
939
            # make writable and try again
 
940
            try:
 
941
                make_writable(path)
 
942
            except (OSError, IOError):
 
943
                pass
 
944
            _delete_file_or_dir(path)
 
945
        else:
 
946
            raise
 
947
 
 
948
 
 
949
def _delete_file_or_dir(path):
 
950
    # Look Before You Leap (LBYL) is appropriate here instead of Easier to Ask for
 
951
    # Forgiveness than Permission (EAFP) because:
 
952
    # - root can damage a solaris file system by using unlink,
 
953
    # - unlink raises different exceptions on different OSes (linux: EISDIR, win32:
 
954
    #   EACCES, OSX: EPERM) when invoked on a directory.
937
955
    if isdir(path): # Takes care of symlinks
938
956
        os.rmdir(path)
939
957
    else: