|
6 | 6 | import argparse |
7 | 7 | import glob |
8 | 8 | import os |
9 | | - |
10 | | -import infer_license |
| 9 | +from typing import List, Tuple |
11 | 10 |
|
12 | 11 | import dfetch.commands.command |
13 | 12 | import dfetch.manifest.manifest |
|
17 | 16 | from dfetch.project.metadata import Metadata |
18 | 17 | from dfetch.project.vcs import VCS |
19 | 18 | from dfetch.reporting import REPORTERS, ReportTypes |
| 19 | +from dfetch.util.license import guess_license_in_file |
20 | 20 |
|
21 | 21 | logger = get_logger(__name__) |
22 | 22 |
|
| 23 | +LICENSE_PROBABILITY_THRESHOLD = 0.80 |
| 24 | + |
23 | 25 |
|
24 | 26 | class Report(dfetch.commands.command.Command): |
25 | 27 | """Generate reports containing information about the projects components. |
@@ -66,37 +68,38 @@ def __call__(self, args: argparse.Namespace) -> None: |
66 | 68 |
|
67 | 69 | with dfetch.util.util.in_directory(os.path.dirname(path)): |
68 | 70 | for project in manifest.selected_projects(args.projects): |
69 | | - determined_license = self._determine_license(project) |
| 71 | + determined_licenses = self._determine_licenses(project) |
70 | 72 | version = self._determine_version(project) |
71 | 73 | reporter.add_project( |
72 | | - project=project, license_name=determined_license, version=version |
| 74 | + project=project, license_names=determined_licenses, version=version |
73 | 75 | ) |
74 | 76 |
|
75 | 77 | if reporter.dump_to_file(args.outfile): |
76 | 78 | logger.info(f"Generated {reporter.name} report: {args.outfile}") |
77 | 79 |
|
78 | 80 | @staticmethod |
79 | | - def _determine_license(project: ProjectEntry) -> str: |
| 81 | + def _determine_licenses(project: ProjectEntry) -> List[Tuple[str, float]]: |
80 | 82 | """Try to determine license of fetched project.""" |
81 | 83 | if not os.path.exists(project.destination): |
82 | 84 | logger.print_warning_line( |
83 | 85 | project.name, "Never fetched, fetch it to get license info." |
84 | 86 | ) |
85 | | - return "" |
| 87 | + return [] |
86 | 88 |
|
| 89 | + license_files = [] |
87 | 90 | with dfetch.util.util.in_directory(project.destination): |
| 91 | + |
88 | 92 | for license_file in filter(VCS.is_license_file, glob.glob("*")): |
89 | 93 | logger.debug(f"Found license file {license_file} for {project.name}") |
90 | | - guessed_license = infer_license.api.guess_file(license_file) |
| 94 | + guessed_license, probability = guess_license_in_file(license_file) |
91 | 95 |
|
92 | 96 | if guessed_license: |
93 | | - return str(guessed_license.name) |
94 | | - |
95 | | - logger.print_warning_line( |
96 | | - project.name, f"Could not determine license in {license_file}" |
97 | | - ) |
98 | | - |
99 | | - return "" |
| 97 | + license_files.append((str(guessed_license.name), probability)) |
| 98 | + else: |
| 99 | + logger.print_warning_line( |
| 100 | + project.name, f"Could not determine license in {license_file}" |
| 101 | + ) |
| 102 | + return license_files |
100 | 103 |
|
101 | 104 | @staticmethod |
102 | 105 | def _determine_version(project: ProjectEntry) -> str: |
|
0 commit comments