Skip to content

Commit dc0bd5d

Browse files
Refine referenced_filenames in license rules
Reference: #3547 Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent 2ddb31c commit dc0bd5d

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/licensedcode/detection.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,25 @@ def identifier_with_expression(self):
333333
id_safe_expression = python_safe_name(s=str(self.license_expression))
334334
return "{}-{}".format(id_safe_expression, self._identifier)
335335

336+
@property
337+
def is_unknown(self):
338+
"""
339+
Return True if there are unknown license keys in the license expression
340+
for this detection, return False otherwise.
341+
"""
342+
unknown_license_keys = [
343+
"unknown-license-reference",
344+
"unknown-spdx",
345+
"unknown",
346+
"free-unknown"
347+
]
348+
349+
for license_key in unknown_license_keys:
350+
if license_key in self.license_expression:
351+
return True
352+
353+
return False
354+
336355
def get_start_end_line(self):
337356
"""
338357
Return start and end line for a license detection issue, from the
@@ -1355,6 +1374,58 @@ def has_references_to_local_files(license_matches):
13551374
)
13561375

13571376

1377+
def use_referenced_license_expression(referenced_license_expression, license_detection, licensing=Licensing()):
1378+
"""
1379+
"""
1380+
if license_detection.is_unknown:
1381+
return True
1382+
1383+
if referenced_license_expression == license_detection.license_expression:
1384+
return True
1385+
1386+
dependent_license_keys = {
1387+
"lgpl": "gpl",
1388+
}
1389+
1390+
license_keys_with_or_later = [
1391+
"gpl", "lgpl", "agpl"
1392+
]
1393+
1394+
license_keys = set(
1395+
licensing.license_keys(expression=license_detection.license_expression)
1396+
)
1397+
referenced_license_keys = set(
1398+
licensing.license_keys(expression=referenced_license_expression)
1399+
)
1400+
same_expression = referenced_license_expression == license_detection.license_expression
1401+
same_license_keys = license_keys == referenced_license_keys
1402+
1403+
if same_license_keys and not same_expression:
1404+
return False
1405+
1406+
for primary_key, dependent_key in dependent_license_keys.items():
1407+
dependent_key_only_in_referenced = dependent_key in referenced_license_keys and dependent_key not in license_keys
1408+
if primary_key in license_keys and dependent_key_only_in_referenced:
1409+
return False
1410+
1411+
all_license_keys_special = [
1412+
key in license_keys_with_or_later
1413+
for key in license_keys
1414+
]
1415+
all_referenced_license_keys_special = [
1416+
key in license_keys_with_or_later
1417+
for key in referenced_license_keys
1418+
]
1419+
1420+
if all_license_keys_special and all_referenced_license_keys_special and not same_license_keys:
1421+
True
1422+
1423+
if len(referenced_license_keys) > 5:
1424+
return False
1425+
1426+
return True
1427+
1428+
13581429
def get_detected_license_expression(
13591430
analysis,
13601431
license_matches=None,

src/licensedcode/plugin_license.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from licensedcode.detection import LicenseDetectionFromResult
3131
from licensedcode.detection import sort_unique_detections
3232
from licensedcode.detection import UniqueDetection
33+
from licensedcode.detection import use_referenced_license_expression
3334
from packagedcode.utils import combine_expressions
3435
from scancode.api import SCANCODE_LICENSEDB_URL
3536

@@ -301,6 +302,12 @@ def add_referenced_filenames_license_matches_for_detections(resource, codebase):
301302
analysis=DetectionCategory.UNKNOWN_FILE_REFERENCE_LOCAL.value,
302303
post_scan=True,
303304
)
305+
if not use_referenced_license_expression(
306+
referenced_license_expression=license_expression,
307+
license_detection=license_detection,
308+
):
309+
continue
310+
304311
license_expression_spdx = build_spdx_license_expression(
305312
license_expression=str(license_expression),
306313
licensing=get_cache().licensing,

src/packagedcode/licensing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from licensedcode.detection import detect_licenses
2727
from licensedcode.detection import LicenseDetectionFromResult
2828
from licensedcode.detection import populate_matches_with_path
29+
from licensedcode.detection import use_referenced_license_expression
2930
from licensedcode.spans import Span
3031
from licensedcode import query
3132

@@ -133,6 +134,11 @@ def add_referenced_license_matches_for_package(resource, codebase):
133134
analysis=DetectionCategory.PACKAGE_UNKNOWN_FILE_REFERENCE_LOCAL.value,
134135
post_scan=True,
135136
)
137+
if not use_referenced_license_expression(
138+
referenced_license_expression=license_expression,
139+
license_detection=license_detection_object,
140+
):
141+
continue
136142
license_expression_spdx = build_spdx_license_expression(
137143
license_expression=str(license_expression),
138144
licensing=get_cache().licensing,
@@ -258,6 +264,11 @@ def add_referenced_license_detection_from_package(resource, codebase):
258264
analysis=analysis,
259265
post_scan=True,
260266
)
267+
if not use_referenced_license_expression(
268+
referenced_license_expression=license_expression,
269+
license_detection=license_detection_object,
270+
):
271+
continue
261272
license_expression_spdx = build_spdx_license_expression(
262273
license_expression=str(license_expression),
263274
licensing=get_cache().licensing,

0 commit comments

Comments
 (0)