Skip to content

Commit c92130a

Browse files
committed
refactor: Move the conversion of spdx to opossum to the spdx package
1 parent 6fbfeec commit c92130a

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

src/opossum_lib/cli.py

+2-30
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@
66

77

88
import logging
9-
import sys
109
from pathlib import Path
1110

1211
import click
13-
from spdx_tools.spdx.model.document import Document as SpdxDocument
14-
from spdx_tools.spdx.parser.error import SPDXParsingError
15-
from spdx_tools.spdx.parser.parse_anything import parse_file
16-
from spdx_tools.spdx.validation.document_validator import validate_full_spdx_document
1712

1813
from opossum_lib.opossum.file_generation import write_opossum_information_to_file
19-
from opossum_lib.spdx.convert_to_opossum import convert_tree_to_opossum_information
20-
21-
from .spdx.graph_generation import generate_graph_from_spdx
22-
from .spdx.tree_generation import generate_tree_from_graph
14+
from opossum_lib.spdx.convert_to_opossum import convert_spdx_to_opossum_information
2315

2416

2517
@click.command()
@@ -43,27 +35,7 @@ def spdx2opossum(infile: str, outfile: str) -> None:
4335
"""
4436
CLI-tool for converting SPDX documents to Opossum documents.
4537
"""
46-
try:
47-
document: SpdxDocument = parse_file(infile)
48-
49-
except SPDXParsingError as err:
50-
log_string = "\n".join(
51-
["There have been issues while parsing the provided document:"]
52-
+ [message for message in err.get_messages()]
53-
)
54-
logging.error(log_string)
55-
sys.exit(1)
56-
validation_messages = validate_full_spdx_document(document)
57-
if validation_messages:
58-
logging.warning(
59-
"The given SPDX document is not valid, this might cause "
60-
"issues with the conversion."
61-
)
62-
63-
graph = generate_graph_from_spdx(document)
64-
tree = generate_tree_from_graph(graph)
65-
66-
opossum_information = convert_tree_to_opossum_information(tree)
38+
opossum_information = convert_spdx_to_opossum_information(infile)
6739

6840
if not outfile.endswith(".opossum"):
6941
outfile += ".opossum"

src/opossum_lib/spdx/convert_to_opossum.py

+31
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import logging
2+
import sys
13
import uuid
24

35
from networkx import DiGraph, shortest_path
46
from spdx_tools.spdx.model.document import CreationInfo
7+
from spdx_tools.spdx.model.document import Document as SpdxDocument
58
from spdx_tools.spdx.model.file import File
69
from spdx_tools.spdx.model.package import Package
710
from spdx_tools.spdx.model.snippet import Snippet
11+
from spdx_tools.spdx.parser.error import SPDXParsingError
12+
from spdx_tools.spdx.parser.parse_anything import parse_file
13+
from spdx_tools.spdx.validation.document_validator import validate_full_spdx_document
814

915
from ..opossum.opossum_file import (
1016
ExternalAttributionSource,
@@ -27,13 +33,38 @@
2733
SPDX_PACKAGE_IDENTIFIER,
2834
SPDX_SNIPPET_IDENTIFIER,
2935
)
36+
from .graph_generation import generate_graph_from_spdx
3037
from .helper_methods import (
3138
_create_file_path_from_graph_path,
3239
_get_source_for_graph_traversal,
3340
_node_represents_a_spdx_element,
3441
_replace_node_ids_with_labels_and_add_resource_type,
3542
_weakly_connected_component_sub_graphs,
3643
)
44+
from .tree_generation import generate_tree_from_graph
45+
46+
47+
def convert_spdx_to_opossum_information(filename: str) -> OpossumInformation:
48+
try:
49+
document: SpdxDocument = parse_file(filename)
50+
51+
except SPDXParsingError as err:
52+
log_string = "\n".join(
53+
["There have been issues while parsing the provided document:"]
54+
+ [message for message in err.get_messages()]
55+
)
56+
logging.error(log_string)
57+
sys.exit(1)
58+
validation_messages = validate_full_spdx_document(document)
59+
if validation_messages:
60+
logging.warning(
61+
"The given SPDX document is not valid, this might cause "
62+
"issues with the conversion."
63+
)
64+
graph = generate_graph_from_spdx(document)
65+
tree = generate_tree_from_graph(graph)
66+
opossum_information = convert_tree_to_opossum_information(tree)
67+
return opossum_information
3768

3869

3970
def convert_tree_to_opossum_information(tree: DiGraph) -> OpossumInformation:

0 commit comments

Comments
 (0)