diff --git a/src/ros_license_toolkit/license_checks/license_file_referenced_check.py b/src/ros_license_toolkit/license_checks/license_file_referenced_check.py index e3311ea..a657ff1 100644 --- a/src/ros_license_toolkit/license_checks/license_file_referenced_check.py +++ b/src/ros_license_toolkit/license_checks/license_file_referenced_check.py @@ -26,44 +26,54 @@ class LicenseFilesReferencedCheck(Check): """Check if all found License file have a reference in package.xml.""" + def __init__(self: 'LicenseFilesReferencedCheck'): + Check.__init__(self) + self.not_covered_texts: Dict[str, str] = {} + self.inofficial_covered_texts: Dict[str, List[str]] = {} + def _check(self, package: Package): - not_covered_texts: Dict[str, str] = {} - inofficial_covered_texts: Dict[str, List[str]] = {} for filename, license_text in package.found_license_texts.items(): # skipping all declarations above the package if not is_in_package(package, filename): continue - if 'detected_license_expression_spdx' in license_text and \ - license_text['detected_license_expression_spdx'] not in \ - package.license_tags: - spdx_expression = license_text[ - 'detected_license_expression_spdx'] - inofficial_licenses = { - lic_tag.id_from_license_text: key - for key, lic_tag in package.license_tags.items() - if lic_tag.id_from_license_text != ''} - if spdx_expression in inofficial_licenses: - inofficial_covered_texts[filename] = \ - [spdx_expression, - inofficial_licenses[spdx_expression]] - else: - not_covered_texts[filename] = \ - spdx_expression - if not_covered_texts: + self._handle_inofficial_licenses(package, filename, license_text) + self._evaluate_results() + + def _handle_inofficial_licenses(self, package: Package, + filename, license_text): + if 'detected_license_expression_spdx' in license_text and \ + license_text['detected_license_expression_spdx'] not in \ + package.license_tags: + spdx_expression = license_text[ + 'detected_license_expression_spdx'] + inofficial_licenses = { + lic_tag.id_from_license_text: key + for key, lic_tag in package.license_tags.items() + if lic_tag.id_from_license_text != ''} + if spdx_expression in inofficial_licenses: + self.inofficial_covered_texts[filename] = \ + [spdx_expression, + inofficial_licenses[spdx_expression]] + else: + self.not_covered_texts[filename] = \ + spdx_expression + + def _evaluate_results(self): + if self.not_covered_texts: info_str = '' info_str += 'The following license files are not' +\ ' mentioned by any tag:\n' +\ '\n'.join( [f" '{x[0]}' is of {x[1]}." - for x in not_covered_texts.items()]) + for x in self.not_covered_texts.items()]) self._failed(info_str) - elif inofficial_covered_texts: + elif self.inofficial_covered_texts: info_str = '' info_str += 'The following license files are not' +\ ' mentioned by any tag:\n' +\ '\n'.join( [f" '{x[0]}' is of {x[1][0]} but its tag is {x[1][1]}." - for x in inofficial_covered_texts.items()]) + for x in self.inofficial_covered_texts.items()]) self._warning(info_str) else: self._success("All license declaration are referenced by a tag.")