|
1 | 1 | # SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
|
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: Apache-2.0
|
4 |
| -import json |
| 4 | + |
| 5 | +import logging |
| 6 | +import sys |
5 | 7 | import uuid
|
6 |
| -from dataclasses import fields |
7 |
| -from pathlib import Path |
8 |
| -from zipfile import ZIP_DEFLATED, ZipFile |
9 | 8 |
|
10 | 9 | from networkx import DiGraph, shortest_path
|
11 | 10 | from spdx_tools.spdx.model.document import CreationInfo
|
| 11 | +from spdx_tools.spdx.model.document import Document as SpdxDocument |
12 | 12 | from spdx_tools.spdx.model.file import File
|
13 | 13 | from spdx_tools.spdx.model.package import Package
|
14 | 14 | from spdx_tools.spdx.model.snippet import Snippet
|
| 15 | +from spdx_tools.spdx.parser.error import SPDXParsingError |
| 16 | +from spdx_tools.spdx.parser.parse_anything import parse_file |
| 17 | +from spdx_tools.spdx.validation.document_validator import validate_full_spdx_document |
15 | 18 |
|
16 |
| -from opossum_lib.attribution_generation import ( |
| 19 | +from opossum_lib.opossum.opossum_file import ( |
| 20 | + ExternalAttributionSource, |
| 21 | + Metadata, |
| 22 | + OpossumInformation, |
| 23 | + OpossumPackage, |
| 24 | + OpossumPackageIdentifier, |
| 25 | + Resource, |
| 26 | + ResourceType, |
| 27 | + SourceInfo, |
| 28 | +) |
| 29 | +from opossum_lib.spdx.attribution_generation import ( |
17 | 30 | create_document_attribution,
|
18 | 31 | create_file_attribution,
|
19 | 32 | create_package_attribution,
|
20 | 33 | create_snippet_attribution,
|
21 | 34 | )
|
22 |
| -from opossum_lib.constants import ( |
23 |
| - COMPRESSION_LEVEL, |
| 35 | +from opossum_lib.spdx.constants import ( |
24 | 36 | SPDX_FILE_IDENTIFIER,
|
25 | 37 | SPDX_PACKAGE_IDENTIFIER,
|
26 | 38 | SPDX_SNIPPET_IDENTIFIER,
|
27 | 39 | )
|
28 |
| -from opossum_lib.helper_methods import ( |
| 40 | +from opossum_lib.spdx.graph_generation import generate_graph_from_spdx |
| 41 | +from opossum_lib.spdx.helper_methods import ( |
29 | 42 | _create_file_path_from_graph_path,
|
30 | 43 | _get_source_for_graph_traversal,
|
31 | 44 | _node_represents_a_spdx_element,
|
32 | 45 | _replace_node_ids_with_labels_and_add_resource_type,
|
33 | 46 | _weakly_connected_component_sub_graphs,
|
34 | 47 | )
|
35 |
| -from opossum_lib.opossum_file import ( |
36 |
| - ExternalAttributionSource, |
37 |
| - Metadata, |
38 |
| - OpossumInformation, |
39 |
| - OpossumPackage, |
40 |
| - OpossumPackageIdentifier, |
41 |
| - Resource, |
42 |
| - ResourceType, |
43 |
| - SourceInfo, |
44 |
| -) |
| 48 | +from opossum_lib.spdx.tree_generation import generate_tree_from_graph |
45 | 49 |
|
46 | 50 |
|
47 |
| -def write_dict_to_file( |
48 |
| - opossum_information: OpossumInformation, file_path: Path |
49 |
| -) -> None: |
50 |
| - with ZipFile( |
51 |
| - file_path, "w", compression=ZIP_DEFLATED, compresslevel=COMPRESSION_LEVEL |
52 |
| - ) as z: |
53 |
| - z.writestr("input.json", json.dumps(to_dict(opossum_information), indent=4)) |
54 |
| - |
55 |
| - |
56 |
| -def to_dict( |
57 |
| - element: Resource |
58 |
| - | Metadata |
59 |
| - | OpossumPackage |
60 |
| - | OpossumInformation |
61 |
| - | SourceInfo |
62 |
| - | ExternalAttributionSource |
63 |
| - | str |
64 |
| - | int |
65 |
| - | bool |
66 |
| - | dict[str, OpossumPackage] |
67 |
| - | dict[str, list[str]] |
68 |
| - | list[str] |
69 |
| - | None, |
70 |
| -) -> dict | str | list[str] | bool | int | None: |
71 |
| - if isinstance(element, Resource): |
72 |
| - return element.to_dict() |
73 |
| - if isinstance( |
74 |
| - element, |
75 |
| - Metadata |
76 |
| - | OpossumPackage |
77 |
| - | OpossumInformation |
78 |
| - | SourceInfo |
79 |
| - | ExternalAttributionSource, |
80 |
| - ): |
81 |
| - result = [] |
82 |
| - for f in fields(element): |
83 |
| - value = to_dict(getattr(element, f.name)) |
84 |
| - result.append((f.name, value)) |
85 |
| - return {k: v for (k, v) in result if v is not None} |
86 |
| - elif isinstance(element, dict): |
87 |
| - return {k: to_dict(v) for k, v in element.items()} |
88 |
| - else: |
89 |
| - return element |
| 51 | +def convert_spdx_to_opossum_information(filename: str) -> OpossumInformation: |
| 52 | + try: |
| 53 | + document: SpdxDocument = parse_file(filename) |
| 54 | + |
| 55 | + except SPDXParsingError as err: |
| 56 | + log_string = "\n".join( |
| 57 | + ["There have been issues while parsing the provided document:"] |
| 58 | + + [message for message in err.get_messages()] |
| 59 | + ) |
| 60 | + logging.error(log_string) |
| 61 | + sys.exit(1) |
| 62 | + validation_messages = validate_full_spdx_document(document) |
| 63 | + if validation_messages: |
| 64 | + logging.warning( |
| 65 | + "The given SPDX document is not valid, this might cause " |
| 66 | + "issues with the conversion." |
| 67 | + ) |
| 68 | + graph = generate_graph_from_spdx(document) |
| 69 | + tree = generate_tree_from_graph(graph) |
| 70 | + opossum_information = convert_tree_to_opossum_information(tree) |
| 71 | + return opossum_information |
90 | 72 |
|
91 | 73 |
|
92 |
| -def generate_json_file_from_tree(tree: DiGraph) -> OpossumInformation: |
| 74 | +def convert_tree_to_opossum_information(tree: DiGraph) -> OpossumInformation: |
93 | 75 | metadata = create_metadata(tree)
|
94 | 76 | resources = Resource(type=ResourceType.TOP_LEVEL)
|
95 | 77 | resources_to_attributions: dict[str, list[str]] = dict()
|
|
0 commit comments