~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: John Arbash Meinel
  • Date: 2009-07-29 21:35:05 UTC
  • mfrom: (4576 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4577.
  • Revision ID: john@arbash-meinel.com-20090729213505-tkqsvy1zfpocu75w
Merge bzr.dev 4576 in prep for NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
722
722
               _format_date(t, offset, timezone, date_fmt, show_offset)
723
723
    date_str = time.strftime(date_fmt, tt)
724
724
    if not isinstance(date_str, unicode):
725
 
        date_str = date_str.decode(bzrlib.user_encoding, 'replace')
 
725
        date_str = date_str.decode(get_user_encoding(), 'replace')
726
726
    return date_str + offset_str
727
727
 
728
728
def _format_date(t, offset, timezone, date_fmt, show_offset):
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:
1022
1040
 
1023
1041
    s = []
1024
1042
    head = rp
1025
 
    while len(head) >= len(base):
 
1043
    while True:
 
1044
        if len(head) <= len(base) and head != base:
 
1045
            raise errors.PathNotChild(rp, base)
1026
1046
        if head == base:
1027
1047
            break
1028
 
        head, tail = os.path.split(head)
 
1048
        head, tail = split(head)
1029
1049
        if tail:
1030
 
            s.insert(0, tail)
1031
 
    else:
1032
 
        raise errors.PathNotChild(rp, base)
 
1050
            s.append(tail)
1033
1051
 
1034
1052
    if s:
1035
 
        return pathjoin(*s)
 
1053
        return pathjoin(*reversed(s))
1036
1054
    else:
1037
1055
        return ''
1038
1056