Skip to contentSkip to conversation

siggen: Fix type conversion issues

PATCH New Richard Purdie 1 participant 1 message Oct 16, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 siggen: Fix type conversion issues

The switch to using json has messed up the type handling as the code does assume that set()s are present. Add a decoder to reconstruct the set() objects. Also fix the change of tuples to lists for the file checksums and fix an existing type bug where dicts insteads of lists was used.

Drop some old siginfo format handling code which is now long since obsolete.

---
 lib/bb/siggen.py | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
lib/bb/siggen.py +16 -11
index 323430270c..730e0f9a14 100644
@@ -24,9 +24,14 @@ hashequiv_logger = logging.getLogger('BitBake.SigGen.HashEquiv')
 class SetEncoder(json.JSONEncoder):
     def default(self, obj):
         if isinstance(obj, set):
-            return list(sorted(obj))
+            return dict(_set_object=list(sorted(obj)))
         return json.JSONEncoder.default(self, obj)
 
+def SetDecoder(dct):
+    if '_set_object' in dct:
+        return set(dct['_set_object'])
+    return dct
+
 def init(d):
     siggens = [obj for obj in globals().values()
                       if type(obj) is type and issubclass(obj, SignatureGenerator)]
@@ -810,9 +815,9 @@ def compare_sigfiles(a, b, recursecb=None, color=False, collapsed=False):
         return formatstr.format(**formatparams)
 
     with bb.compress.zstd.open(a, "rt", encoding="utf-8", num_threads=1) as f:
-        a_data = json.load(f)
+        a_data = json.load(f, object_hook=SetDecoder)
     with bb.compress.zstd.open(b, "rt", encoding="utf-8", num_threads=1) as f:
-        b_data = json.load(f)
+        b_data = json.load(f, object_hook=SetDecoder)
 
     def dict_diff(a, b, whitelist=set()):
         sa = set(a.keys())
@@ -828,11 +833,11 @@ def compare_sigfiles(a, b, recursecb=None, color=False, collapsed=False):
 
     def file_checksums_diff(a, b):
         from collections import Counter
-        # Handle old siginfo format
-        if isinstance(a, dict):
-            a = [(os.path.basename(f), cs) for f, cs in a.items()]
-        if isinstance(b, dict):
-            b = [(os.path.basename(f), cs) for f, cs in b.items()]
+
+        # Convert lists back to tuples
+        a = [(f[0], f[1]) for f in a]
+        b = [(f[0], f[1]) for f in b]
+
         # Compare lists, ensuring we can handle duplicate filenames if they exist
         removedcount = Counter(a)
         removedcount.subtract(b)
@@ -915,9 +920,9 @@ def compare_sigfiles(a, b, recursecb=None, color=False, collapsed=False):
                 output.append(color_format("{color_title}Variable {var} value changed from '{color_default}{oldval}{color_title}' to '{color_default}{newval}{color_title}'{color_default}", var=dep, oldval=oldval, newval=newval))
 
     if not 'file_checksum_values' in a_data:
-         a_data['file_checksum_values'] = {}
+         a_data['file_checksum_values'] = []
     if not 'file_checksum_values' in b_data:
-         b_data['file_checksum_values'] = {}
+         b_data['file_checksum_values'] = []
 
     changed, added, removed = file_checksums_diff(a_data['file_checksum_values'], b_data['file_checksum_values'])
     if changed:
@@ -1047,7 +1052,7 @@ def dump_sigfile(a):
     output = []
 
     with bb.compress.zstd.open(a, "rt", encoding="utf-8", num_threads=1) as f:
-        a_data = json.load(f)
+        a_data = json.load(f, object_hook=SetDecoder)
 
     output.append("basewhitelist: %s" % (a_data['basewhitelist']))
 
Signature · 1 line
2.32.0