Skip to content

Commit f946fea

Browse files
committed
Set the license expression from the detected license value #284
Signed-off-by: Thomas Druez <[email protected]>
1 parent c4523ea commit f946fea

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

scanpipe/pipelines/inspect_manifest.py

+1
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@ def create_packages_from_manifest(self):
7070
raise Exception(f"No packages could be resolved for {input_location}")
7171

7272
for package_data in resolved_packages:
73+
package_data = resolve.set_license_expression(package_data)
7374
update_or_create_package(self.project, package_data)

scanpipe/pipes/resolve.py

+25
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from attributecode.model import About
2626
from packagedcode import APPLICATION_PACKAGE_DATAFILE_HANDLERS
27+
from packagedcode.licensing import get_normalized_expression
2728
from packageurl import PackageURL
2829
from python_inspector.resolve_cli import resolver_api
2930

@@ -83,3 +84,27 @@ def get_default_package_type(input_location):
8384
"about": resolve_about_packages,
8485
"pypi": resolve_pypi_packages,
8586
}
87+
88+
89+
def set_license_expression(package_data):
90+
"""
91+
Sets the license expression from a detected license dict/str in provided
92+
`package_data`.
93+
"""
94+
declared_license = package_data.get("declared_license")
95+
license_expression = package_data.get("license_expression")
96+
97+
if declared_license and not license_expression:
98+
license_str = ""
99+
100+
if isinstance(declared_license, dict):
101+
license_str = declared_license.get("license")
102+
103+
if not license_str:
104+
license_str = repr(declared_license)
105+
106+
license_expression = get_normalized_expression(query_string=license_str)
107+
if license_expression:
108+
package_data["license_expression"] = license_expression
109+
110+
return package_data

scanpipe/tests/test_pipes.py

+18
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from scanpipe.pipes import fetch
4646
from scanpipe.pipes import filename_now
4747
from scanpipe.pipes import make_codebase_resource
48+
from scanpipe.pipes import resolve
4849
from scanpipe.pipes import rootfs
4950
from scanpipe.pipes import scancode
5051
from scanpipe.pipes import strip_root
@@ -735,6 +736,23 @@ def test_scanpipe_pipes_rootfs_has_hash_diff(self):
735736
codebase_resource = CodebaseResource(sha256="sha256", md5="md5")
736737
self.assertFalse(rootfs.has_hash_diff(install_file, codebase_resource))
737738

739+
def test_scanpipe_pipes_resolve_set_license_expression(self):
740+
declared_license = {"license": "MIT"}
741+
data = resolve.set_license_expression({"declared_license": declared_license})
742+
self.assertEqual("mit", data.get("license_expression"))
743+
744+
declared_license = {
745+
"classifiers": [
746+
"License :: OSI Approved :: Python Software Foundation License"
747+
]
748+
}
749+
data = resolve.set_license_expression({"declared_license": declared_license})
750+
self.assertEqual("python", data.get("license_expression"))
751+
752+
declared_license = "GPL 2.0"
753+
data = resolve.set_license_expression({"declared_license": declared_license})
754+
self.assertEqual("gpl-2.0", data.get("license_expression"))
755+
738756
def test_scanpipe_pipes_windows_tag_uninteresting_windows_codebase_resources(self):
739757
p1 = Project.objects.create(name="Analysis")
740758
resource1 = CodebaseResource.objects.create(

0 commit comments

Comments
 (0)