8000 only instantiate get_spdx_licensing() once per module · spdx/tools-python@9d74680 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d74680

Browse files
only instantiate get_spdx_licensing() once per module
this takes quite some time and should be done as few times as possible Signed-off-by: Armin Tänzer <armin.taenzer@tngtech.com>
1 parent 1ecc6f6 commit 9d74680

17 files changed

+95
-63
lines changed

examples/spdx2_document_from_scratch.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
# The document currently does not describe anything. Let's create a package that we can add to it.
5050
# The Package class has quite a few properties (have a look there!),
5151
# but only name, spdx_id and download_location are mandatory in SPDX v2.3.
52+
spdx_licensing = get_spdx_licensing() # this getter takes quite long and should be called as few times as possible
5253
package = Package(
5354
name="package name",
5455
spdx_id="SPDXRef-Package",
@@ -65,9 +66,9 @@
6566
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"),
6667
Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"),
6768
],
68-
license_concluded=get_spdx_licensing().parse("GPL-2.0-only OR MIT"),
69-
license_info_from_files=[get_spdx_licensing().parse("GPL-2.0-only"), get_spdx_licensing().parse("MIT")],
70-
license_declared=get_spdx_licensing().parse("GPL-2.0-only AND MIT"),
69+
license_concluded=spdx_licensing.parse("GPL-2.0-only OR MIT"),
70+
license_info_from_files=[spdx_licensing.parse("GPL-2.0-only"), spdx_licensing.parse("MIT")],
71+
license_declared=spdx_licensing.parse("GPL-2.0-only AND MIT"),
7172
license_comment="license comment",
7273
copyright_text="Copyright 2022 Jane Doe",
7374
description="package description",
@@ -100,8 +101,8 @@
100101
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"),
101102
Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"),
102103
],
103-
license_concluded=get_spdx_licensing().parse("MIT"),
104-
license_info_in_file=[get_spdx_licensing().parse("MIT")],
104+
license_concluded=spdx_licensing.parse("MIT"),
105+
license_info_in_file=[spdx_licensing.parse("MIT")],
105106
copyright_text="Copyright 2022 Jane Doe",
106107
)
107108
file2 = File(
@@ -110,7 +111,7 @@
110111
checksums=[
111112
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2759"),
112113
],
113-
license_concluded=get_spdx_licensing().parse("GPL-2.0-only"),
114+
license_concluded=spdx_licensing.parse("GPL-2.0-only"),
114115
)
115116

116117
# Assuming the package contains those two files, we create two CONTAINS relationships.

src/spdx_tools/spdx/validation/license_expression_validator.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from spdx_tools.spdx.model import Document, SpdxNoAssertion, SpdxNone
99
from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage
1010

11+
spdx_licensing = get_spdx_licensing()
12+
1113

