Skip to content

Commit 71f9620

Browse files
committed
wip
1 parent e8ea016 commit 71f9620

5 files changed

Lines changed: 48 additions & 23 deletions

File tree

dfetch/commands/report.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import argparse
77
import glob
88
import os
9-
from typing import List, Tuple
9+
from typing import List
1010

1111
import dfetch.commands.command
1212
import dfetch.manifest.manifest
@@ -16,7 +16,7 @@
1616
from dfetch.project.metadata import Metadata
1717
from dfetch.project.vcs import VCS
1818
from dfetch.reporting import REPORTERS, ReportTypes
19-
from dfetch.util.license import guess_license_in_file
19+
from dfetch.util.license import License, guess_license_in_file
2020

2121
logger = get_logger(__name__)
2222

@@ -71,14 +71,14 @@ def __call__(self, args: argparse.Namespace) -> None:
7171
determined_licenses = self._determine_licenses(project)
7272
version = self._determine_version(project)
7373
reporter.add_project(
74-
project=project, license_names=determined_licenses, version=version
74+
project=project, licenses=determined_licenses, version=version
7575
)
7676

7777
if reporter.dump_to_file(args.outfile):
7878
logger.info(f"Generated {reporter.name} report: {args.outfile}")
7979

8080
@staticmethod
81-
def _determine_licenses(project: ProjectEntry) -> List[Tuple[str, float]]:
81+
def _determine_licenses(project: ProjectEntry) -> List[License]:
8282
"""Try to determine license of fetched project."""
8383
if not os.path.exists(project.destination):
8484
logger.print_warning_line(
@@ -91,10 +91,13 @@ def _determine_licenses(project: ProjectEntry) -> List[Tuple[str, float]]:
9191

9292
for license_file in filter(VCS.is_license_file, glob.glob("*")):
9393
logger.debug(f"Found license file {license_file} for {project.name}")
94-
guessed_license, probability = guess_license_in_file(license_file)
94+
guessed_license = guess_license_in_file(license_file)
9595

96-
if guessed_license:
97-
license_files.append((str(guessed_license.name), probability))
96+
if (
97+
guessed_license
98+
and guessed_license.probability > LICENSE_PROBABILITY_THRESHOLD
99+
):
100+
license_files.append(guessed_license)
98101
else:
99102
logger.print_warning_line(
100103
project.name, f"Could not determine license in {license_file}"

dfetch/reporting/reporter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Abstract reporting interface."""
22

33
from abc import ABC, abstractmethod
4-
from typing import List, Tuple
4+
from typing import List
55

66
from dfetch.manifest.project import ProjectEntry
7+
from dfetch.util.license import License
78

89

910
class Reporter(ABC):
@@ -15,7 +16,7 @@ class Reporter(ABC):
1516
def add_project(
1617
self,
1718
project: ProjectEntry,
18-
license_names: List[Tuple[str, float]],
19+
licenses: List[License],
1920
version: str,
2021
) -> None:
2122
"""Add a project to the report."""

dfetch/reporting/sbom_reporter.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
An fetched project generates an sbom
1616
"""
1717

18-
from typing import List, Tuple
18+
from typing import List
1919

2020
from cyclonedx.builder.this import this_component as cdx_lib_component
2121
from cyclonedx.model import ExternalReference, ExternalReferenceType, XsUri
@@ -29,6 +29,7 @@
2929
import dfetch.util.purl
3030
from dfetch.manifest.project import ProjectEntry
3131
from dfetch.reporting.reporter import Reporter
32+
from dfetch.util.license import License
3233

3334
# PyRight is pedantic with decorators see https://github.com/madpah/serializable/issues/8
3435
# It might be fixable with https://github.com/microsoft/pyright/discussions/4426, would prefer
@@ -52,7 +53,7 @@ def __init__(self) -> None:
5253
def add_project(
5354
self,
5455
project: ProjectEntry,
55-
license_names: List[Tuple[str, float]],
56+
licenses: List[License],
5657
version: str,
5758
) -> None:
5859
"""Add a project to the report."""
@@ -94,8 +95,8 @@ def add_project(
9495
)
9596
)
9697

97-
for name, _ in license_names:
98-
component.licenses.add(LicenseExpression(name))
98+
for lic in licenses:
99+
component.licenses.add(LicenseExpression(lic.name))
99100
self._bom.components.add(component)
100101

101102
def dump_to_file(self, outfile: str) -> bool:

dfetch/reporting/stdout_reporter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
from the manifest or the metadata (``.dfetch_data.yaml``).
55
"""
66

7-
from typing import List, Tuple
7+
from typing import List
88

99
from dfetch.log import get_logger
1010
from dfetch.manifest.project import ProjectEntry
1111
from dfetch.project.metadata import Metadata
1212
from dfetch.reporting.reporter import Reporter
13+
from dfetch.util.license import License
1314

1415
logger = get_logger(__name__)
1516

@@ -22,7 +23,7 @@ class StdoutReporter(Reporter):
2223
def add_project(
2324
self,
2425
project: ProjectEntry,
25-
license_names: List[Tuple[str, float]],
26+
licenses: List[License],
2627
version: str,
2728
) -> None:
2829
"""Add a project to the report."""
@@ -38,7 +39,7 @@ def add_project(
3839
logger.print_info_field(" revision", metadata.revision)
3940
logger.print_info_field(" patch", metadata.patch)
4041
logger.print_info_field(
41-
" licenses", ",".join(license for license, _ in license_names)
42+
" licenses", ",".join(license.name for license in licenses)
4243
)
4344

4445
except FileNotFoundError:

dfetch/util/license.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
"""*Dfetch* uses *Infer-License* to guess licenses from files."""
22

33
import os
4-
from typing import Optional, Tuple, Union
4+
from dataclasses import dataclass
5+
from typing import Optional, Union
56

67
import infer_license
7-
from infer_license.types import License
8+
from infer_license.types import License as InferredLicense
89

9-
LICENSE_PROBABILITY_THRESHOLD = 0.80
10+
11+
@dataclass
12+
class License:
13+
"""Class to hold license information."""
14+
15+
name: str # SPDX Full name
16+
shortname: str # SPDX Identifier
17+
trove_classifier: Optional[str]
18+
probability: float
19+
20+
@staticmethod
21+
def from_inferred(
22+
inferred_license: InferredLicense, probability: float
23+
) -> "License":
24+
"""Create License from an InferredLicense."""
25+
return License(
26+
name=inferred_license.name,
27+
shortname=inferred_license.shortname,
28+
trove_classifier=inferred_license.trove_classifier,
29+
probability=probability,
30+
)
1031

1132

1233
def guess_license_in_file(
1334
filename: Union[str, "os.PathLike[str]"],
14-
) -> Tuple[Optional[License], float]:
35+
) -> Optional[License]:
1536
"""Guess license from file."""
1637
try:
1738
with open(filename, encoding="utf-8") as f:
@@ -21,7 +42,5 @@ def guess_license_in_file(
2142
license_text = f.read()
2243

2344
probable_license = infer_license.api.probabilities(license_text)
24-
if probable_license and probable_license[0][1] > LICENSE_PROBABILITY_THRESHOLD:
25-
return probable_license[0]
2645

27-
return None, 0.0
46+
return None if not probable_license else License.from_inferred(*probable_license[0])

0 commit comments

Comments
 (0)