~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-10-14 03:18:36 UTC
  • mfrom: (3363.17.29 merge-into2)
  • Revision ID: pqm@pqm.ubuntu.com-20081014031836-0pn8u98igc7gvtv0
Allow merging into PreviewTrees

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
from bzrlib.progress import DummyProgress, ProgressPhase
49
49
from bzrlib.symbol_versioning import (
50
50
        deprecated_function,
 
51
        deprecated_in,
51
52
        )
52
53
from bzrlib.trace import mutter, warning
53
54
from bzrlib import tree
1458
1459
        self._final_paths = FinalPaths(transform)
1459
1460
        self.__by_parent = None
1460
1461
        self._parent_ids = []
 
1462
        self._all_children_cache = {}
 
1463
        self._path2trans_id_cache = {}
 
1464
        self._final_name_cache = {}
1461
1465
 
1462
1466
    def _changes(self, file_id):
1463
1467
        for changes in self._transform.iter_changes():
1550
1554
            return self._transform._tree.has_id(file_id)
1551
1555
 
1552
1556
    def _path2trans_id(self, path):
 
1557
        # We must not use None here, because that is a valid value to store.
 
1558
        trans_id = self._path2trans_id_cache.get(path, object)
 
1559
        if trans_id is not object:
 
1560
            return trans_id
1553
1561
        segments = splitpath(path)
1554
1562
        cur_parent = self._transform.root
1555
1563
        for cur_segment in segments:
1556
1564
            for child in self._all_children(cur_parent):
1557
 
                if self._transform.final_name(child) == cur_segment:
 
1565
                final_name = self._final_name_cache.get(child)
 
1566
                if final_name is None:
 
1567
                    final_name = self._transform.final_name(child)
 
1568
                    self._final_name_cache[child] = final_name
 
1569
                if final_name == cur_segment:
1558
1570
                    cur_parent = child
1559
1571
                    break
1560
1572
            else:
 
1573
                self._path2trans_id_cache[path] = None
1561
1574
                return None
 
1575
        self._path2trans_id_cache[path] = cur_parent
1562
1576
        return cur_parent
1563
1577
 
1564
1578
    def path2id(self, path):
1572
1586
            raise errors.NoSuchId(self, file_id)
1573
1587
 
1574
1588
    def _all_children(self, trans_id):
 
1589
        children = self._all_children_cache.get(trans_id)
 
1590
        if children is not None:
 
1591
            return children
1575
1592
        children = set(self._transform.iter_tree_children(trans_id))
1576
1593
        # children in the _new_parent set are provided by _by_parent.
1577
1594
        children.difference_update(self._transform._new_parent.keys())
1578
1595
        children.update(self._by_parent.get(trans_id, []))
 
1596
        self._all_children_cache[trans_id] = children
1579
1597
        return children
1580
1598
 
1581
1599
    def iter_children(self, file_id):
1690
1708
        try:
1691
1709
            return self._transform._new_executability[trans_id]
1692
1710
        except KeyError:
1693
 
            return self._transform._tree.is_executable(file_id, path)
 
1711
            try:
 
1712
                return self._transform._tree.is_executable(file_id, path)
 
1713
            except OSError, e:
 
1714
                if e.errno == errno.ENOENT:
 
1715
                    return False
 
1716
                raise
 
1717
            except errors.NoSuchId:
 
1718
                return False
1694
1719
 
1695
1720
    def path_content_summary(self, path):
1696
1721
        trans_id = self._path2trans_id(path)
2140
2165
        raise errors.BadFileKindError(name, kind)
2141
2166
 
2142
2167
 
 
2168
@deprecated_function(deprecated_in((1, 9, 0)))
2143
2169
def create_by_entry(tt, entry, tree, trans_id, lines=None, mode_id=None):
2144
 
    """Create new file contents according to an inventory entry."""
 
2170
    """Create new file contents according to an inventory entry.
 
2171
 
 
2172
    DEPRECATED.  Use create_from_tree instead.
 
2173
    """
2145
2174
    if entry.kind == "file":
2146
2175
        if lines is None:
2147
2176
            lines = tree.get_file(entry.file_id).readlines()
2152
2181
        tt.create_directory(trans_id)
2153
2182
 
2154
2183
 
 
2184
def create_from_tree(tt, trans_id, tree, file_id, bytes=None):
 
2185
    """Create new file contents according to tree contents."""
 
2186
    kind = tree.kind(file_id)
 
2187
    if kind == 'directory':
 
2188
        tt.create_directory(trans_id)
 
2189
    elif kind == "file":
 
2190
        if bytes is None:
 
2191
            tree_file = tree.get_file(file_id)
 
2192
            try:
 
2193
                bytes = tree_file.readlines()
 
2194
            finally:
 
2195
                tree_file.close()
 
2196
        tt.create_file(bytes, trans_id)
 
2197
    elif kind == "symlink":
 
2198
        tt.create_symlink(tree.get_symlink_target(file_id), trans_id)
 
2199
    else:
 
2200
        raise AssertionError('Unknown kind %r' % kind)
 
2201
 
 
2202
 
2155
2203
def create_entry_executability(tt, entry, trans_id):
2156
2204
    """Set the executability of a trans_id according to an inventory entry"""
2157
2205
    if entry.kind == "file":