~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugin.py

  • Committer: Martin Pool
  • Date: 2010-04-01 04:41:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5128.
  • Revision ID: mbp@sourcefrog.net-20100401044118-shyctqc02ob08ngz
ignore .testrepository

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
    return path.rstrip("\\/")
82
82
 
83
83
 
84
 
def _get_specific_plugin_paths(paths):
85
 
    """Returns the plugin paths from a string describing the associations.
86
 
 
87
 
    :param paths: A string describing the paths associated with the plugins.
88
 
 
89
 
    :returns: A list of (plugin name, path) tuples.
90
 
 
91
 
    For example, if paths is my_plugin@/test/my-test:her_plugin@/production/her,
92
 
    [('my_plugin', '/test/my-test'), ('her_plugin', '/production/her')] 
93
 
    will be returned.
94
 
 
95
 
    Note that ':' in the example above depends on the os.
96
 
    """
97
 
    if not paths:
98
 
        return []
99
 
    specs = []
100
 
    for spec in paths.split(os.pathsep):
101
 
        try:
102
 
            name, path = spec.split('@')
103
 
        except ValueError:
104
 
            raise errors.BzrCommandError(
105
 
                '"%s" is not a valid <plugin_name>@<plugin_path> description '
106
 
                % spec)
107
 
        specs.append((name, path))
108
 
    return specs
109
 
 
110
 
 
111
84
def set_plugins_path(path=None):
112
85
    """Set the path for plugins to be loaded from.
113
86
 
125
98
        for name in disabled_plugins.split(os.pathsep):
126
99
            PluginImporter.blacklist.add('bzrlib.plugins.' + name)
127
100
    # Set up a the specific paths for plugins
128
 
    for plugin_name, plugin_path in _get_specific_plugin_paths(os.environ.get(
129
 
            'BZR_PLUGINS_AT', None)):
 
101
    specific_plugins = os.environ.get('BZR_PLUGINS_AT', None)
 
102
    if specific_plugins is not None:
 
103
        for spec in specific_plugins.split(os.pathsep):
 
104
            plugin_name, plugin_path = spec.split('@')
130
105
            PluginImporter.specific_paths[
131
106
                'bzrlib.plugins.%s' % plugin_name] = plugin_path
132
107
    return path
328
303
 
329
304
 
330
305
def _load_plugin_module(name, dir):
331
 
    """Load plugin name from dir.
 
306
    """Load plugine name from dir.
332
307
 
333
308
    :param name: The plugin name in the bzrlib.plugins namespace.
334
309
    :param dir: The directory the plugin is loaded from for error messages.
585
560
    def load_module(self, fullname):
586
561
        """Load a plugin from a specific directory."""
587
562
        # We are called only for specific paths
588
 
        plugin_path = self.specific_paths[fullname]
589
 
        loading_path = None
590
 
        if os.path.isdir(plugin_path):
591
 
            for suffix, mode, kind in imp.get_suffixes():
592
 
                if kind not in (imp.PY_SOURCE, imp.PY_COMPILED):
593
 
                    # We don't recognize compiled modules (.so, .dll, etc)
594
 
                    continue
595
 
                init_path = osutils.pathjoin(plugin_path, '__init__' + suffix)
596
 
                if os.path.isfile(init_path):
597
 
                    # We've got a module here and load_module needs specific
598
 
                    # parameters.
599
 
                    loading_path = plugin_path
600
 
                    suffix = ''
601
 
                    mode = ''
602
 
                    kind = imp.PKG_DIRECTORY
603
 
                    break
604
 
        else:
605
 
            for suffix, mode, kind in imp.get_suffixes():
606
 
                if plugin_path.endswith(suffix):
607
 
                    loading_path = plugin_path
608
 
                    break
609
 
        if loading_path is None:
 
563
        plugin_dir = self.specific_paths[fullname]
 
564
        candidate = None
 
565
        maybe_package = False
 
566
        for p in os.listdir(plugin_dir):
 
567
            if os.path.isdir(osutils.pathjoin(plugin_dir, p)):
 
568
                # We're searching for files only and don't want submodules to
 
569
                # be recognized as plugins (they are submodules inside the
 
570
                # plugin).
 
571
                continue
 
572
            name, path, (
 
573
                suffix, mode, kind) = _find_plugin_module(plugin_dir, p)
 
574
            if name is not None:
 
575
                candidate = (name, path, suffix, mode, kind)
 
576
                if kind == imp.PY_SOURCE:
 
577
                    # We favour imp.PY_SOURCE (which will use the compiled
 
578
                    # version if available) over imp.PY_COMPILED (which is used
 
579
                    # only if the source is not available)
 
580
                    break
 
581
        if candidate is None:
610
582
            raise ImportError('%s cannot be loaded from %s'
611
 
                              % (fullname, plugin_path))
612
 
        if kind is imp.PKG_DIRECTORY:
613
 
            f = None
614
 
        else:
615
 
            f = open(loading_path, mode)
 
583
                              % (fullname, plugin_dir))
 
584
        f = open(path, mode)
616
585
        try:
617
 
            mod = imp.load_module(fullname, f, loading_path,
618
 
                                  (suffix, mode, kind))
 
586
            mod = imp.load_module(fullname, f, path, (suffix, mode, kind))
 
587
            # The plugin can contain modules, so be ready
 
588
            mod.__path__ = [plugin_dir]
619
589
            mod.__package__ = fullname
620
590
            return mod
621
591
        finally:
622
 
            if f is not None:
623
 
                f.close()
 
592
            f.close()
624
593
 
625
594
 
626
595
# Install a dedicated importer for plugins requiring special handling