1
# Copyright (C) 2008, 2009 Canonical Ltd.
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.
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.
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
17
1
"""The WSGI application for serving a Bazaar branch."""
23
6
import bzrlib.branch
24
7
import bzrlib.lru_cache
27
10
from paste import httpexceptions
29
12
from loggerhead.apps import static_app
13
from loggerhead.controllers.changelog_ui import ChangeLogUI
14
from loggerhead.controllers.inventory_ui import InventoryUI
30
15
from loggerhead.controllers.annotate_ui import AnnotateUI
16
from loggerhead.controllers.revision_ui import RevisionUI
31
17
from loggerhead.controllers.atom_ui import AtomUI
32
from loggerhead.controllers.changelog_ui import ChangeLogUI
33
from loggerhead.controllers.diff_ui import DiffUI
34
18
from loggerhead.controllers.download_ui import DownloadUI
35
from loggerhead.controllers.filediff_ui import FileDiffUI
36
from loggerhead.controllers.inventory_ui import InventoryUI
37
from loggerhead.controllers.revision_ui import RevisionUI
38
from loggerhead.controllers.revlog_ui import RevLogUI
39
19
from loggerhead.controllers.search_ui import SearchUI
40
20
from loggerhead.history import History
41
21
from loggerhead import util
46
24
class BranchWSGIApp(object):
48
26
def __init__(self, branch, friendly_name=None, config={},
49
graph_cache=None, branch_link=None, is_root=False,
50
served_url=_DEFAULT, use_cdn=False):
27
graph_cache=None, branch_link=None):
51
28
self.branch = branch
52
29
self._config = config
53
30
self.friendly_name = friendly_name
54
31
self.branch_link = branch_link # Currently only used in Launchpad
55
32
self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
56
33
if graph_cache is None:
57
graph_cache = bzrlib.lru_cache.LRUCache(10)
34
graph_cache = bzrlib.lru_cache.LRUCache()
58
35
self.graph_cache = graph_cache
59
self.is_root = is_root
60
self.served_url = served_url
61
self.use_cdn = use_cdn
63
37
def get_history(self):
65
revinfo_disk_cache = None
38
_history = History(self.branch, self.graph_cache)
66
39
cache_path = self._config.get('cachepath', None)
67
40
if cache_path is not None:
68
41
# Only import the cache if we're going to use it.
69
42
# This makes sqlite optional
71
from loggerhead.changecache import (
72
FileChangeCache, RevInfoDiskCache)
44
from loggerhead.changecache import FileChangeCache
73
45
except ImportError:
74
46
self.log.debug("Couldn't load python-sqlite,"
75
47
" continuing without using a cache")
77
file_cache = FileChangeCache(cache_path)
78
revinfo_disk_cache = RevInfoDiskCache(cache_path)
80
self.branch, self.graph_cache, file_cache=file_cache,
81
revinfo_disk_cache=revinfo_disk_cache, cache_key=self.friendly_name)
49
_history.use_file_cache(
50
FileChangeCache(_history, cache_path))
83
53
def url(self, *args, **kw):
84
54
if isinstance(args[0], list):
87
57
for k, v in kw.iteritems():
89
qs.append('%s=%s' % (k, urllib.quote(v)))
59
qs.append('%s=%s'%(k, urllib.quote(v)))
91
61
return request.construct_url(
92
62
self._environ, script_name=self._url_base,
93
path_info=unicode('/'.join(args)).encode('utf-8'),
63
path_info='/'.join(args),
96
66
def context_url(self, *args, **kw):
100
70
def static_url(self, path):
101
71
return self._static_url_base + path
103
def yui_url(self, path):
105
base = 'http://yui.yahooapis.com/3.0.0pr2/build/'
107
base = self.static_url('/static/javascript/yui/build/')
110
73
controllers_dict = {
111
'+filediff': FileDiffUI,
113
74
'annotate': AnnotateUI,
115
75
'changes': ChangeLogUI,
117
'download': DownloadUI,
118
76
'files': InventoryUI,
119
77
'revision': RevisionUI,
78
'download': DownloadUI,
120
80
'search': SearchUI,
123
83
def last_updated(self):
124
84
h = self.get_history()
125
change = h.get_changes([h.last_revid])[0]
85
change = h.get_changes([ h.last_revid ])[0]
126
86
return change.date
128
def public_branch_url(self):
129
89
return self.branch.get_config().get_user_option('public_branch')
131
91
def app(self, environ, start_response):
132
# Check again if the branch is blocked from being served, this is
133
# mostly for tests. It's already checked in apps/transport.py
134
if self.branch.get_config().get_user_option('http_serve') == 'False':
135
raise httpexceptions.HTTPNotFound()
136
92
self._url_base = environ['SCRIPT_NAME']
137
93
self._static_url_base = environ.get('loggerhead.static.url')
138
94
if self._static_url_base is None:
139
95
self._static_url_base = self._url_base
140
96
self._environ = environ
141
if self.served_url is _DEFAULT:
142
public_branch = self.public_branch_url()
143
if public_branch is not None:
144
self.served_url = public_branch
146
# Loggerhead only supports serving .bzr/ on local branches, so
147
# we shouldn't suggest something that won't work.
148
if self.branch.base.startswith('file://'):
149
self.served_url = self.url([])
151
self.served_url = None
152
97
path = request.path_info_pop(environ)
154
99
raise httpexceptions.HTTPMovedPermanently(