~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Michael Hudson
  • Date: 2008-06-24 23:20:59 UTC
  • Revision ID: michael.hudson@canonical.com-20080624232059-63y8w66cci2235dd
misc logging improvements:
 * don't call basicConfig when importing loggerhead.apps.branch.
 * log rendering time for templates.
 * more consistent logger names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""The WSGI application for serving a Bazaar branch."""
2
 
 
3
1
import logging
4
2
import urllib
5
 
import sys
6
 
 
7
 
import bzrlib.branch
8
 
import bzrlib.lru_cache
9
3
 
10
4
from paste import request
11
5
from paste import httpexceptions
 
6
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
12
7
 
13
8
from loggerhead.apps import static_app
 
9
from loggerhead.changecache import FileChangeCache
14
10
from loggerhead.controllers.changelog_ui import ChangeLogUI
15
11
from loggerhead.controllers.inventory_ui import InventoryUI
16
12
from loggerhead.controllers.annotate_ui import AnnotateUI
17
13
from loggerhead.controllers.revision_ui import RevisionUI
18
14
from loggerhead.controllers.atom_ui import AtomUI
19
15
from loggerhead.controllers.download_ui import DownloadUI
20
 
from loggerhead.controllers.search_ui import SearchUI
21
 
from loggerhead.controllers.diff_ui import DiffUI
22
16
from loggerhead.history import History
23
17
from loggerhead import util
24
18
 
25
19
 
26
20
class BranchWSGIApp(object):
27
21
 
28
 
    def __init__(self, branch, friendly_name=None, config={},
29
 
                 graph_cache=None, branch_link=None, is_root=False):
30
 
        self.branch = branch
 
22
    def __init__(self, branch_url, friendly_name=None, config={}):
 
23
        self.branch_url = branch_url
 
24
        self._history = None
31
25
        self._config = config
32
26
        self.friendly_name = friendly_name
33
 
        self.branch_link = branch_link  # Currently only used in Launchpad
34
 
        self.log = logging.getLogger('loggerhead.%s' % friendly_name)
35
 
        if graph_cache is None:
36
 
            graph_cache = bzrlib.lru_cache.LRUCache()
37
 
        self.graph_cache = graph_cache
38
 
        self.is_root = is_root
 
27
        self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
39
28
 
40
 
    def get_history(self):
41
 
        _history = History(self.branch, self.graph_cache)
42
 
        cache_path = self._config.get('cachepath', None)
43
 
        if cache_path is not None:
44
 
            # Only import the cache if we're going to use it.
45
 
            # This makes sqlite optional
46
 
            try:
47
 
                from loggerhead.changecache import FileChangeCache
48
 
            except ImportError:
49
 
                self.log.debug("Couldn't load python-sqlite,"
50
 
                               " continuing without using a cache")
51
 
            else:
52
 
                _history.use_file_cache(
53
 
                    FileChangeCache(_history, cache_path))
54
 
        return _history
 
29
    @property
 
30
    def history(self):
 
31
        if (self._history is None) or self._history.out_of_date():
 
32
            self.log.debug('Reload branch history...')
 
33
            _history = self._history = History.from_folder(self.branch_url)
 
34
            cache_path = self._config.get('cachepath', None)
 
35
            if cache_path is not None:
 
36
                _history.use_file_cache(FileChangeCache(_history, cache_path))
 
37
        return self._history
55
38
 
56
39
    def url(self, *args, **kw):
57
40
        if isinstance(args[0], list):
80
63
        'revision': RevisionUI,
81
64
        'download': DownloadUI,
82
65
        'atom': AtomUI,
83
 
        'search': SearchUI,
84
 
        'diff': DiffUI,
85
66
        }
86
67
 
87
68
    def last_updated(self):
88
 
        h = self.get_history()
89
 
        change = h.get_changes([h.last_revid])[0]
 
69
        h = self.history
 
70
        change = h.get_changes([ h.last_revid ])[0]
90
71
        return change.date
91
72
 
92
73
    def branch_url(self):
93
 
        return self.branch.get_config().get_user_option('public_branch')
 
74
        return self.history.get_config().get_user_option('public_branch')
94
75
 
95
76
    def app(self, environ, start_response):
 
77
        req = WSGIRequest(environ)
 
78
        response = WSGIResponse()
 
79
        response.headers['Content-Type'] = 'text/plain'
96
80
        self._url_base = environ['SCRIPT_NAME']
97
81
        self._static_url_base = environ.get('loggerhead.static.url')
98
82
        if self._static_url_base is None:
107
91
        cls = self.controllers_dict.get(path)
108
92
        if cls is None:
109
93
            raise httpexceptions.HTTPNotFound()
110
 
        self.branch.lock_read()
111
 
        try:
112
 
            try:
113
 
                c = cls(self, self.get_history())
114
 
                return c(environ, start_response)
115
 
            except:
116
 
                environ['exc_info'] = sys.exc_info()
117
 
                environ['branch'] = self
118
 
                raise
119
 
        finally:
120
 
            self.branch.unlock()
 
94
        c = cls(self)
 
95
        c.default(req, response)
 
96
        return response(environ, start_response)