1
"""The WSGI application for serving a Bazaar branch."""
8
import bzrlib.lru_cache
10
from paste import request
11
from paste import httpexceptions
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
26
class BranchWSGIApp(object):
28
def __init__(self, branch, friendly_name=None, config={},
29
graph_cache=None, branch_link=None, is_root=False):
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
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
47
from loggerhead.changecache import FileChangeCache
49
self.log.debug("Couldn't load python-sqlite,"
50
" continuing without using a cache")
52
_history.use_file_cache(
53
FileChangeCache(_history, cache_path))
56
def url(self, *args, **kw):
57
if isinstance(args[0], list):
60
for k, v in kw.iteritems():
62
qs.append('%s=%s'%(k, urllib.quote(v)))
64
return request.construct_url(
65
self._environ, script_name=self._url_base,
66
path_info='/'.join(args),
69
def context_url(self, *args, **kw):
70
kw = util.get_context(**kw)
71
return self.url(*args, **kw)
73
def static_url(self, path):
74
return self._static_url_base + path
77
'annotate': AnnotateUI,
78
'changes': ChangeLogUI,
80
'revision': RevisionUI,
81
'download': DownloadUI,
87
def last_updated(self):
88
h = self.get_history()
89
change = h.get_changes([h.last_revid])[0]
93
return self.branch.get_config().get_user_option('public_branch')
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)
103
raise httpexceptions.HTTPMovedPermanently(
104
self._url_base + '/changes')
106
return static_app(environ, start_response)
107
cls = self.controllers_dict.get(path)
109
raise httpexceptions.HTTPNotFound()
110
self.branch.lock_read()
113
c = cls(self, self.get_history())
114
return c(environ, start_response)
116
environ['exc_info'] = sys.exc_info()
117
environ['branch'] = self