summaryrefslogtreecommitdiff
path: root/buildstream/_plugincontext.py
blob: 9f3bd4d027badbe8a6fb19bbbe253a628bc70af6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
#
#  Copyright (C) 2016 Codethink Limited
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
#  Authors:
#        Tristan Van Berkom <tristan.vanberkom@codethink.co.uk>

import os
from .exceptions import PluginError


# A Context for loading plugin types
#
# Args:
#     plugin_base (PluginBase): The main PluginBase object to work with
#     base_type (type):         A base object type for this context
#     searchpath (list):        A list of paths to search for plugins
#
# Since multiple pipelines can be processed recursively
# within the same interpretor, it's important that we have
# one context associated to the processing of a given pipeline,
# this way sources and element types which are particular to
# a given BuildStream project are isolated to their respective
# Pipelines.
#
class PluginContext():

    def __init__(self, plugin_base, base_type, searchpath=None):

        if not searchpath:
            raise PluginError("Cannot create plugin context without any searchpath")

        self.base_type = base_type  # The base class plugins derive from
        self.source = None          # The PluginSource object
        self.types = {}             # Plugin type lookup table by kind

        self.load_plugins(plugin_base, searchpath)

    # lookup():
    #
    # Fetches a type loaded from a plugin in this plugin context
    #
    # Args:
    #     kind (str): The kind of Plugin to create
    #
    # Returns: the type associated with the given kind
    #
    # Raises: PluginError
    #
    def lookup(self, kind):
        if kind not in self.types:
            raise PluginError("No %s type registered for kind '%s'" %
                              (self.base_type.__name__, kind))

        return self.types[kind]

    def load_plugins(self, base, searchpath):

        # Raise an error if we have more than one plugin with the same name
        self.assert_searchpath(searchpath)

        self.source = base.make_plugin_source(searchpath=searchpath)
        for kind in self.source.list_plugins():
            self.load_plugin(kind)

    def load_plugin(self, kind):

        plugin = self.source.load_plugin(kind)
        try:
            plugin_type = plugin.setup()
        except AttributeError as e:
            raise PluginError("%s plugin '%s' did not provide a setup() function" %
                              (self.base_type.__name__, kind)) from e
        except TypeError as e:
            raise PluginError("setup symbol in %s plugin '%s' is not a function" %
                              (self.base_type.__name__, kind)) from e

        self.assert_plugin(kind, plugin_type)
        self.types[kind] = plugin_type

    def assert_plugin(self, kind, plugin_type):
        if kind in self.types:
            raise PluginError("Tried to register %s plugin for existing kind '%s' "
                              "(already registered %s)" %
                              (self.base_type.__name__, kind, self.types[kind].__name__))
        try:
            if not issubclass(plugin_type, self.base_type):
                raise PluginError("%s plugin '%s' returned type '%s', which is not a subclass of %s" %
                                  (self.base_type.__name__, kind,
                                   plugin_type.__name__,
                                   self.base_type.__name__))
        except TypeError as e:
            raise PluginError("%s plugin '%s' returned something that is not a type (expected subclass of %s)" %
                              (self.base_type.__name__, kind,
                               self.base_type.__name__)) from e

    # We want a PluginError when trying to create a context
    # where more than one plugin has the same name
    def assert_searchpath(self, searchpath):
        names = []
        fullnames = []
        for path in searchpath:
            for filename in os.listdir(path):
                basename = os.path.basename(filename)
                name, extension = os.path.splitext(basename)
                if extension == '.py' and name != '__init__':
                    fullname = os.path.join(path, filename)

                    if name in names:
                        idx = names.index(name)
                        raise PluginError("Failed to register %s plugin '%s' from: %s\n"
                                          "%s plugin '%s' is already registered by: %s" %
                                          (self.base_type.__name__, name, fullname,
                                           self.base_type.__name__, name, fullnames[idx]))

                    names.append(name)
                    fullnames.append(fullname)