/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: John Arbash Meinel
  • Date: 2007-02-28 18:13:00 UTC
  • mto: (2255.11.3 dirstate)
  • mto: This revision was merged to the branch mainline in revision 2322.
  • Revision ID: john@arbash-meinel.com-20070228181300-3hpf53zdxpz16bjg
Add a _walkdirs_utf8 which returns utf8 paths instead of Unicode. Approx 20% faster in walking utf8 filesystems

Show diffs side-by-side

added added

removed removed

Lines of Context:
1106
1106
                pending.append(dir)
1107
1107
 
1108
1108
 
 
1109
def _walkdirs_utf8(top, prefix=""):
 
1110
    """Yield data about all the directories in a tree.
 
1111
 
 
1112
    This yields the same information as walkdirs() only each entry is yielded
 
1113
    in utf-8. On platforms which have a filesystem encoding of utf8 the paths
 
1114
    are returned as exact byte-strings.
 
1115
    """
 
1116
    _lstat = os.lstat
 
1117
    pending = []
 
1118
    _directory = _directory_kind
 
1119
    _listdir = os.listdir
 
1120
    _kind_from_mode = file_kind_from_stat_mode
 
1121
    if sys.platform == 'win32':
 
1122
        # We need to do the listdir using unicode paths, and then encode them
 
1123
        # into utf8.
 
1124
        assert False, 'not supported yet'
 
1125
    if sys.getfilesystemencoding() not in ('UTF-8', 'US-ASCII',
 
1126
                                           'ANSI_X3.4-1968'): # ascii
 
1127
        assert False, 'not supported yet'
 
1128
    # TODO: make these assert instead
 
1129
    if isinstance(top, unicode):
 
1130
        top = top.encode('utf8')
 
1131
    if isinstance(prefix, unicode):
 
1132
        prefix = prefix.encode('utf8')
 
1133
 
 
1134
    # The in-memory dirblocks should always have a prefix ending in '/'
 
1135
    # unless the prefix is '' then it should not have a trailing slash
 
1136
    pending = [(prefix, top)]
 
1137
    while pending:
 
1138
        relroot, top = pending.pop()
 
1139
        if relroot == '':
 
1140
            rel_prefix = ''
 
1141
        else:
 
1142
            rel_prefix = relroot + '/'
 
1143
        top_slash = top + '/'
 
1144
        # In plain for loop form
 
1145
        dirblock = []
 
1146
        for name in sorted(_listdir(top)):
 
1147
            abspath = top_slash + name
 
1148
            statvalue = _lstat(abspath)
 
1149
            kind = _kind_from_mode(statvalue.st_mode)
 
1150
            dirblock.append((rel_prefix + name, name, kind, statvalue, abspath))
 
1151
 
 
1152
        # 0 - relpath, 1- basename, 2- kind, 3- stat, 4-toppath
 
1153
        ## In list/generator comprehension form. On a 55k entry tree, this form
 
1154
        ## takes 1.75s versus 1.8s. So it is saving approx 50ms. Not a huge
 
1155
        ## savings, and may not be worth the complexity. And on smaller trees,
 
1156
        ## I've seen 115ms here versus 102ms in the for loop. So it isn't
 
1157
        ## always a win. This is just left for posterity.
 
1158
        # dirblock = [(rel_prefix + name, # relpath
 
1159
        #              name,           # basename
 
1160
        #              _kind_from_mode(statvalue.st_mode), # kind
 
1161
        #              statvalue,      # stat
 
1162
        #              abspath)        # path on disk
 
1163
        #                for name, abspath, statvalue in
 
1164
        #                    ((name, abspath, _lstat(abspath))
 
1165
        #                     for name, abspath in
 
1166
        #                     ((name, top_slash + name)
 
1167
        #                      for name in sorted(_listdir(top))
 
1168
        #                     )
 
1169
        #                    )
 
1170
        #            ]
 
1171
        yield (relroot, top), dirblock
 
1172
        # push the user specified dirs from dirblock
 
1173
        pending.extend((d[0], d[4])
 
1174
                       for d in reversed(dirblock)
 
1175
                       if d[2] == _directory)
 
1176
 
 
1177
 
1109
1178
def copy_tree(from_path, to_path, handlers={}):
1110
1179
    """Copy all of the entries in from_path into to_path.
1111
1180