27
23
from turbogears import controllers
28
from cherrypy import NotFound
29
from configobj import ConfigObj
24
from cherrypy import HTTPRedirect, NotFound
26
from loggerhead.controllers.changelog_ui import ChangeLogUI
27
from loggerhead.controllers.atom_ui import AtomUI
28
from loggerhead.controllers.revision_ui import RevisionUI
29
from loggerhead.controllers.inventory_ui import InventoryUI
30
from loggerhead.controllers.annotate_ui import AnnotateUI
31
from loggerhead.history import History
31
32
from loggerhead import util
32
from loggerhead.branchview import BranchView
33
from loggerhead.history import History, is_branch
35
35
log = logging.getLogger("loggerhead.controllers")
38
def cherrypy_friendly(s):
40
convert a config section name into a name that pleases cherrypy.
42
return re.sub(r'[^\w\d_]', '_', s)
45
class Project (object):
46
def __init__(self, name, config):
48
self.friendly_name = config.get('name', name)
49
self.description = config.get('description', '')
50
self.long_description = config.get('long_description', '')
54
for view_name in config.sections:
55
log.debug('Configuring (project %s) branch %s...', name, view_name)
56
self._add_view(view_name, config[view_name], config[view_name].get('folder'))
58
self._auto_folder = config.get('auto_publish_folder', None)
60
if self._auto_folder is not None:
61
self._recheck_auto_folders()
63
def _recheck_auto_folders(self):
64
if self._auto_folder is None:
67
# scan a folder for bazaar branches, and add them automatically
68
for path, folders, filenames in os.walk(self._auto_folder):
69
for folder in folders:
70
folder = os.path.join(path, folder)
72
auto_list.append(folder)
74
if auto_list == self._auto_list:
75
# nothing has changed; do nothing.
79
log.debug('Rescanning auto-folder for project %s ...', self.name)
81
for folder in auto_list:
82
view_name = os.path.basename(folder)
83
log.debug('Auto-configuring (project %s) branch %s...', self.name, view_name)
84
self._add_view(view_name, ConfigObj(), folder)
85
self._auto_list = auto_list
87
def _add_view(self, view_name, view_config, folder):
88
c_view_name = cherrypy_friendly(view_name)
89
view = BranchView(self.name, c_view_name, view_name, folder, view_config, self._config)
90
self._views.append(view)
91
setattr(self, c_view_name, view)
93
views = property(lambda self: self._views)
96
37
class Root (controllers.RootController):
97
def __init__(self, config):
100
for project_name in self._config.sections:
101
c_project_name = cherrypy_friendly(project_name)
102
project = Project(c_project_name, self._config[project_name])
103
self._projects.append(project)
104
setattr(self, c_project_name, project)
106
@turbogears.expose(template='loggerhead.templates.browse')
38
changes = ChangeLogUI()
40
revision = RevisionUI()
42
annotate = AnnotateUI()
108
for p in self._projects:
109
p._recheck_auto_folders()
111
'projects': self._projects,
113
'title': self._config.get('title', ''),
116
def _check_rebuild(self):
117
for p in self._projects:
123
# for use in profiling the very-slow get_change() method:
124
#h = Root.bazaar.bzr_dev.get_history()
125
#w = list(h.get_revision_history())
126
#h._get_changes_profiled(w[:100])
46
raise HTTPRedirect(turbogears.url('/changes'))
49
# force history to be read:
53
log.info('Rebuilding revision cache...')
54
last_update = time.time()
55
h = util.get_history()
58
work = h.get_revision_history()
64
if time.time() - last_update > 60:
65
log.info('Revision cache rebuilding continues: %d/%d' % (count, len(work)))
66
last_update = time.time()
68
log.info('Revision cache rebuild completed.')
71
# re-index every hour (for now -- maybe should be even longer?)
74
turbogears.scheduler.add_interval_task(initialdelay=1, interval=index_freq, action=rebuild_cache)