comparison examples/trac/trac/loader.py @ 39:93b4dcbafd7b trunk

Copy Trac to main branch.
author cmlenz
date Mon, 03 Jul 2006 18:53:27 +0000
parents
children
comparison
equal deleted inserted replaced
38:ee669cb9cccc 39:93b4dcbafd7b
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2005-2006 Edgewall Software
4 # Copyright (C) 2005-2006 Christopher Lenz <cmlenz@gmx.de>
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://trac.edgewall.com/license.html.
10 #
11 # This software consists of voluntary contributions made by many
12 # individuals. For the exact contribution history, see the revision
13 # history and logs, available at http://projects.edgewall.com/trac/.
14 #
15 # Author: Christopher Lenz <cmlenz@gmx.de>
16
17 from glob import glob
18 import imp
19 import os
20 import sys
21 from trac.config import default_dir
22 try:
23 set
24 except NameError:
25 from sets import Set as set
26
27 try:
28 import pkg_resources
29 except ImportError:
30 pkg_resources = None
31
32 __all__ = ['load_components']
33
34 def load_components(env):
35 loaded_components = []
36 plugins_dirs = [os.path.normcase(os.path.realpath(os.path.join(env.path,
37 'plugins'))),
38 default_dir('plugins')]
39
40 # First look for Python source files in the plugins directories, which
41 # simply get imported, thereby registering them with the component manager
42 # if they define any components.
43 for plugins_dir in plugins_dirs:
44 auto_enable = plugins_dir != default_dir('plugins')
45 plugin_files = glob(os.path.join(plugins_dir, '*.py'))
46 for plugin_file in plugin_files:
47 try:
48 plugin_name = os.path.basename(plugin_file[:-3])
49 if plugin_name not in loaded_components:
50 env.log.debug('Loading file plugin %s from %s' % (plugin_name,
51 plugin_file))
52 module = imp.load_source(plugin_name, plugin_file)
53 loaded_components.append(plugin_name)
54 if auto_enable and plugin_name + '.*' \
55 not in env.config['components']:
56 env.config['components'].set(plugin_name + '.*', 'enabled')
57 except Exception, e:
58 env.log.error('Failed to load plugin from %s', plugin_file,
59 exc_info=True)
60
61 # If setuptools is installed try to load any eggs from the plugins
62 # directory, and also plugins available on sys.path
63 if pkg_resources is not None:
64 ws = pkg_resources.working_set
65 for plugins_dir in plugins_dirs:
66 ws.add_entry(plugins_dir)
67 pkg_env = pkg_resources.Environment(plugins_dirs + sys.path)
68
69 memo = set()
70 def flatten(dists):
71 for dist in dists:
72 if dist in memo:
73 continue
74 memo.add(dist)
75 try:
76 predecessors = ws.resolve([dist.as_requirement()])
77 for predecessor in flatten(predecessors):
78 yield predecessor
79 yield dist
80 except pkg_resources.DistributionNotFound, e:
81 env.log.error('Skipping "%s" ("%s" not found)', dist, e)
82 except pkg_resources.VersionConflict, e:
83 env.log.error('Skipping "%s" (version conflict: "%s")',
84 dist, e)
85
86 for egg in flatten([pkg_env[name][0] for name in pkg_env]):
87 modules = []
88
89 for name in egg.get_entry_map('trac.plugins'):
90 # Load plugins declared via the `trac.plugins` entry point.
91 # This is the only supported option going forward, the
92 # others will be dropped at some point in the future.
93 env.log.debug('Loading egg plugin %s from %s', name,
94 egg.location)
95 egg.activate()
96 try:
97 entry_point = egg.get_entry_info('trac.plugins', name)
98 if entry_point.module_name not in loaded_components:
99 try:
100 entry_point.load()
101 except pkg_resources.DistributionNotFound, e:
102 env.log.warning('Cannot load plugin %s because it '
103 'requires "%s"', name, e)
104 modules.append(entry_point.module_name)
105 loaded_components.append(entry_point.module_name)
106 except ImportError, e:
107 env.log.error('Failed to load plugin %s from %s', name,
108 egg.location, exc_info=True)
109
110 else:
111 # Support for pre-entry-point plugins
112 if egg.has_metadata('trac_plugin.txt'):
113 env.log.debug('Loading plugin %s from %s', name,
114 egg.location)
115 egg.activate()
116 for modname in egg.get_metadata_lines('trac_plugin.txt'):
117 module = None
118 if modname not in loaded_components:
119 try:
120 module = __import__(modname)
121 loaded_components.append(modname)
122 modules.append(modname)
123 except ImportError, e:
124 env.log.error('Component module %s not found',
125 modname, exc_info=True)
126
127 if modules:
128 # Automatically enable any components provided by plugins
129 # loaded from the environment plugins directory.
130 if os.path.dirname(egg.location) == plugins_dirs[0]:
131 for module in modules:
132 if module + '.*' not in env.config['components']:
133 env.config['components'].set(module + '.*',
134 'enabled')
135
136 # Load default components
137 from trac.db_default import default_components
138 for module in default_components:
139 if not module in loaded_components:
140 __import__(module)
Copyright (C) 2012-2017 Edgewall Software