~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/config.py

  • Committer: Martin Albisetti
  • Date: 2008-08-06 19:38:14 UTC
  • Revision ID: argentina@gmail.com-20080806193814-7h2hgep2fyktlkql
Further cleaning

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""A server that uses a loggerhead.conf file.
 
2
 
 
3
We recreate the branch discovery and url scheme of the old branchview
 
4
code.  It's all a bit horrible really.
 
5
"""
 
6
 
 
7
import logging
 
8
import os
 
9
import posixpath
 
10
 
 
11
import bzrlib.lru_cache
 
12
 
 
13
from configobj import ConfigObj
 
14
 
 
15
from paste.request import path_info_pop
 
16
from paste import httpexceptions
 
17
from paste.wsgiwrappers import WSGIResponse
 
18
 
 
19
from loggerhead.apps.branch import BranchWSGIApp
 
20
from loggerhead.apps import favicon_app, static_app
 
21
from loggerhead.templatefunctions import templatefunctions
 
22
from loggerhead.zptsupport import load_template
 
23
from loggerhead import util
 
24
 
 
25
log = logging.getLogger("loggerhead.controllers")
 
26
 
 
27
from loggerhead.history import is_branch
 
28
 
 
29
class Project(object):
 
30
    """A project contains the branches.
 
31
 
 
32
    There is some complication because we don't want to hold on to the
 
33
    branches when they are not being browsed."""
 
34
 
 
35
    def __init__(self, name, config, root_config, graph_cache):
 
36
        self.name = name
 
37
        self.friendly_name = config.get('name', name)
 
38
        self.description = config.get('description', '')
 
39
        self.long_description = config.get('long_description', '')
 
40
        self._config = config
 
41
        self._root_config = root_config
 
42
        self.graph_cache = graph_cache
 
43
 
 
44
        self.view_names = []
 
45
        self.view_data_by_name = {}
 
46
        for view_name in config.sections:
 
47
            log.debug('Configuring (project %s) branch %s...', name, view_name)
 
48
            self._add_view(
 
49
                view_name, config[view_name], config[view_name].get('folder'))
 
50
 
 
51
        self._auto_folder = config.get('auto_publish_folder', None)
 
52
        self._auto_list = []
 
53
        if self._auto_folder is not None:
 
54
            self._recheck_auto_folders()
 
55
 
 
56
    def _recheck_auto_folders(self):
 
57
        if self._auto_folder is None:
 
58
            return
 
59
        auto_list = []
 
60
        # scan a folder for bazaar branches, and add them automatically
 
61
        for path, folders, filenames in os.walk(self._auto_folder):
 
62
            for folder in folders:
 
63
                folder = os.path.join(path, folder)
 
64
                if is_branch(folder):
 
65
                    auto_list.append(folder)
 
66
        auto_list.sort()
 
67
        if auto_list == self._auto_list:
 
68
            # nothing has changed; do nothing.
 
69
            return
 
70
 
 
71
        # rebuild views:
 
72
        self.view_names = []
 
73
        log.debug('Rescanning auto-folder for project %s ...', self.name)
 
74
        for folder in auto_list:
 
75
            view_name = os.path.basename(folder)
 
76
            log.debug('Auto-configuring (project %s) branch %s...', self.name, view_name)
 
77
            self._add_view(view_name, ConfigObj(), folder)
 
78
        self._auto_list = auto_list
 
79
 
 
80
    def _get_branch_url(self, view, view_config, folder):
 
81
        url = view_config.get('url', None)
 
82
        if url is not None:
 
83
            return url
 
84
        url = self._config.get('url_prefix', None)
 
85
        if url is not None:
 
86
            return posixpath.join(url, folder) + '/'
 
87
        return None
 
88
 
 
89
    def _get_description(self, view, view_config, history):
 
90
        description = view_config.get('description', None)
 
91
        if description is not None:
 
92
            return description
 
93
        description = history._branch.get_config().get_user_option('description')
 
94
        return description
 
95
 
 
96
    def _add_view(self, view_name, view_config, folder):
 
97
        b = bzrlib.branch.Branch.open(folder)
 
98
        view = BranchWSGIApp(b, view_name, view_config, self.graph_cache)
 
99
        b.lock_read()
 
100
        try:
 
101
            history = view.get_history()
 
102
            friendly_name = view_config.get('branch_name', None)
 
103
            if friendly_name is None:
 
104
                friendly_name = history.get_config().get_nickname()
 
105
                if friendly_name is None:
 
106
                    friendly_name = view_name
 
107
            self.view_data_by_name[view_name] = {
 
108
                'branch_path': folder,
 
109
                'args': (view_name, view_config, self.graph_cache),
 
110
                'description': self._get_description(view, view_config, history),
 
111
                '_src_folder': folder,
 
112
                '_view_config': view_config,
 
113
                'friendly_name': friendly_name,
 
114
                'name': view_name,
 
115
                }
 
116
            branch_url = self._get_branch_url(view, view_config, view_name)
 
117
            if branch_url is not None:
 
118
                self.view_data_by_name[view_name]['branch_url'] = branch_url
 
119
            self.view_names.append(view_name)
 
120
        finally:
 
121
            b.unlock()
 
122
 
 
123
    def view_named(self, name):
 
124
        view_data = self.view_data_by_name.get(name)
 
125
        if view_data is None:
 
126
            return None
 
127
        view_data = view_data.copy()
 
128
        branch_path = view_data.pop('branch_path')
 
129
        args = view_data.pop('args')
 
130
        b = bzrlib.branch.Branch.open(branch_path)
 
131
        b.lock_read()
 
132
        view = BranchWSGIApp(b, *args)
 
133
        for k in view_data:
 
134
            setattr(view, k, view_data[k])
 
135
        return view
 
136
 
 
137
    def call(self, environ, start_response):
 
138
        segment = path_info_pop(environ)
 
139
        if not segment:
 
140
            raise httpexceptions.HTTPNotFound()
 
141
        else:
 
142
            view = self.view_named(segment)
 
143
            if view is None:
 
144
                raise httpexceptions.HTTPNotFound()
 
145
            try:
 
146
                return view.app(environ, start_response)
 
147
            finally:
 
148
                view.branch.unlock()
 
149
 
 
150
 
 
151
class Root(object):
 
152
    """The root of the server -- renders as the browse view,
 
