/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: Jelmer Vernooij
  • Date: 2009-05-28 16:04:39 UTC
  • mfrom: (4387 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4405.
  • Revision ID: jelmer@samba.org-20090528160439-4z0xlrk5nejobm7q
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
O_BINARY = getattr(os, 'O_BINARY', 0)
78
78
 
79
79
 
 
80
def get_unicode_argv():
 
81
    try:
 
82
        user_encoding = get_user_encoding()
 
83
        return [a.decode(user_encoding) for a in sys.argv[1:]]
 
84
    except UnicodeDecodeError:
 
85
        raise errors.BzrError(("Parameter '%r' is unsupported by the current "
 
86
                                                            "encoding." % a))
 
87
 
 
88
 
80
89
def make_readonly(filename):
81
90
    """Make a filename read-only."""
82
91
    mod = os.lstat(filename).st_mode
97
106
 
98
107
    :param paths: A container (and hence not None) of paths.
99
108
    :return: A set of paths sufficient to include everything in paths via
100
 
        is_inside_any, drawn from the paths parameter.
 
109
        is_inside, drawn from the paths parameter.
101
110
    """
102
 
    search_paths = set()
103
 
    paths = set(paths)
104
 
    for path in paths:
105
 
        other_paths = paths.difference([path])
106
 
        if not is_inside_any(other_paths, path):
107
 
            # this is a top level path, we must check it.
108
 
            search_paths.add(path)
109
 
    return search_paths
 
111
    if len(paths) < 2:
 
112
        return set(paths)
 
113
 
 
114
    def sort_key(path):
 
115
        return path.split('/')
 
116
    sorted_paths = sorted(list(paths), key=sort_key)
 
117
 
 
118
    search_paths = [sorted_paths[0]]
 
119
    for path in sorted_paths[1:]:
 
120
        if not is_inside(search_paths[-1], path):
 
121
            # This path is unique, add it
 
122
            search_paths.append(path)
 
123
 
 
124
    return set(search_paths)
110
125
 
111
126
 
112
127
_QUOTE_RE = None
384
399
    def rmtree(path, ignore_errors=False, onerror=_win32_delete_readonly):
385
400
        """Replacer for shutil.rmtree: could remove readonly dirs/files"""
386
401
        return shutil.rmtree(path, ignore_errors, onerror)
 
402
 
 
403
    f = win32utils.get_unicode_argv     # special function or None
 
404
    if f is not None:
 
405
        get_unicode_argv = f
 
406
 
387
407
elif sys.platform == 'darwin':
388
408
    getcwd = _mac_getcwd
389
409
 
847
867
    return pathjoin(*p)
848
868
 
849
869
 
 
870
def parent_directories(filename):
 
871
    """Return the list of parent directories, deepest first.
 
872
    
 
873
    For example, parent_directories("a/b/c") -> ["a/b", "a"].
 
874
    """
 
875
    parents = []
 
876
    parts = splitpath(dirname(filename))
 
877
    while parts:
 
878
        parents.append(joinpath(parts))
 
879
        parts.pop()
 
880
    return parents
 
881
 
 
882
 
850
883
try:
851
884
    from bzrlib._chunks_to_lines_pyx import chunks_to_lines
852
885
except ImportError:
925
958
            and sys.platform not in ('cygwin', 'win32'))
926
959
 
927
960
 
 
961
def readlink(abspath):
 
962
    """Return a string representing the path to which the symbolic link points.
 
963
 
 
964
    :param abspath: The link absolute unicode path.
 
965
 
 
966
    This his guaranteed to return the symbolic link in unicode in all python
 
967
    versions.
 
968
    """
 
969
    link = abspath.encode(_fs_enc)
 
970
    target = os.readlink(link)
 
971
    target = target.decode(_fs_enc)
 
972
    return target
 
973
 
 
974
 
928
975
def contains_whitespace(s):
929
976
    """True if there are any whitespace characters in s."""
930
977
    # string.whitespace can include '\xa0' in certain locales, because it is
1030
1077
    return current[len(abs_base)+1:]
1031
1078
 
1032
1079
# XXX - TODO - we need better detection/integration of case-insensitive
1033
 
# file-systems; Linux often sees FAT32 devices, for example, so could
1034
 
# probably benefit from the same basic support there.  For now though, only
1035
 
# Windows gets that support, and it gets it for *all* file-systems!
1036
 
if sys.platform == "win32":
 
1080
# file-systems; Linux often sees FAT32 devices (or NFS-mounted OSX
 
1081
# filesystems), for example, so could probably benefit from the same basic
 
1082
# support there.  For now though, only Windows and OSX get that support, and
 
1083
# they get it for *all* file-systems!
 
1084
if sys.platform in ('win32', 'darwin'):
1037
1085
    canonical_relpath = _cicp_canonical_relpath
1038
1086
else:
1039
1087
    canonical_relpath = relpath
1391
1439
            #       for win98 anyway.
1392
1440
            try:
1393
1441
                from bzrlib._walkdirs_win32 import Win32ReadDir
1394
 
            except ImportError:
1395
 
                _selected_dir_reader = UnicodeDirReader()
1396
 
            else:
1397
1442
                _selected_dir_reader = Win32ReadDir()
1398
 
        elif fs_encoding not in ('UTF-8', 'US-ASCII', 'ANSI_X3.4-1968'):
 
1443
            except ImportError:
 
1444
                pass
 
1445
        elif fs_encoding in ('UTF-8', 'US-ASCII', 'ANSI_X3.4-1968'):
1399
1446
            # ANSI_X3.4-1968 is a form of ASCII
1400
 
            _selected_dir_reader = UnicodeDirReader()
1401
 
        else:
1402
1447
            try:
1403
1448
                from bzrlib._readdir_pyx import UTF8DirReader
1404
 
            except ImportError:
1405
 
                # No optimised code path
1406
 
                _selected_dir_reader = UnicodeDirReader()
1407
 
            else:
1408
1449
                _selected_dir_reader = UTF8DirReader()
 
1450
            except ImportError:
 
1451
                pass
 
1452
 
 
1453
    if _selected_dir_reader is None:
 
1454
        # Fallback to the python version
 
1455
        _selected_dir_reader = UnicodeDirReader()
 
1456
 
1409
1457
    # 0 - relpath, 1- basename, 2- kind, 3- stat, 4-toppath
1410
1458
    # But we don't actually uses 1-3 in pending, so set them to None
1411
1459
    pending = [[_selected_dir_reader.top_prefix_to_starting_dir(top, prefix)]]
1741
1789
 
1742
1790
def re_compile_checked(re_string, flags=0, where=""):
1743
1791
    """Return a compiled re, or raise a sensible error.
1744
 
    
 
1792
 
1745
1793
    This should only be used when compiling user-supplied REs.
1746
1794
 
1747
1795
    :param re_string: Text form of regular expression.
1748
1796
    :param flags: eg re.IGNORECASE
1749
 
    :param where: Message explaining to the user the context where 
 
1797
    :param where: Message explaining to the user the context where
1750
1798
        it occurred, eg 'log search filter'.
1751
1799
    """
1752
1800
    # from https://bugs.launchpad.net/bzr/+bug/251352