~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/branch.py

  • Committer: Michael Hudson
  • Date: 2008-06-22 23:53:25 UTC
  • Revision ID: michael.hudson@canonical.com-20080622235325-fs0yapvfxn3xhkro
make serve-branches.py executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""The WSGI application for serving a Bazaar branch."""
2
 
 
3
1
import logging
4
2
import urllib
5
3
 
6
 
import bzrlib.branch
7
 
import bzrlib.lru_cache
8
 
 
9
4
from paste import request
10
5
from paste import httpexceptions
 
6
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
11
7
 
12
8
from loggerhead.apps import static_app
 
9
from loggerhead.changecache import FileChangeCache
13
10
from loggerhead.controllers.changelog_ui import ChangeLogUI
14
11
from loggerhead.controllers.inventory_ui import InventoryUI
15
12
from loggerhead.controllers.annotate_ui import AnnotateUI
16
13
from loggerhead.controllers.revision_ui import RevisionUI
17
14
from loggerhead.controllers.atom_ui import AtomUI
18
15
from loggerhead.controllers.download_ui import DownloadUI
 
16
from loggerhead.controllers.bundle_ui import BundleUI
19
17
from loggerhead.history import History
20
18
from loggerhead import util
21
19
 
 
20
logging.basicConfig()
 
21
logging.getLogger().setLevel(logging.DEBUG)
22
22
 
23
23
class BranchWSGIApp(object):
24
24
 
25
 
    def __init__(self, branch, friendly_name=None, config={}, graph_cache=None):
26
 
        self.branch = branch
 
25
    def __init__(self, branch_url, friendly_name=None, config={}):
 
26
        self.branch_url = branch_url
 
27
        self._history = None
27
28
        self._config = config
28
29
        self.friendly_name = friendly_name
29
 
        self.log = logging.getLogger('loggerhead.%s' % (friendly_name,))
30
 
        if graph_cache is None:
31
 
            graph_cache = bzrlib.lru_cache.LRUCache()
32
 
        self.graph_cache = graph_cache
 
30
        self.log = logging.getLogger(friendly_name)
33
31
 
34
 
    def get_history(self):
35
 
        _history = History(self.branch, self.graph_cache)
36
 
        cache_path = self._config.get('cachepath', None)
37
 
        if cache_path is not None:
38
 
            # Only import the cache if we're going to use it.
39
 
            # This makes sqlite optional
40
 
            try:
41
 
                from loggerhead.changecache import FileChangeCache
42
 
            except ImportError:
43
 
                self.log.debug("Couldn't load python-sqlite,"
44
 
                               " continuing without using a cache")
45
 
            else:
46
 
                _history.use_file_cache(
47
 
                    FileChangeCache(_history, cache_path))
48
 
        return _history
 
32
    @property
 
33
    def history(self):
 
34
        if (self._history is None) or self._history.out_of_date():
 
35
            self.log.debug('Reload branch history...')
 
36
            _history = self._history = History.from_folder(self.branch_url)
 
37
            cache_path = self._config.get('cachepath', None)
 
38
            if cache_path is not None:
 
39
                _history.use_file_cache(FileChangeCache(_history, cache_path))
 
40
        return self._history
49
41
 
50
42
    def url(self, *args, **kw):
51
43
        if isinstance(args[0], list):
74
66
        'revision': RevisionUI,
75
67
        'download': DownloadUI,
76
68
        'atom': AtomUI,
 
69
        'bundle': BundleUI,
77
70
        }
78
71
 
79
72
    def last_updated(self):
80
 
        h = self.get_history()
 
73
        h = self.history
81
74
        change = h.get_changes([ h.last_revid ])[0]
82
75
        return change.date
83
76
 
84
77
    def branch_url(self):
85
 
        return self.branch.get_config().get_user_option('public_branch')
 
78
        return self.history.get_config().get_user_option('public_branch')
86
79
 
87
80
    def app(self, environ, start_response):
 
81
        req = WSGIRequest(environ)
 
82
        response = WSGIResponse()
 
83
        response.headers['Content-Type'] = 'text/plain'
88
84
        self._url_base = environ['SCRIPT_NAME']
89
85
        self._static_url_base = environ.get('loggerhead.static.url')
90
86
        if self._static_url_base is None:
99
95
        cls = self.controllers_dict.get(path)
100
96
        if cls is None:
101
97
            raise httpexceptions.HTTPNotFound()
102
 
        self.branch.lock_read()
103
 
        try:
104
 
            c = cls(self, self.get_history())
105
 
            return c(environ, start_response)
106
 
        finally:
107
 
            self.branch.unlock()
 
98
        c = cls(self)
 
99
        c.default(req, response)
 
100
        return response(environ, start_response)