diff mbox series

[05/11] binman: Add a function to check for missing properties

Message ID 20220811140412.1671844-6-sjg@chromium.org
State Superseded
Delegated to: Simon Glass
Headers show
Series binman: Enhancements to binman mkimage | expand

Commit Message

Simon Glass Aug. 11, 2022, 2:04 p.m. UTC
Add a new function to Entry to check for missing properties, since this
is likely to be come a common requirement.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/binman/entry.py      | 16 ++++++++++++++++
 tools/binman/etype/fill.py |  3 +--
 tools/binman/ftest.py      |  2 +-
 3 files changed, 18 insertions(+), 3 deletions(-)

Comments

Quentin Schulz Aug. 11, 2022, 3:26 p.m. UTC | #1
Hi Simon,

On 8/11/22 16:04, Simon Glass wrote:
> Add a new function to Entry to check for missing properties, since this
> is likely to be come a common requirement.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> 
>   tools/binman/entry.py      | 16 ++++++++++++++++
>   tools/binman/etype/fill.py |  3 +--
>   tools/binman/ftest.py      |  2 +-
>   3 files changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/binman/entry.py b/tools/binman/entry.py
> index 41f0eb58ae0..3be074ccd66 100644
> --- a/tools/binman/entry.py
> +++ b/tools/binman/entry.py
> @@ -1179,3 +1179,19 @@ features to produce new behaviours.
>           if not os.path.exists(cls.fake_dir):
>               os.mkdir(cls.fake_dir)
>           tout.notice(f"Fake-blob dir is '{cls.fake_dir}'")
> +
> +    def ensure_props(self, prop_list):
> +        """Raise an exception if properties are missing
> +
> +        Args:
> +            prop_list (list of str): List of properties to check for
> +
> +        Raises:
> +            ValueError: Any property is missing
> +        """
> +        not_present = []
> +        for prop in prop_list:
> +            if not prop in self._node.props:
> +                not_present.append(prop)
> +        if not_present:
> +            self.Raise(f"'{self.etype}' entry is missing properties: {' '.join(not_present)}")
> diff --git a/tools/binman/etype/fill.py b/tools/binman/etype/fill.py
> index cd382799199..0b068ba3268 100644
> --- a/tools/binman/etype/fill.py
> +++ b/tools/binman/etype/fill.py
> @@ -26,8 +26,7 @@ class Entry_fill(Entry):
>   
>       def ReadNode(self):
>           super().ReadNode()
> -        if self.size is None:
> -            self.Raise("'fill' entry must have a size property")
> +        self.ensure_props(['size'])

What about having a Dict in Entry for all required properties and then 
check them in Entry.ReadNode directly? So that other classes inheriting 
from Entry don't have to explicitly call ensure_props in ReadNode? Just 
need to add the property name to this Entry.required_props Dict?

Cheers,
Quentin
diff mbox series

Patch

diff --git a/tools/binman/entry.py b/tools/binman/entry.py
index 41f0eb58ae0..3be074ccd66 100644
--- a/tools/binman/entry.py
+++ b/tools/binman/entry.py
@@ -1179,3 +1179,19 @@  features to produce new behaviours.
         if not os.path.exists(cls.fake_dir):
             os.mkdir(cls.fake_dir)
         tout.notice(f"Fake-blob dir is '{cls.fake_dir}'")
+
+    def ensure_props(self, prop_list):
+        """Raise an exception if properties are missing
+
+        Args:
+            prop_list (list of str): List of properties to check for
+
+        Raises:
+            ValueError: Any property is missing
+        """
+        not_present = []
+        for prop in prop_list:
+            if not prop in self._node.props:
+                not_present.append(prop)
+        if not_present:
+            self.Raise(f"'{self.etype}' entry is missing properties: {' '.join(not_present)}")
diff --git a/tools/binman/etype/fill.py b/tools/binman/etype/fill.py
index cd382799199..0b068ba3268 100644
--- a/tools/binman/etype/fill.py
+++ b/tools/binman/etype/fill.py
@@ -26,8 +26,7 @@  class Entry_fill(Entry):
 
     def ReadNode(self):
         super().ReadNode()
-        if self.size is None:
-            self.Raise("'fill' entry must have a size property")
+        self.ensure_props(['size'])
         self.fill_value = fdt_util.GetByte(self._node, 'fill-byte', 0)
 
     def ObtainContents(self):
diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index c8bab5c9416..4f696c68600 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -1693,7 +1693,7 @@  class TestFunctional(unittest.TestCase):
         """Test for an fill entry type with no size"""
         with self.assertRaises(ValueError) as e:
             self._DoReadFile('070_fill_no_size.dts')
-        self.assertIn("'fill' entry must have a size property",
+        self.assertIn("'fill' entry is missing properties: size",
                       str(e.exception))
 
     def _HandleGbbCommand(self, pipe_list):