Skip to content

Commit b1d5dcb

Browse files
committed
Korbit review changes
1 parent 579e96f commit b1d5dcb

5 files changed

Lines changed: 16 additions & 17 deletions

File tree

dfetch/commands/report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
logger = get_logger(__name__)
2222

23-
# Minimum confidence to accept a license guess
23+
# Only accept license guesses with below or higher confidence to avoid false positives
2424
LICENSE_PROBABILITY_THRESHOLD = 0.80
2525

2626

dfetch/reporting/reporter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ class Reporter(ABC):
1313

1414
name: str = "abstract"
1515

16-
@abstractmethod
1716
def __init__(self, manifest: Manifest) -> None:
1817
"""Create the reporter.
1918
2019
Args:
2120
manifest (Manifest): The manifest to report on
2221
"""
22+
self._manifest = manifest
23+
24+
@property
25+
def manifest(self) -> Manifest:
26+
"""Get the manifest."""
27+
return self._manifest
2328

2429
@abstractmethod
2530
def add_project(

dfetch/reporting/sbom_reporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class SbomReporter(Reporter):
102102

103103
def __init__(self, manifest: Manifest) -> None:
104104
"""Start the report."""
105-
self._manifest = manifest
105+
super().__init__(manifest)
106106
self._bom = Bom()
107107
self._bom.metadata.tools.components.add(self.dfetch_tool)
108108
self._bom.metadata.tools.components.add(cdx_lib_component())
@@ -120,7 +120,7 @@ def add_project(
120120

121121
name = project.name if purl.type == "generic" else purl.name
122122

123-
location = self._manifest.find_name_in_manifest(project.name)
123+
location = self.manifest.find_name_in_manifest(project.name)
124124

125125
component = Component(
126126
name=name,
@@ -131,7 +131,7 @@ def add_project(
131131
evidence=ComponentEvidence(
132132
occurrences=[
133133
Occurrence(
134-
location=self._manifest.relative_path,
134+
location=self.manifest.relative_path,
135135
line=location.line_number,
136136
offset=location.start,
137137
)

dfetch/reporting/stdout_reporter.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from typing import List
88

99
from dfetch.log import get_logger
10-
from dfetch.manifest.manifest import Manifest
1110
from dfetch.manifest.project import ProjectEntry
1211
from dfetch.project.metadata import Metadata
1312
from dfetch.reporting.reporter import Reporter
@@ -21,10 +20,6 @@ class StdoutReporter(Reporter):
2120

2221
name = "stdout"
2322

24-
def __init__(self, manifest: Manifest) -> None:
25-
"""Initialize the reporter."""
26-
del manifest
27-
2823
def add_project(
2924
self,
3025
project: ProjectEntry,

dfetch/util/license.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import infer_license
88
from infer_license.types import License as InferredLicense
99

10-
# Limit the max size of alicense file to parse
11-
MAX_LICENSE_FILE_SIZE = 1024 * 1024
10+
# Limit license file size to below number of bytes to prevent memory issues with large files
11+
MAX_LICENSE_FILE_SIZE = 1024 * 1024 # 1 MB
1212

1313

1414
@dataclass
@@ -50,6 +50,9 @@ def guess_license_in_file(
5050
) -> Optional[License]:
5151
"""Attempt to identify the license of a given file.
5252
53+
Tries UTF-8 encoding first, falling back to Latin-1 for legacy license files.
54+
If the file cannot be read or no license is detected, returns None.
55+
5356
Args:
5457
filename (Union[str, os.PathLike[str]]): Path to the file to analyze
5558
@@ -63,11 +66,7 @@ def guess_license_in_file(
6366
license_text = file_bytes.decode("utf-8")
6467
except UnicodeDecodeError:
6568
license_text = file_bytes.decode("latin-1")
66-
except (FileNotFoundError, PermissionError, IsADirectoryError):
67-
# Return None for file access issues
68-
return None
69-
except OSError:
70-
# Handle other OS-level file errors
69+
except (FileNotFoundError, PermissionError, IsADirectoryError, OSError):
7170
return None
7271

7372
probable_licenses = infer_license.api.probabilities(license_text)

0 commit comments

Comments
 (0)