Skip to content

Commit 3263db4

Browse files
fholgerarmintaenzertng
authored andcommitted
Add example to convert between SPDX2 file formats
Signed-off-by: Holger Frydrych <[email protected]>
1 parent d01af72 commit 3263db4

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

examples/spdx2_convert_format.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: 2023 spdx contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from os import path
5+
6+
from spdx_tools.spdx.writer.write_anything import write_file
7+
from spdx_tools.spdx.parser.parse_anything import parse_file
8+
9+
# This example demonstrates how to load an existing SPDX2 file and convert it to a different SPDX2 format
10+
11+
# Provide a path to the input file in tagvalue format
12+
input_path = path.join(path.dirname(__file__), "..", "tests", "spdx", "data", "SPDXLite.spdx")
13+
# Parse the original input file
14+
document = parse_file(input_path)
15+
# Write to XML file format
16+
write_file(document, "converted_format.xml")

tests/spdx/examples/test_examples.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def cleanup_output_files():
1212
yield
1313

14-
files_to_delete = ["spdx2_to_3.jsonld", "my_spdx_document.spdx.json"]
14+
files_to_delete = ["spdx2_to_3.jsonld", "my_spdx_document.spdx.json", "converted_format.xml"]
1515
for file in files_to_delete:
1616
output_file = os.path.join(os.path.dirname(__file__), file)
1717
if os.path.exists(output_file):
@@ -41,3 +41,11 @@ def test_spdx2_document_from_scratch():
4141

4242
output_file = os.path.join(os.path.dirname(__file__), "my_spdx_document.spdx.json")
4343
assert os.path.exists(output_file)
44+
45+
46+
@pytest.mark.usefixtures('cleanup_output_files')
47+
def test_spdx2_convert_format():
48+
run_example("spdx2_convert_format.py")
49+
50+
output_file = os.path.join(os.path.dirname(__file__), "converted_format.xml")
51+
assert os.path.exists(output_file)

0 commit comments

Comments
 (0)