[PATCH v2 05/11] binman: Add a way to check for missing properties

Simon Glass sjg at chromium.org
Sat Aug 13 19:40:44 CEST 2022


Some new entries are likely to have required properties. Support this in a
standard way, with a list of required properties which can be set up by
base classes. Check for missing properties when the entry is read.

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

Changes in v2:
- Use a dict to hold required properties

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

diff --git a/tools/binman/entry.py b/tools/binman/entry.py
index 41f0eb58ae0..5424a0e6e32 100644
--- a/tools/binman/entry.py
+++ b/tools/binman/entry.py
@@ -84,6 +84,8 @@ class Entry(object):
         update_hash: True if this entry's "hash" subnode should be
             updated with a hash of the entry contents
         fake_fname: Fake filename, if one was created, else None
+        required_props (dict of str): Properties which must be present. This can
+            be added to by subclasses
     """
     fake_dir = None
 
@@ -121,6 +123,7 @@ class Entry(object):
         self.missing_bintools = []
         self.update_hash = True
         self.fake_fname = None
+        self.required_props = []
 
     @staticmethod
     def FindEntryClass(etype, expanded):
@@ -238,6 +241,7 @@ class Entry(object):
 
         This reads all the fields we recognise from the node, ready for use.
         """
+        self.ensure_props()
         if 'pos' in self._node.props:
             self.Raise("Please use 'offset' instead of 'pos'")
         if 'expand-size' in self._node.props:
@@ -1179,3 +1183,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):
+        """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 self.required_props:
+            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..c91d0152a8a 100644
--- a/tools/binman/etype/fill.py
+++ b/tools/binman/etype/fill.py
@@ -23,11 +23,10 @@ class Entry_fill(Entry):
     """
     def __init__(self, section, etype, node):
         super().__init__(section, etype, node)
+        self.required_props = ['size']
 
     def ReadNode(self):
         super().ReadNode()
-        if self.size is None:
-            self.Raise("'fill' entry must have a size property")
         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):
-- 
2.37.1.595.g718a3a8f04-goog



More information about the U-Boot mailing list