39
def get_cache_dir(self, extra):
40
"""Get the directory to use for caching the given object.
42
:return: (cache_dir, is_cached)
43
cache_dir: The path to use for caching. If None, caching is disabled
44
is_cached: Has the path already been created?
46
if Benchmark.CACHE_ROOT is None:
48
if not os.path.isdir(Benchmark.CACHE_ROOT):
49
os.mkdir(Benchmark.CACHE_ROOT)
50
cache_dir = osutils.pathjoin(self.CACHE_ROOT, extra)
51
return cache_dir, os.path.exists(cache_dir)
53
39
def make_kernel_like_tree(self, url=None, root='.',
54
40
link_working=False):
55
41
"""Setup a temporary tree roughly like a kernel tree.
127
113
osutils.copy_tree(cache_dir, root,
128
114
handlers={'file':os.link})
130
def _clone_tree(self, source, dest, link_bzr=False, link_working=True,
132
"""Copy the contents from a given location to another location.
133
Optionally hardlink certain pieces of the tree.
135
:param source: The directory to copy
136
:param dest: The destination
137
:param link_bzr: Should the .bzr/ files be hardlinked?
138
:param link_working: Should the working tree be hardlinked?
139
:param hot_cache: Update the hash-cache when you are done
141
# We use shutil.copyfile so that we don't copy permissions
142
# because most of our source trees are marked readonly to
143
# prevent modifying in the case of hardlinks
144
handlers = {'file':shutil.copyfile}
145
if osutils.hardlinks_good():
148
handlers = {'file':os.link}
150
# Don't hardlink files inside bzr
151
def file_handler(source, dest):
152
if '.bzr/' in source:
153
shutil.copyfile(source, dest)
155
os.link(source, dest)
156
handlers = {'file':file_handler}
158
# Only link files inside .bzr/
159
def file_handler(source, dest):
160
if '.bzr/' in source:
161
os.link(source, dest)
163
shutil.copyfile(source, dest)
164
handlers = {'file':file_handler}
165
osutils.copy_tree(source, dest, handlers=handlers)
166
tree = workingtree.WorkingTree.open(dest)
170
# tree._hashcache.scan() just checks and removes
171
# entries that are out of date
172
# we need to actually store new ones
173
for path, ie in tree.inventory.iter_entries_by_dir():
174
tree.get_file_sha1(ie.file_id, path)
177
# If we didn't iterate the tree, the hash cache is technically
178
# invalid, and it would be better to remove it, but there is
179
# no public api for that.
182
def _protect_files(self, root):
183
"""Chmod all files underneath 'root' to prevent writing
185
:param root: The base directory to modify
187
for dirinfo, entries in osutils.walkdirs(root):
188
for relpath, name, kind, st, abspath in entries:
190
os.chmod(abspath, 0440)
192
116
def _create_kernel_like_added_tree(self, root, in_cache=False):
193
117
"""Create a kernel-like tree with the all files added
291
class TreeCreator(object):
292
"""Just a basic class which is used to create various test trees"""
296
def __init__(self, tree_name, link_bzr=False, link_working=False,
298
"""Instantiate a new creator object, supply the id of the tree"""
300
self._tree_name = tree_name
301
self._link_bzr = link_bzr
302
self._link_working = link_working
303
self._hot_cache = hot_cache
306
def is_caching_enabled():
307
"""Will we try to cache the tree we create?"""
308
return TreeCreator.CACHE_ROOT is not None
311
"""Is this tree already cached?"""
312
cache_dir = self._get_cache_dir()
313
if cache_dir is None:
315
return os.path.exists(cache_dir):
317
def create(self, root):
318
"""Create a new tree at 'root'.
320
:return: A WorkingTree object.
322
cache_dir = self._get_cache_dir()
323
if cache_dir is None:
325
return self._create_tree(root, in_cache=False)
327
if not self.is_cached():
328
self._create_tree(root=cache_dir, in_cache=True)
330
return self._clone_cached_tree(root)
332
def _get_cache_dir(self):
333
"""Get the directory to use for caching this tree
335
:return: The path to use for caching. If None, caching is disabled
337
if self.CACHE_ROOT is None:
339
return osutils.pathjoin(self.CACHE_ROOT, extra)
341
def _create_tree(self, root, in_cache=False):
342
"""Create the desired tree in the given location.
344
Children should override this function to provide the actual creation
345
of the desired tree. This will be called by 'create()'. If it is
346
building a tree in the cache, before copying it to the real target,
347
it will pass in_cache=True
349
raise NotImplemented(self._create_tree)
351
def _clone_cached_tree(self, dest):
352
"""Copy the contents of the cached dir into the destination
353
Optionally hardlink certain pieces of the tree.
355
This is just meant as a helper function for child classes
357
:param dest: The destination to copy things to
359
# We use shutil.copyfile so that we don't copy permissions
360
# because most of our source trees are marked readonly to
361
# prevent modifying in the case of hardlinks
362
handlers = {'file':shutil.copyfile}
363
if osutils.hardlinks_good():
364
if self._link_working:
366
handlers = {'file':os.link}
368
# Don't hardlink files inside bzr
369
def file_handler(source, dest):
370
if '.bzr/' in source:
371
shutil.copyfile(source, dest)
373
os.link(source, dest)
374
handlers = {'file':file_handler}
376
# Only link files inside .bzr/
377
def file_handler(source, dest):
378
if '.bzr/' in source:
379
os.link(source, dest)
381
shutil.copyfile(source, dest)
382
handlers = {'file':file_handler}
384
source = self._get_cache_dir()
385
osutils.copy_tree(source, dest, handlers=handlers)
386
tree = workingtree.WorkingTree.open(dest)
390
# tree._hashcache.scan() just checks and removes
391
# entries that are out of date
392
# we need to actually store new ones
393
for path, ie in tree.inventory.iter_entries_by_dir():
394
tree.get_file_sha1(ie.file_id, path)
397
# If we didn't iterate the tree, the hash cache is technically
398
# invalid, and it would be better to remove it, but there is
399
# no public api for that.
402
def _protect_files(self, root):
403
"""Chmod all files underneath 'root' to prevent writing
405
This is a helper function for child classes.
407
:param root: The base directory to modify
409
for dirinfo, entries in osutils.walkdirs(root):
410
for relpath, name, kind, st, abspath in entries:
412
os.chmod(abspath, 0440)
367
415
def test_suite():
368
416
"""Build and return a TestSuite which contains benchmark tests only."""
369
417
testmod_names = [ \