15
16
# You should have received a copy of the GNU General Public License
16
17
# along with this program; if not, write to the Free Software
17
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
from turbogears import controllers
26
from configobj import ConfigObj
22
from paste.request import path_info_pop, parse_querystring
28
24
from loggerhead import util
29
from loggerhead.branchview import BranchView
30
from loggerhead.history import is_branch
31
25
from loggerhead.templatefunctions import templatefunctions
33
log = logging.getLogger("loggerhead.controllers")
36
def cherrypy_friendly(s):
38
convert a config section name into a name that pleases cherrypy.
40
return re.sub(r'[^\w\d_]', '_', s)
43
class Project (object):
44
def __init__(self, name, config, root_config):
46
self.friendly_name = config.get('name', name)
47
self.description = config.get('description', '')
48
self.long_description = config.get('long_description', '')
50
self._root_config = root_config
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'))
57
self._auto_folder = config.get('auto_publish_folder', None)
59
if self._auto_folder is not None:
60
self._recheck_auto_folders()
62
def _recheck_auto_folders(self):
63
if self._auto_folder is None:
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)
71
auto_list.append(folder)
73
if auto_list == self._auto_list:
74
# nothing has changed; do nothing.
78
log.debug('Rescanning auto-folder for project %s ...', self.name)
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
86
def _add_view(self, view_name, view_config, folder):
87
c_view_name = cherrypy_friendly(view_name)
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)
94
views = property(lambda self: self._views)
97
class Root (controllers.RootController):
98
def __init__(self, config):
100
self._config = config
101
for project_name in self._config.sections:
102
c_project_name = cherrypy_friendly(project_name)
104
c_project_name, self._config[project_name], self._config)
105
self._projects.append(project)
106
setattr(self, c_project_name, project)
108
@turbogears.expose(template='zpt:loggerhead.templates.browse')
110
for p in self._projects:
111
p._recheck_auto_folders()
26
from loggerhead.zptsupport import load_template
29
class BufferingWriter(object):
31
def __init__(self, writefunc, buf_limit):
35
self.writefunc = writefunc
36
self.buf_limit = buf_limit
39
self.writefunc(''.join(self.buf))
43
def write(self, data):
45
self.buflen += len(data)
46
self.bytes += len(data)
47
if self.buflen > self.buf_limit:
51
class TemplatedBranchView(object):
55
def __init__(self, branch, history_callable):
57
self._history_callable = history_callable
63
if self.__history is not None:
65
self.__history = self._history_callable()
68
def __call__(self, environ, start_response):
70
kwargs = dict(parse_querystring(environ))
71
util.set_context(kwargs)
74
arg = path_info_pop(environ)
81
path = unicode('/'.join(args[1:]), 'utf-8')
113
'projects': self._projects,
85
'static_url': self._branch.static_url,
86
'branch': self._branch,
115
'title': self._config.get('title', None),
88
'url': self._branch.context_url,
117
90
vals.update(templatefunctions)
120
def _check_rebuild(self):
121
for p in self._projects:
127
# for use in profiling the very-slow get_change() method:
128
#h = Root.bazaar.bzr_dev.get_history()
129
#w = list(h.get_revision_history())
130
#h._get_changes_profiled(w[:100])
93
vals.update(self.get_values(path, kwargs, headers))
95
self.log.info('Getting information for %s: %r secs' % (
96
self.__class__.__name__, time.time() - z))
97
if 'Content-Type' not in headers:
98
headers['Content-Type'] = 'text/html'
99
writer = start_response("200 OK", headers.items())
100
template = load_template(self.template_path)
102
w = BufferingWriter(writer, 8192)
103
template.expand_into(w, **vals)
106
'Rendering %s: %r secs, %s bytes' % (
107
self.__class__.__name__, time.time() - z, w.bytes))
114
if len(self.args) > 0 and self.args != ['']:
115
return h.fix_revid(self.args[0])