Skip to content

Commit

Permalink
created own files for checks
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Utz <[email protected]>
  • Loading branch information
ant-u committed Mar 20, 2024
1 parent 011f104 commit 53c4fe1
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 172 deletions.
158 changes: 2 additions & 156 deletions src/ros_license_toolkit/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import os
from enum import IntEnum
from pprint import pformat
from typing import Any, Dict, List, Optional
from typing import Any, Dict, Optional

from ros_license_toolkit.common import get_spdx_license_name, is_in_package
from ros_license_toolkit.common import get_spdx_license_name
from ros_license_toolkit.license_tag import (LicenseTag,
is_license_name_in_spdx_list)
from ros_license_toolkit.package import Package, PackageException
Expand Down Expand Up @@ -239,156 +238,3 @@ def _evaluate_results(self):
self.found_license_texts.items()]))
else:
self._success("All license tags have a valid license text file.")


class LicensesInCodeCheck(Check):
"""Check if all found licenses have a declaration in the package.xml."""

def __init__(self: 'LicensesInCodeCheck'):
Check.__init__(self)
self.declared_licenses: Dict[str, LicenseTag] = {}
self.files_with_uncovered_licenses: Dict[str, List[str]] = {}
self.files_not_matched_by_any_license_tag: Dict[str, List[str]] = {}
self.files_with_inofficial_tag: Dict[str, List[str]] = {}

def _check(self, package: Package):
if len(package.license_tags) == 0:
self._failed('No license tag defined.')
return
self.declared_licenses = package.license_tags
self._check_license_files(package)
self._evaluate_result(package)

def _check_license_files(self, package: Package) -> None:
for fname, found_licenses in package.found_files_w_licenses.items():
if fname in package.get_license_files():
# the actual license text files are not relevant for this
continue
found_licenses_str = found_licenses[
'detected_license_expression_spdx']
licenses = found_licenses_str.split(' AND ')
for license_str in licenses:
if license_str not in self.declared_licenses:
# this license has an inofficial tag
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 license_str in inofficial_licenses.keys():
if fname not in self.files_with_inofficial_tag:
self.files_with_inofficial_tag[fname] = []
self.files_with_inofficial_tag[fname].append(
license_str)
self.files_with_inofficial_tag[fname].append(
inofficial_licenses[license_str])
continue
# this license is not declared by any license tag
if fname not in self.files_with_uncovered_licenses:
self.files_with_uncovered_licenses[fname] = []
self.files_with_uncovered_licenses[fname].append(
license_str)
continue
if fname not in self.declared_licenses[
license_str].source_files:
# this license is declared by a license tag but the file
# is not listed in the source files of the license tag
if fname not in self.files_not_matched_by_any_license_tag:
self.files_not_matched_by_any_license_tag[fname] = []
self.files_not_matched_by_any_license_tag[fname].append(
license_str)
continue

def _evaluate_result(self, package: Package) -> None:
if self.files_with_uncovered_licenses:
info_str = ''
info_str += '\nThe following files contain licenses that ' +\
'are not covered by any license tag:\n' + '\n'.join(
[f" '{x[0]}': {x[1]}" for x in
self.files_with_uncovered_licenses.items()])
self._print_info(
info_str,
self.files_with_uncovered_licenses,
self.files_not_matched_by_any_license_tag,
package,
)
elif self.files_with_inofficial_tag:
info_str = ''
info_str += 'For the following files, please change the ' +\
'License Tag in the package file to SPDX format:\n' +\
'\n'.join(
[f" '{x[0]}' is of {x[1][0]} but its Tag is {x[1][1]}."
for x in self.files_with_inofficial_tag.items()])
self._warning(info_str)
elif len(self.files_not_matched_by_any_license_tag) > 0:
info_str = ''
info_str += '\nThe following files contain licenses that ' +\
'are covered by a license tag but are not listed in ' +\
'the source files of the license tag:\n' + '\n'.join(
[f" '{x[0]}': {x[1]}" for x in
self.files_not_matched_by_any_license_tag.items()])
self._print_info(
info_str,
self.files_with_uncovered_licenses,
self.files_not_matched_by_any_license_tag,
package,
)
else:
self._success('All licenses found in the code are covered by a '
'license declaration.')

def _print_info(self, info_str, files_with_uncovered_licenses,
files_not_matched_by_any_license_tag, package):
assert info_str != ''
self._failed(info_str)
self.verbose_output = red(
'\n Relevant scan results:\n' + pformat(
list(filter(
lambda x: x[0] in files_with_uncovered_licenses or (
x[0] in files_not_matched_by_any_license_tag),
package.found_files_w_licenses.items()))))


class LicenseFilesReferencedCheck(Check):
"""Check if all found License file have a reference in package.xml."""

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:
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()])
self._failed(info_str)
elif 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()])
self._warning(info_str)
else:
self._success("All license declaration are referenced by a tag.")
13 changes: 0 additions & 13 deletions src/ros_license_toolkit/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import os
from typing import Any, Dict, List, Optional

from ros_license_toolkit.package import Package

REQUIRED_PERCENTAGE_OF_LICENSE_TEXT = 95.0

# files we ignore in scan results
Expand Down Expand Up @@ -59,14 +57,3 @@ def get_ignored_content(pkg_abspath: str) -> List[str]:
if pattern not in ignored_content:
ignored_content.append(pattern)
return ignored_content


def is_in_package(package: Package, file: str) -> bool:
"""Return TRUE if the file is underneath the absolute package path.
Return FALSE if file is located above package."""
parent = os.path.abspath(package.abspath)
child = os.path.abspath(package.abspath + '/' + file)

comm_parent = os.path.commonpath([parent])
comm_child_parent = os.path.commonpath([parent, child])
return comm_parent == comm_child_parent
80 changes: 80 additions & 0 deletions src/ros_license_toolkit/license_file_referenced_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) 2024 - for information on the respective copyright owner
# see the NOTICE file and/or the repository
# https://github.com/boschresearch/ros_license_toolkit

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Check for all License files are referenced by a tag in package.xml"""

import os
from typing import Dict, List

from ros_license_toolkit.checks import Check
from ros_license_toolkit.package import Package


class LicenseFilesReferencedCheck(Check):
"""Check if all found License file have a reference in package.xml."""

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:
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()])
self._failed(info_str)
elif 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()])
self._warning(info_str)
else:
self._success("All license declaration are referenced by a tag.")


def is_in_package(package: Package, file: str) -> bool:
"""Return TRUE if the file is underneath the absolute package path.
Return FALSE if file is located above package."""
parent = os.path.abspath(package.abspath)
child = os.path.abspath(package.abspath + '/' + file)

comm_parent = os.path.commonpath([parent])
comm_child_parent = os.path.commonpath([parent, child])
return comm_parent == comm_child_parent
Loading

0 comments on commit 53c4fe1

Please sign in to comment.