~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Alexandre Garnier
  • Date: 2009-05-30 17:40:05 UTC
  • mto: This revision was merged to the branch mainline in revision 358.
  • Revision ID: zigouigoui.garnier@laposte.net-20090530174005-h4dztserbvrjaw5f
Corrections on bugs info display
 - CSS corrections
 - change order with revision info
 - correct URL in href

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""The WSGI application for serving a Bazaar branch."""
 
2
 
1
3
import logging
2
4
import urllib
 
5
import sys
 
6
 
 
7
import bzrlib.branch
 
8
import bzrlib.lru_cache
3
9
 
4
10
from paste import request
5
11
from paste import httpexceptions
6
 
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
7
12
 
8
13
from loggerhead.apps import static_app
9
 
from loggerhead.changecache import FileChangeCache
 
14
from loggerhead.controllers.annotate_ui import AnnotateUI
 
15
from loggerhead.controllers.atom_ui import AtomUI
10
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
11
20
from loggerhead.controllers.inventory_ui import InventoryUI
12
 
from loggerhead.controllers.annotate_ui import AnnotateUI
13
21
from loggerhead.controllers.revision_ui import RevisionUI
14
 
from loggerhead.controllers.atom_ui import AtomUI
15
 
from loggerhead.controllers.download_ui import DownloadUI
 
22
from loggerhead.controllers.revlog_ui import RevLogUI
 
23
from loggerhead.controllers.search_ui import SearchUI
16
24
from loggerhead.history import History
17
25
from loggerhead import util
18
26
 
19
 
logging.basicConfig()
20
 
logging.getLogger().setLevel(logging.DEBUG)
 
27
 
 
28
_DEFAULT = object()
21
29
 
22
30
class BranchWSGIApp(object):
23
31
 
24
 
    def __init__(self, branch_url, friendly_name=None, config={}):
25
 
        self.branch_url = branch_url
26
 
        self._history = None
 
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
27
36
        self._config = config
28
37
        self.friendly_name = friendly_name
29
 
        self.log = logging.getLogger(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
30
46
 
31
 
    @property
32
 
    def history(self):
33
 
        if (self._history is None) or self._history.out_of_date():
34
 
            self.log.debug('Reload branch history...')
35
 
            _history = self._history = History.from_folder(self.branch_url)
36
 
            cache_path = self._config.get('cachepath', None)
37
 
            if cache_path is not None:
38
 
                _history.use_file_cache(FileChangeCache(_history, cache_path))
39
 
        return self._history
 
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)
40
66
 
41
67
    def url(self, *args, **kw):
42
68
        if isinstance(args[0], list):
48
74
        qs = '&'.join(qs)
49
75
        return request.construct_url(
50
76
            self._environ, script_name=self._url_base,
51
 
            path_info='/'.join(args),
 
77
            path_info=unicode('/'.join(args)).encode('utf-8'),
52
78
            querystring=qs)
53
79
 
54
80
    def context_url(self, *args, **kw):
58
84
    def static_url(self, path):
59
85
        return self._static_url_base + path
60
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
 
61
94
    controllers_dict = {
 
95
        '+filediff': FileDiffUI,
 
96
        '+revlog': RevLogUI,
62
97
        'annotate': AnnotateUI,
 
98
        'atom': AtomUI,
63
99
        'changes': ChangeLogUI,
 
100
        'diff': DiffUI,
 
101
        'download': DownloadUI,
64
102
        'files': InventoryUI,
65
103
        'revision': RevisionUI,
66
 
        'download': DownloadUI,
67
 
        'atom': AtomUI,
 
104
        'search': SearchUI,
68
105
        }
69
106
 
70
107
    def last_updated(self):
71
 
        h = self.history
72
 
        change = h.get_changes([ h.last_revid ])[0]
 
108
        h = self.get_history()
 
109
        change = h.get_changes([h.last_revid])[0]
73
110
        return change.date
74
111
 
75
 
    def branch_url(self):
76
 
        return self.history.get_config().get_user_option('public_branch')
 
112
    def public_branch_url(self):
 
113
        return self.branch.get_config().get_user_option('public_branch')
77
114
 
78
115
    def app(self, environ, start_response):
79
 
        req = WSGIRequest(environ)
80
 
        response = WSGIResponse()
81
 
        response.headers['Content-Type'] = 'text/plain'
82
116
        self._url_base = environ['SCRIPT_NAME']
83
117
        self._static_url_base = environ.get('loggerhead.static.url')
84
118
        if self._static_url_base is None:
85
119
            self._static_url_base = self._url_base
86
120
        self._environ = environ
 
121
        if self.served_url is _DEFAULT:
 
122
            public_branch = self.public_branch_url()
 
123
            if public_branch is not None:
 
124
                self.served_url = public_branch
 
125
            else:
 
126
                # Loggerhead only supports serving .bzr/ on local branches, so
 
127
                # we shouldn't suggest something that won't work.
 
128
                if self.branch.base.startswith('file://'):
 
129
                    self.served_url = self.url([])
 
130
                else:
 
131
                    self.served_url = None
87
132
        path = request.path_info_pop(environ)
88
133
        if not path:
89
134
            raise httpexceptions.HTTPMovedPermanently(
93
138
        cls = self.controllers_dict.get(path)
94
139
        if cls is None:
95
140
            raise httpexceptions.HTTPNotFound()
96
 
        c = cls(self)
97
 
        c.default(req, response)
98
 
        return response(environ, start_response)
 
141
        self.branch.lock_read()
 
142
        try:
 
143
            try:
 
144
                c = cls(self, self.get_history)
 
145
                return c(environ, start_response)
 
146
            except:
 
147
                environ['exc_info'] = sys.exc_info()
 
148
                environ['branch'] = self
 
149
                raise
 
150
        finally:
 
151
            self.branch.unlock()