| 1 | # -*- coding: utf-8 -*- |
---|
| 2 | # |
---|
| 3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
| 4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
| 5 | # All rights reserved. |
---|
| 6 | # |
---|
| 7 | # This software is licensed as described in the file COPYING, which |
---|
| 8 | # you should have received as part of this distribution. The terms |
---|
| 9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
| 10 | |
---|
| 11 | import inspect |
---|
| 12 | import os |
---|
| 13 | import textwrap |
---|
| 14 | |
---|
| 15 | from trac.attachment import ILegacyAttachmentPolicyDelegate |
---|
| 16 | from trac.core import * |
---|
| 17 | from trac.db import DatabaseManager |
---|
| 18 | from trac.env import IEnvironmentSetupParticipant |
---|
| 19 | from trac.perm import IPermissionRequestor |
---|
| 20 | from trac.resource import IResourceManager |
---|
| 21 | from trac.util.html import escape |
---|
| 22 | from trac.wiki import IWikiSyntaxProvider |
---|
| 23 | from bitten.api import IBuildListener |
---|
| 24 | from bitten.model import schema, schema_version, Build, BuildConfig |
---|
| 25 | |
---|
| 26 | __all__ = ['BuildSystem'] |
---|
| 27 | __docformat__ = 'restructuredtext en' |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | class BuildSetup(Component): |
---|
| 31 | |
---|
| 32 | implements(IEnvironmentSetupParticipant) |
---|
| 33 | |
---|
| 34 | # IEnvironmentSetupParticipant methods |
---|
| 35 | |
---|
| 36 | def environment_created(self): |
---|
| 37 | # Create the required tables |
---|
| 38 | db = self.env.get_db_cnx() |
---|
| 39 | connector, _ = DatabaseManager(self.env)._get_connector() |
---|
| 40 | cursor = db.cursor() |
---|
| 41 | for table in schema: |
---|
| 42 | for stmt in connector.to_sql(table): |
---|
| 43 | cursor.execute(stmt) |
---|
| 44 | |
---|
| 45 | # Insert a global version flag |
---|
| 46 | cursor.execute("INSERT INTO system (name,value) " |
---|
| 47 | "VALUES ('bitten_version',%s)", (schema_version,)) |
---|
| 48 | |
---|
| 49 | db.commit() |
---|
| 50 | |
---|
| 51 | def environment_needs_upgrade(self, db): |
---|
| 52 | cursor = db.cursor() |
---|
| 53 | cursor.execute("SELECT value FROM system WHERE name='bitten_version'") |
---|
| 54 | row = cursor.fetchone() |
---|
| 55 | if not row or int(row[0]) < schema_version: |
---|
| 56 | return True |
---|
| 57 | |
---|
| 58 | def upgrade_environment(self, db): |
---|
| 59 | cursor = db.cursor() |
---|
| 60 | cursor.execute("SELECT value FROM system WHERE name='bitten_version'") |
---|
| 61 | row = cursor.fetchone() |
---|
| 62 | if not row: |
---|
| 63 | self.environment_created() |
---|
| 64 | else: |
---|
| 65 | current_version = int(row[0]) |
---|
| 66 | from bitten import upgrades |
---|
| 67 | for version in range(current_version + 1, schema_version + 1): |
---|
| 68 | for function in upgrades.map.get(version): |
---|
| 69 | print textwrap.fill(inspect.getdoc(function)) |
---|
| 70 | function(self.env, db) |
---|
| 71 | cursor.execute("UPDATE system SET value=%s WHERE " |
---|
| 72 | "name='bitten_version'", (version,)) |
---|
| 73 | db.commit() |
---|
| 74 | print "Bitten upgrade to version %d done." % version |
---|
| 75 | self.log.info('Upgraded Bitten tables to version %d', |
---|
| 76 | version) |
---|
| 77 | |
---|
| 78 | class BuildSystem(Component): |
---|
| 79 | |
---|
| 80 | implements(IPermissionRequestor, |
---|
| 81 | IWikiSyntaxProvider, IResourceManager, |
---|
| 82 | ILegacyAttachmentPolicyDelegate) |
---|
| 83 | |
---|
| 84 | listeners = ExtensionPoint(IBuildListener) |
---|
| 85 | |
---|
| 86 | # IPermissionRequestor methods |
---|
| 87 | |
---|
| 88 | def get_permission_actions(self): |
---|
| 89 | actions = ['BUILD_VIEW', 'BUILD_CREATE', 'BUILD_MODIFY', 'BUILD_DELETE', |
---|
| 90 | 'BUILD_EXEC'] |
---|
| 91 | return actions + [('BUILD_ADMIN', actions)] |
---|
| 92 | |
---|
| 93 | # IWikiSyntaxProvider methods |
---|
| 94 | |
---|
| 95 | def get_wiki_syntax(self): |
---|
| 96 | return [] |
---|
| 97 | |
---|
| 98 | def get_link_resolvers(self): |
---|
| 99 | def _format_link(formatter, ns, name, label): |
---|
| 100 | segments = name.split('#') |
---|
| 101 | name = segments[0] |
---|
| 102 | step = len(segments) == 2 and segments[1] or '' |
---|
| 103 | try: |
---|
| 104 | name = int(name) |
---|
| 105 | except ValueError: |
---|
| 106 | return label |
---|
| 107 | build = Build.fetch(self.env, name) |
---|
| 108 | if build: |
---|
| 109 | config = BuildConfig.fetch(self.env, build.config) |
---|
| 110 | title = 'Build %d ([%s] of %s) by %s' % (build.id, build.rev, |
---|
| 111 | config.label, build.slave) |
---|
| 112 | if step: |
---|
| 113 | if not step.startswith('step_'): |
---|
| 114 | step = 'step_' + step |
---|
| 115 | step = '#' + escape(step) |
---|
| 116 | return '<a class="build" href="%s" title="%s">%s</a>' \ |
---|
| 117 | % (formatter.href.build(build.config, build.id) + step, |
---|
| 118 | title, label) |
---|
| 119 | return label |
---|
| 120 | yield 'build', _format_link |
---|
| 121 | |
---|
| 122 | # IResourceManager methods |
---|
| 123 | |
---|
| 124 | def get_resource_realms(self): |
---|
| 125 | yield 'build' |
---|
| 126 | |
---|
| 127 | def get_resource_url(self, resource, href, **kwargs): |
---|
| 128 | config_name, build_id = self._parse_resource(resource.id) |
---|
| 129 | return href.build(config_name, build_id) |
---|
| 130 | |
---|
| 131 | def get_resource_description(self, resource, format=None, context=None, |
---|
| 132 | **kwargs): |
---|
| 133 | config_name, build_id = self._parse_resource(resource.id) |
---|
| 134 | config = BuildConfig.fetch(self.env, config_name) |
---|
| 135 | config_label = config and config.label and config.label or config_name |
---|
| 136 | if context: |
---|
| 137 | if build_id: |
---|
| 138 | return tag.a('Build %d ("%s")' % (build_id, config_label), |
---|
| 139 | href=href.build(config_name, build_id)) |
---|
| 140 | elif config_name: |
---|
| 141 | return tag.a('Build Configuration "%s"' % config_label, |
---|
| 142 | href=href.build(config_name, build_id)) |
---|
| 143 | else: |
---|
| 144 | if build_id: |
---|
| 145 | return 'Build %d ("%s")' % (build_id, config_label) |
---|
| 146 | elif config_name: |
---|
| 147 | return 'Build Configuration "%s"' % config_label |
---|
| 148 | self.log.error("Unknown build/config resource.id: %s" % resource.id) |
---|
| 149 | return 'Unknown Build or Config' |
---|
| 150 | |
---|
| 151 | def resource_exists(self, resource): |
---|
| 152 | config_name, build_id = self._parse_resource(resource.id) |
---|
| 153 | if build_id: |
---|
| 154 | build = Build.fetch(self.env, build_id) |
---|
| 155 | return build and build.exists or False |
---|
| 156 | elif config_name: |
---|
| 157 | config = BuildConfig.fetch(self.env, config_name) |
---|
| 158 | return config and config.exists or False |
---|
| 159 | return False |
---|
| 160 | |
---|
| 161 | def _parse_resource(self, resource_id): |
---|
| 162 | """ Returns a (config_name, build_id) tuple. """ |
---|
| 163 | r = resource_id.split('/', 1) |
---|
| 164 | if len(r) == 1: |
---|
| 165 | return r[0], None |
---|
| 166 | elif len(r) == 2: |
---|
| 167 | try: |
---|
| 168 | return r[0], int(r[1]) |
---|
| 169 | except: |
---|
| 170 | return r[0], None |
---|
| 171 | return None, None |
---|
| 172 | |
---|
| 173 | # ILegacyAttachmentPolicyDelegate methods |
---|
| 174 | |
---|
| 175 | def check_attachment_permission(self, action, username, resource, perm): |
---|
| 176 | """ Respond to the various actions into the legacy attachment |
---|
| 177 | permissions used by the Attachment module. """ |
---|
| 178 | if resource.parent.realm == 'build': |
---|
| 179 | if action == 'ATTACHMENT_VIEW': |
---|
| 180 | return 'BUILD_VIEW' in perm(resource.parent) |
---|
| 181 | elif action == 'ATTACHMENT_CREATE': |
---|
| 182 | return 'BUILD_MODIFY' in perm(resource.parent) \ |
---|
| 183 | or 'BUILD_CREATE' in perm(resource.parent) |
---|
| 184 | elif action == 'ATTACHMENT_DELETE': |
---|
| 185 | return 'BUILD_DELETE' in perm(resource.parent) |
---|