~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Patch Queue Manager
  • Date: 2014-04-09 13:36:25 UTC
  • mfrom: (6592.1.2 1303879-py27-issues)
  • Revision ID: pqm@pqm.ubuntu.com-20140409133625-s24spv3kha2w2860
(vila) Fix python-2.7.6 test failures. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
# But those depend on its position within a particular inventory, and
24
24
# it would be nice not to need to hold the backpointer here.
25
25
 
 
26
from __future__ import absolute_import
 
27
 
26
28
# This should really be an id randomly assigned when the tree is
27
29
# created, but it's not for now.
28
30
ROOT_ID = "TREE_ROOT"
48
50
    )
49
51
 
50
52
from bzrlib.static_tuple import StaticTuple
 
53
from bzrlib.symbol_versioning import (
 
54
    deprecated_in,
 
55
    deprecated_method,
 
56
    )
51
57
 
52
58
 
53
59
class InventoryEntry(object):
100
106
    InventoryDirectory('2325', 'wibble', parent_id='123', revision=None)
101
107
    >>> i.path2id('src/wibble')
102
108
    '2325'
103
 
    >>> '2325' in i
104
 
    True
105
109
    >>> i.add(InventoryFile('2326', 'wibble.c', '2325'))
106
110
    InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None, revision=None)
107
111
    >>> i['2326']
170
174
        candidates = {}
171
175
        # identify candidate head revision ids.
172
176
        for inv in previous_inventories:
173
 
            if self.file_id in inv:
 
177
            if inv.has_id(self.file_id):
174
178
                ie = inv[self.file_id]
175
179
                if ie.revision in candidates:
176
180
                    # same revision value in two different inventories:
629
633
    inserted, other than through the Inventory API.
630
634
    """
631
635
 
632
 
    def __contains__(self, file_id):
633
 
        """True if this entry contains a file with given id.
634
 
 
635
 
        >>> inv = Inventory()
636
 
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
637
 
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
638
 
        >>> '123' in inv
639
 
        True
640
 
        >>> '456' in inv
641
 
        False
642
 
 
643
 
        Note that this method along with __iter__ are not encouraged for use as
644
 
        they are less clear than specific query methods - they may be rmeoved
645
 
        in the future.
646
 
        """
647
 
        return self.has_id(file_id)
648
 
 
649
636
    def has_filename(self, filename):
650
637
        return bool(self.path2id(filename))
651
638
 
756
743
            if (not yield_parents and specific_file_ids is not None and
757
744
                len(specific_file_ids) == 1):
758
745
                file_id = list(specific_file_ids)[0]
759
 
                if file_id in self:
 
746
                if self.has_id(file_id):
760
747
                    yield self.id2path(file_id), self[file_id]
761
748
                return
762
749
            from_dir = self.root
772
759
            parents = set()
773
760
            byid = self
774
761
            def add_ancestors(file_id):
775
 
                if file_id not in byid:
 
762
                if not byid.has_id(file_id):
776
763
                    return
777
764
                parent_id = byid[file_id].parent_id
778
765
                if parent_id is None:
845
832
            descend(self.root, u'')
846
833
        return accum
847
834
 
848
 
    def directories(self):
849
 
        """Return (path, entry) pairs for all directories, including the root.
850
 
        """
851
 
        accum = []
852
 
        def descend(parent_ie, parent_path):
853
 
            accum.append((parent_path, parent_ie))
854
 
 
855
 
            kids = [(ie.name, ie) for ie in parent_ie.children.itervalues() if ie.kind == 'directory']
856
 
            kids.sort()
857
 
 
858
 
            for name, child_ie in kids:
859
 
                child_path = osutils.pathjoin(parent_path, name)
860
 
                descend(child_ie, child_path)
861
 
        descend(self.root, u'')
862
 
        return accum
863
 
 
864
835
    def path2id(self, relpath):
865
836
        """Walk down through directories to return entry of last component.
866
837
 
962
933
 
963
934
    >>> inv.path2id('hello.c')
964
935
    '123-123'
965
 
    >>> '123-123' in inv
 
936
    >>> inv.has_id('123-123')
966
937
    True
967
938
 
968
939
    There are iterators over the contents:
1231
1202
        >>> inv = Inventory()
1232
1203
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
1233
1204
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
1234
 
        >>> '123' in inv
 
1205
        >>> inv.has_id('123')
1235
1206
        True
1236
1207
        >>> del inv['123']
1237
 
        >>> '123' in inv
 
1208
        >>> inv.has_id('123')
1238
1209
        False
1239
1210
        """
1240
1211
        ie = self[file_id]
1487
1458
            if entry.kind == 'directory':
1488
1459
                directories_to_expand.add(entry.file_id)
1489
1460
            interesting.add(entry.parent_id)
1490
 
            children_of_parent_id.setdefault(entry.parent_id, []
1491
 
                                             ).append(entry.file_id)
 
1461
            children_of_parent_id.setdefault(entry.parent_id, set()
 
1462
                                             ).add(entry.file_id)
1492
1463
 
1493
1464
        # Now, interesting has all of the direct parents, but not the
1494
1465
        # parents of those parents. It also may have some duplicates with
1502
1473
            next_parents = set()
1503
1474
            for entry in self._getitems(remaining_parents):
1504
1475
                next_parents.add(entry.parent_id)
1505
 
                children_of_parent_id.setdefault(entry.parent_id, []
1506
 
                                                 ).append(entry.file_id)
 
1476
                children_of_parent_id.setdefault(entry.parent_id, set()
 
1477
                                                 ).add(entry.file_id)
1507
1478
            # Remove any search tips we've already processed
1508
1479
            remaining_parents = next_parents.difference(interesting)
1509
1480
            interesting.update(remaining_parents)
1522
1493
            for entry in self._getitems(next_file_ids):
1523
1494
                if entry.kind == 'directory':
1524
1495
                    directories_to_expand.add(entry.file_id)
1525
 
                children_of_parent_id.setdefault(entry.parent_id, []
1526
 
                                                 ).append(entry.file_id)
 
1496
                children_of_parent_id.setdefault(entry.parent_id, set()
 
1497
                                                 ).add(entry.file_id)
1527
1498
        return interesting, children_of_parent_id
1528
1499
 
1529
1500
    def filter(self, specific_fileids):
2122
2093
    def path2id(self, relpath):
2123
2094
        """See CommonInventory.path2id()."""
2124
2095
        # TODO: perhaps support negative hits?
 
2096
        if isinstance(relpath, basestring):
 
2097
            names = osutils.splitpath(relpath)
 
2098
        else:
 
2099
            names = relpath
 
2100
            if relpath == []:
 
2101
                relpath = [""]
 
2102
            relpath = osutils.pathjoin(*relpath)
2125
2103
        result = self._path_to_fileid_cache.get(relpath, None)
2126
2104
        if result is not None:
2127
2105
            return result
2128
 
        if isinstance(relpath, basestring):
2129
 
            names = osutils.splitpath(relpath)
2130
 
        else:
2131
 
            names = relpath
2132
2106
        current_id = self.root_id
2133
2107
        if current_id is None:
2134
2108
            return None