Skip to content

Commit 84dce36

Browse files
committed
Use import.resources instead of __file__
Potentially fix spdx#257 and allow zip-safe = true in pyproject.toml Signed-off-by: Arthit Suriyawongkul <[email protected]>
1 parent 8dc336f commit 84dce36

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

src/spdx_tools/spdx3/writer/json_ld/json_ld_writer.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
import json
5-
import os
5+
from importlib import resources
66

77
from spdx_tools.spdx3.payload import Payload
8-
from spdx_tools.spdx3.writer.json_ld.json_ld_converter import convert_payload_to_json_ld_list_of_elements
8+
from spdx_tools.spdx3.writer.json_ld.json_ld_converter import (
9+
convert_payload_to_json_ld_list_of_elements,
10+
)
911

1012

1113
def write_payload(payload: Payload, file_name: str):
1214
element_list = convert_payload_to_json_ld_list_of_elements(payload)
1315

1416
# this will be obsolete as soon as the context is publicly available under some URI
15-
with open(os.path.join(os.path.dirname(__file__), "context.json"), "r") as infile:
17+
# Note: 3.0.1 context is now available at
18+
# https://spdx.org/rdf/3.0.1/spdx-context.jsonld
19+
with resources.files("spdx_tools.spdx3.writer.json_ld").joinpath("context.json").open("r") as infile:
1620
context = json.load(infile)
1721

1822
complete_dict = {"@context": context, "@graph": element_list}
1923

20-
with open(file_name + ".jsonld", "w") as out:
24+
with open(file_name + ".jsonld", "w", encoding="utf-8") as out:
2125
json.dump(complete_dict, out, indent=2)

src/spdx_tools/spdx3/writer/json_ld/owl_to_context.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
import json
5-
import os.path
5+
from importlib import resources
66

77
# current workflow: markdown files + spec_parser -> model.ttl -> convert to json_ld: SPDX_OWL.json ->
88
# use the function below to generate context.json
@@ -108,8 +108,11 @@ def convert_spdx_owl_to_jsonld_context(spdx_owl: str = "SPDX_OWL.json"):
108108
else:
109109
print(f"unknown node_type: {node_type}")
110110

111-
with open(os.path.join(os.path.dirname(__file__), "context.json"), "w") as infile:
112-
json.dump(context_dict, infile)
111+
with resources.as_file(
112+
resources.files("spdx_tools.spdx3.writer.json_ld").joinpath("context.json")
113+
) as context_path:
114+
with open(context_path, "w", encoding="utf-8") as outfile:
115+
json.dump(context_dict, outfile)
113116

114117

115118
if __name__ == "__main__":

tests/spdx/test_cli.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-FileCopyrightText: 2023 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4-
import os
4+
from importlib import resources
55

66
import pytest
77
from click.testing import CliRunner
@@ -12,16 +12,16 @@
1212
@pytest.mark.parametrize(
1313
"options",
1414
[
15-
("--infile", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json")),
16-
("-i", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"), "--novalidation"),
15+
("--infile", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json"))),
16+
("-i", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")), "--novalidation"),
1717
(
1818
"-i",
19-
os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"),
19+
str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")),
2020
"--novalidation",
2121
"--version",
2222
"SPDX-2.3",
2323
),
24-
("-i", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"), "-o", "-"),
24+
("-i", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")), "-o", "-"),
2525
],
2626
)
2727
def test_cli_with_system_exit_code_0(options):
@@ -37,9 +37,10 @@ def test_cli_with_system_exit_code_0(options):
3737
[
3838
(
3939
"-i",
40-
os.path.join(
41-
os.path.dirname(__file__),
42-
"data/invalid/spdx-trivy-vmware_log-intelligence-fluentd-sha256_086af034f561f343f633be9d9f9e95f65ae6c61b8ddb2c6755ef5bb25b40f53a.json", # noqa: E501
40+
str(
41+
resources.files("tests.spdx.data.invalid").joinpath(
42+
"spdx-trivy-vmware_log-intelligence-fluentd-sha256_086af034f561f343f633be9d9f9e95f65ae6c61b8ddb2c6755ef5bb25b40f53a.json"
43+
)
4344
),
4445
),
4546
("-i", "non_existent_file.spdx"),
@@ -57,8 +58,8 @@ def test_cli_with_system_exit_code_1(options):
5758
"options",
5859
[
5960
(),
60-
("-i", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"), "--version"),
61-
("-i", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"), "-o"),
61+
("-i", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")), "--version"),
62+
("-i", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")), "-o"),
6263
],
6364
)
6465
def test_cli_with_system_exit_code_2(options):

tests/spdx3/writer/json_ld/test_json_ld_writer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-FileCopyrightText: 2023 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4-
import os
4+
from importlib import resources
55

66
from spdx_tools.spdx3.bump_from_spdx2.spdx_document import bump_spdx_document
77
from spdx_tools.spdx3.payload import Payload
@@ -15,4 +15,5 @@ def test_json_writer():
1515
payload: Payload = bump_spdx_document(spdx2_document)
1616

1717
# this currently generates an actual file to look at, this should be changed to a temp file later
18-
write_payload(payload, os.path.join(os.path.dirname(__file__), "../../../SPDX3_jsonld_test"))
18+
with resources.as_file(resources.files("tests.spdx3.writer.json_ld").joinpath("SPDX3_jsonld_test")) as output_file:
19+
write_payload(payload, str(output_file))

0 commit comments

Comments
 (0)