Given a Resource, check if the detected package_data doesn't have license detections and the file has license detections, and if so, populate the package_data license expression and detection fields from the file license.
(resource, codebase)
| 311 | |
| 312 | |
| 313 | def add_license_from_file(resource, codebase): |
| 314 | """ |
| 315 | Given a Resource, check if the detected package_data doesn't have license detections |
| 316 | and the file has license detections, and if so, populate the package_data license |
| 317 | expression and detection fields from the file license. |
| 318 | """ |
| 319 | if TRACE_LICENSE: |
| 320 | logger_debug(f'packagedcode.plugin_package: add_license_from_file: resource: {resource.path}') |
| 321 | |
| 322 | if not resource.is_file: |
| 323 | return |
| 324 | |
| 325 | license_detections_file = resource.license_detections |
| 326 | |
| 327 | if TRACE_LICENSE: |
| 328 | logger_debug(f'add_license_from_file: license_detections_file: {license_detections_file}') |
| 329 | if not license_detections_file: |
| 330 | return |
| 331 | |
| 332 | package_data = resource.package_data |
| 333 | if not package_data: |
| 334 | return |
| 335 | |
| 336 | for pkg in package_data: |
| 337 | license_detections_pkg = pkg["license_detections"] |
| 338 | if TRACE_LICENSE: |
| 339 | logger_debug(f'add_license_from_file: license_detections_pkg: {license_detections_pkg}') |
| 340 | |
| 341 | if not license_detections_pkg: |
| 342 | pkg["license_detections"] = license_detections_file.copy() |
| 343 | for detection in pkg["license_detections"]: |
| 344 | if "detection_log" in detection: |
| 345 | detection["detection_log"].append(DetectionRule.PACKAGE_ADD_FROM_FILE.value) |
| 346 | |
| 347 | license_expression = get_license_expression_from_detection_mappings( |
| 348 | detections=license_detections_file, |
| 349 | valid_expression=True |
| 350 | ) |
| 351 | pkg["declared_license_expression"] = license_expression |
| 352 | pkg["declared_license_expression_spdx"] = str(build_spdx_license_expression( |
| 353 | license_expression=license_expression, |
| 354 | licensing=get_cache().licensing, |
| 355 | )) |
| 356 | |
| 357 | codebase.save_resource(resource) |
| 358 | return pkg |
| 359 | |
| 360 | |
| 361 | def get_installed_packages(root_dir, processes=2, **kwargs): |
no test coverage detected