~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Michael Hudson
  • Date: 2007-11-19 08:55:10 UTC
  • mto: This revision was merged to the branch mainline in revision 142.
  • Revision ID: michael.hudson@canonical.com-20071119085510-ossv2rsqdcg7ikzh
oops

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""The WSGI application for serving a Bazaar branch."""
2
 
 
3
 
import logging
4
 
import urllib
5
 
import sys
6
 
 
7
 
import bzrlib.branch
8
 
import bzrlib.lru_cache
9
 
 
10
 
from paste import request
11
 
from paste import httpexceptions
12
 
 
13
 
from loggerhead.apps import static_app
14
 
from loggerhead.controllers.changelog_ui import ChangeLogUI
15
 
from loggerhead.controllers.inventory_ui import InventoryUI
16
 
from loggerhead.controllers.annotate_ui import AnnotateUI
17
 
from loggerhead.controllers.revision_ui import RevisionUI
18
 
from loggerhead.controllers.atom_ui import AtomUI
19
 
from loggerhead.controllers.download_ui import DownloadUI
20
 
from loggerhead.controllers.search_ui import SearchUI
21
 
from loggerhead.controllers.diff_ui import DiffUI
22
 
from loggerhead.history import History
23
 
from loggerhead import util
24
 
 
25
 
 
26
 
class BranchWSGIApp(object):
27
 
 
28
 
    def __init__(self, branch, friendly_name=None, config={},
29
 
                 graph_cache=None, branch_link=None, is_root=False):
30
 
        self.branch = branch
31
 
        self._config = config
32
 
        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
39
 
 
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
55
 
 
56
 
    def url(self, *args, **kw):
57
 
        if isinstance(args[0], list):
58
 
            args = args[0]
59
 
        qs = []
60
 
        for k, v in kw.iteritems():
61
 
            if v is not None:
62
 
                qs.append('%s=%s'%(k, urllib.quote(v)))
63
 
        qs = '&'.join(qs)
64
 
        return request.construct_url(
65
 
            self._environ, script_name=self._url_base,
66
 
            path_info='/'.join(args),
67
 
            querystring=qs)
68
 
 
69
 
    def context_url(self, *args, **kw):
70
 
        kw = util.get_context(**kw)
71
 
        return self.url(*args, **kw)
72
 
 
73
 
    def static_url(self, path):
74
 
        return self._static_url_base + path
75
 
 
76
 
    controllers_dict = {
77
 
        'annotate': AnnotateUI,
78
 
        'changes': ChangeLogUI,
79
 
        'files': InventoryUI,
80
 
        'revision': RevisionUI,
81
 
        'download': DownloadUI,
82
 
        'atom': AtomUI,
83
 
        'search': SearchUI,
84
 
        'diff': DiffUI,
85
 
        }
86
 
 
87
 
    def last_updated(self):
88
 
        h = self.get_history()
89
 
        change = h.get_changes([h.last_revid])[0]
90
 
        return change.date
91
 
 
92
 
    def branch_url(self):
93
 
        return self.branch.get_config().get_user_option('public_branch')
94
 
 
95
 
    def app(self, environ, start_response):
96
 
        self._url_base = environ['SCRIPT_NAME']
97
 
        self._static_url_base = environ.get('loggerhead.static.url')
98
 
        if self._static_url_base is None:
99
 
            self._static_url_base = self._url_base
100
 
        self._environ = environ
101
 
        path = request.path_info_pop(environ)
102
 
        if not path:
103
 
            raise httpexceptions.HTTPMovedPermanently(
104
 
                self._url_base + '/changes')
105
 
        if path == 'static':
106
 
            return static_app(environ, start_response)
107
 
        cls = self.controllers_dict.get(path)
108
 
        if cls is None:
109
 
            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()