|
| 1 | +"""Summary generators.""" |
| 2 | +import datetime |
| 3 | +import jinja2 |
| 4 | +import logging |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +from typing import List, Tuple, Optional, Dict, Any |
| 8 | + |
| 9 | +from mbed_tools_ci_scripts.spdx_report.spdx_helpers import is_package_licence_checked |
| 10 | +from mbed_tools_ci_scripts.spdx_report.spdx_package import SpdxPackage |
| 11 | + |
| 12 | +JINJA_TEMPLATE_SUMMARY_HTML = "third_party_IP_report.html.jinja2" |
| 13 | +JINJA_TEMPLATE_SUMMARY_CSV = "third_party_IP_report.csv.jinja2" |
| 14 | +JINJA_TEMPLATE_SUMMARY_TEXT = "third_party_IP_report.txt.jinja2" |
| 15 | +JINJA_TEMPLATES = [JINJA_TEMPLATE_SUMMARY_HTML, JINJA_TEMPLATE_SUMMARY_CSV, JINJA_TEMPLATE_SUMMARY_TEXT] |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | +try: |
| 18 | + jinja2_env = jinja2.Environment( |
| 19 | + loader=jinja2.PackageLoader("mbed_tools_ci_scripts.spdx_report.spdx_summary", "templates"), |
| 20 | + autoescape=jinja2.select_autoescape(["html", "xml"]), |
| 21 | + ) |
| 22 | +except ModuleNotFoundError as e: |
| 23 | + logger.error(e) |
| 24 | + |
| 25 | + |
| 26 | +def generate_file_based_on_template( |
| 27 | + output_dir: Path, template_name: str, template_args: dict, suffix: str = None |
| 28 | +) -> None: |
| 29 | + """Write file based on template and arguments.""" |
| 30 | + logger.info("Loading template '%s'.", template_name) |
| 31 | + template = jinja2_env.get_template(template_name) |
| 32 | + filename = Path(template_name.rsplit(".", 1)[0]) |
| 33 | + if suffix: |
| 34 | + filename = Path( |
| 35 | + "{0}_{2}{1}".format( |
| 36 | + *(str(filename.name), str(filename.suffix), str(suffix.replace(".", "_").replace("-", "_")),) |
| 37 | + ) |
| 38 | + ) |
| 39 | + output_filename = output_dir.joinpath(filename) |
| 40 | + rendered = template.render(**template_args) |
| 41 | + logger.info("Writing to '%s'.", output_filename) |
| 42 | + output_filename.write_text(rendered, encoding="utf8") |
| 43 | + |
| 44 | + |
| 45 | +class SummaryGenerator: |
| 46 | + """Licensing summary generator.""" |
| 47 | + |
| 48 | + def __init__(self, project_package: SpdxPackage, dependencies_documents: List[SpdxPackage]) -> None: |
| 49 | + """Initialiser.""" |
| 50 | + self.project = project_package |
| 51 | + self.all_packages = list(dependencies_documents) |
| 52 | + self.all_packages.append(self.project) |
| 53 | + self._template_arguments: Optional[dict] = None |
| 54 | + |
| 55 | + def _generate_template_arguments(self) -> Dict[str, Any]: |
| 56 | + arguments: Dict[str, Any] = dict() |
| 57 | + |
| 58 | + global_compliance, description_list = self._generate_packages_description() |
| 59 | + arguments["project"] = { |
| 60 | + "name": self.project.name, |
| 61 | + "compliance": global_compliance, |
| 62 | + "compliance_details": ( |
| 63 | + f"Project [{self.project.name}]'s licence is compliant: {self.project.licence}.{os.linesep}" |
| 64 | + "All its dependencies are also compliant licence-wise." |
| 65 | + ) |
| 66 | + if global_compliance |
| 67 | + else f"Project [{self.project.name}] or one, at least, of its dependencies has a non compliant licence", |
| 68 | + } |
| 69 | + arguments["packages"] = description_list |
| 70 | + arguments["render_time"] = datetime.datetime.now() |
| 71 | + return arguments |
| 72 | + |
| 73 | + def _generate_packages_description(self) -> Tuple[bool, dict]: |
| 74 | + description_list = dict() |
| 75 | + global_compliance = True |
| 76 | + for p in self.all_packages: |
| 77 | + main_licence_valid = p.is_main_licence_accepted |
| 78 | + actual_licence_valid = p.is_licence_accepted |
| 79 | + package_checked = is_package_licence_checked(p.name) |
| 80 | + is_licence_compliant = main_licence_valid and actual_licence_valid |
| 81 | + is_compliant = is_licence_compliant or package_checked |
| 82 | + if not is_compliant: |
| 83 | + global_compliance = False |
| 84 | + description_list[p.name] = self._generate_description_for_one_package( |
| 85 | + is_compliant, is_licence_compliant, package_checked, p |
| 86 | + ) |
| 87 | + |
| 88 | + return global_compliance, description_list |
| 89 | + |
| 90 | + def _generate_description_for_one_package( |
| 91 | + self, is_compliant: bool, is_licence_compliant: bool, package_checked: bool, p: SpdxPackage |
| 92 | + ) -> dict: |
| 93 | + return { |
| 94 | + "name": p.name, |
| 95 | + "is_dependency": p.is_dependency, |
| 96 | + "url": p.url, |
| 97 | + "licence": p.licence, |
| 98 | + "is_compliant": is_compliant, |
| 99 | + "mark_as_problematic": not is_licence_compliant, |
| 100 | + "licence_compliance_details": "Licence is compliant." |
| 101 | + if is_licence_compliant |
| 102 | + else ( |
| 103 | + "Package's licence has been checked" |
| 104 | + if package_checked |
| 105 | + else "Licence is not compliant according to project's configuration." |
| 106 | + ), |
| 107 | + } |
| 108 | + |
| 109 | + @property |
| 110 | + def template_arguments(self) -> dict: |
| 111 | + """Gets template arguments.""" |
| 112 | + if not self._template_arguments: |
| 113 | + self._template_arguments = self._generate_template_arguments() |
| 114 | + return self._template_arguments |
| 115 | + |
| 116 | + def generate_summary(self, dir: Path) -> None: |
| 117 | + """Generates a licensing summary into the specified directory. |
| 118 | +
|
| 119 | + Args: |
| 120 | + dir: output directory |
| 121 | + """ |
| 122 | + for t in JINJA_TEMPLATES: |
| 123 | + generate_file_based_on_template(dir, t, self.template_arguments) |
0 commit comments