1515from commoncode .cliutils import PluggableCommandLineOption
1616from commoncode .cliutils import SCAN_GROUP
1717from commoncode .cliutils import SCAN_OPTIONS_GROUP
18+ from license_expression import combine_expressions
1819from plugincode .scan import ScanPlugin
1920from plugincode .scan import scan_impl
2021
3031from licensedcode .detection import LicenseDetectionFromResult
3132from licensedcode .detection import sort_unique_detections
3233from licensedcode .detection import UniqueDetection
34+ from licensedcode .detection import use_referenced_license_expression
3335from packagedcode .utils import combine_expressions
3436from scancode .api import SCANCODE_LICENSEDB_URL
3537
3638TRACE = os .environ .get ('SCANCODE_DEBUG_PLUGIN_LICENSE' , False )
39+ TRACE_REFERENCE = os .environ .get ('SCANCODE_DEBUG_PLUGIN_LICENSE_REFERENCE' , False )
3740
3841
3942def logger_debug (* args ):
@@ -42,7 +45,7 @@ def logger_debug(*args):
4245
4346logger = logging .getLogger (__name__ )
4447
45- if TRACE :
48+ if TRACE or TRACE_REFERENCE :
4649 import sys
4750 logging .basicConfig (stream = sys .stdout )
4851 logger .setLevel (logging .DEBUG )
@@ -214,6 +217,8 @@ def process_codebase(self, codebase, license_text=False, license_diagnostics=Fal
214217 f'before: { license_expressions_before } \n '
215218 f'after : { license_expressions_after } '
216219 )
220+
221+ #raise Exception()
217222
218223 license_detections = collect_license_detections (
219224 codebase = codebase ,
@@ -259,20 +264,28 @@ def add_referenced_filenames_license_matches_for_detections(resource, codebase):
259264
260265 modified = False
261266
267+ if TRACE_REFERENCE :
268+ logger_debug (
269+ f'add_referenced_license_matches: resource_path: { resource .path } ' ,
270+ )
271+
262272 for license_detection_mapping in license_detection_mappings :
263273
264274 license_detection = LicenseDetectionFromResult .from_license_detection_mapping (
265275 license_detection_mapping = license_detection_mapping ,
266276 file_path = resource .path ,
267277 )
268- detection_modified = False
269- detections_added = []
270278 license_match_mappings = license_detection_mapping ["matches" ]
271279 referenced_filenames = get_referenced_filenames (license_detection .matches )
272280
273281 if not referenced_filenames :
282+ if TRACE_REFERENCE :
283+ logger_debug (
284+ f'No references at license detection with expression: { license_detection .license_expression } ' ,
285+ )
274286 continue
275287
288+ referenced_detections = []
276289 for referenced_filename in referenced_filenames :
277290 referenced_resource = find_referenced_resource (
278291 referenced_filename = referenced_filename ,
@@ -281,26 +294,53 @@ def add_referenced_filenames_license_matches_for_detections(resource, codebase):
281294 )
282295
283296 if referenced_resource and referenced_resource .license_detections :
284- modified = True
285- detection_modified = True
286- detections_added .extend (referenced_resource .license_detections )
287- matches_to_extend = get_matches_from_detection_mappings (
288- license_detections = referenced_resource .license_detections
297+ referenced_detections .extend (
298+ referenced_resource .license_detections
289299 )
290- populate_matches_with_path (
291- matches = matches_to_extend ,
292- path = referenced_resource .path
293- )
294- license_match_mappings .extend (matches_to_extend )
295300
296- if not detection_modified :
301+ for detection in referenced_resource .license_detections :
302+ populate_matches_with_path (
303+ matches = detection ["matches" ],
304+ path = referenced_resource .path
305+ )
306+
307+ referenced_license_expression = combine_expressions (
308+ expressions = [
309+ detection ["license_expression" ]
310+ for detection in referenced_detections
311+ ],
312+ )
313+ if not use_referenced_license_expression (
314+ referenced_license_expression = referenced_license_expression ,
315+ license_detection = license_detection ,
316+ ):
317+ if TRACE_REFERENCE :
318+ logger_debug (
319+ f'use_referenced_license_expression: False for '
320+ f'resource: { referenced_resource .path } and '
321+ f'license_expression: { referenced_license_expression } ' ,
322+ )
297323 continue
298324
325+ if TRACE_REFERENCE :
326+ logger_debug (
327+ f'use_referenced_license_expression: True for '
328+ f'resource: { referenced_resource .path } and '
329+ f'license_expression: { referenced_license_expression } ' ,
330+ )
331+
332+ modified = True
333+ matches_to_extend = get_matches_from_detection_mappings (
334+ license_detections = referenced_detections
335+ )
336+ license_match_mappings .extend (matches_to_extend )
337+
299338 detection_log , license_expression = get_detected_license_expression (
300339 license_match_mappings = license_match_mappings ,
301340 analysis = DetectionCategory .UNKNOWN_FILE_REFERENCE_LOCAL .value ,
302341 post_scan = True ,
303342 )
343+
304344 license_expression_spdx = build_spdx_license_expression (
305345 license_expression = str (license_expression ),
306346 licensing = get_cache ().licensing ,
@@ -310,7 +350,7 @@ def add_referenced_filenames_license_matches_for_detections(resource, codebase):
310350 license_detection_mapping ["detection_log" ] = detection_log
311351 license_detection_mapping ["identifier" ] = get_new_identifier_from_detections (
312352 initial_detection = license_detection_mapping ,
313- detections_added = detections_added ,
353+ detections_added = referenced_detections ,
314354 license_expression = license_expression ,
315355 )
316356
0 commit comments