~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/branchview.py

use 2.4 by default

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
"""
22
22
 
23
23
import logging
 
24
import posixpath
24
25
import threading
 
26
import urllib
25
27
 
26
28
import turbogears
27
29
from cherrypy import HTTPRedirect
28
30
 
29
31
from loggerhead import util
30
 
from loggerhead.changecache import ChangeCache
 
32
from loggerhead.changecache import ChangeCache, FileChangeCache
31
33
from loggerhead.history import History
32
34
from loggerhead.textindex import TextIndex
33
35
from loggerhead.controllers.changelog_ui import ChangeLogUI
43
45
 
44
46
 
45
47
class BranchView (object):
46
 
    def __init__(self, group_name, name, config):
 
48
    def __init__(self, group_name, name, subfolder, absfolder, config,
 
49
                 project_config, root_config):
47
50
        self._group_name = group_name
48
51
        self._name = name
 
52
        self._folder = subfolder
 
53
        self._absfolder = absfolder
49
54
        self._config = config
 
55
        self._project_config = project_config
 
56
        self._root_config = root_config
50
57
        self.log = logging.getLogger('loggerhead.%s' % (name,))
51
 
        
 
58
 
52
59
        # branch history
53
60
        self._history_lock = threading.RLock()
54
61
        self._history = None
55
 
        
 
62
        self._closed = False
 
63
 
56
64
        self.changes = ChangeLogUI(self)
57
65
        self.revision = RevisionUI(self)
58
66
        self.files = InventoryUI(self)
60
68
        self.download = DownloadUI(self)
61
69
        self.atom = AtomUI(self)
62
70
        self.bundle = BundleUI(self)
63
 
        
 
71
 
64
72
        # force history object to be loaded:
65
73
        self.get_history()
66
 
    
 
74
 
 
75
        turbogears.startup.call_on_shutdown.append(self.close)
 
76
 
 
77
    @with_history_lock
 
78
    def close(self):
 
79
        # it's important that we cleanly detach the history, so the cache
 
80
        # files can be closed correctly and hopefully remain uncorrupted.
 
81
        # this should also stop any ongoing indexing.
 
82
        self._history.detach()
 
83
        self._history = None
 
84
        self._closed = True
 
85
 
67
86
    config = property(lambda self: self._config)
68
 
    
 
87
 
69
88
    name = property(lambda self: self._name)
70
89
 
71
90
    group_name = property(lambda self: self._group_name)
72
 
    
73
 
    friendly_name = property(lambda self: self._config.get('branch_name', self._name))
74
 
 
75
 
    description = property(lambda self: self._config.get('description', ''))
76
 
    
77
 
    branch_url = property(lambda self: self._config.get('url', ''))
 
91
 
 
92
    def _get_friendly_name(self):
 
93
        name = self._config.get('branch_name', None)
 
94
        if name is not None:
 
95
            return name
 
96
        # try branch-specific config?
 
97
        name = self.get_history().get_config().get_nickname()
 
98
        if name is not None:
 
99
            return name
 
100
        return self._name
 
101
 
 
102
    friendly_name = property(_get_friendly_name)
 
103
 
 
104
    def _get_description(self):
 
105
        description = self._config.get('description', None)
 
106
        if description is not None:
 
107
            return description
 
108
        # try branch-specific config?
 
109
        description = self.get_history().get_config().get_user_option('description')
 
110
        return description
 
111
 
 
112
    description = property(_get_description)
 
113
 
 
114
    def _get_branch_url(self):
 
115
        url = self._config.get('url', None)
 
116
        if url is not None:
 
117
            return url
 
118
        # try to assemble one from the project, if an url_prefix was defined.
 
119
        url = self._project_config.get('url_prefix', None)
 
120
        if url is not None:
 
121
            return posixpath.join(url, self._folder) + '/'
 
122
        # try branch-specific config?
 
123
        url = self.get_history().get_config().get_user_option('public_branch')
 
124
        return url
 
125
 
 
126
    branch_url = property(_get_branch_url)
78
127
 
79
128
    @turbogears.expose()
80
129
    def index(self):
81
130
        raise HTTPRedirect(self.url('/changes'))
82
131
 
 
132
    def get_config_item(self, item, default=None):
 
133
        for conf in self._config, self._project_config, self._root_config:
 
134
            if item in conf:
 
135
                return conf[item]
 
136
        return default
 
137
 
83
138
    @with_history_lock
84
139
    def get_history(self):
85
140
        """
88
143
        calls.  but if the bazaar branch on-disk has been updated since this
89
144
        History was created, a new object will be created and returned.
90
145
        """
 
146
        if self._closed:
 
147
            return None
91
148
        if (self._history is None) or self._history.out_of_date():
92
149
            self.log.debug('Reload branch history...')
93
150
            if self._history is not None:
94
151
                self._history.detach()
95
 
            self._history = History.from_folder(self._config.get('folder'), self._name)
 
152
            _history = self._history = History.from_folder(
 
153
                self._absfolder, self._name)
96
154
            cache_path = self._config.get('cachepath', None)
 
155
            if cache_path is None:
 
156
                # try the project config
 
157
                cache_path = self._project_config.get('cachepath', None)
97
158
            if cache_path is not None:
98
 
                self._history.use_cache(ChangeCache(self._history, cache_path))
99
 
                self._history.use_search_index(TextIndex(self._history, cache_path))
 
159
                _history.use_cache(ChangeCache(_history, cache_path))
 
160
                _history.use_file_cache(FileChangeCache(_history, cache_path))
 
161
                _history.use_search_index(TextIndex(_history, cache_path))
100
162
        return self._history
101
 
    
 
163
 
102
164
    def check_rebuild(self):
103
165
        h = self.get_history()
104
 
        h.check_rebuild()
105
 
    
 
166
        if h is not None:
 
167
            h.check_rebuild()
 
168
 
106
169
    def url(self, elements, **kw):
 
170
        "build an url relative to this branch"
107
171
        if not isinstance(elements, list):
108
172
            elements = [elements]
109
173
        if elements[0].startswith('/'):
110
174
            elements[0] = elements[0][1:]
 
175
        elements = [urllib.quote(x) for x in elements]
111
176
        return turbogears.url([ '/' + self.group_name, self.name ] + elements, **kw)
112
177
 
 
178
    def context_url(self, elements, **kw):
 
179
        "build an url relative to this branch, bringing along browsing context"
 
180
        return self.url(elements, **util.get_context(**kw))
 
181
 
113
182
    def last_updated(self):
114
183
        h = self.get_history()
115
184
        change = h.get_changes([ h.last_revid ])[0]