~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
 
21
import time
 
22
 
 
23
from configobj import ConfigObj
23
24
 
24
25
import turbogears
25
26
from turbogears import controllers
26
 
from configobj import ConfigObj
 
27
from cherrypy import HTTPRedirect, NotFound
27
28
 
28
29
from loggerhead import util
29
 
from loggerhead.branchview import BranchView
30
 
from loggerhead.history import is_branch
31
 
from loggerhead.templatefunctions import templatefunctions
 
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
32
40
 
33
41
log = logging.getLogger("loggerhead.controllers")
34
42
 
35
 
 
36
 
def cherrypy_friendly(s):
37
 
    """
38
 
    convert a config section name into a name that pleases cherrypy.
39
 
    """
40
 
    return re.sub(r'[^\w\d_]', '_', s)
41
 
 
42
 
 
43
 
class Project (object):
44
 
    def __init__(self, name, config, root_config):
45
 
        self.name = name
46
 
        self.friendly_name = config.get('name', name)
47
 
        self.description = config.get('description', '')
48
 
        self.long_description = config.get('long_description', '')
49
 
        self._config = config
50
 
        self._root_config = root_config
51
 
        
52
 
        self._views = []
53
 
        for view_name in config.sections:
54
 
            log.debug('Configuring (project %s) branch %s...', name, view_name)
55
 
            self._add_view(view_name, config[view_name], config[view_name].get('folder'))
56
 
        
57
 
        self._auto_folder = config.get('auto_publish_folder', None)
58
 
        self._auto_list = []
59
 
        if self._auto_folder is not None:
60
 
            self._recheck_auto_folders()
61
 
    
62
 
    def _recheck_auto_folders(self):
63
 
        if self._auto_folder is None:
64
 
            return
65
 
        auto_list = []
66
 
        # scan a folder for bazaar branches, and add them automatically
67
 
        for path, folders, filenames in os.walk(self._auto_folder):
68
 
            for folder in folders:
69
 
                folder = os.path.join(path, folder)
70
 
                if is_branch(folder):
71
 
                    auto_list.append(folder)
72
 
        auto_list.sort()
73
 
        if auto_list == self._auto_list:
74
 
            # nothing has changed; do nothing.
75
 
            return
76
 
 
77
 
        # rebuild views:
78
 
        log.debug('Rescanning auto-folder for project %s ...', self.name)
79
 
        self._views = []
80
 
        for folder in auto_list:
81
 
            view_name = os.path.basename(folder)
82
 
            log.debug('Auto-configuring (project %s) branch %s...', self.name, view_name)
83
 
            self._add_view(view_name, ConfigObj(), folder)
84
 
        self._auto_list = auto_list
85
 
        
86
 
    def _add_view(self, view_name, view_config, folder):
87
 
        c_view_name = cherrypy_friendly(view_name)
88
 
        view = BranchView(
89
 
            self.name, c_view_name, view_name, folder, view_config,
90
 
            self._config, self._root_config)
91
 
        self._views.append(view)
92
 
        setattr(self, c_view_name, view)
93
 
        
94
 
    views = property(lambda self: self._views)
95
 
 
96
 
 
97
43
class Root (controllers.RootController):
98
 
    def __init__(self, config):
99
 
        self._projects = []
100
 
        self._config = config
101
 
        for project_name in self._config.sections:
102
 
            c_project_name = cherrypy_friendly(project_name)
103
 
            project = Project(
104
 
                c_project_name, self._config[project_name], self._config)
105
 
            self._projects.append(project)
106
 
            setattr(self, c_project_name, project)
107
 
        
108
 
    @turbogears.expose(template='zpt:loggerhead.templates.browse')
 
44
    changes = ChangeLogUI()
 
45
    atom = AtomUI()
 
46
    revision = RevisionUI()
 
47
    files = InventoryUI()
 
48
    annotate = AnnotateUI()
 
49
    
 
50
    @turbogears.expose()
109
51
    def index(self):
110
 
        for p in self._projects:
111
 
            p._recheck_auto_folders()
112
 
        vals = {
113
 
            'projects': self._projects,
114
 
            'util': util,
115
 
            'title': self._config.get('title', None),
116
 
        }
117
 
        vals.update(templatefunctions)
118
 
        return vals
 
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)
 
83
 
 
84
# for use in profiling the very-slow get_change() method:
 
85
#h = util.get_history()
 
86
#h._get_change_profiled(h.last_revid)
 
87