Skip to content

Commit 0935356

Browse files
authored
Added a way to describe package's licence manual check (#50)
1 parent 5330dc1 commit 0935356

File tree

6 files changed

+104
-22
lines changed

6 files changed

+104
-22
lines changed

mbed_tools_ci_scripts/spdx_report/spdx_helpers.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import toml
2020
from pathlib import Path
2121
from spdx.utils import SPDXNone, UnKnown
22-
from typing import Union, Optional, Iterator, Iterable, Any
22+
from typing import Union, Optional, Iterator, Any, Tuple
2323

2424
from mbed_tools_ci_scripts.utils.configuration import ConfigurationVariable, configuration
2525
from mbed_tools_ci_scripts.utils.definitions import UNKNOWN
@@ -106,21 +106,39 @@ def ignore_path(p: Path) -> bool:
106106
return list_all_files(project_root, ignore_path)
107107

108108

109-
def determine_checked_packages_from_string(checked_packages: Any) -> Iterable[Any]:
110-
"""Determines the list of packages for which the licence has been checked."""
109+
def _convert_list_into_dict(checked_packages: Any) -> dict:
110+
checked_package_description = dict()
111+
for item in checked_packages:
112+
info = item.split("=" if "=" in item else ":")
113+
checked_package_description[info[0].strip()] = info[-1].strip() if len(info) > 1 else None
114+
return checked_package_description
115+
116+
117+
def determine_checked_packages_from_configuration_entry(checked_packages: Any) -> dict:
118+
"""Determines the list of packages for which the licence has been manually checked."""
111119
if isinstance(checked_packages, str):
112120
checked_packages = checked_packages.split(", ")
113-
if isinstance(checked_packages, (list, dict, tuple, set)):
114-
yield from checked_packages
121+
if isinstance(checked_packages, (list, tuple, set)):
122+
return _convert_list_into_dict(checked_packages)
123+
if isinstance(checked_packages, dict):
124+
return checked_packages
125+
return dict()
115126

116127

