Skip to content

Commit ca72624

Browse files
instantiate get_spdx_licensing() in a singleton module
this getter takes quite some time and should be called as few times as possible Signed-off-by: Armin Tänzer <[email protected]>
1 parent 1ecc6f6 commit ca72624

19 files changed

+95
-100
lines changed

examples/spdx2_document_from_scratch.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from datetime import datetime
66
from typing import List
77

8-
from license_expression import get_spdx_licensing
9-
8+
from spdx_tools.common.spdx_licensing import spdx_licensing
109
from spdx_tools.spdx.model import (
1110
Actor,
1211
ActorType,
@@ -65,9 +64,9 @@
6564
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"),
6665
Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"),
6766
],
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"),
67+
license_concluded=spdx_licensing.parse("GPL-2.0-only OR MIT"),
68+
license_info_from_files=[spdx_licensing.parse("GPL-2.0-only"), spdx_licensing.parse("MIT")],
69+
license_declared=spdx_licensing.parse("GPL-2.0-only AND MIT"),
7170
license_comment="license comment",
7271
copyright_text="Copyright 2022 Jane Doe",
7372
description="package description",
@@ -100,8 +99,8 @@
10099
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"),
101100
Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"),
102101
],
103-
license_concluded=get_spdx_licensing().parse("MIT"),
104-
license_info_in_file=[get_spdx_licensing().parse("MIT")],
102+
license_concluded=spdx_licensing.parse("MIT"),
103+
license_info_in_file=[spdx_licensing.parse("MIT")],
105104
copyright_text="Copyright 2022 Jane Doe",
106105
)
107106
file2 = File(
@@ -110,7 +109,7 @@
110109
checksums=[
111110
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2759"),
112111
],
113-
license_concluded=get_spdx_licensing().parse("GPL-2.0-only"),
112+
license_concluded=spdx_licensing.parse("GPL-2.0-only"),
114113
)
115114

116115
# Assuming the package contains those two files, we create two CONTAINS relationships.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: 2023 spdx contributors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from license_expression import get_spdx_licensing
5+
6+
# this getter takes quite long so we only call it once in this singleton module
7+
spdx_licensing = get_spdx_licensing()

src/spdx_tools/spdx/parser/rdf/license_expression_parser.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
from beartype.typing import Optional, Union
5-
from license_expression import LicenseExpression, get_spdx_licensing
5+
from license_expression import LicenseExpression
66
from rdflib import RDF, Graph
77
from rdflib.term import BNode, Identifier, Node, URIRef
88

9+
from spdx_tools.common.spdx_licensing import spdx_licensing
910
from spdx_tools.spdx.parser.logger import Logger
1011
from spdx_tools.spdx.parser.rdf.graph_parsing_functions import get_value_from_graph, remove_prefix
1112
from spdx_tools.spdx.rdfschema.namespace import LICENSE_NAMESPACE, SPDX_NAMESPACE
@@ -19,7 +20,7 @@ def parse_license_expression(
1920
) -> LicenseExpression:
2021
if not logger:
2122
logger = Logger()
22-
spdx_licensing = get_spdx_licensing()
23+
2324
expression = ""
2425
if license_expression_node.startswith(LICENSE_NAMESPACE):
2526
expression = remove_prefix(license_expression_node, LICENSE_NAMESPACE)

src/spdx_tools/spdx/validation/license_expression_validator.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from beartype.typing import List, Optional, Union
6-
from license_expression import ExpressionError, ExpressionParseError, LicenseExpression, get_spdx_licensing
6+
from license_expression import ExpressionError, ExpressionParseError, LicenseExpression
77

8+
from spdx_tools.common.spdx_licensing import spdx_licensing
89
from spdx_tools.spdx.model import Document, SpdxNoAssertion, SpdxNone
910
from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage
1011

