~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: 2011-06-30 18:28:17 UTC
  • mfrom: (5967.10.2 test-cat)
  • Revision ID: pqm@pqm.ubuntu.com-20110630182817-83a5q9r9rxfkdn8r
(mbp) don't use subprocesses for testing cat (Martin Pool)

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
 
 
28
26
# This should really be an id randomly assigned when the tree is
29
27
# created, but it's not for now.
30
28
ROOT_ID = "TREE_ROOT"
633
631
    inserted, other than through the Inventory API.
634
632
    """
635
633
 
 
634
    @deprecated_method(deprecated_in((2, 4, 0)))
 
635
    def __contains__(self, file_id):
 
636
        """True if this entry contains a file with given id.
 
637
 
 
638
        >>> inv = Inventory()
 
639
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
 
640
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
 
641
        >>> inv.has_id('123')
 
642
        True
 
643
        >>> inv.has_id('456')
 
644
        False
 
645
 
 
646
        Note that this method along with __iter__ are not encouraged for use as
 
647
        they are less clear than specific query methods - they may be rmeoved
 
648
        in the future.
 
649
        """
 
650
        return self.has_id(file_id)
 
651
 
636
652
    def has_filename(self, filename):
637
653
        return bool(self.path2id(filename))
638
654
 
832
848
            descend(self.root, u'')
833
849
        return accum
834
850
 
 
851
    def directories(self):
 
852
        """Return (path, entry) pairs for all directories, including the root.
 
853
        """
 
854
        accum = []
 
855
        def descend(parent_ie, parent_path):
 
856
            accum.append((parent_path, parent_ie))
 
857
 
 
858
            kids = [(ie.name, ie) for ie in parent_ie.children.itervalues() if ie.kind == 'directory']
 
859
            kids.sort()
 
860
 
 
861
            for name, child_ie in kids:
 
862
                child_path = osutils.pathjoin(parent_path, name)
 
863
                descend(child_ie, child_path)
 
864
        descend(self.root, u'')
 
865
        return accum
 
866
 
835
867
    def path2id(self, relpath):
836
868
        """Walk down through directories to return entry of last component.
837
869
 
1458
1490
            if entry.kind == 'directory':
1459
1491
                directories_to_expand.add(entry.file_id)
1460
1492
            interesting.add(entry.parent_id)
1461
 
            children_of_parent_id.setdefault(entry.parent_id, set()
1462
 
                                             ).add(entry.file_id)
 
1493
            children_of_parent_id.setdefault(entry.parent_id, []
 
1494
                                             ).append(entry.file_id)
1463
1495
 
1464
1496
        # Now, interesting has all of the direct parents, but not the
1465
1497
        # parents of those parents. It also may have some duplicates with
1473
1505
            next_parents = set()
1474
1506
            for entry in self._getitems(remaining_parents):
1475
1507
                next_parents.add(entry.parent_id)
1476
 
                children_of_parent_id.setdefault(entry.parent_id, set()
1477
 
                                                 ).add(entry.file_id)
 
1508
                children_of_parent_id.setdefault(entry.parent_id, []
 
1509
                                                 ).append(entry.file_id)
1478
1510
            # Remove any search tips we've already processed
1479
1511
            remaining_parents = next_parents.difference(interesting)
1480
1512
            interesting.update(remaining_parents)
1493
1525
            for entry in self._getitems(next_file_ids):
1494
1526
                if entry.kind == 'directory':
1495
1527
                    directories_to_expand.add(entry.file_id)
1496
 
                children_of_parent_id.setdefault(entry.parent_id, set()
1497
 
                                                 ).add(entry.file_id)
 
1528
                children_of_parent_id.setdefault(entry.parent_id, []
 
1529
                                                 ).append(entry.file_id)
1498
1530
        return interesting, children_of_parent_id
1499
1531
 
1500
1532
    def filter(self, specific_fileids):
2093
2125
    def path2id(self, relpath):
2094
2126
        """See CommonInventory.path2id()."""
2095
2127
        # 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)
2103
2128
        result = self._path_to_fileid_cache.get(relpath, None)
2104
2129
        if result is not None:
2105
2130
            return result
 
2131
        if isinstance(relpath, basestring):
 
2132
            names = osutils.splitpath(relpath)
 
2133
        else:
 
2134
            names = relpath
2106
2135
        current_id = self.root_id
2107
2136
        if current_id is None:
2108
2137
            return None