/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Mark Hammond
  • Date: 2008-10-28 04:30:16 UTC
  • mto: (3932.3.1 cicp-1.11)
  • mto: This revision was merged to the branch mainline in revision 3937.
  • Revision ID: mhammond@skippinet.com.au-20081028043016-4etcd37j1f700xan
 Add canonical_relpath api function

Show diffs side-by-side

added added

removed removed

Lines of Context:
941
941
        return ''
942
942
 
943
943
 
 
944
def _win32_canonical_relpath(base, path):
 
945
    """Return the canonical path relative to base.
 
946
 
 
947
    Like relpath, but on case-insensitive-case-preserving file-systems, this
 
948
    will return the relpath as stored on the file-system rather than in the case
 
949
    specified in the input string, for all existing portions of the path.
 
950
 
 
951
    TODO: it should be possible to optimize this by using the win32 API
 
952
    FindFiles function to look for the specified name - but using os.listdir()
 
953
    still gives us the correct semantics in the short term.
 
954
    """
 
955
    rel = relpath(base, path)
 
956
    # '.' will have been turned into ''
 
957
    if not rel:
 
958
        return rel
 
959
 
 
960
    abs_base = abspath(base)
 
961
    current = abs_base
 
962
    _listdir = os.listdir
 
963
 
 
964
    # use an explicit iterator so we can easily consume the rest on early exit.
 
965
    bit_iter = iter(rel.replace('\\', '/').split('/'))
 
966
    for bit in bit_iter:
 
967
        lbit = bit.lower()
 
968
        for look in _listdir(current):
 
969
            if lbit == look.lower():
 
970
                current = pathjoin(current, look)
 
971
                break
 
972
        else:
 
973
            # got to the end, nothing matched, so we just return the
 
974
            # non-existing bits as they were specified (the filename may be
 
975
            # the target of a move, for example).
 
976
            current = pathjoin(current, bit, *list(bit_iter))
 
977
            break
 
978
    return current[len(abs_base)+1:]
 
979
 
 
980
if sys.platform == "win32":
 
981
    canonical_relpath = _win32_canonical_relpath
 
982
else:
 
983
    canonical_relpath = relpath
 
984
 
 
985
 
944
986
def safe_unicode(unicode_or_utf8_string):
945
987
    """Coerce unicode_or_utf8_string into unicode.
946
988