Skip to content

Commit 119dabd

Browse files
committed
feat: read file and validate opossum file structure
1 parent a8ebc17 commit 119dabd

7 files changed

+63
-2
lines changed

src/opossum_lib/cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def convert_after_valid_input(
8080
the_spdx_file = spdx[0]
8181
return convert_spdx_to_opossum_information(the_spdx_file)
8282
else:
83-
opossum_file = opossum_files[0]
84-
return read_opossum_file(opossum_file)
83+
opossum_input_file = opossum_files[0]
84+
return read_opossum_file(opossum_input_file)
8585

8686

8787
if __name__ == "__main__":

src/opossum_lib/opossum/read_opossum_file.py

+25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
import logging
5+
import sys
56
from datetime import datetime
7+
from zipfile import ZipFile
68

79
from opossum_lib.opossum.opossum_file import (
810
Metadata,
@@ -14,6 +16,29 @@
1416

1517
def read_opossum_file(filename: str) -> OpossumInformation:
1618
logging.info(f"Converting opossum to opossum {filename}")
19+
20+
try:
21+
with (
22+
ZipFile(filename, "r") as input_zip_file,
23+
):
24+
if "input.json" not in input_zip_file.namelist():
25+
logging.error(
26+
f"Opossum file {filename} is corrupt"
27+
f" and does not contain 'input.json'"
28+
)
29+
sys.exit(1)
30+
if "output.json" in input_zip_file.namelist():
31+
logging.error(
32+
f"Opossum file {filename} also contains"
33+
f" 'output.json' which cannot be processed"
34+
)
35+
sys.exit(1)
36+
37+
38+
except Exception as e:
39+
# handle the exception
40+
print(f"Error reading file {filename}: {e}")
41+
1742
dummy_metadata = Metadata(
1843
projectId="test id",
1944
fileCreationDate=datetime.now().isoformat(),

src/opossum_lib/spdx/convert_to_opossum.py

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050

5151
def convert_spdx_to_opossum_information(filename: str) -> OpossumInformation:
52+
logging.info(f"Converting {filename} to opossum information.")
5253
try:
5354
document: SpdxDocument = parse_file(filename)
5455

tests/data/opossum_input.opossum

-16 Bytes
Binary file not shown.
34.7 KB
Binary file not shown.
42.8 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from pathlib import Path
5+
6+
import pytest
7+
from _pytest.logging import LogCaptureFixture
8+
9+
from opossum_lib.opossum.read_opossum_file import read_opossum_file
10+
11+
12+
def test_read_opossum_file_corrupted_file_exits_1(caplog: LogCaptureFixture) -> None:
13+
input_path = (
14+
Path(__file__).resolve().parent.parent
15+
/ "data"
16+
/ "opossum_input_corrupt.opossum"
17+
)
18+
19+
with pytest.raises(SystemExit) as system_exit:
20+
read_opossum_file(str(input_path))
21+
assert system_exit.value.code == 1
22+
assert "is corrupt and does not contain 'input.json'" in caplog.messages[0]
23+
24+
25+
def test_read_opossum_file_with_result_json_exits_1(caplog: LogCaptureFixture) -> None:
26+
input_path = (
27+
Path(__file__).resolve().parent.parent
28+
/ "data"
29+
/ "opossum_input_with_result.opossum"
30+
)
31+
32+
with pytest.raises(SystemExit) as system_exit:
33+
read_opossum_file(str(input_path))
34+
assert system_exit.value.code == 1
35+
assert "also contains 'output.json' which cannot be processed" in caplog.messages[0]

0 commit comments

Comments
 (0)