153
    dispatches to Project above for each 'project'."""
 
154
 
 
155
    def __init__(self, config):
 
156
        self.projects = []
 
157
        self.config = config
 
158
        self.projects_by_name = {}
 
159
        graph_cache = bzrlib.lru_cache.LRUCache()
 
160
        for project_name in self.config.sections:
 
161
            project = Project(
 
162
                project_name, self.config[project_name], self.config, graph_cache)
 
163
            self.projects.append(project)
 
164
            self.projects_by_name[project_name] = project
 
165
 
 
166
    def browse(self, response):
 
167
        # This is insanely complicated because we want to open and
 
168
        # lock all the branches, render the view and then unlock the
 
169
        # branches again.
 
170
        for p in self.projects:
 
171
            p._recheck_auto_folders()
 
172
        class branch(object):
 
173
            @staticmethod
 
174
            def static_url(path):
 
175
                return self._static_url_base + path
 
176
        views_by_project = {}
 
177
        all_views = []
 
178
        try:
 
179
            for p in self.projects:
 
180
                views_by_project[p] = []
 
181
                for vn in p.view_names:
 
182
                    v = p.view_named(vn)
 
183
                    all_views.append(v)
 
184
                    views_by_project[p].append(v)
 
185
            vals = {
 
186
                'projects': self.projects,
 
187
                'util': util,
 
188
                'title': self.config.get('title', None),
 
189
                'branch': branch,
 
190
                'views_by_project': views_by_project,
 
191
            }
 
192
            vals.update(templatefunctions)
 
193
            response.headers['Content-Type'] = 'text/html'
 
194
            template = load_template('loggerhead.templates.browse')
 
195
            template.expand_into(response, **vals)
 
196
        finally:
 
197
            for v in all_views:
 
198
                v.branch.unlock()
 
199
 
 
200
    def __call__(self, environ, start_response):
 
201
        self._static_url_base = environ['loggerhead.static.url'] = \
 
202
                                environ['SCRIPT_NAME']
 
203
        segment = path_info_pop(environ)
 
204
        if segment is None:
 
205
            raise httpexceptions.HTTPMovedPermanently(
 
206
                environ['SCRIPT_NAME'] + '/')
 
207
        elif segment == '':
 
208
            response = WSGIResponse()
 
209
            self.browse(response)
 
210
            return response(environ, start_response)
 
211
        elif segment == 'static':
 
212
            return static_app(environ, start_response)
 
213
        elif segment == 'favicon.ico':
 
214
            return favicon_app(environ, start_response)
 
215
        else:
 
216
            project = self.projects_by_name.get(segment)
 
217
            if project is None:
 
218
                raise httpexceptions.HTTPNotFound()
 
219
            return project.call(environ, start_response)