~bzr-pqm/bzr/bzr.dev

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,
25
    )
26
27
from bzrlib.benchmarks.tree_creator import TreeCreator
28
29
30
class KernelLikeTreeCreator(TreeCreator):
31
    """Create a basic tree with ~10k unversioned files""" 
32
33
    def __init__(self, test, link_working=False, url=None):
34
        super(KernelLikeTreeCreator, self).__init__(test,
35
            tree_name='kernel_like_tree',
36
            link_working=link_working,
37
            link_bzr=False)
38
39
        self._url = url
40
41
    def create(self, root):
42
        """Create all the kernel files in the given location.
43
44
        This is overloaded for compatibility reasons.
45
        """
46
        if self._url is not None:
47
            b = bzrdir.BzrDir.create_branch_convenience(self._url)
48
            d = bzrdir.BzrDir.create(root)
49
            bzrlib.branch.BranchReferenceFormat().initialize(d, b)
50
            tree = d.create_workingtree()
51
        else:
52
            tree = bzrdir.BzrDir.create_standalone_workingtree(root)
53
54
        if not self._link_working or not self.is_caching_enabled():
55
            # Turns out that 'shutil.copytree()' is no faster than
56
            # just creating them. Probably the python overhead.
57
            # Plain _make_kernel_files takes 3-5s
58
            # cp -a takes 3s
59
            # using hardlinks takes < 1s.
60
            self._create_tree(root=root, in_cache=False)
61
            return tree
62
63
        self.ensure_cached()
64
        cache_dir = self._get_cache_dir()
65
        osutils.copy_tree(cache_dir, root,
66
                          handlers={'file':os.link})
67
        return tree
68
69
    def _create_tree(self, root, in_cache=False):
70
        # a kernel tree has ~10000 and 500 directory, with most files around 
71
        # 3-4 levels deep. 
72
        # we simulate this by three levels of dirs named 0-7, givin 512 dirs,
73
        # and 20 files each.
74
        files = []
75
        for outer in range(8):
76
            files.append("%s/" % outer)
77
            for middle in range(8):
78
                files.append("%s/%s/" % (outer, middle))
79
                for inner in range(8):
80
                    prefix = "%s/%s/%s/" % (outer, middle, inner)
81
                    files.append(prefix)
82
                    files.extend([prefix + str(foo) for foo in range(20)])
83
        cwd = osutils.getcwd()
84
        os.chdir(root)
85
        self._test.build_tree(files)
86
        os.chdir(cwd)
87
        if in_cache:
88
            self._protect_files(root)
89
90
91
class KernelLikeAddedTreeCreator(TreeCreator):
92
93
    def __init__(self, test, link_working=False, hot_cache=True):
94
        super(KernelLikeAddedTreeCreator, self).__init__(test,
95
            tree_name='kernel_like_added_tree',
96
            link_working=link_working,
97
            link_bzr=False,
98
            hot_cache=hot_cache)
99
100
    def _create_tree(self, root, in_cache=False):
101
        """Create a kernel-like tree with the all files added
102
103
        :param root: The root directory to create the files
104
        :param in_cache: Is this being created in the cache dir?
105
        """
106
        kernel_creator = KernelLikeTreeCreator(self._test,
107
                                               link_working=in_cache)
108
        tree = kernel_creator.create(root=root)
109
110
        # Add everything to it
111
        tree.lock_write()
112
        try:
113
            add.smart_add_tree(tree, [root], recurse=True, save=True)
114
            if in_cache:
115
                self._protect_files(root+'/.bzr')
116
        finally:
117
            tree.unlock()
118
        return tree
119
120
121
class KernelLikeCommittedTreeCreator(TreeCreator):
122
    """Create a tree with ~10K files, and a single commit adding all of them"""
123
124
    def __init__(self, test, link_working=False, link_bzr=False,
125
                 hot_cache=True):
126
        super(KernelLikeCommittedTreeCreator, self).__init__(test,
127
            tree_name='kernel_like_committed_tree',
128
            link_working=link_working,
129
            link_bzr=link_bzr,
130
            hot_cache=hot_cache)
131
132
    def _create_tree(self, root, in_cache=False):
133
        """Create a kernel-like tree with all files committed
134
135
        :param root: The root directory to create the files
136
        :param in_cache: Is this being created in the cache dir?
137
        """
138
        kernel_creator = KernelLikeAddedTreeCreator(self._test,
139
                                                    link_working=in_cache,
140
                                                    hot_cache=(not in_cache))
141
        tree = kernel_creator.create(root=root)
142
        tree.commit('first post', rev_id='r1')
143
144
        if in_cache:
145
            self._protect_files(root+'/.bzr')
146
        return tree