/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/benchmarks/__init__.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-15 00:30:42 UTC
  • mto: This revision was merged to the branch mainline in revision 1923.
  • Revision ID: john@arbash-meinel.com-20060815003042-8025a9f154b0f56c
Factor out all helper functions into a tree creator class

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
    CACHE_ROOT = None
38
38
 
39
 
    def get_cache_dir(self, extra):
40
 
        """Get the directory to use for caching the given object.
41
 
 
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?
45
 
        """
46
 
        if Benchmark.CACHE_ROOT is None:
47
 
            return None, False
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)
52
 
 
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})
129
115
 
130
 
    def _clone_tree(self, source, dest, link_bzr=False, link_working=True,
131
 
                    hot_cache=True):
132
 
        """Copy the contents from a given location to another location.
133
 
        Optionally hardlink certain pieces of the tree.
134
 
 
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
140
 
        """
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():
146
 
            if link_working:
147
 
                if link_bzr:
148
 
                    handlers = {'file':os.link}
149
 
                else:
150
 
                    # Don't hardlink files inside bzr
151
 
                    def file_handler(source, dest):
152
 
                        if '.bzr/' in source:
153
 
                            shutil.copyfile(source, dest)
154
 
                        else:
155
 
                            os.link(source, dest)
156
 
                    handlers = {'file':file_handler}
157
 
            elif link_bzr:
158
 
                # Only link files inside .bzr/
159
 
                def file_handler(source, dest):
160
 
                    if '.bzr/' in source:
161
 
                        os.link(source, dest)
162
 
                    else:
163
 
                        shutil.copyfile(source, dest)
164
 
                handlers = {'file':file_handler}
165
 
        osutils.copy_tree(source, dest, handlers=handlers)
166
 
        tree = workingtree.WorkingTree.open(dest)
167
 
        if hot_cache:
168
 
            tree.lock_write()
169
 
            try:
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)
175
 
            finally:
176
 
                tree.unlock()
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.
180
 
        return tree
181
 
 
182
 
    def _protect_files(self, root):
183
 
        """Chmod all files underneath 'root' to prevent writing
184
 
 
185
 
        :param root: The base directory to modify
186
 
        """
187
 
        for dirinfo, entries in osutils.walkdirs(root):
188
 
            for relpath, name, kind, st, abspath in entries:
189
 
                if kind == 'file':
190
 
                    os.chmod(abspath, 0440)
191
 
 
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
194
118
 
364
288
                                hot_cache=True)
365
289
 
366
290
 
 
291
class TreeCreator(object):
 
292
    """Just a basic class which is used to create various test trees"""
 
293
 
 
294
    CACHE_ROOT = None
 
295
 
 
296
    def __init__(self, tree_name, link_bzr=False, link_working=False,
 
297
                 hot_cache=True):
 
298
        """Instantiate a new creator object, supply the id of the tree"""
 
299
 
 
300
        self._tree_name = tree_name
 
301
        self._link_bzr = link_bzr
 
302
        self._link_working = link_working
 
303
        self._hot_cache = hot_cache
 
304
 
 
305
    @staticmethod
 
306
    def is_caching_enabled():
 
307
        """Will we try to cache the tree we create?"""
 
308
        return TreeCreator.CACHE_ROOT is not None
 
309
 
 
310
    def is_cached(self):
 
311
        """Is this tree already cached?"""
 
312
        cache_dir = self._get_cache_dir()
 
313
        if cache_dir is None:
 
314
            return False
 
315
        return os.path.exists(cache_dir):
 
316
        
 
317
    def create(self, root):
 
318
        """Create a new tree at 'root'.
 
319
 
 
320
        :return: A WorkingTree object.
 
321
        """
 
322
        cache_dir = self._get_cache_dir()
 
323
        if cache_dir is None:
 
324
            # Not caching
 
325
            return self._create_tree(root, in_cache=False)
 
326
 
 
327
        if not self.is_cached():
 
328
            self._create_tree(root=cache_dir, in_cache=True)
 
329
 
 
330
        return self._clone_cached_tree(root)
 
331
 
 
332
    def _get_cache_dir(self):
 
333
        """Get the directory to use for caching this tree
 
334
 
 
335
        :return: The path to use for caching. If None, caching is disabled
 
336
        """
 
337
        if self.CACHE_ROOT is None:
 
338
            return None
 
339
        return osutils.pathjoin(self.CACHE_ROOT, extra)
 
340
 
 
341
    def _create_tree(self, root, in_cache=False):
 
342
        """Create the desired tree in the given location.
 
343
 
 
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
 
348
        """
 
349
        raise NotImplemented(self._create_tree)
 
350
 
 
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.
 
354
 
 
355
        This is just meant as a helper function for child classes
 
356
 
 
357
        :param dest: The destination to copy things to
 
358
        """
 
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:
 
365
                if self._link_bzr:
 
366
                    handlers = {'file':os.link}
 
367
                else:
 
368
                    # Don't hardlink files inside bzr
 
369
                    def file_handler(source, dest):
 
370
                        if '.bzr/' in source:
 
371
                            shutil.copyfile(source, dest)
 
372
                        else:
 
373
                            os.link(source, dest)
 
374
                    handlers = {'file':file_handler}
 
375
            elif self._link_bzr:
 
376
                # Only link files inside .bzr/
 
377
                def file_handler(source, dest):
 
378
                    if '.bzr/' in source:
 
379
                        os.link(source, dest)
 
380
                    else:
 
381
                        shutil.copyfile(source, dest)
 
382
                handlers = {'file':file_handler}
 
383
 
 
384
        source = self._get_cache_dir()
 
385
        osutils.copy_tree(source, dest, handlers=handlers)
 
386
        tree = workingtree.WorkingTree.open(dest)
 
387
        if self._hot_cache:
 
388
            tree.lock_write()
 
389
            try:
 
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)
 
395
            finally:
 
396
                tree.unlock()
 
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.
 
400
        return tree
 
401
 
 
402
    def _protect_files(self, root):
 
403
        """Chmod all files underneath 'root' to prevent writing
 
404
 
 
405
        This is a helper function for child classes.
 
406
 
 
407
        :param root: The base directory to modify
 
408
        """
 
409
        for dirinfo, entries in osutils.walkdirs(root):
 
410
            for relpath, name, kind, st, abspath in entries:
 
411
                if kind == 'file':
 
412
                    os.chmod(abspath, 0440)
 
413
 
 
414
 
367
415
def test_suite():
368
416
    """Build and return a TestSuite which contains benchmark tests only."""
369
417
    testmod_names = [ \