1
# Copyright (C) 2006 Canonical Ltd
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.
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.
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
17
"""Base implementation of TreeCreator classes
19
These are classes that are used to easily create test trees.
31
class TreeCreator(object):
32
"""Just a basic class which is used to create various test trees"""
36
def __init__(self, test, tree_name,
40
"""Instantiate a new creator object, supply the id of the tree
42
:param test: A TestCaseWithTransport object (most creators need
43
we need the build_tree functionality)
46
self._cache_root = TreeCreator.CACHE_ROOT
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
55
def is_caching_enabled(self):
56
"""Will we try to cache the tree we create?"""
57
return self._cache_root is not None
60
"""Is this tree already cached?"""
61
cache_dir = self._get_cache_dir()
64
return os.path.exists(cache_dir)
66
def disable_cache(self):
67
"""Do not use the cache"""
68
self._cache_root = None
70
def ensure_cached(self):
71
"""If caching, make sure the cached copy exists"""
72
cache_dir = self._get_cache_dir()
76
if not self.is_cached():
77
self._create_tree(root=cache_dir, in_cache=True)
79
def create(self, root):
80
"""Create a new tree at 'root'.
82
:return: A WorkingTree object.
84
cache_dir = self._get_cache_dir()
87
return self._create_tree(root, in_cache=False)
91
return self._clone_cached_tree(root)
93
def _get_cache_dir(self):
94
"""Get the directory to use for caching this tree
96
:return: The path to use for caching. If None, caching is disabled
98
if self._cache_root is None:
100
return osutils.pathjoin(self._cache_root, self._tree_name)
102
def _create_tree(self, root, in_cache=False):
103
"""Create the desired tree in the given location.
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
110
raise NotImplemented(self._create_tree)
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.
116
This is just meant as a helper function for child classes
118
:param dest: The destination to copy things to
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:
127
handlers = {'file':os.link}
129
# Don't hardlink files inside bzr
130
def file_handler(source, dest):
131
if '.bzr/' in source:
132
shutil.copyfile(source, dest)
134
os.link(source, dest)
135
handlers = {'file':file_handler}
137
# Only link files inside .bzr/
138
def file_handler(source, dest):
139
if '.bzr/' in source:
140
os.link(source, dest)
142
shutil.copyfile(source, dest)
143
handlers = {'file':file_handler}
145
source = self._get_cache_dir()
146
osutils.copy_tree(source, dest, handlers=handlers)
147
tree = workingtree.WorkingTree.open(dest)
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)
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.
163
def _protect_files(self, root):
164
"""Chmod all files underneath 'root' to prevent writing
166
This is a helper function for child classes.
168
:param root: The base directory to modify
170
for dirinfo, entries in osutils.walkdirs(root):
171
for relpath, name, kind, st, abspath in entries:
173
os.chmod(abspath, 0440)