1214
def validate_license_expressions(
1315
license_expressions: List[Union[LicenseExpression, SpdxNoAssertion, SpdxNone]], document: Document, parent_id: str
@@ -40,7 +42,7 @@ def validate_license_expression(
4042
validation_messages = []
4143
license_ref_ids: List[str] = [license_ref.license_id for license_ref in document.extracted_licensing_info]
4244

43-
for non_spdx_token in get_spdx_licensing().validate(license_expression).invalid_symbols:
45+
for non_spdx_token in spdx_licensing.validate(license_expression).invalid_symbols:
4446
if non_spdx_token not in license_ref_ids:
4547
validation_messages.append(
4648
ValidationMessage(
@@ -51,14 +53,14 @@ def validate_license_expression(
5153
)
5254

5355
try:
54-
get_spdx_licensing().parse(str(license_expression), validate=True, strict=True)
56+
spdx_licensing.parse(str(license_expression), validate=True, strict=True)
5557
except ExpressionParseError as err:
5658
# This error is raised when an exception symbol is used as a license symbol and vice versa.
5759
# So far, it only catches the first such error in the provided string.
5860
validation_messages.append(ValidationMessage(f"{err}. for license_expression: {license_expression}", context))
5961
except ExpressionError:
6062
# This error is raised for invalid symbols within the license_expression, but it provides only a string of
61-
# these. On the other hand, get_spdx_licensing().validate() gives an actual list of invalid symbols, so this is
63+
# these. On the other hand, spdx_licensing.validate() gives an actual list of invalid symbols, so this is
6264
# handled above.
6365
pass
6466

src/spdx_tools/spdx/writer/rdf/license_expression_writer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from spdx_tools.spdx.model import SpdxNoAssertion, SpdxNone
1919
from spdx_tools.spdx.rdfschema.namespace import LICENSE_NAMESPACE, SPDX_NAMESPACE
2020

21+
spdx_licensing = get_spdx_licensing()
22+
2123

2224
def add_license_expression_or_none_or_no_assertion(
2325
value: Union[
@@ -75,7 +77,7 @@ def add_license_expression_to_graph(
7577

7678

7779
def license_or_exception_is_on_spdx_licensing_list(license_symbol: LicenseSymbol) -> bool:
78-
symbol_info: ExpressionInfo = get_spdx_licensing().validate(license_symbol)
80+
symbol_info: ExpressionInfo = spdx_licensing.validate(license_symbol)
7981
return not symbol_info.errors
8082

8183

src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
)
2929
from spdx_tools.spdx.model import ExtractedLicensingInfo, SpdxNoAssertion, SpdxNone
3030

31+
spdx_licensing = get_spdx_licensing()
32+
3133

3234
def bump_license_expression_or_none_or_no_assertion(
3335
element: Union[LicenseExpression, SpdxNoAssertion, SpdxNone],
@@ -61,7 +63,7 @@ def bump_license_expression(
6163
subject_addition=bump_license_exception(license_expression.exception_symbol, extracted_licensing_info),
6264
)
6365
if isinstance(license_expression, LicenseSymbol):
64-
if not get_spdx_licensing().validate(license_expression).invalid_symbols:
66+
if not spdx_licensing.validate(license_expression).invalid_symbols:
6567
return ListedLicense(license_expression.key, license_expression.obj, "blank")
6668
else:
6769
for licensing_info in extracted_licensing_info:
@@ -80,7 +82,7 @@ def bump_license_expression(
8082
def bump_license_exception(
8183
license_exception: LicenseSymbol, extracted_licensing_info: List[ExtractedLicensingInfo]
8284
) -> LicenseAddition:
83-
if not get_spdx_licensing().validate(license_exception).invalid_symbols:
85+
if not spdx_licensing.validate(license_exception).invalid_symbols:
8486
return ListedLicenseException(license_exception.key, "", "")
8587
else:
8688
for licensing_info in extracted_licensing_info:

tests/spdx/examples/test_spdx2_document_from_scratch.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def test_spdx2_document_from_scratch():
5151
# The document currently does not describe anything. Let's create a package that we can add to it.
5252
# The Package class has quite a few properties (have a look there!),
5353
# but only name, spdx_id and download_location are mandatory in SPDX v2.3.
54+
spdx_licensing = get_spdx_licensing() # this getter takes quite long and should be called as few times as possible
5455
package = Package(
5556
name="package name",
5657
spdx_id="SPDXRef-Package",
@@ -67,9 +68,9 @@ def test_spdx2_document_from_scratch():
6768
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"),
6869
Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"),
6970
],
70-
license_concluded=get_spdx_licensing().parse("GPL-2.0-only OR MIT"),
71-
license_info_from_files=[get_spdx_licensing().parse("GPL-2.0-only"), get_spdx_licensing().parse("MIT")],
72-
license_declared=get_spdx_licensing().parse("GPL-2.0-only AND MIT"),
71+
license_concluded=spdx_licensing.parse("GPL-2.0-only OR MIT"),
72+
license_info_from_files=[spdx_licensing.parse("GPL-2.0-only"), spdx_licensing.parse("MIT")],
73+
license_declared=spdx_licensing.parse("GPL-2.0-only AND MIT"),
7374
license_comment="license comment",
7475
copyright_text="Copyright 2022 Jane Doe",
7576
description="package description",
@@ -102,8 +103,8 @@ def test_spdx2_document_from_scratch():
102103
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"),
103104
Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"),
104105
],
105-
license_concluded=get_spdx_licensing().parse("MIT"),
106-
license_info_in_file=[get_spdx_licensing().parse("MIT")],
106+
license_concluded=spdx_licensing.parse("MIT"),
107+
license_info_in_file=[spdx_licensing.parse("MIT")],
107108
copyright_text="Copyright 2022 Jane Doe",
108109
)
109110
file2 = File(
@@ -112,7 +113,7 @@ def test_spdx2_document_from_scratch():
112113
checksums=[
113114
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2759"),
114115
],
115-
license_concluded=get_spdx_licensing().parse("GPL-2.0-only"),
116+
license_concluded=spdx_licensing.parse("GPL-2.0-only"),
116117
)
117118

118119
# Assuming the package contains those two files, we create two CONTAINS relationships.

tests/spdx/fixtures.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
Version,
3333
)
3434

35+
spdx_licensing = get_spdx_licensing()
36+
37+
3538
# Utility methods to create data model instances. All properties have valid defaults, so they don't need to be
3639
# specified unless relevant for the test.
3740

@@ -88,7 +91,7 @@ def file_fixture(
8891
spdx_id="SPDXRef-File",
8992
checksums=None,
9093
file_types=None,
91-
license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"),
94+
license_concluded=spdx_licensing.parse("MIT and GPL-2.0"),
9295
license_info_in_file=None,
9396
license_comment="licenseComment",
9497
copyright_text="copyrightText",
@@ -100,7 +103,7 @@ def file_fixture(
100103
checksums = [checksum_fixture()] if checksums is None else checksums
101104
file_types = [FileType.TEXT] if file_types is None else file_types
102105
license_info_in_file = (
103-
[get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()]
106+
[spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()]
104107
if license_info_in_file is None
105108
else license_info_in_file
106109
)
@@ -135,9 +138,9 @@ def package_fixture(
135138
checksums=None,
136139
homepage="https://homepage.com",
137140
source_info="sourceInfo",
138-
license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"),
141+
license_concluded=spdx_licensing.parse("MIT and GPL-2.0"),
139142
license_info_from_files=None,
140-
license_declared=get_spdx_licensing().parse("MIT and GPL-2.0"),
143+
license_declared=spdx_licensing.parse("MIT and GPL-2.0"),
141144
license_comment="packageLicenseComment",
142145
copyright_text="packageCopyrightText",
143146
summary="packageSummary",
@@ -152,7 +155,7 @@ def package_fixture(
152155
) -> Package:
153156
checksums = [checksum_fixture()] if checksums is None else checksums
154157
license_info_from_files = (
155-
[get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()]
158+
[spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()]
156159
if license_info_from_files is None
157160
else license_info_from_files
158161
)
@@ -208,7 +211,7 @@ def snippet_fixture(
208211
file_spdx_id="SPDXRef-File",
209212
byte_range=(1, 2),
210213
line_range=(3, 4),
211-
license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"),
214+
license_concluded=spdx_licensing.parse("MIT and GPL-2.0"),
212215
license_info_in_snippet=None,
213216
license_comment="snippetLicenseComment",
214217
copyright_text="licenseCopyrightText",
@@ -217,7 +220,7 @@ def snippet_fixture(
217220
attribution_texts=None,
218221
) -> Snippet:
219222
license_info_in_snippet = (
220-
[get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNone()]
223+
[spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNone()]
221224
if license_info_in_snippet is None
222225
else license_info_in_snippet
223226
)

tests/spdx/parser/jsonlikedict/test_license_expression_parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
from spdx_tools.spdx.parser.error import SPDXParsingError
1111
from spdx_tools.spdx.parser.jsonlikedict.license_expression_parser import LicenseExpressionParser
1212

13+
spdx_licensing = get_spdx_licensing()
14+
1315

1416
@pytest.mark.parametrize(
1517
"license_expression_str, expected_license",
1618
[
17-
("First License", get_spdx_licensing().parse("First License")),
18-
("Second License", get_spdx_licensing().parse("Second License")),
19+
("First License", spdx_licensing.parse("First License")),
20+
("Second License", spdx_licensing.parse("Second License")),
1921
("NOASSERTION", SpdxNoAssertion()),
2022
("NONE", SpdxNone()),
2123
],

tests/spdx/parser/rdf/test_file_parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from spdx_tools.spdx.parser.rdf.file_parser import parse_file
1414
from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE
1515

16+
spdx_licensing = get_spdx_licensing()
17+
1618

1719
def test_parse_file():
1820
graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml"))
@@ -29,10 +31,10 @@ def test_parse_file():
2931
assert file.comment == "fileComment"
3032
assert file.copyright_text == "copyrightText"
3133
assert file.contributors == ["fileContributor"]
32-
assert file.license_concluded == get_spdx_licensing().parse("MIT AND GPL-2.0")
34+
assert file.license_concluded == spdx_licensing.parse("MIT AND GPL-2.0")
3335
TestCase().assertCountEqual(
3436
file.license_info_in_file,
35-
[get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()],
37+
[spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()],
3638
)
3739
assert file.license_comment == "licenseComment"
3840
assert file.notice == "fileNotice"

tests/spdx/parser/rdf/test_license_expression_parser.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from spdx_tools.spdx.parser.rdf.license_expression_parser import parse_license_expression
1212
from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE
1313

14+
spdx_licensing = get_spdx_licensing()
15+
1416

1517
def test_license_expression_parser():
1618
graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml"))
@@ -19,7 +21,7 @@ def test_license_expression_parser():
1921

2022
license_expression = parse_license_expression(license_expression_node, graph, "https://some.namespace#")
2123

22-
assert license_expression == get_spdx_licensing().parse("GPL-2.0 AND MIT")
24+
assert license_expression == spdx_licensing.parse("GPL-2.0 AND MIT")
2325

2426

2527
def test_license_expression_parser_with_coupled_licenses():
@@ -30,19 +32,19 @@ def test_license_expression_parser_with_coupled_licenses():
3032
packages_by_spdx_id = {package.spdx_id: package for package in doc.packages}
3133
files_by_spdx_id = {file.spdx_id: file for file in doc.files}
323 10000 4

33-
assert packages_by_spdx_id["SPDXRef-Package"].license_declared == get_spdx_licensing().parse(
35+
assert packages_by_spdx_id["SPDXRef-Package"].license_declared == spdx_licensing.parse(
3436
"LGPL-2.0-only AND LicenseRef-3"
3537
)
36-
assert packages_by_spdx_id["SPDXRef-Package"].license_concluded == get_spdx_licensing().parse(
38+
assert packages_by_spdx_id["SPDXRef-Package"].license_concluded == spdx_licensing.parse(
3739
"LGPL-2.0-only OR LicenseRef-3"
3840
)
3941
TestCase().assertCountEqual(
4042
packages_by_spdx_id["SPDXRef-Package"].license_info_from_files,
4143
[
42-
get_spdx_licensing().parse("GPL-2.0"),
43-
get_spdx_licensing().parse("LicenseRef-1"),
44-
get_spdx_licensing().parse("LicenseRef-2"),
44+
spdx_licensing.parse("GPL-2.0"),
45+
spdx_licensing.parse("LicenseRef-1"),
46+
spdx_licensing.parse("LicenseRef-2"),
4547
],
4648
)
4749

48-
assert files_by_spdx_id["SPDXRef-JenaLib"].license_concluded == get_spdx_licensing().parse("LicenseRef-1")
50+
assert files_by_spdx_id["SPDXRef-JenaLib"].license_concluded == spdx_licensing.parse("LicenseRef-1")

tests/spdx/parser/rdf/test_package_parser.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from spdx_tools.spdx.parser.rdf.package_parser import parse_external_package_ref, parse_package
2323
from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE
2424

25+
spdx_licensing = get_spdx_licensing()
26+
2527

2628
def test_package_parser():
2729
graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml"))
@@ -41,11 +43,11 @@ def test_package_parser():
4143
assert package.files_analyzed is True
4244
assert package.checksums == [Checksum(ChecksumAlgorithm.SHA1, "71c4025dd9897b364f3ebbb42c484ff43d00791c")]
4345
assert package.source_info == "sourceInfo"
44-
assert package.license_concluded == get_spdx_licensing().parse("MIT AND GPL-2.0")
45-
assert package.license_declared == get_spdx_licensing().parse("MIT AND GPL-2.0")
46+
assert package.license_concluded == spdx_licensing.parse("MIT AND GPL-2.0")
47+
assert package.license_declared == spdx_licensing.parse("MIT AND GPL-2.0")
4648
TestCase().assertCountEqual(
4749
package.license_info_from_files,
48-
[get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()],
50+
[spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()],
4951
)
5052
assert package.license_comment == "packageLicenseComment"
5153
assert package.copyright_text == "packageCopyrightText"

0 commit comments

Comments
 (0)
0