MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / collect_license_detections

Function collect_license_detections

src/licensedcode/detection.py:736–866  ·  view source on GitHub ↗

Return a list of LicenseDetectionFromResult object rehydrated from LicenseDetection mappings, from resources and packages in a ``codebase``. As a side effect, this also corrects `declared_license_expression` in packages according to their license detections. This is required becaus

(codebase, include_license_clues=True)

Source from the content-addressed store, hash-verified

734
735
736def collect_license_detections(codebase, include_license_clues=True):
737 """
738 Return a list of LicenseDetectionFromResult object rehydrated from
739 LicenseDetection mappings, from resources and packages in a ``codebase``.
740
741 As a side effect, this also corrects `declared_license_expression` in packages
742 according to their license detections. This is required because package fields
743 are populated in package plugin, which runs before the license plugin, and thus
744 the license plugin step where unknown references to other files are dereferenced
745 does not show up automatically in package attributes.
746
747 Also populate from_file attributes with resource paths for matches which have
748 origin in the same file.
749 """
750 has_packages = hasattr(codebase.root, 'package_data')
751 has_licenses = hasattr(codebase.root, 'license_detections')
752
753 all_license_detections = []
754
755 for resource in codebase.walk():
756
757 resource_license_detections = []
758 if has_licenses:
759 license_detections = getattr(resource, 'license_detections', []) or []
760 for detection in license_detections:
761 populate_matches_with_path(matches=detection["matches"], path=resource.path)
762 license_clues = getattr(resource, 'license_clues', []) or []
763 populate_matches_with_path(matches=license_clues, path=resource.path)
764 codebase.save_resource(resource)
765
766 if license_detections:
767 license_detection_objects = detections_from_license_detection_mappings(
768 license_detection_mappings=license_detections,
769 file_path=resource.path,
770 )
771 resource_license_detections.extend(license_detection_objects)
772
773 if include_license_clues and license_clues:
774 license_matches = LicenseMatchFromResult.from_dicts(
775 license_match_mappings=license_clues,
776 )
777
778 for group_of_matches in group_matches(license_matches=license_matches):
779 detection = LicenseDetection.from_matches(matches=group_of_matches)
780 detection.file_region = detection.get_file_region(path=resource.path)
781 resource_license_detections.append(detection)
782
783 all_license_detections.extend(resource_license_detections)
784
785 if TRACE:
786 logger_debug(
787 f'license detections collected at path {resource.path}:',
788 f'resource_license_detections: {resource_license_detections}\n',
789 f'all_license_detections: {all_license_detections}',
790 )
791
792 if has_packages:
793 package_data = getattr(resource, 'package_data', []) or []

Callers 2

process_codebaseMethod · 0.90
process_codebaseMethod · 0.90

Calls 14

get_licensingFunction · 0.90
group_matchesFunction · 0.85
save_resourceMethod · 0.80
from_dictsMethod · 0.80
from_matchesMethod · 0.80
get_file_regionMethod · 0.80
get_resourceMethod · 0.80
logger_debugFunction · 0.70

Tested by

no test coverage detected