/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1908.2.16 by John Arbash Meinel
Move all the new TreeCreator classes into separate files.
1
# Copyright (C) 2006 by Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tree creators for kernel-like trees"""
18
19
import os
20
21
from bzrlib import (
22
    add,
23
    bzrdir,
24
    osutils,
1934.1.16 by John Arbash Meinel
Add a cache for a kernel-like inventory
25
    workingtree,
26
    xml5,
1908.2.16 by John Arbash Meinel
Move all the new TreeCreator classes into separate files.
27
    )
28
29
from bzrlib.benchmarks.tree_creator import TreeCreator
30
31
32
class KernelLikeTreeCreator(TreeCreator):
33
    """Create a basic tree with ~10k unversioned files""" 
34
35
    def __init__(self, test, link_working=False, url=None):
36
        super(KernelLikeTreeCreator, self).__init__(test,
37
            tree_name='kernel_like_tree',
38
            link_working=link_working,
39
            link_bzr=False)
40
41
        self._url = url
42
43
    def create(self, root):
44
        """Create all the kernel files in the given location.
45
46
        This is overloaded for compatibility reasons.
47
        """
48
        if self._url is not None:
49
            b = bzrdir.BzrDir.create_branch_convenience(self._url)
50
            d = bzrdir.BzrDir.create(root)
51
            bzrlib.branch.BranchReferenceFormat().initialize(d, b)
52
            tree = d.create_workingtree()
53
        else:
54
            tree = bzrdir.BzrDir.create_standalone_workingtree(root)
55
56
        if not self._link_working or not self.is_caching_enabled():
57
            # Turns out that 'shutil.copytree()' is no faster than
58
            # just creating them. Probably the python overhead.
59
            # Plain _make_kernel_files takes 3-5s
60
            # cp -a takes 3s
61
            # using hardlinks takes < 1s.
62
            self._create_tree(root=root, in_cache=False)
63
            return tree
64
65
        self.ensure_cached()
66
        cache_dir = self._get_cache_dir()
67
        osutils.copy_tree(cache_dir, root,
68
                          handlers={'file':os.link})
69
        return tree
70
71
    def _create_tree(self, root, in_cache=False):
72
        # a kernel tree has ~10000 and 500 directory, with most files around 
73
        # 3-4 levels deep. 
74
        # we simulate this by three levels of dirs named 0-7, givin 512 dirs,
75
        # and 20 files each.
76
        files = []
77
        for outer in range(8):
78
            files.append("%s/" % outer)
79
            for middle in range(8):
80
                files.append("%s/%s/" % (outer, middle))
81
                for inner in range(8):
82
                    prefix = "%s/%s/%s/" % (outer, middle, inner)
83
                    files.append(prefix)
84
                    files.extend([prefix + str(foo) for foo in range(20)])
85
        cwd = osutils.getcwd()
86
        os.chdir(root)
87
        self._test.build_tree(files)
88
        os.chdir(cwd)
89
        if in_cache:
90
            self._protect_files(root)
91
92
93
class KernelLikeAddedTreeCreator(TreeCreator):
94
95
    def __init__(self, test, link_working=False, hot_cache=True):
96
        super(KernelLikeAddedTreeCreator, self).__init__(test,
97
            tree_name='kernel_like_added_tree',
98
            link_working=link_working,
99
            link_bzr=False,
100
            hot_cache=hot_cache)
101
102
    def _create_tree(self, root, in_cache=False):
103
        """Create a kernel-like tree with the all files added
104
105
        :param root: The root directory to create the files
106
        :param in_cache: Is this being created in the cache dir?
107
        """
108
        kernel_creator = KernelLikeTreeCreator(self._test,
109
                                               link_working=in_cache)
110
        tree = kernel_creator.create(root=root)
111
112
        # Add everything to it
113
        tree.lock_write()
114
        try:
115
            add.smart_add_tree(tree, [root], recurse=True, save=True)
116
            if in_cache:
117
                self._protect_files(root+'/.bzr')
118
        finally:
119
            tree.unlock()
120
        return tree
121
122
123
class KernelLikeCommittedTreeCreator(TreeCreator):
124
    """Create a tree with ~10K files, and a single commit adding all of them"""
125
126
    def __init__(self, test, link_working=False, link_bzr=False,
127
                 hot_cache=True):
128
        super(KernelLikeCommittedTreeCreator, self).__init__(test,
129
            tree_name='kernel_like_committed_tree',
130
            link_working=link_working,
131
            link_bzr=link_bzr,
132
            hot_cache=hot_cache)
133
134
    def _create_tree(self, root, in_cache=False):
135
        """Create a kernel-like tree with all files committed
136
137
        :param root: The root directory to create the files
138
        :param in_cache: Is this being created in the cache dir?
139
        """
140
        kernel_creator = KernelLikeAddedTreeCreator(self._test,
141
                                                    link_working=in_cache,
142
                                                    hot_cache=(not in_cache))
143
        tree = kernel_creator.create(root=root)
144
        tree.commit('first post', rev_id='r1')
145
146
        if in_cache:
147
            self._protect_files(root+'/.bzr')
148
        return tree
1934.1.16 by John Arbash Meinel
Add a cache for a kernel-like inventory
149
150
151
class KernelLikeInventoryCreator(TreeCreator):
152
    """Return just the memory representation of a committed kernel-like tree"""
153
154
    def __init__(self, test):
155
        super(KernelLikeInventoryCreator, self).__init__(test,
156
            tree_name='kernel_like_inventory',
157
            link_working=True,
158
            link_bzr=True,
159
            hot_cache=True)
160
161
    def ensure_cached(self):
162
        """Make sure we have a cached version of the kernel-like inventory"""
163
        cache_dir = self._get_cache_dir()
164
        if cache_dir is None:
165
            return
166
167
        if self.is_cached():
168
            return
169
170
        committed_creator = KernelLikeCommittedTreeCreator(self._test,
171
                                                           link_working=True,
172
                                                           link_bzr=True,
173
                                                           hot_cache=False)
174
        committed_creator.ensure_cached()
175
        committed_cache_dir = committed_creator._get_cache_dir()
176
        committed_tree = workingtree.WorkingTree.open(committed_cache_dir)
177
        rev_tree = committed_tree.basis_tree()
178
        os.mkdir(cache_dir)
179
        f = open(cache_dir+'/inventory', 'wb')
180
        try:
181
            xml5.serializer_v5.write_inventory(rev_tree.inventory, f)
182
        finally:
183
            f.close()
184
185
    def create(self):
186
        """Create a kernel like inventory
187
188
        :return: An Inventory object.
189
        """
190
        cache_dir = self._get_cache_dir()
191
        if cache_dir is None:
192
            return self._create_and_return()
193
194
        self.ensure_cached()
195
        return self._open_cached(cache_dir)
196
197
    def _create_and_return(self):
198
        """Create a kernel-like tree, and return its inventory"""
199
        creator = KernelLikeCommittedTreeCreator(self._test,
200
                                                 link_working=True,
201
                                                 link_bzr=True,
202
                                                 hot_cache=False)
203
        tree = creator.create('.')
204
        return tree.basis_tree().inventory
205
206
    def _open_cached(self, cache_dir):
207
        f = open(cache_dir + '/inventory', 'rb')
208
        try:
209
            return xml5.serializer_v5.read_inventory(f)
210
        finally:
211
            f.close()