~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
 
# Copyright (C) 2008-2011 Canonical Ltd.
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
#
17
1
"""The WSGI application for serving a Bazaar branch."""
18
2
 
19
3
import logging
20
4
import urllib
21
 
import sys
22
5
 
23
6
import bzrlib.branch
24
 
import bzrlib.errors
25
7
import bzrlib.lru_cache
26
8
 
27
9
from paste import request
28
10
from paste import httpexceptions
29
11
 
30
12
from loggerhead.apps import static_app
 
13
from loggerhead.controllers.changelog_ui import ChangeLogUI
 
14
from loggerhead.controllers.inventory_ui import InventoryUI
31
15
from loggerhead.controllers.annotate_ui import AnnotateUI
32
 
from loggerhead.controllers.view_ui import ViewUI
 
16
from loggerhead.controllers.revision_ui import RevisionUI
33
17
from loggerhead.controllers.atom_ui import AtomUI
34
 
from loggerhead.controllers.changelog_ui import ChangeLogUI
35
 
from loggerhead.controllers.diff_ui import DiffUI
36
18
from loggerhead.controllers.download_ui import DownloadUI
37
 
from loggerhead.controllers.filediff_ui import FileDiffUI
38
 
from loggerhead.controllers.inventory_ui import InventoryUI
39
 
from loggerhead.controllers.revision_ui import RevisionUI
40
 
from loggerhead.controllers.revlog_ui import RevLogUI
41
19
from loggerhead.controllers.search_ui import SearchUI
42
20
from loggerhead.history import History
43
21
from loggerhead import util
44
22
 
45
23
 
46
 
_DEFAULT = object()
47
 
 
48
24
class BranchWSGIApp(object):
49
25
 
50
26
    def __init__(self, branch, friendly_name=None, config={},
51
 
                 graph_cache=None, branch_link=None, is_root=False,
52
 
                 served_url=_DEFAULT, use_cdn=False):
 
27
                 graph_cache=None, branch_link=None):
53
28
        self.branch = branch
54
29
        self._config = config
55
30
        self.friendly_name = friendly_name
56
31
        self.branch_link = branch_link  # Currently only used in Launchpad
57
32
        self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
58
33
        if graph_cache is None:
59
 
            graph_cache = bzrlib.lru_cache.LRUCache(10)
 
34
            graph_cache = bzrlib.lru_cache.LRUCache()
60
35
        self.graph_cache = graph_cache
61
 
        self.is_root = is_root
62
 
        self.served_url = served_url
63
 
        self.use_cdn = use_cdn
64
36
 
65
37
    def get_history(self):
66
 
        revinfo_disk_cache = None
 
38
        _history = History(self.branch, self.graph_cache)
67
39
        cache_path = self._config.get('cachepath', None)
68
40
        if cache_path is not None:
69
41
            # Only import the cache if we're going to use it.
70
42
            # This makes sqlite optional
71
43
            try:
72
 
                from loggerhead.changecache import RevInfoDiskCache
 
44
                from loggerhead.changecache import FileChangeCache
73
45
            except ImportError:
74
46
                self.log.debug("Couldn't load python-sqlite,"
75
47
                               " continuing without using a cache")
76
48
            else:
77
 
                revinfo_disk_cache = RevInfoDiskCache(cache_path)
78
 
        return History(
79
 
            self.branch, self.graph_cache,
80
 
            revinfo_disk_cache=revinfo_disk_cache, cache_key=self.friendly_name)
 
49
                _history.use_file_cache(
 
50
                    FileChangeCache(_history, cache_path))
 
51
        return _history
81
52
 
82
53
    def url(self, *args, **kw):
83
54
        if isinstance(args[0], list):
85
56
        qs = []
86
57
        for k, v in kw.iteritems():
87
58
            if v is not None:
88
 
                qs.append('%s=%s' % (k, urllib.quote(v)))
 
59
                qs.append('%s=%s'%(k, urllib.quote(v)))
89
60
        qs = '&'.join(qs)
90
 
        path_info = urllib.quote(
91
 
            unicode('/'.join(args)).encode('utf-8'), safe='/~:')
92
 
        if qs:
93
 
            path_info += '?' + qs
94
 
        return self._url_base + path_info
