~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

Remove anything that has to do with KID

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
 
import sys
6
 
 
7
 
import bzrlib.branch
8
 
import bzrlib.lru_cache
9
 
 
10
 
from paste import request
11
 
from paste import httpexceptions
12
 
 
13
 
from loggerhead.apps import static_app
14
 
from loggerhead.controllers.annotate_ui import AnnotateUI
15
 
from loggerhead.controllers.atom_ui import AtomUI
16
 
from loggerhead.controllers.changelog_ui import ChangeLogUI
17
 
from loggerhead.controllers.diff_ui import DiffUI
18
 
from loggerhead.controllers.download_ui import DownloadUI
19
 
from loggerhead.controllers.filediff_ui import FileDiffUI
20
 
from loggerhead.controllers.inventory_ui import InventoryUI
21
 
from loggerhead.controllers.revision_ui import RevisionUI
22
 
from loggerhead.controllers.revlog_ui import RevLogUI
23
 
from loggerhead.controllers.search_ui import SearchUI
24
 
from loggerhead.history import History
25
 
from loggerhead import util
26
 
 
27
 
 
28
 
_DEFAULT = object()
29
 
 
30
 
class BranchWSGIApp(object):
31
 
 
32
 
    def __init__(self, branch, friendly_name=None, config={},
33
 
                 graph_cache=None, branch_link=None, is_root=False,
34
 
                 served_url=_DEFAULT, use_cdn=False):
35
 
        self.branch = branch
36
 
        self._config = config
37
 
        self.friendly_name = friendly_name
38
 
        self.branch_link = branch_link  # Currently only used in Launchpad
39
 
        self.log = logging.getLogger('loggerhead.%s' % friendly_name)
40
 
        if graph_cache is None:
41
 
            graph_cache = bzrlib.lru_cache.LRUCache(10)
42
 
        self.graph_cache = graph_cache
43
 
        self.is_root = is_root
44
 
        self.served_url = served_url
45
 
        self.use_cdn = use_cdn
46
 
 
47
 
    def get_history(self):
48
 
        file_cache = None
49
 
        revinfo_disk_cache = None
50
 
        cache_path = self._config.get('cachepath', None)
51
 
        if cache_path is not None:
52
 
            # Only import the cache if we're going to use it.
53
 
            # This makes sqlite optional
54
 
            try:
55
 
                from loggerhead.changecache import (
56
 
                    FileChangeCache, RevInfoDiskCache)
57
 
            except ImportError:
58
 
                self.log.debug("Couldn't load python-sqlite,"
59
 
                               " continuing without using a cache")
60
 
            else:
61
 
                file_cache = FileChangeCache(cache_path)
62
 
                revinfo_disk_cache = RevInfoDiskCache(cache_path)
63
 
        return History(
64
 
            self.branch, self.graph_cache, file_cache=file_cache,
65
 
            revinfo_disk_cache=revinfo_disk_cache, cache_key=self.friendly_name)
66
 
 
67
 
    def url(self, *args, **kw):
68
 
        if isinstance(args[0], list):
69
 
            args = args[0]
70
 
        qs = []
71
 
        for k, v in kw.iteritems():
72
 
            if v is not None:
73
 
                qs.append('%s=%s'%(k, urllib.quote(v)))
74
 
        qs = '&'.join(qs)
75
 
        return request.construct_url(
76
 
            self._environ, script_name=self._url_base,
77
 
            path_info=unicode('/'.join(args)).encode('utf-8'),
78
 
            querystring=qs)
79
 
 
80
 
    def context_url(self, *args, **kw):
81
 
        kw = util.get_context(**kw)
82
 
        return self.url(*args, **kw)
83
 
 
84
 
    def static_url(self, path):
85
 
        return self._static_url_base + path
86
 
 
87
 
    def yui_url(self, path):
88
 
        if self.use_cdn:
89
 
            base = 'http://yui.yahooapis.com/3.0.0pr2/build/'
90
 
        else:
91
 
            base = self.static_url('/static/javascript/yui/build/')
92
 
        return base + path
93
 
 
94
 
    controllers_dict = {
95
 
        '+filediff': FileDiffUI,
96
 
        '+revlog': RevLogUI,
97
 
        'annotate': AnnotateUI,
98
 
        'atom': AtomUI,
99
 
        'changes': ChangeLogUI,
100
 
        'diff': DiffUI,
101
 
        'download': DownloadUI,
102
 
        'files': InventoryUI,
103
 
        'revision': RevisionUI,
104
 
        'search': SearchUI,
105
 
        }
106
 
 
107
 
    def last_updated(self):
108
 
        h = self.get_history()
109
 
        change = h.get_changes([h.last_revid])[0]
110
 
        return change.date
111
 
 
112
 
    def branch_url(self):
113
 
        return self.branch.get_config().get_user_option('public_branch')
114
 
 
115
 
    def app(self, environ, start_response):
116
 
        self._url_base = environ['SCRIPT_NAME']
117
 
        self._static_url_base = environ.get('loggerhead.static.url')
118
 
        if self._static_url_base is None:
119
 
            self._static_url_base = self._url_base
120
 
        self._environ = environ
121
 
        if self.served_url is _DEFAULT:
122
 
            public_branch = self.branch_url()
123
 
            if public_branch is not None:
124
 
                self.served_url = public_branch
125
 
            else:
126
 
                self.served_url = self.url([])
127
 
        path = request.path_info_pop(environ)
128
 
        if not path:
129
 
            raise httpexceptions.HTTPMovedPermanently(
130
 
                self._url_base + '/changes')
131
 
        if path == 'static':
132
 
            return static_app(environ, start_response)
133
 
        cls = self.controllers_dict.get(path)
134
 
        if cls is None:
135
 
            raise httpexceptions.HTTPNotFound()
136
 
        self.branch.lock_read()
137
 
        try:
138
 
            try:
139
 
                c = cls(self, self.get_history)
140
 
                return c(environ, start_response)
141
 
            except:
142
 
                environ['exc_info'] = sys.exc_info()
143
 
                environ['branch'] = self
144
 
                raise
145
 
        finally:
146
 
            self.branch.unlock()