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.history import History
21
from loggerhead import util
24
class BranchWSGIApp(object):
26
def __init__(self, branch, friendly_name=None, config={}, graph_cache=None):
29
self.friendly_name = friendly_name
30
self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
31
if graph_cache is None:
32
graph_cache = bzrlib.lru_cache.LRUCache()
33
self.graph_cache = graph_cache
35
def get_history(self):
36
_history = History(self.branch, self.graph_cache)
37
cache_path = self._config.get('cachepath', None)
38
if cache_path is not None:
39
# Only import the cache if we're going to use it.
40
# This makes sqlite optional
42
from loggerhead.changecache import FileChangeCache
44
self.log.debug("Couldn't load python-sqlite,"
45
" continuing without using a cache")
47
_history.use_file_cache(
48
FileChangeCache(_history, cache_path))
51
def url(self, *args, **kw):
52
if isinstance(args[0], list):
55
for k, v in kw.iteritems():
57
qs.append('%s=%s'%(k, urllib.quote(v)))
59
return request.construct_url(
60
self._environ, script_name=self._url_base,
61
path_info='/'.join(args),
64
def context_url(self, *args, **kw):
65
kw = util.get_context(**kw)
66
return self.url(*args, **kw)
68
def static_url(self, path):
69
return self._static_url_base + path
72
'annotate': AnnotateUI,
73
'changes': ChangeLogUI,
75
'revision': RevisionUI,
76
'download': DownloadUI,
81
def last_updated(self):
82
h = self.get_history()
83
change = h.get_changes([ h.last_revid ])[0]
87
return self.branch.get_config().get_user_option('public_branch')
89
def app(self, environ, start_response):
90
self._url_base = environ['SCRIPT_NAME']
91
self._static_url_base = environ.get('loggerhead.static.url')
92
if self._static_url_base is None:
93
self._static_url_base = self._url_base
94
self._environ = environ
95
path = request.path_info_pop(environ)
97
raise httpexceptions.HTTPMovedPermanently(
98
self._url_base + '/changes')
100
return static_app(environ, start_response)
101
cls = self.controllers_dict.get(path)
103
raise httpexceptions.HTTPNotFound()
104
self.branch.lock_read()
106
c = cls(self, self.get_history())
107
return c(environ, start_response)