95
 
 
96
 
    def absolute_url(self, *args, **kw):
97
 
        rel_url = self.url(*args, **kw)
98
 
        return request.resolve_relative_url(rel_url, self._environ)
 
61
        return request.construct_url(
 
62
            self._environ, script_name=self._url_base,
 
63
            path_info='/'.join(args),
 
64
            querystring=qs)
99
65
 
100
66
    def context_url(self, *args, **kw):
101
67
        kw = util.get_context(**kw)
104
70
    def static_url(self, path):
105
71
        return self._static_url_base + path
106
72
 
107
 
    def yui_url(self, path):
108
 
        if self.use_cdn:
109
 
            base = 'http://yui.yahooapis.com/3.0.0pr2/build/'
110
 
        else:
111
 
            base = self.static_url('/static/javascript/yui/build/')
112
 
        return base + path
113
 
 
114
73
    controllers_dict = {
115
 
        '+filediff': FileDiffUI,
116
 
        '+revlog': RevLogUI,
117
74
        'annotate': AnnotateUI,
118
 
        'atom': AtomUI,
119
75
        'changes': ChangeLogUI,
120
 
        'diff': DiffUI,
121
 
        'download': DownloadUI,
122
76
        'files': InventoryUI,
123
77
        'revision': RevisionUI,
 
78
        'download': DownloadUI,
 
79
        'atom': AtomUI,
124
80
        'search': SearchUI,
125
 
        'view': ViewUI,
126
81
        }
127
82
 
128
83
    def last_updated(self):
129
84
        h = self.get_history()
130
 
        change = h.get_changes([h.last_revid])[0]
 
85
        change = h.get_changes([ h.last_revid ])[0]
131
86
        return change.date
132
87
 
133
 
    def public_branch_url(self):
 
88
    def branch_url(self):
134
89
        return self.branch.get_config().get_user_option('public_branch')
135
90
 
136
 
    def lookup_app(self, environ):
137
 
        # Check again if the branch is blocked from being served, this is
138
 
        # mostly for tests. It's already checked in apps/transport.py
139
 
        if self.branch.get_config().get_user_option('http_serve') == 'False':
140
 
            raise httpexceptions.HTTPNotFound()
 
91
    def app(self, environ, start_response):
141
92
        self._url_base = environ['SCRIPT_NAME']
142
93
        self._static_url_base = environ.get('loggerhead.static.url')
143
94
        if self._static_url_base is None:
144
95
            self._static_url_base = self._url_base
145
96
        self._environ = environ
146
 
        if self.served_url is _DEFAULT:
147
 
            public_branch = self.public_branch_url()
148
 
            if public_branch is not None:
149
 
                self.served_url = public_branch
150
 
            else:
151
 
                # Loggerhead only supports serving .bzr/ on local branches, so
152
 
                # we shouldn't suggest something that won't work.
153
 
                try:
154
 
                    util.local_path_from_url(self.branch.base)
155
 
                    self.served_url = self.url([])
156
 
                except bzrlib.errors.InvalidURL:
157
 
                    self.served_url = None
158
97
        path = request.path_info_pop(environ)
159
98
        if not path:
160
99
            raise httpexceptions.HTTPMovedPermanently(
161
 
                self.absolute_url('/changes'))
 
100
                self._url_base + '/changes')
162
101
        if path == 'static':
163
 
            return static_app
164
 
        elif path == '+json':
165
 
            environ['loggerhead.as_json'] = True
166
 
            path = request.path_info_pop(environ)
 
102
            return static_app(environ, start_response)
167
103
        cls = self.controllers_dict.get(path)
168
104
        if cls is None:
169
105
            raise httpexceptions.HTTPNotFound()
170
 
        return cls(self, self.get_history)
171
 
 
172
 
    def app(self, environ, start_response):
173
106
        self.branch.lock_read()
174
107
        try:
175
 
            try:
176
 
                c = self.lookup_app(environ)
177
 
                return c(environ, start_response)
178
 
            except:
179
 
                environ['exc_info'] = sys.exc_info()
180
 
                environ['branch'] = self
181
 
                raise
 
108
            c = cls(self, self.get_history())
 
109
            return c(environ, start_response)
182
110
        finally:
183
111
            self.branch.unlock()