~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/benchmarks/tree_creator/kernel_like.py

Merge updated set_parents api.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tree creators for kernel-like trees"""
18
18
 
 
19
import errno
19
20
import os
20
21
 
21
22
from bzrlib import (
22
23
    add,
23
24
    bzrdir,
24
25
    osutils,
 
26
    workingtree,
 
27
    xml5,
25
28
    )
26
29
 
27
30
from bzrlib.benchmarks.tree_creator import TreeCreator
81
84
                    files.append(prefix)
82
85
                    files.extend([prefix + str(foo) for foo in range(20)])
83
86
        cwd = osutils.getcwd()
 
87
        try:
 
88
            os.mkdir(root)
 
89
        except OSError, e:
 
90
            if e.errno not in (errno.EEXIST,):
 
91
                raise
84
92
        os.chdir(root)
85
93
        self._test.build_tree(files)
86
94
        os.chdir(cwd)
144
152
        if in_cache:
145
153
            self._protect_files(root+'/.bzr')
146
154
        return tree
 
155
 
 
156
 
 
157
class KernelLikeInventoryCreator(TreeCreator):
 
158
    """Return just the memory representation of a committed kernel-like tree"""
 
159
 
 
160
    def __init__(self, test):
 
161
        super(KernelLikeInventoryCreator, self).__init__(test,
 
162
            tree_name='kernel_like_inventory',
 
163
            link_working=True,
 
164
            link_bzr=True,
 
165
            hot_cache=True)
 
166
 
 
167
    def ensure_cached(self):
 
168
        """Make sure we have a cached version of the kernel-like inventory"""
 
169
        cache_dir = self._get_cache_dir()
 
170
        if cache_dir is None:
 
171
            return
 
172
 
 
173
        if self.is_cached():
 
174
            return
 
175
 
 
176
        committed_creator = KernelLikeCommittedTreeCreator(self._test,
 
177
                                                           link_working=True,
 
178
                                                           link_bzr=True,
 
179
                                                           hot_cache=False)
 
180
        committed_creator.ensure_cached()
 
181
        committed_cache_dir = committed_creator._get_cache_dir()
 
182
        committed_tree = workingtree.WorkingTree.open(committed_cache_dir)
 
183
        rev_tree = committed_tree.basis_tree()
 
184
        os.mkdir(cache_dir)
 
185
        f = open(cache_dir+'/inventory', 'wb')
 
186
        try:
 
187
            xml5.serializer_v5.write_inventory(rev_tree.inventory, f)
 
188
        finally:
 
189
            f.close()
 
190
 
 
191
    def create(self):
 
192
        """Create a kernel like inventory
 
193
 
 
194
        :return: An Inventory object.
 
195
        """
 
196
        cache_dir = self._get_cache_dir()
 
197
        if cache_dir is None:
 
198
            return self._create_and_return()
 
199
 
 
200
        self.ensure_cached()
 
201
        return self._open_cached(cache_dir)
 
202
 
 
203
    def _create_and_return(self):
 
204
        """Create a kernel-like tree, and return its inventory"""
 
205
        creator = KernelLikeCommittedTreeCreator(self._test,
 
206
                                                 link_working=True,
 
207
                                                 link_bzr=True,
 
208
                                                 hot_cache=False)
 
209
        tree = creator.create('.')
 
210
        return tree.basis_tree().inventory
 
211
 
 
212
    def _open_cached(self, cache_dir):
 
213
        f = open(cache_dir + '/inventory', 'rb')
 
214
        try:
 
215
            return xml5.serializer_v5.read_inventory(f)
 
216
        finally:
 
217
            f.close()