~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Robey Pointer
  • Date: 2006-12-11 06:44:19 UTC
  • Revision ID: robey@lag.net-20061211064419-8ssa7mlsiflpmy0c
initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2011 Canonical Ltd.
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
#
17
 
"""The WSGI application for serving a Bazaar branch."""
18
 
 
19
 
import logging
20
 
import urllib
21
 
import sys
22
 
 
23
 
import bzrlib.branch
24
 
import bzrlib.errors
25
 
from bzrlib.hooks import Hooks
26
 
import bzrlib.lru_cache
27
 
 
28
 
from paste import request
29
 
from paste import httpexceptions
30
 
 
31
 
from loggerhead.apps import static_app
32
 
from loggerhead.controllers.annotate_ui import AnnotateUI
33
 
from loggerhead.controllers.view_ui import ViewUI
34
 
from loggerhead.controllers.atom_ui import AtomUI
35
 
from loggerhead.controllers.changelog_ui import ChangeLogUI
36
 
from loggerhead.controllers.diff_ui import DiffUI
37
 
from loggerhead.controllers.download_ui import DownloadUI
38
 
from loggerhead.controllers.filediff_ui import FileDiffUI
39
 
from loggerhead.controllers.inventory_ui import InventoryUI
40
 
from loggerhead.controllers.revision_ui import RevisionUI
41
 
from loggerhead.controllers.revlog_ui import RevLogUI
42
 
from loggerhead.controllers.search_ui import SearchUI
43
 
from loggerhead.history import History
44
 
from loggerhead import util
45
 
 
46
 
 
47
 
_DEFAULT = object()
48
 
 
49
 
class BranchWSGIApp(object):
50
 
 
51
 
    def __init__(self, branch, friendly_name=None, config={},
52
 
                 graph_cache=None, branch_link=None, is_root=False,
53
 
                 served_url=_DEFAULT, use_cdn=False, private=False):
54
 
        self.branch = branch
55
 
        self._config = config
56
 
        self.friendly_name = friendly_name
57
 
        self.branch_link = branch_link  # Currently only used in Launchpad
58
 
        self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
59
 
        if graph_cache is None:
60
 
            graph_cache = bzrlib.lru_cache.LRUCache(10)
61
 
        self.graph_cache = graph_cache
62
 
        self.is_root = is_root
63
 
        self.served_url = served_url
64
 
        self.use_cdn = use_cdn
65
 
        self.private = private
66
 
 
67
 
    def public_private_css(self):
68
 
        if self.private:
69
 
            return "private"
70
 
        else:
71
 
            return "public"
72
 
 
73
 
    def get_history(self):
74
 
        revinfo_disk_cache = None
75
 
        cache_path = self._config.get('cachepath', None)
76
 
        if cache_path is not None:
77
 
            # Only import the cache if we're going to use it.
78
 
            # This makes sqlite optional
79
 
            try:
80
 
                from loggerhead.changecache import RevInfoDiskCache
81
 
            except ImportError:
82
 
                self.log.debug("Couldn't load python-sqlite,"
83
 
                               " continuing without using a cache")
84
 
            else:
85
 
                revinfo_disk_cache = RevInfoDiskCache(cache_path)
86
 
        return History(
87
 
            self.branch, self.graph_cache,
88
 
            revinfo_disk_cache=revinfo_disk_cache, cache_key=self.friendly_name)
89
 
 
90
 
    def url(self, *args, **kw):
91
 
        if isinstance(args[0], list):
92
 
            args = args[0]
93
 
        qs = []
94
 
        for k, v in kw.iteritems():
95
 
            if v is not None:
96
 
                qs.append('%s=%s' % (k, urllib.quote(v)))
97
 
        qs = '&'.join(qs)
98
 
        path_info = urllib.quote(
99
 
            unicode('/'.join(args)).encode('utf-8'), safe='/~:')
100
 
        if qs:
101
 
            path_info += '?' + qs
102
 
        return self._url_base + path_info
103
 
 
104
 
    def absolute_url(self, *args, **kw):
105
 
        rel_url = self.url(*args, **kw)
106
 
        return request.resolve_relative_url(rel_url, self._environ)
107
 
 
108
 
    def context_url(self, *args, **kw):
109
 
        kw = util.get_context(**kw)
110
 
        return self.url(*args, **kw)
111
 
 
112
 
    def static_url(self, path):
