1
"""The WSGI application for serving a Bazaar branch."""
7
import bzrlib.lru_cache
9
from paste import request
10
from paste import httpexceptions
12
from loggerhead.apps import static_app
13
from loggerhead.controllers.changelog_ui import ChangeLogUI
14
from loggerhead.controllers.inventory_ui import InventoryUI
15
from loggerhead.controllers.annotate_ui import AnnotateUI
16
from loggerhead.controllers.revision_ui import RevisionUI
17
from loggerhead.controllers.atom_ui import AtomUI
18
from loggerhead.controllers.download_ui import DownloadUI
19
from loggerhead.controllers.search_ui import SearchUI
20
from loggerhead.controllers.diff_ui import DiffUI
21
from loggerhead.history import History
22
from loggerhead import util
25
class BranchWSGIApp(object):
27
def __init__(self, branch, friendly_name=None, config={},
28
graph_cache=None, branch_link=None, is_root=False):
31
self.friendly_name = friendly_name
32
self.branch_link = branch_link # Currently only used in Launchpad
33
self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
34
if graph_cache is None:
35
graph_cache = bzrlib.lru_cache.LRUCache()
36
self.graph_cache = graph_cache
37
self.is_root = is_root
39
def get_history(self):
40
_history = History(self.branch, self.graph_cache)
41
cache_path = self._config.get('cachepath', None)
42
if cache_path is not None:
43
# Only import the cache if we're going to use it.
44
# This makes sqlite optional
46
from loggerhead.changecache import FileChangeCache
48
self.log.debug("Couldn't load python-sqlite,"
49
" continuing without using a cache")
51
_history.use_file_cache(
52
FileChangeCache(_history, cache_path))
55
def url(self, *args, **kw):
56
if isinstance(args[0], list):
59
for k, v in kw.iteritems():
61
qs.append('%s=%s'%(k, urllib.quote(v)))
63
return request.construct_url(
64
self._environ, script_name=self._url_base,
65
path_info='/'.join(args),
68
def context_url(self, *args, **kw):
69
kw = util.get_context(**kw)
70
return self.url(*args, **kw)
72
def static_url(self, path):
73
return self._static_url_base + path
76
'annotate': AnnotateUI,
77
'changes': ChangeLogUI,
79
'revision': RevisionUI,
80
'download': DownloadUI,
86
def last_updated(self):
87
h = self.get_history()
88
change = h.get_changes([ h.last_revid ])[0]
92
return self.branch.get_config().get_user_option('public_branch')
94
def app(self, environ, start_response):
95
self._url_base = environ['SCRIPT_NAME']
96
self._static_url_base = environ.get('loggerhead.static.url')
97
if self._static_url_base is None:
98
self._static_url_base = self._url_base
99
self._environ = environ
100
path = request.path_info_pop(environ)
102
raise httpexceptions.HTTPMovedPermanently(
103
self._url_base + '/changes')
105
return static_app(environ, start_response)
106
cls = self.controllers_dict.get(path)
108
raise httpexceptions.HTTPNotFound()
109
self.branch.lock_read()
111
c = cls(self, self.get_history())
112
return c(environ, start_response)