| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | from componentdependencies.interface import IRequireComponents |
|---|
| 4 | from trac.core import * |
|---|
| 5 | from trac.env import IEnvironmentSetupParticipant |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | class ComponentDependencyPlugin(Component): |
|---|
| 9 | |
|---|
| 10 | implements(IEnvironmentSetupParticipant) |
|---|
| 11 | dependencies = ExtensionPoint(IRequireComponents) |
|---|
| 12 | |
|---|
| 13 | ### methods for IEnvironmentSetupParticipant |
|---|
| 14 | |
|---|
| 15 | """Extension point interface for components that need to participate in the |
|---|
| 16 | creation and upgrading of Trac environments, for example to create |
|---|
| 17 | additional database tables.""" |
|---|
| 18 | |
|---|
| 19 | def environment_created(self): |
|---|
| 20 | """Called when a new Trac environment is created.""" |
|---|
| 21 | if self.environment_needs_upgrade(None): |
|---|
| 22 | self.upgrade_environment(None) |
|---|
| 23 | |
|---|
| 24 | def environment_needs_upgrade(self, db=None): |
|---|
| 25 | """Called when Trac checks whether the environment needs to be upgraded. |
|---|
| 26 | |
|---|
| 27 | Should return `True` if this participant needs an upgrade to be |
|---|
| 28 | performed, `False` otherwise. |
|---|
| 29 | """ |
|---|
| 30 | for dependency in self.dependencies: |
|---|
| 31 | for requirement in dependency.requires(): |
|---|
| 32 | if not self.env.is_component_enabled(requirement): |
|---|
| 33 | return True |
|---|
| 34 | return False |
|---|
| 35 | |
|---|
| 36 | def upgrade_environment(self, db=None): |
|---|
| 37 | """Actually perform an environment upgrade. |
|---|
| 38 | |
|---|
| 39 | Implementations of this method should not commit any database |
|---|
| 40 | transactions. This is done implicitly after all participants have |
|---|
| 41 | performed the upgrades they need without an error being raised. |
|---|
| 42 | """ |
|---|
| 43 | needed = set() |
|---|
| 44 | |
|---|
| 45 | for dependency in self.dependencies: |
|---|
| 46 | for requirement in dependency.requires(): |
|---|
| 47 | if not self.env.is_component_enabled(requirement): |
|---|
| 48 | needed.add(requirement) |
|---|
| 49 | needed = set([ '%s.%s' % (i.__module__, i.__name__.lower()) for i in needed ]) |
|---|
| 50 | for component in needed: |
|---|
| 51 | self.config.set('components', component, 'enabled') |
|---|
| 52 | self.config.save() |
|---|