~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Jelmer Vernooij
  • Date: 2008-08-06 18:33:20 UTC
  • mto: (197.1.9 pathargs)
  • mto: This revision was merged to the branch mainline in revision 202.
  • Revision ID: jelmer@samba.org-20080806183320-6llann0k480dlb9y
add --log-folder option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""The WSGI application for serving a Bazaar branch."""
 
2
 
 
3
import logging
 
4
import urllib
 
5
 
 
6
import bzrlib.branch
 
7
import bzrlib.lru_cache
 
8
 
 
9
from paste import request
 
10
from paste import httpexceptions
 
11
 
 
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
 
22
 
 
23
 
 
24
class BranchWSGIApp(object):
 
25
 
 
26
    def __init__(self, branch, friendly_name=None, config={},
 
27
                 graph_cache=None, branch_link=None):
 
28
        self.branch = branch
 
29
        self._config = config
 
30
        self.friendly_name = friendly_name
 
31
        self.branch_link = branch_link  # Currently only used in Launchpad
 
32
        self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
 
33
        if graph_cache is None:
 
34
            graph_cache = bzrlib.lru_cache.LRUCache()
 
35
        self.graph_cache = graph_cache
 
36
 
 
37
    def get_history(self):
 
38
        _history = History(self.branch, self.graph_cache)
 
39
        cache_path = self._config.get('cachepath', None)
 
40
        if cache_path is not None:
 
41
            # Only import the cache if we're going to use it.
 
42
            # This makes sqlite optional
 
43
            try:
 
44
                from loggerhead.changecache import FileChangeCache
 
45
            except ImportError:
 
46
                self.log.debug("Couldn't load python-sqlite,"
 
47
                               " continuing without using a cache")
 
48
            else:
 
49
                _history.use_file_cache(
 
50
                    FileChangeCache(_history, cache_path))
 
51
        return _history
 
52
 
 
53
    def url(self, *args, **kw):
 
54
        if isinstance(args[0], list):
 
55
            args = args[0]
 
56
        qs = []
 
57
        for k, v in kw.iteritems():
 
58
            if v is not None:
 
59
                qs.append('%s=%s'%(k, urllib.quote(v)))
 
60
        qs = '&'.join(qs)
 
61
        return request.construct_url(
 
62
            self._environ, script_name=self._url_base,
 
63
            path_info='/'.join(args),
 
64
            querystring=qs)
 
65
 
 
66
    def context_url(self, *args, **kw):
 
67
        kw = util.get_context(**kw)
 
68
        return self.url(*args, **kw)
 
69
 
 
70
    def static_url(self, path):
 
71
        return self._static_url_base + path
 
72
 
 
73
    controllers_dict = {
 
74
        'annotate': AnnotateUI,
 
75
        'changes': ChangeLogUI,
 
76
        'files': InventoryUI,
 
77
        'revision': RevisionUI,
 
78
        'download': DownloadUI,
 
79
        'atom': AtomUI,
 
80
        'search': SearchUI,
 
81
        }
 
82
 
 
83
    def last_updated(self):
 
84
        h = self.get_history()
 
85
        change = h.get_changes([ h.last_revid ])[0]
 
86
        return change.date
 
87
 
 
88
    def branch_url(self):
 
89
        return self.branch.get_config().get_user_option('public_branch')
 
90
 
 
91
    def app(self, environ, start_response):
 
92
        self._url_base = environ['SCRIPT_NAME']
 
93
        self._static_url_base = environ.get('loggerhead.static.url')
 
94
        if self._static_url_base is None:
 
95
            self._static_url_base = self._url_base
 
96
        self._environ = environ
 
97
        path = request.path_info_pop(environ)
 
98
        if not path:
 
99
            raise httpexceptions.HTTPMovedPermanently(
 
100
                self._url_base + '/changes')
 
101
        if path == 'static':
 
102
            return static_app(environ, start_response)
 
103
        cls = self.controllers_dict.get(path)
 
104
        if cls is None:
 
105
            raise httpexceptions.HTTPNotFound()
 
106
        self.branch.lock_read()
 
107
        try:
 
108
            c = cls(self, self.get_history())
 
109
            return c(environ, start_response)
 
110
        finally:
 
111
            self.branch.unlock()