Skip to content

Commit f886a48

Browse files
fholgerarmintaenzertng
authored andcommitted
Add example for generating relationship graphs
Signed-off-by: Holger Frydrych <[email protected]>
1 parent 3263db4 commit f886a48

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

examples/spdx2_generate_graph.py

+16
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.graph_generation import export_graph_from_document
7+
from spdx_tools.spdx.parser.parse_anything import parse_file
8+
9+
# This example demonstrates how to generate a relationship graph for an SPDX2 document
10+
11+
# Provide a path to the input file
12+
input_path = path.join(path.dirname(__file__), "..", "tests", "spdx", "data", "SPDXJSONExample-v2.3.spdx.json")
13+
# Parse the file
14+
document = parse_file(input_path)
15+
# Generate the graph (note: you need to have installed the optional dependency networkx, pygraphviz)
16+
export_graph_from_document(document, "graph.png")

tests/spdx/examples/test_examples.py

+18-4
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", "converted_format.xml"]
14+
files_to_delete = ["spdx2_to_3.jsonld", "my_spdx_document.spdx.json", "converted_format.xml", "graph.png"]
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):
@@ -27,25 +27,39 @@ def test_spdx2_parse_file():
2727
run_example("spdx2_parse_file.py")
2828

2929

30-
@pytest.mark.usefixtures('cleanup_output_files')
30+
@pytest.mark.usefixtures("cleanup_output_files")
3131
def test_spdx2_convert_to_spdx3():
3232
run_example("spdx2_convert_to_spdx3.py")
3333

3434
output_file = os.path.join(os.path.dirname(__file__), "spdx2_to_3.jsonld")
3535
assert os.path.exists(output_file)
3636

3737

38-
@pytest.mark.usefixtures('cleanup_output_files')
38+
@pytest.mark.usefixtures("cleanup_output_files")
3939
def test_spdx2_document_from_scratch():
4040
run_example("spdx2_document_from_scratch.py")
4141

4242
output_file = os.path.join(os.path.dirname(__file__), "my_spdx_document.spdx.json")
4343
assert os.path.exists(output_file)
4444

4545

46-
@pytest.mark.usefixtures('cleanup_output_files')
46+
@pytest.mark.usefixtures("cleanup_output_files")
4747
def test_spdx2_convert_format():
4848
run_example("spdx2_convert_format.py")
4949

5050
output_file = os.path.join(os.path.dirname(__file__), "converted_format.xml")
5151
assert os.path.exists(output_file)
52+
53+
54+
@pytest.mark.usefixtures("cleanup_output_files")
55+
def test_spdx2_generate_graph():
56+
try:
57+
import networkx # noqa F401
58+
import pygraphviz # noqa F401
59+
except ImportError:
60+
pytest.skip("Missing optional dependencies")
61+
62+
run_example("spdx2_generate_graph.py")
63+
64+
output_file = os.path.join(os.path.dirname(__file__), "graph.png")
65+
assert os.path.exists(output_file)

0 commit comments

Comments
 (0)