~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

  • Committer: Robey Pointer
  • Date: 2006-12-18 04:16:27 UTC
  • Revision ID: robey@lag.net-20061218041627-u7dyjoqpo6nygzut
remove tabs

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#
19
19
 
20
20
import logging
21
 
import os
22
 
import re
23
 
import sys
24
21
import time
25
22
 
 
23
from configobj import ConfigObj
 
24
 
26
25
import turbogears
27
26
from turbogears import controllers
28
27
from cherrypy import HTTPRedirect, NotFound
29
 
from configobj import ConfigObj
30
28
 
31
29
from loggerhead import util
32
 
from loggerhead.branchview import BranchView
33
 
from loggerhead.history import History, is_branch
 
30
 
 
31
my_config = ConfigObj('loggerhead.conf', encoding='utf-8')
 
32
util.set_config(my_config)
 
33
 
 
34
from loggerhead.controllers.changelog_ui import ChangeLogUI
 
35
from loggerhead.controllers.atom_ui import AtomUI
 
36
from loggerhead.controllers.revision_ui import RevisionUI
 
37
from loggerhead.controllers.inventory_ui import InventoryUI
 
38
from loggerhead.controllers.annotate_ui import AnnotateUI
 
39
from loggerhead.history import History
34
40
 
35
41
log = logging.getLogger("loggerhead.controllers")
36
42
 
37
 
 
38
 
def cherrypy_friendly(s):
39
 
    """
40
 
    convert a config section name into a name that pleases cherrypy.
41
 
    """
42
 
    return re.sub(r'[^\w\d_]', '_', s)
43
 
 
44
 
 
45
 
class Project (object):
46
 
    def __init__(self, name, config):
47
 
        self.name = name
48
 
        self.friendly_name = config.get('name', name)
49
 
        self.description = config.get('description', '')
50
 
        self.long_description = config.get('long_description', '')
51
 
        self._config = config
52
 
        
53
 
        self._views = []
54
 
        for view_name in config.sections:
55
 
            log.debug('Configuring (project %r) branch %r...', name, view_name)
56
 
            self._add_view(view_name, config[view_name], config[view_name].get('folder'))
57
 
        
58
 
        auto_folder = config.get('auto_publish_folder', None)
59
 
        if auto_folder is not None:
60
 
            auto_list = []
61
 
            # scan a folder for bazaar branches, and add them automatically
62
 
            for path, folders, filenames in os.walk(auto_folder):
63
 
                for folder in folders:
64
 
                    folder = os.path.join(path, folder)
65
 
                    if is_branch(folder):
66
 
                        auto_list.append(folder)
67
 
            auto_list.sort()
68
 
            for folder in auto_list:
69
 
                view_name = os.path.basename(folder)
70
 
                log.debug('Auto-configuring (project %r) branch %r...', name, view_name)
71
 
                self._add_view(view_name, ConfigObj(), folder)
72
 
    
73
 
    def _add_view(self, view_name, view_config, folder):
74
 
        c_view_name = cherrypy_friendly(view_name)
75
 
        view = BranchView(self.name, c_view_name, view_name, folder, view_config, self._config)
76
 
        self._views.append(view)
77
 
        setattr(self, c_view_name, view)
78
 
        
79
 
    views = property(lambda self: self._views)
80
 
 
81
 
 
82
43
class Root (controllers.RootController):
83
 
    def __init__(self):
84
 
        self._projects = []
85
 
        for project_name in sys._loggerhead_config.sections:
86
 
            c_project_name = cherrypy_friendly(project_name)
87
 
            project = Project(c_project_name, sys._loggerhead_config[project_name])
88
 
            self._projects.append(project)
89
 
            setattr(self, c_project_name, project)
90
 
        
91
 
    @turbogears.expose(template='loggerhead.templates.browse')
 
44
    changes = ChangeLogUI()
 
45
    atom = AtomUI()
 
46
    revision = RevisionUI()
 
47
    files = InventoryUI()
 
48
    annotate = AnnotateUI()
 
49
    
 
50
    @turbogears.expose()
92
51
    def index(self):
93
 
        return {
94
 
            'projects': self._projects,
95
 
            'util': util,
96
 
            'title': sys._loggerhead_config.get('title', ''),
97
 
        }
98
 
 
99
 
    def _check_rebuild(self):
100
 
        for p in self._projects:
101
 
            for v in p.views:
102
 
                v.check_rebuild()
103
 
 
104
 
 
105
 
# singleton:
106
 
Root = Root()
107
 
 
 
52
        raise HTTPRedirect(turbogears.url('/changes'))
 
53
 
 
54
 
 
55
# force history to be read:
 
56
util.get_history()
 
57
 
 
58
 
 
59
 
 
60
def rebuild_cache():
 
61
    log.info('Rebuilding revision cache...')
 
62
    last_update = time.time()
 
63
    h = util.get_history()
 
64
    count = 0
 
65
    
 
66
    work = h.get_revision_history()
 
67
    for r in work:
 
68
        h.get_change(r)
 
69
        if h.out_of_date():
 
70
            return
 
71
        count += 1
 
72
        if time.time() - last_update > 60:
 
73
            log.info('Revision cache rebuilding continues: %d/%d' % (count, len(work)))
 
74
            last_update = time.time()
 
75
            h.flush_cache()
 
76
    log.info('Revision cache rebuild completed.')
 
77
 
 
78
 
 
79
# re-index every hour (for now -- maybe should be even longer?)
 
80
index_freq = 3600
 
81
 
 
82
turbogears.scheduler.add_interval_task(initialdelay=1, interval=index_freq, action=rebuild_cache)
108
83
 
109
84
# for use in profiling the very-slow get_change() method:
110
 
#h = Root.bazaar.bzr_dev.get_history()
111
 
#w = list(h.get_revision_history())
112
 
#h._get_changes_profiled(w[:100])
 
85
#h = util.get_history()
 
86
#h._get_change_profiled(h.last_revid)
113
87