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