~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Martin Albisetti
  • Date: 2008-09-01 21:47:21 UTC
  • mfrom: (215.1.6 1.6.1)
  • Revision ID: argentina@gmail.com-20080901214721-cdlcjue6ggr6y86q
Tags: loggerhead-1.6.1
* Added --port, --host and --prefix options to serve-branches
* Fixed broken template for project browsing with start-loggerhead

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