This is an automated email from the ASF dual-hosted git repository. tvb pushed a commit to branch validation in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit d5499e2c505342ceea6266013ee1c8329facf786 Author: Tristan Maat <[email protected]> AuthorDate: Thu Sep 7 12:20:43 2017 +0100 Add element plugin node validations --- buildstream/buildelement.py | 9 +++++---- buildstream/plugin.py | 23 +++++++++++++++++++++++ buildstream/plugins/elements/compose.py | 4 ++++ buildstream/plugins/elements/dpkg_deploy.py | 5 +++++ buildstream/plugins/elements/script.py | 6 ++++++ buildstream/plugins/elements/x86image.py | 3 +++ tests/project/data/plugins/elements/custom.py | 1 + 7 files changed, 47 insertions(+), 4 deletions(-) diff --git a/buildstream/buildelement.py b/buildstream/buildelement.py index 859b682..0401991 100644 --- a/buildstream/buildelement.py +++ b/buildstream/buildelement.py @@ -114,11 +114,12 @@ class BuildElement(Element): def configure(self, node): self.commands = {} + command_names = [prefix + step for step in _command_steps for prefix in _command_prefixes] - for step in _command_steps: - for prefix in _command_prefixes: - command_name = prefix + step - self.commands[command_name] = self._get_commands(node, command_name) + self.node_validate(node, command_names) + + for command_name in command_names: + self.commands[command_name] = self._get_commands(node, command_name) def preflight(self): pass diff --git a/buildstream/plugin.py b/buildstream/plugin.py index 7752154..5cfd235 100644 --- a/buildstream/plugin.py +++ b/buildstream/plugin.py @@ -207,6 +207,29 @@ class Plugin(): """ return _yaml.node_get(node, expected_type, member_name, default_value=default_value) + def node_validate(self, node, valid_keys): + """Validate that a node only contains keys from a list of valid + keys. This is useful to avoid typos (such as knd: autotools). + + Args: + node (dict): A dictionary loaded from YAML + valid_keys (iterable): A list of valid keys for the node + + Raises: + :class:`.LoadError`: When an invalid key is found + + **Example:** + + .. code:: python + + # Ensure our node only contains valid autotools config keys + self.node_validate(node, [ + 'configure-commands', 'build-commands', + 'install-commands', 'strip-commands' + ]) + """ + _yaml.validate_node(node, valid_keys) + def node_get_list_element(self, node, expected_type, member_name, indices): """Fetch the value of a list element from a node member, raising an error if the value is incorrectly typed. diff --git a/buildstream/plugins/elements/compose.py b/buildstream/plugins/elements/compose.py index 4d58c1c..9d701c7 100644 --- a/buildstream/plugins/elements/compose.py +++ b/buildstream/plugins/elements/compose.py @@ -48,6 +48,10 @@ class ComposeElement(Element): BST_STRICT_REBUILD = True def configure(self, node): + self.node_validate(node, [ + 'integrate', 'include', 'exclude', 'include-orphans' + ]) + # We name this variable 'integration' only to avoid # collision with the Element.integrate() method. self.integration = self.node_get_member(node, bool, 'integrate') diff --git a/buildstream/plugins/elements/dpkg_deploy.py b/buildstream/plugins/elements/dpkg_deploy.py index e2b3470..c87357c 100644 --- a/buildstream/plugins/elements/dpkg_deploy.py +++ b/buildstream/plugins/elements/dpkg_deploy.py @@ -141,6 +141,11 @@ class DpkgDeployElement(ScriptElement): prefixes = ["pre-", "", "post-"] groups = ["build-commands"] + self.node_validate(node, [ + 'pre-build-commands', 'build-commands', 'post-build-commands', + 'base', 'input' + ]) + self.__input = self.node_subst_member(node, 'input') self.layout_add(self.node_subst_member(node, 'base'), "/") self.layout_add(None, '/buildstream') diff --git a/buildstream/plugins/elements/script.py b/buildstream/plugins/elements/script.py index 7198af8..2b3ca96 100644 --- a/buildstream/plugins/elements/script.py +++ b/buildstream/plugins/elements/script.py @@ -48,6 +48,12 @@ class ScriptElement(buildstream.ScriptElement): cmds = [] prefixes = ["pre-", "", "post-"] + + self.node_validate(node, [ + 'pre-commands', 'commands', 'post-commands', + 'root-read-only', 'layout' + ]) + if "commands" not in node: raise ElementError("{}: Unexpectedly missing command group 'commands'" .format(self)) diff --git a/buildstream/plugins/elements/x86image.py b/buildstream/plugins/elements/x86image.py index 410b53d..4bd0ca9 100644 --- a/buildstream/plugins/elements/x86image.py +++ b/buildstream/plugins/elements/x86image.py @@ -41,6 +41,9 @@ class X86ImageElement(ScriptElement): "partition-commands", "final-commands" ] + + self.node_validate(node, (prefix + group for group in groups for prefix in prefixes)) + for group in groups: cmds = [] if group not in node: diff --git a/tests/project/data/plugins/elements/custom.py b/tests/project/data/plugins/elements/custom.py index 2894a92..9737931 100644 --- a/tests/project/data/plugins/elements/custom.py +++ b/tests/project/data/plugins/elements/custom.py @@ -5,6 +5,7 @@ class CustomElement(Element): def configure(self, node): print("Element Data: %s" % node) + self.node_validate(node, ['configuration']) self.configuration = self.node_subst_member(node, "configuration", default_value='') def preflight(self):
