|
39 | 39 | from extractcode import api as extractcode_api |
40 | 40 | from packagedcode import get_package_handler |
41 | 41 | from packagedcode import models as packagedcode_models |
| 42 | +from licensedcode.detection import FileRegion |
42 | 43 | from scancode import Scanner |
43 | 44 | from scancode import api as scancode_api |
44 | 45 | from scancode import cli as scancode_cli |
@@ -401,6 +402,67 @@ def add_resource_to_package(package_uid, resource, project): |
401 | 402 | resource.discovered_packages.add(package) |
402 | 403 |
|
403 | 404 |
|
| 405 | +def collect_and_create_license_detections(project): |
| 406 | + """ |
| 407 | + Create instances of DiscoveredLicense for `project` from the parsed |
| 408 | + license detections present in the CodebaseResources and |
| 409 | + DiscoveredPackages of `project`. |
| 410 | + """ |
| 411 | + logger.info(f"Project {project} collect_license_detections:") |
| 412 | + |
| 413 | + for resource in project.codebaseresources.has_license_detections(): |
| 414 | + logger.info(f" Processing: {resource.path} for licenses") |
| 415 | + |
| 416 | + for detection_data in resource.license_detections: |
| 417 | + pipes.update_or_create_license_detection( |
| 418 | + project=project, |
| 419 | + detection_data=detection_data, |
| 420 | + resource_path=resource.path, |
| 421 | + ) |
| 422 | + |
| 423 | + for resource in project.codebaseresources.has_package_data(): |
| 424 | + |
| 425 | + for package_mapping in resource.package_data: |
| 426 | + package_data = packagedcode_models.PackageData.from_dict( |
| 427 | + mapping=package_mapping, |
| 428 | + ) |
| 429 | + |
| 430 | + for detection in package_data.license_detections: |
| 431 | + pipes.update_or_create_license_detection( |
| 432 | + project=project, |
| 433 | + detection_data=detection, |
| 434 | + resource_path=resource.path, |
| 435 | + from_package=True, |
| 436 | + ) |
| 437 | + |
| 438 | + for detection in package_data.other_license_detections: |
| 439 | + pipes.update_or_create_license_detection( |
| 440 | + project=project, |
| 441 | + detection_data=detection, |
| 442 | + resource_path=resource.path, |
| 443 | + from_package=True, |
| 444 | + ) |
| 445 | + |
| 446 | + |
| 447 | +def get_file_region(detection_data, resource_path): |
| 448 | + """ |
| 449 | + From a LicenseDetection mapping `detection_data`, create a FileRegion |
| 450 | + object containing information about where this license was detected |
| 451 | + exactly in a codebase, with `resource_path`, with start and end lines. |
| 452 | + """ |
| 453 | + start_line = min( |
| 454 | + [match['start_line'] for match in detection_data["matches"]] |
| 455 | + ) |
| 456 | + end_line = max( |
| 457 | + [match['end_line'] for match in detection_data["matches"]] |
| 458 | + ) |
| 459 | + return FileRegion( |
| 460 | + path=resource_path, |
| 461 | + start_line=start_line, |
| 462 | + end_line=end_line, |
| 463 | + ) |
| 464 | + |
| 465 | + |
404 | 466 | def assemble_packages(project): |
405 | 467 | """ |
406 | 468 | Create instances of DiscoveredPackage and DiscoveredDependency for `project` |
|
0 commit comments