~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugin.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-08-19 12:06:01 UTC
  • mto: (1092.1.41) (1185.3.4) (974.1.47)
  • mto: This revision was merged to the branch mainline in revision 1110.
  • Revision ID: aaron.bentley@utoronto.ca-20050819120601-58525b75283a9c1c
Initial greedy fetch work

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# commands, import bzrlib.commands and add your new command to the
23
23
# plugin_cmds variable.
24
24
 
25
 
import sys, os, imp
26
 
try:
27
 
    set
28
 
except NameError:
29
 
    from sets import Set as set         # python2.3
30
 
from bzrlib.trace import log_error
31
 
 
32
 
 
33
 
DEFAULT_PLUGIN_PATH = '~/.bzr.conf/plugins'
 
25
 
 
26
import os
 
27
from bzrlib.osutils import config_dir
 
28
DEFAULT_PLUGIN_PATH = os.path.join(config_dir(), 'plugins')
 
29
 
 
30
all_plugins = []
 
31
_loaded = False
34
32
 
35
33
 
36
34
def load_plugins():
37
 
    """Find all python files which are plugins, and load them
38
 
 
39
 
    The environment variable BZR_PLUGIN_PATH is considered a delimited set of
40
 
    paths to look through. Each entry is searched for *.py files (and whatever
41
 
    other extensions are used in the platform, such as *.pyd).
42
 
    """
43
 
    bzrpath = os.environ.get('BZR_PLUGIN_PATH')
44
 
    if not bzrpath:
45
 
        bzrpath = os.path.expanduser(DEFAULT_PLUGIN_PATH)
 
35
    """
 
36
    Find all python plugins and load them.
 
37
 
 
38
    Loading a plugin means importing it into the python interpreter.
 
39
    The plugin is expected to make calls to register commands when
 
40
    it's loaded (or perhaps access other hooks in future.)
 
41
 
 
42
    A list of plugs is stored in bzrlib.plugin.all_plugins for future
 
43
    reference.
 
44
 
 
45
    The environment variable BZR_PLUGIN_PATH is considered a delimited
 
46
    set of paths to look through. Each entry is searched for *.py
 
47
    files (and whatever other extensions are used in the platform,
 
48
    such as *.pyd).
 
49
    """
 
50
 
 
51
    global all_plugins, _loaded
 
52
    if _loaded:
 
53
        # People can make sure plugins are loaded, they just won't be twice
 
54
        return
 
55
        #raise BzrError("plugins already initialized")
 
56
    _loaded = True
 
57
 
 
58
    import sys, os, imp
 
59
    
 
60
    from bzrlib.trace import log_error, mutter, log_exception
 
61
    from bzrlib.errors import BzrError
 
62
    from bzrlib import plugins
 
63
 
 
64
    dirs = os.environ.get('BZR_PLUGIN_PATH', DEFAULT_PLUGIN_PATH).split(":")
 
65
    dirs.insert(0, os.path.dirname(plugins.__file__))
46
66
 
47
67
    # The problem with imp.get_suffixes() is that it doesn't include
48
68
    # .pyo which is technically valid
52
72
    suffixes = imp.get_suffixes()
53
73
    suffixes.append(('.pyo', 'rb', imp.PY_COMPILED))
54
74
    package_entries = ['__init__.py', '__init__.pyc', '__init__.pyo']
55
 
    for d in bzrpath.split(os.pathsep):
56
 
        # going trough them one by one allows different plugins with the same
 
75
    for d in dirs:
 
76
        # going through them one by one allows different plugins with the same
57
77
        # filename in different directories in the path
 
78
        mutter('looking for plugins in %s' % d)
58
79
        if not d:
59
80
            continue
60
81
        plugin_names = set()
79
100
                        break
80
101
                else:
81
102
                    continue
 
103
            mutter('add plugin name %s' % f)
82
104
            plugin_names.add(f)
83
105
 
84
106
        plugin_names = list(plugin_names)
86
108
        for name in plugin_names:
87
109
            try:
88
110
                plugin_info = imp.find_module(name, [d])
 
111
                mutter('load plugin %r' % (plugin_info,))
89
112
                try:
90
113
                    plugin = imp.load_module('bzrlib.plugin.' + name,
91
114
                                             *plugin_info)
 
115
                    all_plugins.append(plugin)
92
116
                finally:
93
117
                    if plugin_info[0] is not None:
94
118
                        plugin_info[0].close()
95
119
            except Exception, e:
96
 
                log_error('Unable to load plugin: %r from %r\n%s' % (name, d, e))
 
120
                log_error('Unable to load plugin %r from %r' % (name, d))
 
121
                log_error(str(e))
 
122
                log_exception()
97
123