[elbe-devel] [PATCH 1/1] elbepack: commands: cyclonedx-sbom.py: minor code fixes

Eduard Krein eduard.krein at linutronix.de
Tue May 7 15:12:29 CEST 2024


minor code fixes

Signed-off-by: Eduard Krein <eduard.krein at linutronix.de>
---
 elbepack/commands/cyclonedx-sbom.py | 48 ++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 18 deletions(-)

diff --git a/elbepack/commands/cyclonedx-sbom.py b/elbepack/commands/cyclonedx-sbom.py
index e8fce54f..765318ae 100644
--- a/elbepack/commands/cyclonedx-sbom.py
+++ b/elbepack/commands/cyclonedx-sbom.py
@@ -2,19 +2,25 @@
 
 import datetime
 import json
+import optparse
 import os
-from json import JSONEncoder
-from optparse import OptionParser
+import sys
 
 from elbepack.elbexml import ElbeXML
 from elbepack.uuid7 import uuid7
 
 
+class CycloneDXEncoder(json.JSONEncoder):
+    def default(self, obj):
+        if isinstance(obj, (datetime.date, datetime.datetime)):
+            return obj.isoformat()
+
+
 def run_command(argv):
 
-    oparser = OptionParser()
-    oparser.add_option('-d', type='string', dest='elbe_build')
-    (options, args) = oparser.parse_args()
+    oparser = optparse.OptionParser()
+    oparser.add_option('-d', dest='elbe_build')
+    options, args = oparser.parse_args()
 
     xmlpath = os.path.join(options.elbe_build, 'source.xml')
     data = ElbeXML(xmlpath)
@@ -22,16 +28,22 @@ def run_command(argv):
     elbe_uuid = uuid7(ts)
     urn_uuid = 'urn:uuid:' + str(elbe_uuid)
     elbeversion = data.get_elbe_version()
-    output = {'bomFormat': 'CycloneDX', 'specVersion': '1.4',
-              'serialNumber': urn_uuid, 'version': 1, 'metadata':
-              {'timestamp': ts, 'tools': [{'vendor': 'Linutronix',
-               'name': 'Elbe', 'version': elbeversion}]}}
-
-    json_string = json.dumps(output, indent=2, cls=DateTimeEncoder)
-    print(json_string)
-
-
-class DateTimeEncoder(JSONEncoder):
-    def default(self, obj):
-        if isinstance(obj, (datetime.date, datetime.datetime)):
-            return obj.isoformat()
+    output = {
+        'bomFormat': 'CycloneDX',
+        'specVersion': '1.4',
+        'serialNumber': urn_uuid,
+        'version': 1,
+        'metadata': {
+          'timestamp': ts,
+          'tools': [
+            {
+              'vendor': 'Linutronix',
+              'name': 'Elbe',
+              'version': elbeversion
+            }
+          ]
+        }
+    }
+
+    json.dump(output, sys.stdout, indent=2, cls=CycloneDXEncoder)
+    sys.stdout.write('\n')
-- 
2.39.2



More information about the elbe-devel mailing list