1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2007-2010 Edgewall Software |
---|
4 | # All rights reserved. |
---|
5 | # |
---|
6 | # This software is licensed as described in the file COPYING, which |
---|
7 | # you should have received as part of this distribution. The terms |
---|
8 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
9 | |
---|
10 | """Implementation of the web administration interface.""" |
---|
11 | |
---|
12 | from pkg_resources import require, DistributionNotFound |
---|
13 | import re |
---|
14 | |
---|
15 | from trac.core import * |
---|
16 | from trac.admin import IAdminPanelProvider |
---|
17 | from trac.web.chrome import add_stylesheet, add_script, add_warning, add_notice |
---|
18 | from trac.versioncontrol.api import RepositoryManager |
---|
19 | |
---|
20 | from bitten import __multirepos__ |
---|
21 | from bitten.model import BuildConfig, TargetPlatform |
---|
22 | from bitten.recipe import Recipe, InvalidRecipeError |
---|
23 | from bitten.util import xmlio |
---|
24 | from bitten.util.repository import get_repos |
---|
25 | |
---|
26 | |
---|
27 | class BuildMasterAdminPageProvider(Component): |
---|
28 | """Web administration panel for configuring the build master.""" |
---|
29 | |
---|
30 | implements(IAdminPanelProvider) |
---|
31 | |
---|
32 | # IAdminPanelProvider methods |
---|
33 | |
---|
34 | def get_admin_panels(self, req): |
---|
35 | if req.perm.has_permission('BUILD_ADMIN'): |
---|
36 | yield ('bitten', 'Builds', 'master', 'Master Settings') |
---|
37 | |
---|
38 | def render_admin_panel(self, req, cat, page, path_info): |
---|
39 | from bitten.master import BuildMaster |
---|
40 | master = BuildMaster(self.env) |
---|
41 | |
---|
42 | if req.method == 'POST': |
---|
43 | self._save_config_changes(req, master) |
---|
44 | req.redirect(req.abs_href.admin(cat, page)) |
---|
45 | |
---|
46 | data = {'master': master} |
---|
47 | add_stylesheet(req, 'bitten/admin.css') |
---|
48 | return 'bitten_admin_master.html', data |
---|
49 | |
---|
50 | # Internal methods |
---|
51 | |
---|
52 | def _save_config_changes(self, req, master): |
---|
53 | changed = False |
---|
54 | |
---|
55 | build_all = 'build_all' in req.args |
---|
56 | if build_all != master.build_all: |
---|
57 | self.config['bitten'].set('build_all', |
---|
58 | build_all and 'yes' or 'no') |
---|
59 | changed = True |
---|
60 | |
---|
61 | adjust_timestamps = 'adjust_timestamps' in req.args |
---|
62 | if adjust_timestamps != master.adjust_timestamps: |
---|
63 | self.config['bitten'].set('adjust_timestamps', |
---|
64 | adjust_timestamps and 'yes' or 'no') |
---|
65 | changed = True |
---|
66 | |
---|
67 | stabilize_wait = int(req.args.get('stabilize_wait', 0)) |
---|
68 | if stabilize_wait != master.stabilize_wait: |
---|
69 | self.config['bitten'].set('stabilize_wait', str(stabilize_wait)) |
---|
70 | changed = True |
---|
71 | |
---|
72 | slave_timeout = int(req.args.get('slave_timeout', 0)) |
---|
73 | if slave_timeout != master.slave_timeout: |
---|
74 | self.config['bitten'].set('slave_timeout', str(slave_timeout)) |
---|
75 | changed = True |
---|
76 | |
---|
77 | quick_status = 'quick_status' in req.args |
---|
78 | if quick_status != master.quick_status: |
---|
79 | self.config['bitten'].set('quick_status', |
---|
80 | quick_status and 'yes' or 'no') |
---|
81 | changed = True |
---|
82 | |
---|
83 | logs_dir = req.args.get('logs_dir', None) |
---|
84 | if logs_dir != master.logs_dir: |
---|
85 | self.config['bitten'].set('logs_dir', str(logs_dir)) |
---|
86 | changed = True |
---|
87 | |
---|
88 | if changed: |
---|
89 | self.config.save() |
---|
90 | |
---|
91 | return master |
---|
92 | |
---|
93 | |
---|
94 | class BuildConfigurationsAdminPageProvider(Component): |
---|
95 | """Web administration panel for configuring the build master.""" |
---|
96 | |
---|
97 | implements(IAdminPanelProvider) |
---|
98 | |
---|
99 | # IAdminPanelProvider methods |
---|
100 | |
---|
101 | def get_admin_panels(self, req): |
---|
102 | if req.perm.has_permission('BUILD_MODIFY'): |
---|
103 | yield ('bitten', 'Builds', 'configs', 'Configurations') |
---|
104 | |
---|
105 | def render_admin_panel(self, req, cat, page, path_info): |
---|
106 | data = {} |
---|
107 | |
---|
108 | # Analyze url |
---|
109 | try: |
---|
110 | config_name, platform_id = path_info.split('/', 1) |
---|
111 | except: |
---|
112 | config_name = path_info |
---|
113 | platform_id = None |
---|
114 | |
---|
115 | if config_name: # Existing build config |
---|
116 | warnings = [] |
---|
117 | if platform_id or ( |
---|
118 | # Editing or creating one of the config's target platforms |
---|
119 | req.method == 'POST' and 'new' in req.args): |
---|
120 | |
---|
121 | if platform_id: # Editing target platform |
---|
122 | platform_id = int(platform_id) |
---|
123 | platform = TargetPlatform.fetch(self.env, platform_id) |
---|
124 | |
---|
125 | if req.method == 'POST': |
---|
126 | if 'cancel' in req.args or \ |
---|
127 | self._update_platform(req, platform): |
---|
128 | req.redirect(req.abs_href.admin(cat, page, |
---|
129 | config_name)) |
---|
130 | else: # creating target platform |
---|
131 | platform = self._create_platform(req, config_name) |
---|
132 | req.redirect(req.abs_href.admin(cat, page, |
---|
133 | config_name, platform.id)) |
---|
134 | |
---|
135 | # Set up template variables |
---|
136 | data['platform'] = { |
---|
137 | 'id': platform.id, 'name': platform.name, |
---|
138 | 'exists': platform.exists, |
---|
139 | 'rules': [ |
---|
140 | {'property': propname, 'pattern': pattern} |
---|
141 | for propname, pattern in platform.rules |
---|
142 | ] or [('', '')] |
---|
143 | } |
---|
144 | |
---|
145 | else: # Editing existing build config itself |
---|
146 | config = BuildConfig.fetch(self.env, config_name) |
---|
147 | platforms = list(TargetPlatform.select(self.env, |
---|
148 | config=config.name)) |
---|
149 | |
---|
150 | if req.method == 'POST': |
---|
151 | if 'remove' in req.args: # Remove selected platforms |
---|
152 | self._remove_platforms(req) |
---|
153 | add_notice(req, "Target Platform(s) Removed.") |
---|
154 | req.redirect(req.abs_href.admin(cat, page, config.name)) |
---|
155 | |
---|
156 | elif 'save' in req.args: # Save this build config |
---|
157 | warnings = self._update_config(req, config) |
---|
158 | |
---|
159 | if not warnings: |
---|
160 | add_notice(req, "Configuration Saved.") |
---|
161 | req.redirect(req.abs_href.admin(cat, page, config.name)) |
---|
162 | |
---|
163 | for warning in warnings: |
---|
164 | add_warning(req, warning) |
---|
165 | |
---|
166 | # FIXME: Deprecation notice for old namespace. |
---|
167 | # Remove notice code when migration to new namespace is complete |
---|
168 | if 'http://bitten.cmlenz.net/tools/' in config.recipe: |
---|
169 | add_notice(req, "Recipe uses a deprecated namespace. " |
---|
170 | "Replace 'http://bitten.cmlenz.net/tools/' with " |
---|
171 | "'http://bitten.edgewall.org/tools/'.") |
---|
172 | |
---|
173 | # Add a notice if configuration is not active |
---|
174 | if not warnings and not config.active and config.recipe: |
---|
175 | add_notice(req, "Configuration is not active. Activate " |
---|
176 | "from main 'Configurations' listing to enable it.") |
---|
177 | |
---|
178 | # Prepare template variables |
---|
179 | data['config'] = { |
---|
180 | 'name': config.name, 'label': config.label or config.name, |
---|
181 | 'active': config.active, 'path': config.path, |
---|
182 | 'min_rev': config.min_rev, 'max_rev': config.max_rev, |
---|
183 | 'description': config.description, |
---|
184 | 'recipe': config.recipe, |
---|
185 | 'platforms': [{ |
---|
186 | 'name': platform.name, |
---|
187 | 'id': platform.id, |
---|
188 | 'href': req.href.admin('bitten', 'configs', config.name, |
---|
189 | platform.id), |
---|
190 | 'rules': [{'property': propname, 'pattern': pattern} |
---|
191 | for propname, pattern in platform.rules] |
---|
192 | } for platform in platforms] |
---|
193 | } |
---|
194 | |
---|
195 | else: # At the top level build config list |
---|
196 | if req.method == 'POST': |
---|
197 | if 'add' in req.args: # Add build config |
---|
198 | config = self._create_config(req) |
---|
199 | req.redirect(req.abs_href.admin(cat, page, config.name)) |
---|
200 | |
---|
201 | elif 'remove' in req.args: # Remove selected build configs |
---|
202 | self._remove_configs(req) |
---|
203 | |
---|
204 | elif 'apply' in req.args: # Update active state of configs |
---|
205 | self._activate_configs(req) |
---|
206 | req.redirect(req.abs_href.admin(cat, page)) |
---|
207 | |
---|
208 | # Prepare template variables |
---|
209 | configs = [] |
---|
210 | for config in BuildConfig.select(self.env, include_inactive=True): |
---|
211 | configs.append({ |
---|
212 | 'name': config.name, 'label': config.label or config.name, |
---|
213 | 'active': config.active, 'path': config.path, |
---|
214 | 'min_rev': config.min_rev, 'max_rev': config.max_rev, |
---|
215 | 'href': req.href.admin('bitten', 'configs', config.name), |
---|
216 | 'recipe': config.recipe and True or False |
---|
217 | }) |
---|
218 | data['configs'] = sorted(configs, key=lambda x:x['label'].lower()) |
---|
219 | |
---|
220 | add_stylesheet(req, 'bitten/admin.css') |
---|
221 | add_script(req, 'common/js/suggest.js') |
---|
222 | return 'bitten_admin_configs.html', data |
---|
223 | |
---|
224 | # Internal methods |
---|
225 | |
---|
226 | def _activate_configs(self, req): |
---|
227 | req.perm.assert_permission('BUILD_MODIFY') |
---|
228 | |
---|
229 | active = req.args.get('active') or [] |
---|
230 | active = isinstance(active, list) and active or [active] |
---|
231 | |
---|
232 | db = self.env.get_db_cnx() |
---|
233 | for config in list(BuildConfig.select(self.env, db=db, |
---|
234 | include_inactive=True)): |
---|
235 | config.active = config.name in active |
---|
236 | config.update(db=db) |
---|
237 | db.commit() |
---|
238 | |
---|
239 | def _create_config(self, req): |
---|
240 | req.perm.assert_permission('BUILD_CREATE') |
---|
241 | |
---|
242 | config = BuildConfig(self.env) |
---|
243 | warnings = self._update_config(req, config) |
---|
244 | if warnings: |
---|
245 | if len(warnings) == 1: |
---|
246 | raise TracError(warnings[0], 'Add Configuration') |
---|
247 | else: |
---|
248 | raise TracError('Errors: %s' % ' '.join(warnings), |
---|
249 | 'Add Configuration') |
---|
250 | return config |
---|
251 | |
---|
252 | def _remove_configs(self, req): |
---|
253 | req.perm.assert_permission('BUILD_DELETE') |
---|
254 | |
---|
255 | sel = req.args.get('sel') |
---|
256 | if not sel: |
---|
257 | raise TracError('No configuration selected') |
---|
258 | sel = isinstance(sel, list) and sel or [sel] |
---|
259 | |
---|
260 | db = self.env.get_db_cnx() |
---|
261 | for name in sel: |
---|
262 | config = BuildConfig.fetch(self.env, name, db=db) |
---|
263 | if not config: |
---|
264 | raise TracError('Configuration %r not found' % name) |
---|
265 | config.delete(db=db) |
---|
266 | db.commit() |
---|
267 | |
---|
268 | def _update_config(self, req, config): |
---|
269 | warnings = [] |
---|
270 | req.perm.assert_permission('BUILD_MODIFY') |
---|
271 | |
---|
272 | name = req.args.get('name') |
---|
273 | if not name: |
---|
274 | warnings.append('Missing required field "name".') |
---|
275 | if name and not re.match(r'^[\w.-]+$', name): |
---|
276 | warnings.append('The field "name" may only contain letters, ' |
---|
277 | 'digits, periods, or dashes.') |
---|
278 | |
---|
279 | path = req.args.get('path', '') |
---|
280 | repos_name, repos, repos_path = get_repos(self.env, path, req.authname) |
---|
281 | max_rev = req.args.get('max_rev') or None |
---|
282 | min_rev = req.args.get('min_rev') or None |
---|
283 | |
---|
284 | try: |
---|
285 | node = repos.get_node(repos_path, max_rev) |
---|
286 | assert node.isdir, '%s is not a directory' % node.path |
---|
287 | except (AssertionError, TracError), e: |
---|
288 | warnings.append('Invalid Repository Path "%s".' % path) |
---|
289 | |
---|
290 | if min_rev: |
---|
291 | try: |
---|
292 | repos.get_node(repos_path, min_rev) |
---|
293 | except TracError, e: |
---|
294 | warnings.append('Invalid Oldest Revision: %s.' % unicode(e)) |
---|
295 | |
---|
296 | recipe_xml = req.args.get('recipe', '') |
---|
297 | if recipe_xml: |
---|
298 | try: |
---|
299 | Recipe(xmlio.parse(recipe_xml)).validate() |
---|
300 | except xmlio.ParseError, e: |
---|
301 | warnings.append('Failure parsing recipe: %s.' % unicode(e)) |
---|
302 | except InvalidRecipeError, e: |
---|
303 | warnings.append('Invalid Recipe: %s.' % unicode(e)) |
---|
304 | |
---|
305 | config.name = name |
---|
306 | config.path = __multirepos__ and path or repos.normalize_path(path) |
---|
307 | config.recipe = recipe_xml |
---|
308 | config.min_rev = min_rev |
---|
309 | config.max_rev = max_rev |
---|
310 | config.label = req.args.get('label', config.name) |
---|
311 | config.description = req.args.get('description', '') |
---|
312 | |
---|
313 | if warnings: # abort |
---|
314 | return warnings |
---|
315 | |
---|
316 | if config.exists: |
---|
317 | config.update() |
---|
318 | else: |
---|
319 | config.insert() |
---|
320 | return [] |
---|
321 | |
---|
322 | def _create_platform(self, req, config_name): |
---|
323 | req.perm.assert_permission('BUILD_MODIFY') |
---|
324 | |
---|
325 | name = req.args.get('platform_name') |
---|
326 | if not name: |
---|
327 | raise TracError('Missing required field "name"', 'Missing field') |
---|
328 | |
---|
329 | platform = TargetPlatform(self.env, config=config_name, name=name) |
---|
330 | platform.insert() |
---|
331 | return platform |
---|
332 | |
---|
333 | def _remove_platforms(self, req): |
---|
334 | req.perm.assert_permission('BUILD_MODIFY') |
---|
335 | |
---|
336 | sel = req.args.get('sel') |
---|
337 | if not sel: |
---|
338 | raise TracError('No platform selected') |
---|
339 | sel = isinstance(sel, list) and sel or [sel] |
---|
340 | |
---|
341 | db = self.env.get_db_cnx() |
---|
342 | for platform_id in sel: |
---|
343 | platform = TargetPlatform.fetch(self.env, platform_id, db=db) |
---|
344 | if not platform: |
---|
345 | raise TracError('Target platform %r not found' % platform_id) |
---|
346 | platform.delete(db=db) |
---|
347 | db.commit() |
---|
348 | |
---|
349 | def _update_platform(self, req, platform): |
---|
350 | platform.name = req.args.get('name') |
---|
351 | |
---|
352 | properties = [int(key[9:]) for key in req.args.keys() |
---|
353 | if key.startswith('property_')] |
---|
354 | properties.sort() |
---|
355 | patterns = [int(key[8:]) for key in req.args.keys() |
---|
356 | if key.startswith('pattern_')] |
---|
357 | patterns.sort() |
---|
358 | platform.rules = [(req.args.get('property_%d' % property).strip(), |
---|
359 | req.args.get('pattern_%d' % pattern).strip()) |
---|
360 | for property, pattern in zip(properties, patterns) |
---|
361 | if req.args.get('property_%d' % property)] |
---|
362 | |
---|
363 | if platform.exists: |
---|
364 | platform.update() |
---|
365 | else: |
---|
366 | platform.insert() |
---|
367 | |
---|
368 | add_rules = [int(key[9:]) for key in req.args.keys() |
---|
369 | if key.startswith('add_rule_')] |
---|
370 | if add_rules: |
---|
371 | platform.rules.insert(add_rules[0] + 1, ('', '')) |
---|
372 | return False |
---|
373 | rm_rules = [int(key[8:]) for key in req.args.keys() |
---|
374 | if key.startswith('rm_rule_')] |
---|
375 | if rm_rules: |
---|
376 | if rm_rules[0] < len(platform.rules): |
---|
377 | del platform.rules[rm_rules[0]] |
---|
378 | return False |
---|
379 | |
---|
380 | return True |
---|