@@ -40,7 +41,7 @@ def validate_license_expression(
4041
validation_messages = []
4142
license_ref_ids: List[str] = [license_ref.license_id for license_ref in document.extracted_licensing_info]
4243

43-
for non_spdx_token in get_spdx_licensing().validate(license_expression).invalid_symbols:
44+
for non_spdx_token in spdx_licensing.validate(license_expression).invalid_symbols:
4445
if non_spdx_token not in license_ref_ids:
4546
validation_messages.append(
4647
ValidationMessage(
@@ -51,14 +52,14 @@ def validate_license_expression(
5152
)
5253

5354
try:
54-
get_spdx_licensing().parse(str(license_expression), validate=True, strict=True)
55+
spdx_licensing.parse(str(license_expression), validate=True, strict=True)
5556
except ExpressionParseError as err:
5657
# This error is raised when an exception symbol is used as a license symbol and vice versa.
5758
# So far, it only catches the first such error in the provided string.
5859
validation_messages.append(ValidationMessage(f"{err}. for license_expression: {license_expression}", context))
5960
except ExpressionError:
6061
# 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
62+
# these. On the other hand, spdx_licensing.validate() gives an actual list of invalid symbols, so this is
6263
# handled above.
6364
pass
6465

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

+3-10
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@
33
# SPDX-License-Identifier: Apache-2.0
44
from beartype.typing import List, Union
55
from boolean import Expression
6-
from license_expression import (
7-
AND,
8-
OR,
9-
ExpressionInfo,
10-
LicenseExpression,
11-
LicenseSymbol,
12-
LicenseWithExceptionSymbol,
13-
get_spdx_licensing,
14-
)
6+
from license_expression import AND, OR, ExpressionInfo, LicenseExpression, LicenseSymbol, LicenseWithExceptionSymbol
157
from rdflib import RDF, BNode, Graph, URIRef
168
from rdflib.term import Literal, Node
179

10+
from spdx_tools.common.spdx_licensing import spdx_licensing
1811
from spdx_tools.spdx.model import SpdxNoAssertion, SpdxNone
1912
from spdx_tools.spdx.rdfschema.namespace import LICENSE_NAMESPACE, SPDX_NAMESPACE
2013

@@ -75,7 +68,7 @@ def add_license_expression_to_graph(
7568

7669

7770
def license_or_exception_is_on_spdx_licensing_list(license_symbol: LicenseSymbol) -> bool:
78-
symbol_info: ExpressionInfo = get_spdx_licensing().validate(license_symbol)
71+
symbol_info: ExpressionInfo = spdx_licensing.validate(license_symbol)
7972
return not symbol_info.errors
8073

8174

src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
from beartype.typing import List, Union
5-
from license_expression import (
6-
AND,
7-
OR,
8-
LicenseExpression,
9-
LicenseSymbol,
10-
LicenseWithExceptionSymbol,
11-
get_spdx_licensing,
12-
)
5+
from license_expression import AND, OR, LicenseExpression, LicenseSymbol, LicenseWithExceptionSymbol
136

7+
from spdx_tools.common.spdx_licensing import spdx_licensing
148
from spdx_tools.spdx3.model.licensing import (
159
AnyLicenseInfo,
1610
ConjunctiveLicenseSet,
@@ -61,7 +55,7 @@ def bump_license_expression(
6155
subject_addition=bump_license_exception(license_expression.exception_symbol, extracted_licensing_info),
6256
)
6357
if isinstance(license_expression, LicenseSymbol):
64-
if not get_spdx_licensing().validate(license_expression).invalid_symbols:
58+
if not spdx_licensing.validate(license_expression).invalid_symbols:
6559
return ListedLicense(license_expression.key, license_expression.obj, "blank")
6660
else:
6761
for licensing_info in extracted_licensing_info:
@@ -80,7 +74,7 @@ def bump_license_expression(
8074
def bump_license_exception(
8175
license_exception: LicenseSymbol, extracted_licensing_info: List[ExtractedLicensingInfo]
8276
) -> LicenseAddition:
83-
if not get_spdx_licensing().validate(license_exception).invalid_symbols:
77+
if not spdx_licensing.validate(license_exception).invalid_symbols:
8478
return ListedLicenseException(license_exception.key, "", "")
8579
else:
8680
for licensing_info in extracted_licensing_info:

tests/spdx/examples/test_spdx2_document_from_scratch.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from datetime import datetime
66
from typing import List
77

8-
from license_expression import get_spdx_licensing
9-
8+
from spdx_tools.common.spdx_licensing import spdx_licensing
109
from spdx_tools.spdx.model import (
1110
Actor,
1211
ActorType,
@@ -67,9 +66,9 @@ def test_spdx2_document_from_scratch():
6766
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"),
6867
Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"),
6968
],
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"),
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"),
7372
license_comment="license comment",
7473
copyright_text="Copyright 2022 Jane Doe",
7574
description="package description",
@@ -102,8 +101,8 @@ def test_spdx2_document_from_scratch():
102101
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"),
103102
Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"),
104103
],
105-
license_concluded=get_spdx_licensing().parse("MIT"),
106-
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")],
107106
copyright_text="Copyright 2022 Jane Doe",
108107
)
109108
file2 = File(
@@ -112,7 +111,7 @@ def test_spdx2_document_from_scratch():
112111
checksums=[
113112
Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2759"),
114113
],
115-
license_concluded=get_spdx_licensing().parse("GPL-2.0-only"),
114+
license_concluded=spdx_licensing.parse("GPL-2.0-only"),
116115
)
117116

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

tests/spdx/fixtures.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44
from datetime import datetime
55

6-
from license_expression import get_spdx_licensing
7-
6+
from spdx_tools.common.spdx_licensing import spdx_licensing
87
from spdx_tools.spdx.constants import DOCUMENT_SPDX_ID
98
from spdx_tools.spdx.model import (
109
Actor,
@@ -88,7 +87,7 @@ def file_fixture(
8887
spdx_id="SPDXRef-File",
8988
checksums=None,
9089
file_types=None,
91-
license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"),
90+
license_concluded=spdx_licensing.parse("MIT and GPL-2.0"),
9291
license_info_in_file=None,
9392
license_comment="licenseComment",
9493
copyright_text="copyrightText",
@@ -100,7 +99,7 @@ def file_fixture(
10099
checksums = [checksum_fixture()] if checksums is None else checksums
101100
file_types = [FileType.TEXT] if file_types is None else file_types
102101
license_info_in_file = (
103-
[get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()]
102+
[spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()]
104103
if license_info_in_file is None
105104
else license_info_in_file
106105
)
@@ -135,9 +134,9 @@ def package_fixture(
135134
checksums=None,
136135
homepage="https://homepage.com",
137136
source_info="sourceInfo",
138-
license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"),
137+
license_concluded=spdx_licensing.parse("MIT and GPL-2.0"),
139138
license_info_from_files=None,
140-
license_declared=get_spdx_licensing().parse("MIT and GPL-2.0"),
139+
license_declared=spdx_licensing.parse("MIT and GPL-2.0"),
141140
license_comment="packageLicenseComment",
142141
copyright_text="packageCopyrightText",
143142
summary="packageSummary",
@@ -152,7 +151,7 @@ def package_fixture(
152151
) -> Package:
153152
checksums = [checksum_fixture()] if checksums is None else checksums
154153
license_info_from_files = (
155-
[get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()]
154+
[spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()]
156155
if license_info_from_files is None
157156
else license_info_from_files
158157
)
@@ -208,7 +207,7 @@ def snippet_fixture(
208207
file_spdx_id="SPDXRef-File",
209208
byte_range=(1, 2),
210209
line_range=(3, 4),
211-
license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"),
210+
license_concluded=spdx_licensing.parse("MIT and GPL-2.0"),
212211
license_info_in_snippet=None,
213212
license_comment="snippetLicenseComment",
214213
copyright_text="licenseCopyrightText",
@@ -217,7 +216,7 @@ def snippet_fixture(
217216
attribution_texts=None,
218217
) -> Snippet:
219218
license_info_in_snippet = (
220-
[get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNone()]
219+
[spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNone()]
221220
if license_info_in_snippet is None
222221
else license_info_in_snippet
223222
)

tests/spdx/parser/jsonlikedict/test_license_expression_parser.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from unittest import TestCase
55

66
import pytest
7-
from license_expression import get_spdx_licensing
87

8+
from spdx_tools.common.spdx_licensing import spdx_licensing
99
from spdx_tools.spdx.model import SpdxNoAssertion, SpdxNone
1010
from spdx_tools.spdx.parser.error import SPDXParsingError
1111
from spdx_tools.spdx.parser.jsonlikedict.license_expression_parser import LicenseExpressionParser
@@ -14,8 +14,8 @@
1414
@pytest.mark.parametrize(
1515
"license_expression_str, expected_license",
1616
[
17-
("First License", get_spdx_licensing().parse("First License")),
18-
("Second License", get_spdx_licensing().parse("Second License")),
17+
("First License", spdx_licensing.parse("First License")),
18+
("Second License", spdx_licensing.parse("Second License")),
1919
("NOASSERTION", SpdxNoAssertion()),
2020
("NONE", SpdxNone()),
2121
],

tests/spdx/parser/rdf/test_file_parser.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from unittest import TestCase
66

77
import pytest
8-
from license_expression import get_spdx_licensing
98
from rdflib import RDF, BNode, Graph, URIRef
109

10+
from spdx_tools.common.spdx_licensing import spdx_licensing
1111
from spdx_tools.spdx.model import Checksum, ChecksumAlgorithm, FileType, SpdxNoAssertion
1212
from spdx_tools.spdx.parser.error import SPDXParsingError
1313
from spdx_tools.spdx.parser.rdf.file_parser import parse_file
@@ -29,10 +29,10 @@ def test_parse_file():
2929
assert file.comment == "fileComment"
3030
assert file.copyright_text == "copyrightText"
3131
assert file.contributors == ["fileContributor"]
32-
assert file.license_concluded == get_spdx_licensing().parse("MIT AND GPL-2.0")
32+
assert file.license_concluded == spdx_licensing.parse("MIT AND GPL-2.0")
3333
TestCase().assertCountEqual(
3434
file.license_info_in_file,
35-
[get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()],
35+
[spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()],
3636
)
3737
assert file.license_comment == "licenseComment"
3838
assert file.notice == "fileNotice"

tests/spdx/parser/rdf/test_license_expression_parser.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import os
55
from unittest import TestCase
66

7-
from license_expression import get_spdx_licensing
87
from rdflib import RDF, Graph
98

9+
from spdx_tools.common.spdx_licensing import spdx_licensing
1010
from spdx_tools.spdx.parser.rdf import rdf_parser
1111
from spdx_tools.spdx.parser.rdf.license_expression_parser import parse_license_expression
1212
from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE
@@ -19,7 +19,7 @@ def test_license_expression_parser():
1919

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

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

2424

2525
def test_license_expression_parser_with_coupled_licenses():
@@ -30,19 +30,19 @@ def test_license_expression_parser_with_coupled_licenses():
3030
packages_by_spdx_id = {package.spdx_id: package for package in doc.packages}
3131
files_by_spdx_id = {file.spdx_id: file for file in doc.files}
3232

33-
assert packages_by_spdx_id["SPDXRef-Package"].license_declared == get_spdx_licensing().parse(
33+
assert packages_by_spdx_id["SPDXRef-Package"].license_declared == spdx_licensing.parse(
3434
"LGPL-2.0-only AND LicenseRef-3"
3535
)
36-
assert packages_by_spdx_id["SPDXRef-Package"].license_concluded == get_spdx_licensing().parse(
36+
assert packages_by_spdx_id["SPDXRef-Package"].license_concluded == spdx_licensing.parse(
3737
"LGPL-2.0-only OR LicenseRef-3"
3838
)
3939
TestCase().assertCountEqual(
4040
packages_by_spdx_id["SPDXRef-Package"].license_info_from_files,
4141
[
42-
get_spdx_licensing().parse("GPL-2.0"),
43-
get_spdx_licensing().parse("LicenseRef-1"),
44-
get_spdx_licensing().parse("LicenseRef-2"),
42+
spdx_licensing.parse("GPL-2.0"),
43+
spdx_licensing.parse("LicenseRef-1"),
44+
spdx_licensing.parse("LicenseRef-2"),
4545
],
4646
)
4747

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

0 commit comments

Comments
 (0)