~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

extend list_files() with from_dir and recursive parameters

Show diffs side-by-side

added added

removed removed

Lines of Context:
1115
1115
    def _kind(self, relpath):
1116
1116
        return osutils.file_kind(self.abspath(relpath))
1117
1117
 
1118
 
    def list_files(self, include_root=False):
1119
 
        """Recursively list all files as (path, class, kind, id, entry).
 
1118
    def list_files(self, include_root=False, from_dir=None, recursive=True):
 
1119
        """List all files as (path, class, kind, id, entry).
1120
1120
 
1121
1121
        Lists, but does not descend into unversioned directories.
1122
 
 
1123
1122
        This does not include files that have been deleted in this
1124
 
        tree.
 
1123
        tree. Skips the control directory.
1125
1124
 
1126
 
        Skips the control directory.
 
1125
        :param include_root: if True, do not return an entry for the root
 
1126
        :param from_dir: start from this directory or None for the root
 
1127
        :param recursive: whether to recurse into subdirectories or not
1127
1128
        """
1128
1129
        # list_files is an iterator, so @needs_read_lock doesn't work properly
1129
1130
        # with it. So callers should be careful to always read_lock the tree.
1131
1132
            raise errors.ObjectNotLocked(self)
1132
1133
 
1133
1134
        inv = self.inventory
1134
 
        if include_root is True:
 
1135
        if from_dir is None and include_root is True:
1135
1136
            yield ('', 'V', 'directory', inv.root.file_id, inv.root)
1136
1137
        # Convert these into local objects to save lookup times
1137
1138
        pathjoin = osutils.pathjoin
1144
1145
        fk_entries = {'directory':TreeDirectory, 'file':TreeFile, 'symlink':TreeLink}
1145
1146
 
1146
1147
        # directory file_id, relative path, absolute path, reverse sorted children
1147
 
        children = os.listdir(self.basedir)
 
1148
        if from_dir is not None:
 
1149
            from_dir_id = inv.path2id(from_dir)
 
1150
            from_dir_abspath = pathjoin(self.basedir, from_dir)
 
1151
        else:
 
1152
            from_dir_id = inv.root.file_id
 
1153
            from_dir_abspath = self.basedir
 
1154
        children = os.listdir(from_dir_abspath)
1148
1155
        children.sort()
1149
1156
        # jam 20060527 The kernel sized tree seems equivalent whether we
1150
1157
        # use a deque and popleft to keep them sorted, or if we use a plain
1151
1158
        # list and just reverse() them.
1152
1159
        children = collections.deque(children)
1153
 
        stack = [(inv.root.file_id, u'', self.basedir, children)]
 
1160
        stack = [(from_dir_id, u'', from_dir_abspath, children)]
1154
1161
        while stack:
1155
1162
            from_dir_id, from_dir_relpath, from_dir_abspath, children = stack[-1]
1156
1163
 
1214
1221
                if fk != 'directory':
1215
1222
                    continue
1216
1223
 
1217
 
                # But do this child first
1218
 
                new_children = os.listdir(fap)
1219
 
                new_children.sort()
1220
 
                new_children = collections.deque(new_children)
1221
 
                stack.append((f_ie.file_id, fp, fap, new_children))
1222
 
                # Break out of inner loop,
1223
 
                # so that we start outer loop with child
1224
 
                break
 
1224
                # But do this child first if recursing down
 
1225
                if recursive:
 
1226
                    new_children = os.listdir(fap)
 
1227
                    new_children.sort()
 
1228
                    new_children = collections.deque(new_children)
 
1229
                    stack.append((f_ie.file_id, fp, fap, new_children))
 
1230
                    # Break out of inner loop,
 
1231
                    # so that we start outer loop with child
 
1232
                    break
1225
1233
            else:
1226
1234
                # if we finished all children, pop it off the stack
1227
1235
                stack.pop()