113
 
        return self._static_url_base + path
114
 
 
115
 
    def yui_url(self, path):
116
 
        if self.use_cdn:
117
 
            base = 'http://yui.yahooapis.com/3.0.0pr2/build/'
118
 
        else:
119
 
            base = self.static_url('/static/javascript/yui/build/')
120
 
        return base + path
121
 
 
122
 
    controllers_dict = {
123
 
        '+filediff': FileDiffUI,
124
 
        '+revlog': RevLogUI,
125
 
        'annotate': AnnotateUI,
126
 
        'atom': AtomUI,
127
 
        'changes': ChangeLogUI,
128
 
        'diff': DiffUI,
129
 
        'download': DownloadUI,
130
 
        'files': InventoryUI,
131
 
        'revision': RevisionUI,
132
 
        'search': SearchUI,
133
 
        'view': ViewUI,
134
 
        }
135
 
 
136
 
    def last_updated(self):
137
 
        h = self.get_history()
138
 
        change = h.get_changes([h.last_revid])[0]
139
 
        return change.date
140
 
 
141
 
    def public_branch_url(self):
142
 
        return self.branch.get_config().get_user_option('public_branch')
143
 
 
144
 
    def lookup_app(self, environ):
145
 
        # Check again if the branch is blocked from being served, this is
146
 
        # mostly for tests. It's already checked in apps/transport.py
147
 
        if self.branch.get_config().get_user_option('http_serve') == 'False':
148
 
            raise httpexceptions.HTTPNotFound()
149
 
        self._url_base = environ['SCRIPT_NAME']
150
 
        self._static_url_base = environ.get('loggerhead.static.url')
151
 
        if self._static_url_base is None:
152
 
            self._static_url_base = self._url_base
153
 
        self._environ = environ
154
 
        if self.served_url is _DEFAULT:
155
 
            public_branch = self.public_branch_url()
156
 
            if public_branch is not None:
157
 
                self.served_url = public_branch
158
 
            else:
159
 
                # Loggerhead only supports serving .bzr/ on local branches, so
160
 
                # we shouldn't suggest something that won't work.
161
 
                try:
162
 
                    util.local_path_from_url(self.branch.base)
163
 
                    self.served_url = self.url([])
164
 
                except bzrlib.errors.InvalidURL:
165
 
                    self.served_url = None
166
 
        for hook in self.hooks['controller']:
167
 
            controller = hook(self, environ)
168
 
            if controller is not None:
169
 
                return controller
170
 
        path = request.path_info_pop(environ)
171
 
        if not path:
172
 
            raise httpexceptions.HTTPMovedPermanently(
173
 
                self.absolute_url('/changes'))
174
 
        if path == 'static':
175
 
            return static_app
176
 
        elif path == '+json':
177
 
            environ['loggerhead.as_json'] = True
178
 
            path = request.path_info_pop(environ)
179
 
        cls = self.controllers_dict.get(path)
180
 
        if cls is not None:
181
 
            return cls(self, self.get_history)
182
 
        raise httpexceptions.HTTPNotFound()
183
 
 
184
 
    def app(self, environ, start_response):
185
 
        self.branch.lock_read()
186
 
        try:
187
 
            try:
188
 
                c = self.lookup_app(environ)
189
 
                return c(environ, start_response)
190
 
            except:
191
 
                environ['exc_info'] = sys.exc_info()
192
 
                environ['branch'] = self
193
 
                raise
194
 
        finally:
195
 
            self.branch.unlock()
196
 
 
197
 
 
198
 
class BranchWSGIAppHooks(Hooks):
199
 
    """A dictionary mapping hook name to a list of callables for WSGI app branch hooks.
200
 
    """
201
 
 
202
 
    def __init__(self):
203
 
        """Create the default hooks.
204
 
        """
205
 
        Hooks.__init__(self, "bzrlib.plugins.loggerhead.apps.branch",
206
 
            "BranchWSGIApp.hooks")
207
 
        self.add_hook('controller',
208
 
            "Invoked when looking for the controller to use for a "
209
 
            "branch subpage. The api signature is (branch_app, environ)."
210
 
            "If a hook can provide a controller, it should return one, "
211
 
            "as a standard WSGI app. If it can't provide a controller, "
212
 
            "it should return None", (1, 19))
213
 
 
214
 
 
215
 
BranchWSGIApp.hooks = BranchWSGIAppHooks()