~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-08-02 23:49:34 UTC
  • mfrom: (5362.1.1 merge-2.2-into-trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20100802234934-d963xmqwx5gzevr0
(Andrew Bennetts) Merge 2.2 branch back into trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Base implementation of TreeCreator classes
18
 
 
19
 
These are classes that are used to easily create test trees.
20
 
"""
21
 
 
22
 
import os
23
 
import shutil
24
 
 
25
 
from bzrlib import (
26
 
    osutils,
27
 
    workingtree,
28
 
    )
29
 
 
30
 
 
31
 
class TreeCreator(object):
32
 
    """Just a basic class which is used to create various test trees"""
33
 
 
34
 
    CACHE_ROOT = None
35
 
 
36
 
    def __init__(self, test, tree_name,
37
 
                 link_bzr=False,
38
 
                 link_working=False,
39
 
                 hot_cache=True):
40
 
        """Instantiate a new creator object, supply the id of the tree
41
 
 
42
 
        :param test: A TestCaseWithTransport object (most creators need
43
 
            we need the build_tree functionality)
44
 
        """
45
 
 
46
 
        self._cache_root = TreeCreator.CACHE_ROOT
47
 
        self._test = test
48
 
        self._tree_name = tree_name
49
 
        self._link_bzr = link_bzr
50
 
        self._link_working = link_working
51
 
        self._hot_cache = hot_cache
52
 
        if not osutils.hardlinks_good():
53
 
            self._link_working = self._link_bzr = False
54
 
 
55
 
    def is_caching_enabled(self):
56
 
        """Will we try to cache the tree we create?"""
57
 
        return self._cache_root is not None
58
 
 
59
 
    def is_cached(self):
60
 
        """Is this tree already cached?"""
61
 
        cache_dir = self._get_cache_dir()
62
 
        if cache_dir is None:
63
 
            return False
64
 
        return os.path.exists(cache_dir)
65
 
 
66
 
    def disable_cache(self):
67
 
        """Do not use the cache"""
68
 
        self._cache_root = None
69
 
 
70
 
    def ensure_cached(self):
71
 
        """If caching, make sure the cached copy exists"""
72
 
        cache_dir = self._get_cache_dir()
73
 
        if cache_dir is None:
74
 
            return
75
 
 
76
 
        if not self.is_cached():
77
 
            self._create_tree(root=cache_dir, in_cache=True)
78
 
 
79
 
    def create(self, root):
80
 
        """Create a new tree at 'root'.
81
 
 
82
 
        :return: A WorkingTree object.
83
 
        """
84
 
        cache_dir = self._get_cache_dir()
85
 
        if cache_dir is None:
86
 
            # Not caching
87
 
            return self._create_tree(root, in_cache=False)
88
 
 
89
 
        self.ensure_cached()
90
 
 
91
 
        return self._clone_cached_tree(root)
92
 
 
93
 
    def _get_cache_dir(self):
94
 
        """Get the directory to use for caching this tree
95
 
 
96
 
        :return: The path to use for caching. If None, caching is disabled
97
 
        """
98
 
        if self._cache_root is None:
99
 
            return None
100
 
        return osutils.pathjoin(self._cache_root, self._tree_name)
101
 
 
102
 
    def _create_tree(self, root, in_cache=False):
103
 
        """Create the desired tree in the given location.
104
 
 
105
 
        Children should override this function to provide the actual creation
106
 
        of the desired tree. This will be called by 'create()'. If it is
107
 
        building a tree in the cache, before copying it to the real target,
108
 
        it will pass in_cache=True
109
 
        """
110
 
        raise NotImplemented(self._create_tree)
111
 
 
112
 
    def _clone_cached_tree(self, dest):
113
 
        """Copy the contents of the cached dir into the destination
114
 
        Optionally hardlink certain pieces of the tree.
115
 
 
116
 
        This is just meant as a helper function for child classes
117
 
 
118
 
        :param dest: The destination to copy things to
119
 
        """
120
 
        # We use shutil.copyfile so that we don't copy permissions
121
 
        # because most of our source trees are marked readonly to
122
 
        # prevent modifying in the case of hardlinks
123
 
        handlers = {'file':shutil.copyfile}
124
 
        if osutils.hardlinks_good():
125
 
            if self._link_working:
126
 
                if self._link_bzr:
127
 
                    handlers = {'file':os.link}
128
 
                else:
129
 
                    # Don't hardlink files inside bzr
130
 
                    def file_handler(source, dest):
131
 
                        if '.bzr/' in source:
132
 
                            shutil.copyfile(source, dest)
133
 
                        else:
134
 
                            os.link(source, dest)
135
 
                    handlers = {'file':file_handler}
136
 
            elif self._link_bzr:
137
 
                # Only link files inside .bzr/
138
 
                def file_handler(source, dest):
139
 
                    if '.bzr/' in source:
140
 
                        os.link(source, dest)
141
 
                    else:
142
 
                        shutil.copyfile(source, dest)
143
 
                handlers = {'file':file_handler}
144
 
 
145
 
        source = self._get_cache_dir()
146
 
        osutils.copy_tree(source, dest, handlers=handlers)
147
 
        tree = workingtree.WorkingTree.open(dest)
148
 
        if self._hot_cache:
149
 
            tree.lock_write()
150
 
            try:
151
 
                # tree._hashcache.scan() just checks and removes
152
 
                # entries that are out of date
153
 
                # we need to actually store new ones
154
 
                for path, ie in tree.inventory.iter_entries_by_dir():
155
 
                    tree.get_file_sha1(ie.file_id, path)
156
 
            finally:
157
 
                tree.unlock()
158
 
        # If we didn't iterate the tree, the hash cache is technically
159
 
        # invalid, and it would be better to remove it, but there is
160
 
        # no public api for that.
161
 
        return tree
162
 
 
163
 
    def _protect_files(self, root):
164
 
        """Chmod all files underneath 'root' to prevent writing
165
 
 
166
 
        This is a helper function for child classes.
167
 
 
168
 
        :param root: The base directory to modify
169
 
        """
170
 
        for dirinfo, entries in osutils.walkdirs(root):
171
 
            for relpath, name, kind, st, abspath in entries:
172
 
                if kind == 'file':
173
 
                    os.chmod(abspath, 0440)
174
 
 
175