~flormayer/loggerhead/path

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Michael Hudson
  • Date: 2008-07-01 04:04:03 UTC
  • mfrom: (179.1.16 dont-hold-branches-open)
  • Revision ID: michael.hudson@canonical.com-20080701040403-payspbvmsfmp4e84
refactor things to avoid keeping branches open when they are not being viewed.
history objects are now ephemeral wrappers around branches, with the
expensive-to-calculate whole history data being cached elsewhere, in a
LRUCache.
also fixes a logging setup regression and deletes some dead code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""The WSGI application for serving a Bazaar branch."""
 
2
 
1
3
import logging
2
4
import urllib
3
5
 
 
6
import bzrlib.branch
 
7
import bzrlib.lru_cache
 
8
 
4
9
from paste import request
5
10
from paste import httpexceptions
6
11
 
7
12
from loggerhead.apps import static_app
8
 
 
9
13
from loggerhead.controllers.changelog_ui import ChangeLogUI
10
14
from loggerhead.controllers.inventory_ui import InventoryUI
11
15
from loggerhead.controllers.annotate_ui import AnnotateUI
18
22
 
19
23
class BranchWSGIApp(object):
20
24
 
21
 
    def __init__(self, branch_url, friendly_name=None, config={}):
22
 
        self.branch_url = branch_url
23
 
        self._history = None
 
25
    def __init__(self, branch, friendly_name=None, config={}, graph_cache=None):
 
26
        self.branch = branch
24
27
        self._config = config
25
28
        self.friendly_name = friendly_name
26
29
        self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
 
30
        if graph_cache is None:
 
31
            graph_cache = bzrlib.lru_cache.LRUCache()
 
32
        self.graph_cache = graph_cache
27
33
 
28
 
    @property
29
 
    def history(self):
30
 
        if (self._history is None) or self._history.out_of_date():
31
 
            self.log.debug('Reload branch history...')
32
 
            _history = self._history = History.from_folder(self.branch_url)
33
 
            cache_path = self._config.get('cachepath', None)
34
 
            if cache_path is not None:
35
 
                # Only import the cache if we're going to use it.
36
 
                # This makes sqlite optional
37
 
                try:
38
 
                    from loggerhead.changecache import FileChangeCache
39
 
                except ImportError:
40
 
                    self.log.debug("Couldn't load python-sqlite," 
41
 
                                   " continuing without using a cache")
42
 
                else:
43
 
                    _history.use_file_cache(FileChangeCache(_history, 
44
 
                                                            cache_path))
45
 
        return self._history
 
34
    def get_history(self):
 
35
        _history = History(self.branch, self.graph_cache)
 
36
        cache_path = self._config.get('cachepath', None)
 
37
        if cache_path is not None:
 
38
            # Only import the cache if we're going to use it.
 
39
            # This makes sqlite optional
 
40
            try:
 
41
                from loggerhead.changecache import FileChangeCache
 
42
            except ImportError:
 
43
                self.log.debug("Couldn't load python-sqlite,"
 
44
                               " continuing without using a cache")
 
45
            else:
 
46
                _history.use_file_cache(
 
47
                    FileChangeCache(_history, cache_path))
 
48
        return _history
46
49
 
47
50
    def url(self, *args, **kw):
48
51
        if isinstance(args[0], list):
74
77
        }
75
78
 
76
79
    def last_updated(self):
77
 
        h = self.history
 
80
        h = self.get_history()
78
81
        change = h.get_changes([ h.last_revid ])[0]
79
82
        return change.date
80
83
 
81
84
    def branch_url(self):
82
 
        return self.history.get_config().get_user_option('public_branch')
 
85
        return self.branch.get_config().get_user_option('public_branch')
83
86
 
84
87
    def app(self, environ, start_response):
85
88
        self._url_base = environ['SCRIPT_NAME']
96
99
        cls = self.controllers_dict.get(path)
97
100
        if cls is None:
98
101
            raise httpexceptions.HTTPNotFound()
99
 
        c = cls(self)
100
 
        return c(environ, start_response)
 
102
        self.branch.lock_read()
 
103
        try:
 
104
            c = cls(self, self.get_history())
 
105
            return c(environ, start_response)
 
106
        finally:
 
107
            self.branch.unlock()