From 1c2aeb40361dff5b41dbc56aec4e63a1af731fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armin=20T=C3=A4nzer?= Date: Tue, 22 Aug 2023 15:17:57 +0200 Subject: [PATCH] instantiate get_spdx_licensing() in a singleton module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit this getter takes quite some time and should be called as few times as possible Signed-off-by: Armin Tänzer --- examples/spdx2_document_from_scratch.py | 15 +++++++------ src/spdx_tools/common/spdx_licensing.py | 7 +++++++ .../parser/rdf/license_expression_parser.py | 5 +++-- .../license_expression_validator.py | 9 ++++---- .../writer/rdf/license_expression_writer.py | 13 +++--------- .../bump_from_spdx2/license_expression.py | 14 ++++--------- .../test_spdx2_document_from_scratch.py | 15 +++++++------ tests/spdx/fixtures.py | 17 +++++++-------- .../test_license_expression_parser.py | 6 +++--- tests/spdx/parser/rdf/test_file_parser.py | 6 +++--- .../rdf/test_license_expression_parser.py | 16 +++++++------- tests/spdx/parser/rdf/test_package_parser.py | 8 +++---- tests/spdx/parser/rdf/test_snippet_parser.py | 6 +++--- .../spdx/parser/tagvalue/test_file_parser.py | 6 +++--- .../parser/tagvalue/test_package_parser.py | 6 +++--- .../parser/tagvalue/test_snippet_parser.py | 4 ++-- .../test_license_expression_validator.py | 13 ++++++------ .../rdf/test_license_expression_writer.py | 8 +++---- .../bump/test_license_expression_bump.py | 21 ++++++++++--------- 19 files changed, 95 insertions(+), 100 deletions(-) create mode 100644 src/spdx_tools/common/spdx_licensing.py diff --git a/examples/spdx2_document_from_scratch.py b/examples/spdx2_document_from_scratch.py index e74ccf3a1..bc92175a8 100644 --- a/examples/spdx2_document_from_scratch.py +++ b/examples/spdx2_document_from_scratch.py @@ -5,8 +5,7 @@ from datetime import datetime from typing import List -from license_expression import get_spdx_licensing - +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import ( Actor, ActorType, @@ -65,9 +64,9 @@ Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"), Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"), ], - license_concluded=get_spdx_licensing().parse("GPL-2.0-only OR MIT"), - license_info_from_files=[get_spdx_licensing().parse("GPL-2.0-only"), get_spdx_licensing().parse("MIT")], - license_declared=get_spdx_licensing().parse("GPL-2.0-only AND MIT"), + license_concluded=spdx_licensing.parse("GPL-2.0-only OR MIT"), + license_info_from_files=[spdx_licensing.parse("GPL-2.0-only"), spdx_licensing.parse("MIT")], + license_declared=spdx_licensing.parse("GPL-2.0-only AND MIT"), license_comment="license comment", copyright_text="Copyright 2022 Jane Doe", description="package description", @@ -100,8 +99,8 @@ Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"), Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"), ], - license_concluded=get_spdx_licensing().parse("MIT"), - license_info_in_file=[get_spdx_licensing().parse("MIT")], + license_concluded=spdx_licensing.parse("MIT"), + license_info_in_file=[spdx_licensing.parse("MIT")], copyright_text="Copyright 2022 Jane Doe", ) file2 = File( @@ -110,7 +109,7 @@ checksums=[ Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2759"), ], - license_concluded=get_spdx_licensing().parse("GPL-2.0-only"), + license_concluded=spdx_licensing.parse("GPL-2.0-only"), ) # Assuming the package contains those two files, we create two CONTAINS relationships. diff --git a/src/spdx_tools/common/spdx_licensing.py b/src/spdx_tools/common/spdx_licensing.py new file mode 100644 index 000000000..a9a17e973 --- /dev/null +++ b/src/spdx_tools/common/spdx_licensing.py @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: 2023 spdx contributors +# +# SPDX-License-Identifier: Apache-2.0 +from license_expression import get_spdx_licensing + +# this getter takes quite long so we only call it once in this singleton module +spdx_licensing = get_spdx_licensing() diff --git a/src/spdx_tools/spdx/parser/rdf/license_expression_parser.py b/src/spdx_tools/spdx/parser/rdf/license_expression_parser.py index 64cc36755..2ae547232 100644 --- a/src/spdx_tools/spdx/parser/rdf/license_expression_parser.py +++ b/src/spdx_tools/spdx/parser/rdf/license_expression_parser.py @@ -2,10 +2,11 @@ # # SPDX-License-Identifier: Apache-2.0 from beartype.typing import Optional, Union -from license_expression import LicenseExpression, get_spdx_licensing +from license_expression import LicenseExpression from rdflib import RDF, Graph from rdflib.term import BNode, Identifier, Node, URIRef +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.parser.logger import Logger from spdx_tools.spdx.parser.rdf.graph_parsing_functions import get_value_from_graph, remove_prefix from spdx_tools.spdx.rdfschema.namespace import LICENSE_NAMESPACE, SPDX_NAMESPACE @@ -19,7 +20,7 @@ def parse_license_expression( ) -> LicenseExpression: if not logger: logger = Logger() - spdx_licensing = get_spdx_licensing() + expression = "" if license_expression_node.startswith(LICENSE_NAMESPACE): expression = remove_prefix(license_expression_node, LICENSE_NAMESPACE) diff --git a/src/spdx_tools/spdx/validation/license_expression_validator.py b/src/spdx_tools/spdx/validation/license_expression_validator.py index bce5c9eb3..a59aec9fa 100644 --- a/src/spdx_tools/spdx/validation/license_expression_validator.py +++ b/src/spdx_tools/spdx/validation/license_expression_validator.py @@ -3,8 +3,9 @@ # SPDX-License-Identifier: Apache-2.0 from beartype.typing import List, Optional, Union -from license_expression import ExpressionError, ExpressionParseError, LicenseExpression, get_spdx_licensing +from license_expression import ExpressionError, ExpressionParseError, LicenseExpression +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import Document, SpdxNoAssertion, SpdxNone from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage @@ -40,7 +41,7 @@ def validate_license_expression( validation_messages = [] license_ref_ids: List[str] = [license_ref.license_id for license_ref in document.extracted_licensing_info] - for non_spdx_token in get_spdx_licensing().validate(license_expression).invalid_symbols: + for non_spdx_token in spdx_licensing.validate(license_expression).invalid_symbols: if non_spdx_token not in license_ref_ids: validation_messages.append( ValidationMessage( @@ -51,14 +52,14 @@ def validate_license_expression( ) try: - get_spdx_licensing().parse(str(license_expression), validate=True, strict=True) + spdx_licensing.parse(str(license_expression), validate=True, strict=True) except ExpressionParseError as err: # This error is raised when an exception symbol is used as a license symbol and vice versa. # So far, it only catches the first such error in the provided string. validation_messages.append(ValidationMessage(f"{err}. for license_expression: {license_expression}", context)) except ExpressionError: # This error is raised for invalid symbols within the license_expression, but it provides only a string of - # these. On the other hand, get_spdx_licensing().validate() gives an actual list of invalid symbols, so this is + # these. On the other hand, spdx_licensing.validate() gives an actual list of invalid symbols, so this is # handled above. pass diff --git a/src/spdx_tools/spdx/writer/rdf/license_expression_writer.py b/src/spdx_tools/spdx/writer/rdf/license_expression_writer.py index 1057f6efd..c8a76035a 100644 --- a/src/spdx_tools/spdx/writer/rdf/license_expression_writer.py +++ b/src/spdx_tools/spdx/writer/rdf/license_expression_writer.py @@ -3,18 +3,11 @@ # SPDX-License-Identifier: Apache-2.0 from beartype.typing import List, Union from boolean import Expression -from license_expression import ( - AND, - OR, - ExpressionInfo, - LicenseExpression, - LicenseSymbol, - LicenseWithExceptionSymbol, - get_spdx_licensing, -) +from license_expression import AND, OR, ExpressionInfo, LicenseExpression, LicenseSymbol, LicenseWithExceptionSymbol from rdflib import RDF, BNode, Graph, URIRef from rdflib.term import Literal, Node +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import SpdxNoAssertion, SpdxNone from spdx_tools.spdx.rdfschema.namespace import LICENSE_NAMESPACE, SPDX_NAMESPACE @@ -75,7 +68,7 @@ def add_license_expression_to_graph( def license_or_exception_is_on_spdx_licensing_list(license_symbol: LicenseSymbol) -> bool: - symbol_info: ExpressionInfo = get_spdx_licensing().validate(license_symbol) + symbol_info: ExpressionInfo = spdx_licensing.validate(license_symbol) return not symbol_info.errors diff --git a/src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py b/src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py index ddd04ecdd..de5f006d3 100644 --- a/src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py +++ b/src/spdx_tools/spdx3/bump_from_spdx2/license_expression.py @@ -2,15 +2,9 @@ # # SPDX-License-Identifier: Apache-2.0 from beartype.typing import List, Union -from license_expression import ( - AND, - OR, - LicenseExpression, - LicenseSymbol, - LicenseWithExceptionSymbol, - get_spdx_licensing, -) +from license_expression import AND, OR, LicenseExpression, LicenseSymbol, LicenseWithExceptionSymbol +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx3.model.licensing import ( AnyLicenseInfo, ConjunctiveLicenseSet, @@ -61,7 +55,7 @@ def bump_license_expression( subject_addition=bump_license_exception(license_expression.exception_symbol, extracted_licensing_info), ) if isinstance(license_expression, LicenseSymbol): - if not get_spdx_licensing().validate(license_expression).invalid_symbols: + if not spdx_licensing.validate(license_expression).invalid_symbols: return ListedLicense(license_expression.key, license_expression.obj, "blank") else: for licensing_info in extracted_licensing_info: @@ -80,7 +74,7 @@ def bump_license_expression( def bump_license_exception( license_exception: LicenseSymbol, extracted_licensing_info: List[ExtractedLicensingInfo] ) -> LicenseAddition: - if not get_spdx_licensing().validate(license_exception).invalid_symbols: + if not spdx_licensing.validate(license_exception).invalid_symbols: return ListedLicenseException(license_exception.key, "", "") else: for licensing_info in extracted_licensing_info: diff --git a/tests/spdx/examples/test_spdx2_document_from_scratch.py b/tests/spdx/examples/test_spdx2_document_from_scratch.py index 538610bb6..7c3228d91 100644 --- a/tests/spdx/examples/test_spdx2_document_from_scratch.py +++ b/tests/spdx/examples/test_spdx2_document_from_scratch.py @@ -5,8 +5,7 @@ from datetime import datetime from typing import List -from license_expression import get_spdx_licensing - +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import ( Actor, ActorType, @@ -67,9 +66,9 @@ def test_spdx2_document_from_scratch(): Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"), Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"), ], - license_concluded=get_spdx_licensing().parse("GPL-2.0-only OR MIT"), - license_info_from_files=[get_spdx_licensing().parse("GPL-2.0-only"), get_spdx_licensing().parse("MIT")], - license_declared=get_spdx_licensing().parse("GPL-2.0-only AND MIT"), + license_concluded=spdx_licensing.parse("GPL-2.0-only OR MIT"), + license_info_from_files=[spdx_licensing.parse("GPL-2.0-only"), spdx_licensing.parse("MIT")], + license_declared=spdx_licensing.parse("GPL-2.0-only AND MIT"), license_comment="license comment", copyright_text="Copyright 2022 Jane Doe", description="package description", @@ -102,8 +101,8 @@ def test_spdx2_document_from_scratch(): Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758"), Checksum(ChecksumAlgorithm.MD5, "624c1abb3664f4b35547e7c73864ad24"), ], - license_concluded=get_spdx_licensing().parse("MIT"), - license_info_in_file=[get_spdx_licensing().parse("MIT")], + license_concluded=spdx_licensing.parse("MIT"), + license_info_in_file=[spdx_licensing.parse("MIT")], copyright_text="Copyright 2022 Jane Doe", ) file2 = File( @@ -112,7 +111,7 @@ def test_spdx2_document_from_scratch(): checksums=[ Checksum(ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2759"), ], - license_concluded=get_spdx_licensing().parse("GPL-2.0-only"), + license_concluded=spdx_licensing.parse("GPL-2.0-only"), ) # Assuming the package contains those two files, we create two CONTAINS relationships. diff --git a/tests/spdx/fixtures.py b/tests/spdx/fixtures.py index f0d8f14b7..eebfb0f76 100644 --- a/tests/spdx/fixtures.py +++ b/tests/spdx/fixtures.py @@ -3,8 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 from datetime import datetime -from license_expression import get_spdx_licensing - +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.constants import DOCUMENT_SPDX_ID from spdx_tools.spdx.model import ( Actor, @@ -88,7 +87,7 @@ def file_fixture( spdx_id="SPDXRef-File", checksums=None, file_types=None, - license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"), + license_concluded=spdx_licensing.parse("MIT and GPL-2.0"), license_info_in_file=None, license_comment="licenseComment", copyright_text="copyrightText", @@ -100,7 +99,7 @@ def file_fixture( checksums = [checksum_fixture()] if checksums is None else checksums file_types = [FileType.TEXT] if file_types is None else file_types license_info_in_file = ( - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()] + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()] if license_info_in_file is None else license_info_in_file ) @@ -135,9 +134,9 @@ def package_fixture( checksums=None, homepage="https://homepage.com", source_info="sourceInfo", - license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"), + license_concluded=spdx_licensing.parse("MIT and GPL-2.0"), license_info_from_files=None, - license_declared=get_spdx_licensing().parse("MIT and GPL-2.0"), + license_declared=spdx_licensing.parse("MIT and GPL-2.0"), license_comment="packageLicenseComment", copyright_text="packageCopyrightText", summary="packageSummary", @@ -152,7 +151,7 @@ def package_fixture( ) -> Package: checksums = [checksum_fixture()] if checksums is None else checksums license_info_from_files = ( - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()] + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()] if license_info_from_files is None else license_info_from_files ) @@ -208,7 +207,7 @@ def snippet_fixture( file_spdx_id="SPDXRef-File", byte_range=(1, 2), line_range=(3, 4), - license_concluded=get_spdx_licensing().parse("MIT and GPL-2.0"), + license_concluded=spdx_licensing.parse("MIT and GPL-2.0"), license_info_in_snippet=None, license_comment="snippetLicenseComment", copyright_text="licenseCopyrightText", @@ -217,7 +216,7 @@ def snippet_fixture( attribution_texts=None, ) -> Snippet: license_info_in_snippet = ( - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNone()] + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNone()] if license_info_in_snippet is None else license_info_in_snippet ) diff --git a/tests/spdx/parser/jsonlikedict/test_license_expression_parser.py b/tests/spdx/parser/jsonlikedict/test_license_expression_parser.py index a1364d556..f2177692c 100644 --- a/tests/spdx/parser/jsonlikedict/test_license_expression_parser.py +++ b/tests/spdx/parser/jsonlikedict/test_license_expression_parser.py @@ -4,8 +4,8 @@ from unittest import TestCase import pytest -from license_expression import get_spdx_licensing +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import SpdxNoAssertion, SpdxNone from spdx_tools.spdx.parser.error import SPDXParsingError from spdx_tools.spdx.parser.jsonlikedict.license_expression_parser import LicenseExpressionParser @@ -14,8 +14,8 @@ @pytest.mark.parametrize( "license_expression_str, expected_license", [ - ("First License", get_spdx_licensing().parse("First License")), - ("Second License", get_spdx_licensing().parse("Second License")), + ("First License", spdx_licensing.parse("First License")), + ("Second License", spdx_licensing.parse("Second License")), ("NOASSERTION", SpdxNoAssertion()), ("NONE", SpdxNone()), ], diff --git a/tests/spdx/parser/rdf/test_file_parser.py b/tests/spdx/parser/rdf/test_file_parser.py index 7facfce98..2d99a5d0a 100644 --- a/tests/spdx/parser/rdf/test_file_parser.py +++ b/tests/spdx/parser/rdf/test_file_parser.py @@ -5,9 +5,9 @@ from unittest import TestCase import pytest -from license_expression import get_spdx_licensing from rdflib import RDF, BNode, Graph, URIRef +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import Checksum, ChecksumAlgorithm, FileType, SpdxNoAssertion from spdx_tools.spdx.parser.error import SPDXParsingError from spdx_tools.spdx.parser.rdf.file_parser import parse_file @@ -29,10 +29,10 @@ def test_parse_file(): assert file.comment == "fileComment" assert file.copyright_text == "copyrightText" assert file.contributors == ["fileContributor"] - assert file.license_concluded == get_spdx_licensing().parse("MIT AND GPL-2.0") + assert file.license_concluded == spdx_licensing.parse("MIT AND GPL-2.0") TestCase().assertCountEqual( file.license_info_in_file, - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()], + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()], ) assert file.license_comment == "licenseComment" assert file.notice == "fileNotice" diff --git a/tests/spdx/parser/rdf/test_license_expression_parser.py b/tests/spdx/parser/rdf/test_license_expression_parser.py index 5f3ada8c7..d9e7d5986 100644 --- a/tests/spdx/parser/rdf/test_license_expression_parser.py +++ b/tests/spdx/parser/rdf/test_license_expression_parser.py @@ -4,9 +4,9 @@ import os from unittest import TestCase -from license_expression import get_spdx_licensing from rdflib import RDF, Graph +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.parser.rdf import rdf_parser from spdx_tools.spdx.parser.rdf.license_expression_parser import parse_license_expression from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE @@ -19,7 +19,7 @@ def test_license_expression_parser(): license_expression = parse_license_expression(license_expression_node, graph, "https://some.namespace#") - assert license_expression == get_spdx_licensing().parse("GPL-2.0 AND MIT") + assert license_expression == spdx_licensing.parse("GPL-2.0 AND MIT") def test_license_expression_parser_with_coupled_licenses(): @@ -30,19 +30,19 @@ def test_license_expression_parser_with_coupled_licenses(): packages_by_spdx_id = {package.spdx_id: package for package in doc.packages} files_by_spdx_id = {file.spdx_id: file for file in doc.files} - assert packages_by_spdx_id["SPDXRef-Package"].license_declared == get_spdx_licensing().parse( + assert packages_by_spdx_id["SPDXRef-Package"].license_declared == spdx_licensing.parse( "LGPL-2.0-only AND LicenseRef-3" ) - assert packages_by_spdx_id["SPDXRef-Package"].license_concluded == get_spdx_licensing().parse( + assert packages_by_spdx_id["SPDXRef-Package"].license_concluded == spdx_licensing.parse( "LGPL-2.0-only OR LicenseRef-3" ) TestCase().assertCountEqual( packages_by_spdx_id["SPDXRef-Package"].license_info_from_files, [ - get_spdx_licensing().parse("GPL-2.0"), - get_spdx_licensing().parse("LicenseRef-1"), - get_spdx_licensing().parse("LicenseRef-2"), + spdx_licensing.parse("GPL-2.0"), + spdx_licensing.parse("LicenseRef-1"), + spdx_licensing.parse("LicenseRef-2"), ], ) - assert files_by_spdx_id["SPDXRef-JenaLib"].license_concluded == get_spdx_licensing().parse("LicenseRef-1") + assert files_by_spdx_id["SPDXRef-JenaLib"].license_concluded == spdx_licensing.parse("LicenseRef-1") diff --git a/tests/spdx/parser/rdf/test_package_parser.py b/tests/spdx/parser/rdf/test_package_parser.py index 814ceceee..df1907ad1 100644 --- a/tests/spdx/parser/rdf/test_package_parser.py +++ b/tests/spdx/parser/rdf/test_package_parser.py @@ -5,9 +5,9 @@ from unittest import TestCase import pytest -from license_expression import get_spdx_licensing from rdflib import RDF, BNode, Graph, Literal, URIRef +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import ( Actor, ActorType, @@ -41,11 +41,11 @@ def test_package_parser(): assert package.files_analyzed is True assert package.checksums == [Checksum(ChecksumAlgorithm.SHA1, "71c4025dd9897b364f3ebbb42c484ff43d00791c")] assert package.source_info == "sourceInfo" - assert package.license_concluded == get_spdx_licensing().parse("MIT AND GPL-2.0") - assert package.license_declared == get_spdx_licensing().parse("MIT AND GPL-2.0") + assert package.license_concluded == spdx_licensing.parse("MIT AND GPL-2.0") + assert package.license_declared == spdx_licensing.parse("MIT AND GPL-2.0") TestCase().assertCountEqual( package.license_info_from_files, - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()], + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()], ) assert package.license_comment == "packageLicenseComment" assert package.copyright_text == "packageCopyrightText" diff --git a/tests/spdx/parser/rdf/test_snippet_parser.py b/tests/spdx/parser/rdf/test_snippet_parser.py index da2267221..e3256e4bd 100644 --- a/tests/spdx/parser/rdf/test_snippet_parser.py +++ b/tests/spdx/parser/rdf/test_snippet_parser.py @@ -5,9 +5,9 @@ from unittest import TestCase import pytest -from license_expression import get_spdx_licensing from rdflib import RDF, BNode, Graph, Literal, URIRef +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import SpdxNoAssertion from spdx_tools.spdx.parser.error import SPDXParsingError from spdx_tools.spdx.parser.rdf.snippet_parser import parse_ranges, parse_snippet @@ -26,10 +26,10 @@ def test_parse_snippet(): assert snippet.file_spdx_id == "SPDXRef-File" assert snippet.byte_range == (1, 2) assert snippet.line_range == (3, 4) - assert snippet.license_concluded == get_spdx_licensing().parse("MIT AND GPL-2.0") + assert snippet.license_concluded == spdx_licensing.parse("MIT AND GPL-2.0") TestCase().assertCountEqual( snippet.license_info_in_snippet, - [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse("GPL-2.0"), SpdxNoAssertion()], + [spdx_licensing.parse("MIT"), spdx_licensing.parse("GPL-2.0"), SpdxNoAssertion()], ) assert snippet.license_comment == "snippetLicenseComment" assert snippet.copyright_text == "licenseCopyrightText" diff --git a/tests/spdx/parser/tagvalue/test_file_parser.py b/tests/spdx/parser/tagvalue/test_file_parser.py index 859516cbf..aedf197b5 100644 --- a/tests/spdx/parser/tagvalue/test_file_parser.py +++ b/tests/spdx/parser/tagvalue/test_file_parser.py @@ -2,8 +2,8 @@ # # SPDX-License-Identifier: Apache-2.0 import pytest -from license_expression import get_spdx_licensing +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import FileType, SpdxNoAssertion from spdx_tools.spdx.parser.error import SPDXParsingError from spdx_tools.spdx.parser.tagvalue.parser import Parser @@ -39,8 +39,8 @@ def test_parse_file(): assert spdx_file.attribution_texts == [ "Acknowledgements that might be required to be communicated in some contexts." ] - assert spdx_file.license_info_in_file == [get_spdx_licensing().parse("Apache-2.0"), SpdxNoAssertion()] - assert spdx_file.license_concluded == get_spdx_licensing().parse("Apache-2.0") + assert spdx_file.license_info_in_file == [spdx_licensing.parse("Apache-2.0"), SpdxNoAssertion()] + assert spdx_file.license_concluded == spdx_licensing.parse("Apache-2.0") def test_parse_invalid_file(): diff --git a/tests/spdx/parser/tagvalue/test_package_parser.py b/tests/spdx/parser/tagvalue/test_package_parser.py index dbbeef415..e38351b48 100644 --- a/tests/spdx/parser/tagvalue/test_package_parser.py +++ b/tests/spdx/parser/tagvalue/test_package_parser.py @@ -5,8 +5,8 @@ from unittest import TestCase import pytest -from license_expression import get_spdx_licensing +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.constants import DOCUMENT_SPDX_ID from spdx_tools.spdx.model import ExternalPackageRef, ExternalPackageRefCategory, PackagePurpose, SpdxNone from spdx_tools.spdx.parser.error import SPDXParsingError @@ -57,9 +57,9 @@ def test_parse_package(): assert len(package.license_info_from_files) == 3 TestCase().assertCountEqual( package.license_info_from_files, - [get_spdx_licensing().parse("Apache-1.0"), get_spdx_licensing().parse("Apache-2.0"), SpdxNone()], + [spdx_licensing.parse("Apache-1.0"), spdx_licensing.parse("Apache-2.0"), SpdxNone()], ) - assert package.license_concluded == get_spdx_licensing().parse("LicenseRef-2.0 AND Apache-2.0") + assert package.license_concluded == spdx_licensing.parse("LicenseRef-2.0 AND Apache-2.0") assert package.files_analyzed is True assert package.comment == "Comment on the package." assert len(package.external_references) == 2 diff --git a/tests/spdx/parser/tagvalue/test_snippet_parser.py b/tests/spdx/parser/tagvalue/test_snippet_parser.py index 8bd82595c..79d5de670 100644 --- a/tests/spdx/parser/tagvalue/test_snippet_parser.py +++ b/tests/spdx/parser/tagvalue/test_snippet_parser.py @@ -4,8 +4,8 @@ from unittest import TestCase import pytest -from license_expression import get_spdx_licensing +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import SpdxNoAssertion from spdx_tools.spdx.parser.error import SPDXParsingError from spdx_tools.spdx.parser.tagvalue.parser import Parser @@ -41,7 +41,7 @@ def test_parse_snippet(): assert snippet.copyright_text == " Copyright 2008-2010 John Smith " assert snippet.license_comment == "Some lic comment." assert snippet.file_spdx_id == "SPDXRef-DoapSource" - assert snippet.license_concluded == get_spdx_licensing().parse("Apache-2.0") + assert snippet.license_concluded == spdx_licensing.parse("Apache-2.0") assert snippet.license_info_in_snippet == [SpdxNoAssertion()] assert snippet.byte_range[0] == 310 assert snippet.byte_range[1] == 420 diff --git a/tests/spdx/validation/test_license_expression_validator.py b/tests/spdx/validation/test_license_expression_validator.py index 03c0eddad..cb0ef0c66 100644 --- a/tests/spdx/validation/test_license_expression_validator.py +++ b/tests/spdx/validation/test_license_expression_validator.py @@ -6,8 +6,9 @@ from unittest import TestCase import pytest -from license_expression import LicenseExpression, get_spdx_licensing +from license_expression import LicenseExpression +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.model import Document, SpdxNoAssertion, SpdxNone from spdx_tools.spdx.validation.license_expression_validator import ( validate_license_expression, @@ -29,7 +30,7 @@ ) def test_valid_license_expression(expression_string): document: Document = document_fixture() - license_expression: LicenseExpression = get_spdx_licensing().parse(expression_string) + license_expression: LicenseExpression = spdx_licensing.parse(expression_string) validation_messages: List[ValidationMessage] = validate_license_expression( license_expression, document, parent_id="SPDXRef-File" ) @@ -51,8 +52,8 @@ def test_none_and_no_assertion(expression): [ [SpdxNone()], [SpdxNoAssertion()], - [get_spdx_licensing().parse("MIT and GPL-3.0-only"), get_spdx_licensing().parse(FIXTURE_LICENSE_ID)], - [SpdxNone(), get_spdx_licensing().parse("MIT"), SpdxNoAssertion()], + [spdx_licensing.parse("MIT and GPL-3.0-only"), spdx_licensing.parse(FIXTURE_LICENSE_ID)], + [SpdxNone(), spdx_licensing.parse("MIT"), SpdxNoAssertion()], ], ) def test_valid_license_expressions(expression_list): @@ -72,7 +73,7 @@ def test_valid_license_expressions(expression_list): ) def test_invalid_license_expression_with_unknown_symbols(expression_string, unknown_symbols): document: Document = document_fixture() - license_expression: LicenseExpression = get_spdx_licensing().parse(expression_string) + license_expression: LicenseExpression = spdx_licensing.parse(expression_string) parent_id = "SPDXRef-File" context = ValidationContext( parent_id=parent_id, element_type=SpdxElementType.LICENSE_EXPRESSION, full_element=license_expression @@ -125,7 +126,7 @@ def test_invalid_license_expression_with_unknown_symbols(expression_string, unkn ) def test_invalid_license_expression_with_invalid_exceptions(expression_string, expected_message): document: Document = document_fixture() - license_expression: LicenseExpression = get_spdx_licensing().parse(expression_string) + license_expression: LicenseExpression = spdx_licensing.parse(expression_string) parent_id = "SPDXRef-File" context = ValidationContext( parent_id=parent_id, element_type=SpdxElementType.LICENSE_EXPRESSION, full_element=license_expression diff --git a/tests/spdx/writer/rdf/test_license_expression_writer.py b/tests/spdx/writer/rdf/test_license_expression_writer.py index d77d08ffb..78fbec616 100644 --- a/tests/spdx/writer/rdf/test_license_expression_writer.py +++ b/tests/spdx/writer/rdf/test_license_expression_writer.py @@ -2,16 +2,16 @@ # # SPDX-License-Identifier: Apache-2.0 import pytest -from license_expression import get_spdx_licensing from rdflib import RDF, Graph, Literal, URIRef +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE from spdx_tools.spdx.writer.rdf.license_expression_writer import add_license_expression_to_graph def test_add_conjunctive_license_set_to_graph(): graph = Graph() - license_expression = get_spdx_licensing().parse("MIT AND GPL-2.0") + license_expression = spdx_licensing.parse("MIT AND GPL-2.0") add_license_expression_to_graph( license_expression, graph, URIRef("parentNode"), SPDX_NAMESPACE.licenseConcluded, "https://namespace" @@ -25,7 +25,7 @@ def test_add_conjunctive_license_set_to_graph(): def test_add_disjunctive_license_set_to_graph(): graph = Graph() - license_expression = get_spdx_licensing().parse("MIT OR GPL-2.0") + license_expression = spdx_licensing.parse("MIT OR GPL-2.0") add_license_expression_to_graph( license_expression, graph, URIRef("parentNode"), SPDX_NAMESPACE.licenseConcluded, "https://namespace" @@ -49,7 +49,7 @@ def test_add_disjunctive_license_set_to_graph(): ) def test_license_exception_to_graph(license_with_exception, expected_triple): graph = Graph() - license_expression = get_spdx_licensing().parse(license_with_exception) + license_expression = spdx_licensing.parse(license_with_exception) add_license_expression_to_graph( license_expression, graph, URIRef("parentNode"), SPDX_NAMESPACE.licenseConcluded, "https://namespace" diff --git a/tests/spdx3/bump/test_license_expression_bump.py b/tests/spdx3/bump/test_license_expression_bump.py index 6f3b8aa20..0f63299cf 100644 --- a/tests/spdx3/bump/test_license_expression_bump.py +++ b/tests/spdx3/bump/test_license_expression_bump.py @@ -2,8 +2,9 @@ # # SPDX-License-Identifier: Apache-2.0 import pytest -from license_expression import LicenseExpression, get_spdx_licensing +from license_expression import LicenseExpression +from spdx_tools.common.spdx_licensing import spdx_licensing from spdx_tools.spdx3.bump_from_spdx2.license_expression import ( bump_license_expression, bump_license_expression_or_none_or_no_assertion, @@ -28,7 +29,7 @@ [ (SpdxNoAssertion(), NoAssertionLicense), (SpdxNone(), NoneLicense), - (get_spdx_licensing().parse("MIT"), ListedLicense), + (spdx_licensing.parse("MIT"), ListedLicense), ], ) def test_license_expression_or_none_or_no_assertion(element, expected_class): @@ -40,22 +41,22 @@ def test_license_expression_or_none_or_no_assertion(element, expected_class): @pytest.mark.parametrize( "license_expression, extracted_licensing_info, expected_element", [ - (get_spdx_licensing().parse("MIT"), [], ListedLicense("MIT", "MIT", "blank")), - (get_spdx_licensing().parse("LGPL-2.0"), [], ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank")), + (spdx_licensing.parse("MIT"), [], ListedLicense("MIT", "MIT", "blank")), + (spdx_licensing.parse("LGPL-2.0"), [], ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank")), ( - get_spdx_licensing().parse("LicenseRef-1"), + spdx_licensing.parse("LicenseRef-1"), [extracted_licensing_info_fixture()], CustomLicense("LicenseRef-1", "licenseName", "extractedText"), ), ( - get_spdx_licensing().parse("MIT AND LGPL-2.0"), + spdx_licensing.parse("MIT AND LGPL-2.0"), [], ConjunctiveLicenseSet( [ListedLicense("MIT", "MIT", "blank"), ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank")] ), ), ( - get_spdx_licensing().parse("LicenseRef-1 OR LGPL-2.0"), + spdx_licensing.parse("LicenseRef-1 OR LGPL-2.0"), [extracted_licensing_info_fixture()], DisjunctiveLicenseSet( [ @@ -65,7 +66,7 @@ def test_license_expression_or_none_or_no_assertion(element, expected_class): ), ), ( - get_spdx_licensing().parse("LGPL-2.0 WITH 389-exception"), + spdx_licensing.parse("LGPL-2.0 WITH 389-exception"), [], WithAdditionOperator( ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank"), @@ -73,7 +74,7 @@ def test_license_expression_or_none_or_no_assertion(element, expected_class): ), ), ( - get_spdx_licensing().parse("LicenseRef-1 WITH custom-exception"), + spdx_licensing.parse("LicenseRef-1 WITH custom-exception"), [ extracted_licensing_info_fixture(), extracted_licensing_info_fixture("custom-exception", "This is a custom exception", "exceptionName"), @@ -84,7 +85,7 @@ def test_license_expression_or_none_or_no_assertion(element, expected_class): ), ), ( - get_spdx_licensing().parse("MIT AND LicenseRef-1 WITH custom-exception"), + spdx_licensing.parse("MIT AND LicenseRef-1 WITH custom-exception"), [ extracted_licensing_info_fixture(), extracted_licensing_info_fixture("custom-exception", "This is a custom exception", "exceptionName"),