source: componentdependencyplugin/0.11/componentdependencies/componentdependency.py

Last change on this file was 17595, checked in by Ryan J Ollos, 4 years ago

TracComponentDependency 0.2: Prepare to publish on PyPI

Refs #13691.

File size: 1.9 KB
Line 
1# -*- coding: utf-8 -*-
2
3from componentdependencies.interface import IRequireComponents
4from trac.core import *
5from trac.env import IEnvironmentSetupParticipant
6
7
8class 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()
Note: See TracBrowser for help on using the repository browser.