Skip to content

Commit

Permalink
updated to latest versions of spdx tools and scancode-toolkit. Remove…
Browse files Browse the repository at this point in the history
…d functionality of translating name to tag, therefore adapted some tests.

Signed-off-by: Anton Utz <[email protected]>
  • Loading branch information
ant-u committed Apr 4, 2024
1 parent 13b8561 commit f10f821
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 33 deletions.
39 changes: 10 additions & 29 deletions src/ros_license_toolkit/license_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,12 @@
from glob import glob
from typing import Any, Dict, List, Optional, Set

from spdx.config import LICENSE_MAP
from license_expression import get_spdx_licensing


def is_license_name_in_spdx_list(license_name: str) -> bool:
"""Check if a license name is in the SPDX list of licenses."""
return license_name in LICENSE_MAP or \
license_name in LICENSE_MAP.values()


def to_spdx_license_tag(license_name: str) -> str:
"""Convert a license name to a SPDX license tag
(assuming it is shorter than the name).
This is because the dict from spdx.config.LICENSE_MAP
contains both pairings (tag, name) and (name, tag).
"""
for tag, name in LICENSE_MAP.items():
if license_name in [tag, name]:
if len(tag) < len(name):
return tag
# else
return name
raise ValueError("License name not in SPDX list.")
return license_name in get_spdx_licensing().known_symbols


def _eval_glob(glob_str: str, pkg_path: str) -> Set[str]:
Expand Down Expand Up @@ -74,17 +58,14 @@ def __init__(self, element: ET.Element,
# be found out through declaration, this field contains the tag
self.id_from_license_text: Optional[str] = None

try:
self.id = to_spdx_license_tag(raw_license_name)
except ValueError:
# If the license name is not in the SPDX list,
# we assume it is a custom license and use the name as-is.
# This will be detected in `LicenseTagIsInSpdxListCheck`.
self.id = raw_license_name
# If a file is linked to the tag, set its id for internal checks
if license_file_scan_results:
self.id_from_license_text = \
get_id_from_license_text(license_file_scan_results)
# If the license name is not in the SPDX list,
# we assume it is a custom license and use the name as-is.
# This will be detected in `LicenseTagIsInSpdxListCheck`.
self.id = raw_license_name
# If a file is linked to the tag, set its id for internal checks
if license_file_scan_results:
self.id_from_license_text = \
get_id_from_license_text(license_file_scan_results)

# Path to the file containing the license text
# (relative to package root)
Expand Down
2 changes: 1 addition & 1 deletion test/_test_data/copyright_file_contents/test_pkg_spdx_name
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Upstream-Name: test_pkg_spdx_name
Files:
**
Copyright: 2003 Lawrence E. Rosen
License: AFL-2.0
License: Academic Free License v2.0
The Academic Free License
v. 2.0

Expand Down
3 changes: 0 additions & 3 deletions test/unittest/test_license_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ def test_init(self):
by_spdx_tag = LicenseTag(ET.fromstring(
"<license>Apache-2.0</license>"), "")
self.assertEqual(by_spdx_tag.id, "Apache-2.0")
by_spdx_name = LicenseTag(ET.fromstring(
"<license>Apache License 2.0</license>"), "")
self.assertEqual(by_spdx_name.id, "Apache-2.0")


if __name__ == '__main__':
Expand Down

0 comments on commit f10f821

Please sign in to comment.