[elbe-devel] [PATCH 2/8] elbepack: cyclonedx-sbom: add explicit output argument

Thomas Weißschuh thomas.weissschuh at linutronix.de
Fri Aug 30 09:10:09 CEST 2024


The output will probably be redirected somewhere,
make that easier.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/cyclonedx-sbom.py             |  7 ++++---
 elbepack/tests/cyclonedx/test_cyclonedx_sbom.py | 10 +++++-----
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/elbepack/commands/cyclonedx-sbom.py b/elbepack/commands/cyclonedx-sbom.py
index 8b2809b673c3..5a89434b01fe 100644
--- a/elbepack/commands/cyclonedx-sbom.py
+++ b/elbepack/commands/cyclonedx-sbom.py
@@ -7,7 +7,6 @@ import datetime
 import itertools
 import json
 import os
-import sys
 import urllib
 
 from elbepack.aptpkgutils import XMLPackage
@@ -85,6 +84,7 @@ def _component_from_apt_pkg(pkg):
 
 def run_command(argv):
     aparser = argparse.ArgumentParser(prog='elbe cyclonedx-sbom')
+    aparser.add_argument('-o', '--output', type=argparse.FileType('w'), default='-')
     aparser.add_argument('-d', dest='elbe_build', required=True)
     args = aparser.parse_args(argv)
 
@@ -144,5 +144,6 @@ def run_command(argv):
         ],
     }
 
-    json.dump(output, sys.stdout, indent=2, cls=CycloneDXEncoder)
-    sys.stdout.write('\n')
+    with args.output:
+        json.dump(output, args.output, indent=2, cls=CycloneDXEncoder)
+        args.output.write('\n')
diff --git a/elbepack/tests/cyclonedx/test_cyclonedx_sbom.py b/elbepack/tests/cyclonedx/test_cyclonedx_sbom.py
index f77369be9c76..ffe63235932f 100644
--- a/elbepack/tests/cyclonedx/test_cyclonedx_sbom.py
+++ b/elbepack/tests/cyclonedx/test_cyclonedx_sbom.py
@@ -2,10 +2,9 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2020 Linutronix GmbH
 
-import contextlib
-import io
 import json
 import pathlib
+import tempfile
 import uuid
 
 import jsonschema
@@ -17,11 +16,12 @@ here = pathlib.Path(__file__).parent
 
 def generate_test_bom():
     source_dir = here.joinpath('build-simple-example')
-    with contextlib.redirect_stdout(io.StringIO()) as stdout:
+    with tempfile.NamedTemporaryFile() as output:
         run_elbe_subcommand([
-            'cyclonedx-sbom', '-d', source_dir,
+            'cyclonedx-sbom', '--output', output.name, '-d', source_dir,
         ])
-    return json.loads(stdout.getvalue())
+        output.seek(0)
+        return json.load(output)
 
 
 def test_schema():
-- 
2.46.0



More information about the elbe-devel mailing list