117-
def get_packages_with_checked_licence() -> Iterable[str]:
128+
def get_packages_with_checked_licence() -> dict:
118129
"""Determines the list of packages for which the licence has been checked from configuration."""
119-
yield from determine_checked_packages_from_string(
130+
return determine_checked_packages_from_configuration_entry(
120131
configuration.get_value(ConfigurationVariable.PACKAGES_WITH_CHECKED_LICENCE)
121132
)
122133

123134

124-
def is_package_licence_checked(package_name: str) -> bool:
125-
"""States whether the licence of a package has been checked and hence, that its licence is compliant."""
126-
return package_name.strip() in get_packages_with_checked_licence()
135+
def get_package_manual_check(package_name: str) -> Tuple[bool, Optional[str]]:
136+
"""Gets information about package licence manual check."""
137+
checked_packages = get_packages_with_checked_licence()
138+
return bool(package_name.strip() in checked_packages), checked_packages.get(package_name.strip())
139+
140+
141+
def is_package_licence_manually_checked(package_name: str) -> bool:
142+
"""States whether the licence of a package has been manually checked and hence, that its licence is compliant."""
143+
checked, _ = get_package_manual_check(package_name)
144+
return checked

mbed_tools_ci_scripts/spdx_report/spdx_project.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from mbed_tools_ci_scripts.spdx_report.spdx_document import SpdxDocument
1414
from mbed_tools_ci_scripts.utils.hash_helpers import determine_sha1_hash_of_file
1515
from mbed_tools_ci_scripts.utils.package_helpers import ProjectMetadataParser
16-
from mbed_tools_ci_scripts.spdx_report.spdx_helpers import is_package_licence_checked
16+
from mbed_tools_ci_scripts.spdx_report.spdx_helpers import is_package_licence_manually_checked
1717
from mbed_tools_ci_scripts.spdx_report.spdx_summary import SummaryGenerator
1818

1919

@@ -126,7 +126,7 @@ def _report_issues(self, issues: Dict[str, str]) -> None:
126126

127127
def _check_one_licence_compliance(self, spdx_document: SpdxDocument, issues: Dict[str, str]) -> None:
128128
main_valid, actual_valid, name, main_licence, actual_licence = _check_package_licence(spdx_document)
129-
if not ((main_valid and actual_valid) or is_package_licence_checked(name)):
129+
if not ((main_valid and actual_valid) or is_package_licence_manually_checked(name)):
130130
issues[name] = actual_licence if main_valid else main_licence
131131

132132
def _check_package_dependencies_licence_compliance(self, issues: Dict[str, str]) -> None:

mbed_tools_ci_scripts/spdx_report/spdx_summary.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
#
2+
# Copyright (C) 2020 Arm Mbed. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
15
"""Summary generators."""
26
import datetime
37
import jinja2
48
import logging
5-
import os
69
from pathlib import Path
710
from typing import List, Tuple, Optional, Dict, Any
811

9-
from mbed_tools_ci_scripts.spdx_report.spdx_helpers import is_package_licence_checked
12+
from mbed_tools_ci_scripts.spdx_report.spdx_helpers import get_package_manual_check
1013
from mbed_tools_ci_scripts.spdx_report.spdx_package import SpdxPackage
1114

1215
JINJA_TEMPLATE_SUMMARY_HTML = "third_party_IP_report.html.jinja2"
@@ -60,7 +63,7 @@ def _generate_template_arguments(self) -> Dict[str, Any]:
6063
"name": self.project.name,
6164
"compliance": global_compliance,
6265
"compliance_details": (
63-
f"Project [{self.project.name}]'s licence is compliant: {self.project.licence}.{os.linesep}"
66+
f"Project [{self.project.name}]'s licence is compliant: {self.project.licence}."
6467
"All its dependencies are also compliant licence-wise."
6568
)
6669
if global_compliance
@@ -76,19 +79,24 @@ def _generate_packages_description(self) -> Tuple[bool, dict]:
7679
for p in self.all_packages:
7780
main_licence_valid = p.is_main_licence_accepted
7881
actual_licence_valid = p.is_licence_accepted
79-
package_checked = is_package_licence_checked(p.name)
82+
package_manually_checked, manual_check_details = get_package_manual_check(p.name)
8083
is_licence_compliant = main_licence_valid and actual_licence_valid
81-
is_compliant = is_licence_compliant or package_checked
84+
is_compliant = is_licence_compliant or package_manually_checked
8285
if not is_compliant:
8386
global_compliance = False
8487
description_list[p.name] = self._generate_description_for_one_package(
85-
is_compliant, is_licence_compliant, package_checked, p
88+
is_compliant, is_licence_compliant, package_manually_checked, manual_check_details, p
8689
)
8790

8891
return global_compliance, description_list
8992

9093
def _generate_description_for_one_package(
91-
self, is_compliant: bool, is_licence_compliant: bool, package_checked: bool, p: SpdxPackage
94+
self,
95+
is_compliant: bool,
96+
is_licence_compliant: bool,
97+
package_manually_checked: bool,
98+
manual_check_details: Optional[str],
99+
p: SpdxPackage,
92100
) -> dict:
93101
return {
94102
"name": p.name,
@@ -100,8 +108,8 @@ def _generate_description_for_one_package(
100108
"licence_compliance_details": "Licence is compliant."
101109
if is_licence_compliant
102110
else (
103-
"Package's licence has been checked"
104-
if package_checked
111+
f"Package's licence manually checked: {manual_check_details}"
112+
if package_manually_checked
105113
else "Licence is not compliant according to project's configuration."
106114
),
107115
}

news/20200424.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Changed the configuration type entry for package licence compliance check.

pyproject.toml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ DOCUMENTATION_PRODUCTION_OUTPUT_PATH = "docs"
1818
VERSION_FILE_PATH = "mbed_tools_ci_scripts/_version.py"
1919
CHANGELOG_FILE_PATH = "CHANGELOG.md"
2020
AWS_BUCKET=""
21-
PACKAGES_WITH_CHECKED_LICENCE=["jeepney", "chardet", "zipp", "python-dateutil", "keyring", "setuptools", "GitPython", "pdoc3", "python-dotenv", "twine", "jellyfish"]
21+
22+
[ProjectConfig.PACKAGES_WITH_CHECKED_LICENCE]
23+
jeepney="MIT"
24+
chardet="LGPL-2.1+ but accepted for this project since not distributed."
25+
zipp="MIT"
26+
python-dateutil="All contributions after December 1, 2017 released under dual license - either Apache 2.0 License or the BSD 3-Clause License."
27+
keyring="MIT"
28+
setuptools="MIT"
29+
GitPython="BSD-3-Clause"
30+
pdoc3="Accepted for this project since not distributed"
31+
python-dotenv="BSD-3-Clause"
32+
twine="Apache-2.0"
33+
jellyfish="BSD-2-Clause"
2234

2335
[AutoVersionConfig]
2436
CONFIG_NAME = "DEFAULT"

tests/spdx/test_spdx_helper.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# Copyright (C) 2020 Arm Mbed. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
from unittest import TestCase
6+
7+
from mbed_tools_ci_scripts.spdx_report.spdx_helpers import determine_checked_packages_from_configuration_entry
8+
9+
10+
class TestSpdxHelpers(TestCase):
11+
def test_parse_configuration_entry(self):
12+
expected_dictionary = dict(package1="Checked", package2="Accepted licence : MIT", package3="!!!!!")
13+
self.assertDictEqual(
14+
expected_dictionary, determine_checked_packages_from_configuration_entry(expected_dictionary)
15+
)
16+
self.assertDictEqual(
17+
expected_dictionary,
18+
determine_checked_packages_from_configuration_entry(
19+
"package1 = Checked, package2= Accepted licence : MIT , package3=!!!!! "
20+
),
21+
)
22+
self.assertDictEqual(
23+
expected_dictionary,
24+
determine_checked_packages_from_configuration_entry(
25+
["package1 = Checked", " package2= Accepted licence : MIT ", " package3=!!!!! "]
26+
),
27+
)
28+
expected_dictionary = dict(package1="Checked", package2="Accepted licence (MIT)", package3="!!!!!")
29+
self.assertDictEqual(
30+
expected_dictionary, determine_checked_packages_from_configuration_entry(expected_dictionary)
31+
)
32+
self.assertDictEqual(
33+
expected_dictionary,
34+
determine_checked_packages_from_configuration_entry(
35+
"package1 : Checked, package2= Accepted licence (MIT) , package3:!!!!! "
36+
),
37+
)
38+
self.assertDictEqual(
39+
expected_dictionary,
40+
determine_checked_packages_from_configuration_entry(
41+
["package1 : Checked", " package2: Accepted licence (MIT) ", " package3=!!!!! "]
42+
),
43+
)

0 commit comments

Comments
 (0)