diff options
author | Tristan Maat <tristan.maat@codethink.co.uk> | 2017-09-12 14:10:07 +0100 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-09-14 14:36:32 -0400 |
commit | 032d0f3bbc337663576fdeee70f6a49e66f836c8 (patch) | |
tree | a0b9180d2f0c632eb845936e7dc73ec26ebc7406 | |
parent | 6a326f465888fbf411682dd6c7c58cc7bc597e6b (diff) | |
download | buildstream-032d0f3bbc337663576fdeee70f6a49e66f836c8.tar.gz |
Add support for YAML default config loading
-rw-r--r-- | buildstream/_elementfactory.py | 4 | ||||
-rw-r--r-- | buildstream/_plugincontext.py | 20 | ||||
-rw-r--r-- | buildstream/_sourcefactory.py | 2 | ||||
-rw-r--r-- | buildstream/element.py | 14 |
4 files changed, 24 insertions, 16 deletions
diff --git a/buildstream/_elementfactory.py b/buildstream/_elementfactory.py index cd3c2755d..deaa13c5e 100644 --- a/buildstream/_elementfactory.py +++ b/buildstream/_elementfactory.py @@ -58,5 +58,5 @@ class ElementFactory(PluginContext): # LoadError (if the element itself took issue with the config) # def create(self, kind, context, project, artifacts, meta): - element_type = self.lookup(kind) - return element_type(context, project, artifacts, meta) + element_type, default_config = self.lookup(kind) + return element_type(context, project, artifacts, meta, default_config) diff --git a/buildstream/_plugincontext.py b/buildstream/_plugincontext.py index 8c208af48..96ee2e2c5 100644 --- a/buildstream/_plugincontext.py +++ b/buildstream/_plugincontext.py @@ -75,6 +75,7 @@ class PluginContext(): if kind not in self.types: source = None + defaults = None dist, package = self.split_name(kind) if dist: @@ -88,6 +89,13 @@ class PluginContext(): plugin.module_name.replace('.', os.sep) + '.py' ) + # Also load the defaults - required since setuptools + # may need to extract the file. + defaults = plugin.dist.get_resource_filename( + pkg_resources._manager, + plugin.module_name.replace('.', os.sep) + '.yaml' + ) + # Set the plugin-base source to the setuptools directory source = self.plugin_base.make_plugin_source(searchpath=[os.path.dirname(location)]) @@ -97,15 +105,21 @@ class PluginContext(): if not source: raise PluginError("No {} type registered for kind '{}'" .format(self.base_type.__name__, kind)) - self.types[kind] = self.load_plugin(source, package) + self.types[kind] = self.load_plugin(source, package, defaults) return self.types[kind] - def load_plugin(self, source, kind): + def load_plugin(self, source, kind, defaults): try: plugin = source.load_plugin(kind) + if not defaults: + plugin_file = inspect.getfile(plugin) + plugin_dir = os.path.dirname(plugin_file) + plugin_conf_name = "{}.yaml".format(kind) + defaults = os.path.join(plugin_dir, plugin_conf_name) + except ImportError as e: raise PluginError("Failed to load {} plugin '{}': {}" .format(self.base_type.__name__, kind, e)) from e @@ -121,7 +135,7 @@ class PluginContext(): self.assert_plugin(kind, plugin_type) self.assert_version(kind, plugin_type) - return plugin_type + return (plugin_type, defaults) def split_name(self, name): if name.count(':') > 1: diff --git a/buildstream/_sourcefactory.py b/buildstream/_sourcefactory.py index 78fb7ec5f..3461ed2ff 100644 --- a/buildstream/_sourcefactory.py +++ b/buildstream/_sourcefactory.py @@ -59,5 +59,5 @@ class SourceFactory(PluginContext): # LoadError (if the source itself took issue with the config) # def create(self, kind, context, project, meta): - source_type = self.lookup(kind) + source_type, _ = self.lookup(kind) return source_type(context, project, meta) diff --git a/buildstream/element.py b/buildstream/element.py index 9476cf3d6..7029acd7e 100644 --- a/buildstream/element.py +++ b/buildstream/element.py @@ -108,7 +108,7 @@ class Element(Plugin): any of the dependencies have changed. """ - def __init__(self, context, project, artifacts, meta): + def __init__(self, context, project, artifacts, meta, plugin_conf): super().__init__(meta.name, context, project, meta.provenance, "element") @@ -141,7 +141,7 @@ class Element(Plugin): self.__splits = None # Ensure we have loaded this class's defaults - self.__init_defaults() + self.__init_defaults(plugin_conf) # Collect the composited variables and resolve them variables = self.__extract_variables(meta) @@ -1442,22 +1442,16 @@ class Element(Plugin): element_public['bst'] = element_bst defaults['public'] = element_public - def __init_defaults(self): + def __init_defaults(self, plugin_conf): # Defaults are loaded once per class and then reused # if not self.__defaults_set: - # Get the yaml file in the same directory as the plugin - plugin_file = inspect.getfile(type(self)) - plugin_dir = os.path.dirname(plugin_file) - plugin_conf_name = "{}.yaml".format(self.get_kind()) - plugin_conf = os.path.join(plugin_dir, plugin_conf_name) - # Load the plugin's accompanying .yaml file if one was provided defaults = {} try: - defaults = _yaml.load(plugin_conf, plugin_conf_name) + defaults = _yaml.load(plugin_conf, os.path.basename(plugin_conf)) except LoadError as e: if e.reason != LoadErrorReason.MISSING_FILE: raise e |