~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-05-19 06:14:38 UTC
  • mfrom: (1704.2.23 bzr.mbp.integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060519061438-6300caf3926c3cff
(mbp) small fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
ROOT_ID = "TREE_ROOT"
29
29
 
30
30
 
31
 
import collections
32
31
import os.path
33
32
import re
34
33
import sys
669
668
 
670
669
    def _read_tree_state(self, path, work_tree):
671
670
        """See InventoryEntry._read_tree_state."""
672
 
        self.text_sha1 = work_tree.get_file_sha1(self.file_id, path=path)
673
 
        self.executable = work_tree.is_executable(self.file_id, path=path)
 
671
        self.text_sha1 = work_tree.get_file_sha1(self.file_id)
 
672
        self.executable = work_tree.is_executable(self.file_id)
674
673
 
675
674
    def _forget_tree_state(self):
676
675
        self.text_sha1 = None
828
827
    May also look up by name:
829
828
 
830
829
    >>> [x[0] for x in inv.iter_entries()]
831
 
    [u'hello.c']
 
830
    ['hello.c']
832
831
    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
833
832
    >>> inv.add(InventoryFile('123-123', 'hello.c', ROOT_ID))
834
833
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT-12345678-12345678')
881
880
        elif isinstance(from_dir, basestring):
882
881
            from_dir = self._byid[from_dir]
883
882
            
884
 
        # unrolling the recursive called changed the time from
885
 
        # 440ms/663ms (inline/total) to 116ms/116ms
886
 
        children = from_dir.children.items()
887
 
        children.sort()
888
 
        children = collections.deque(children)
889
 
        stack = [(u'', children)]
890
 
        while stack:
891
 
            from_dir_relpath, children = stack[-1]
892
 
 
893
 
            while children:
894
 
                name, ie = children.popleft()
895
 
 
896
 
                # we know that from_dir_relpath never ends in a slash
897
 
                # and 'f' doesn't begin with one, we can do a string op, rather
898
 
                # than the checks of pathjoin(), though this means that all paths
899
 
                # start with a slash
900
 
                path = from_dir_relpath + '/' + name
901
 
 
902
 
                yield path[1:], ie
903
 
 
904
 
                if ie.kind != 'directory':
905
 
                    continue
906
 
 
907
 
                # But do this child first
908
 
                new_children = ie.children.items()
909
 
                new_children.sort()
910
 
                new_children = collections.deque(new_children)
911
 
                stack.append((path, new_children))
912
 
                # Break out of inner loop, so that we start outer loop with child
913
 
                break
914
 
            else:
915
 
                # if we finished all children, pop it off the stack
916
 
                stack.pop()
 
883
        kids = from_dir.children.items()
 
884
        kids.sort()
 
885
        for name, ie in kids:
 
886
            yield name, ie
 
887
            if ie.kind == 'directory':
 
888
                for cn, cie in self.iter_entries(from_dir=ie.file_id):
 
889
                    yield pathjoin(name, cn), cie
 
890
 
917
891
 
918
892
    def entries(self):
919
893
        """Return list of (path, ie) for all entries except the root.
1019
993
        return entry
1020
994
 
1021
995
 
1022
 
    def add_path(self, relpath, kind, file_id=None, parent_id=None):
 
996
    def add_path(self, relpath, kind, file_id=None):
1023
997
        """Add entry from a path.
1024
998
 
1025
999
        The immediate parent must already be versioned.
1035
1009
            self._byid = {self.root.file_id: self.root}
1036
1010
            return
1037
1011
        else:
 
1012
            if file_id is None:
 
1013
                file_id = bzrlib.workingtree.gen_file_id(parts[-1])
1038
1014
            parent_path = parts[:-1]
1039
1015
            parent_id = self.path2id(parent_path)
1040
1016
            if parent_id == None:
1041
1017
                raise NotVersionedError(path=parent_path)
1042
 
        ie = make_entry(kind, parts[-1], parent_id, file_id)
 
1018
        if kind == 'directory':
 
1019
            ie = InventoryDirectory(file_id, parts[-1], parent_id)
 
1020
        elif kind == 'file':
 
1021
            ie = InventoryFile(file_id, parts[-1], parent_id)
 
1022
        elif kind == 'symlink':
 
1023
            ie = InventoryLink(file_id, parts[-1], parent_id)
 
1024
        else:
 
1025
            raise BzrError("unknown kind %r" % kind)
1043
1026
        return self.add(ie)
1044
1027
 
 
1028
 
1045
1029
    def __delitem__(self, file_id):
1046
1030
        """Remove entry by id.
1047
1031
 
1143
1127
        This returns the entry of the last component in the path,
1144
1128
        which may be either a file or a directory.
1145
1129
 
1146
 
        Returns None IFF the path is not found.
 
1130
        Returns None iff the path is not found.
1147
1131
        """
1148
1132
        if isinstance(name, types.StringTypes):
1149
1133
            name = splitpath(name)
1150
1134
 
1151
 
        # mutter("lookup path %r" % name)
 
1135
        mutter("lookup path %r" % name)
1152
1136
 
1153
1137
        parent = self.root
1154
1138
        for f in name:
1202
1186
        file_ie.parent_id = new_parent_id
1203
1187
 
1204
1188
 
1205
 
def make_entry(kind, name, parent_id, file_id=None):
1206
 
    """Create an inventory entry.
1207
 
 
1208
 
    :param kind: the type of inventory entry to create.
1209
 
    :param name: the basename of the entry.
1210
 
    :param parent_id: the parent_id of the entry.
1211
 
    :param file_id: the file_id to use. if None, one will be created.
1212
 
    """
1213
 
    if file_id is None:
1214
 
        file_id = bzrlib.workingtree.gen_file_id(name)
1215
 
    if kind == 'directory':
1216
 
        return InventoryDirectory(file_id, name, parent_id)
1217
 
    elif kind == 'file':
1218
 
        return InventoryFile(file_id, name, parent_id)
1219
 
    elif kind == 'symlink':
1220
 
        return InventoryLink(file_id, name, parent_id)
1221
 
    else:
1222
 
        raise BzrError("unknown kind %r" % kind)
1223
 
 
1224
1189
 
1225
1190
 
1226
1191
_NAME_RE = None