Skip to content

Commit ae025a2

Browse files
Use license/notice files from About data
Reference: #1004 Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent b139359 commit ae025a2

4 files changed

Lines changed: 52 additions & 17 deletions

File tree

scanpipe/pipes/d2d.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,26 @@ def map_deployed_to_devel_using_about(self, to_resources):
921921

922922
return mapped_to_resources
923923

924+
def get_about_file_companions(self, about_path):
925+
"""
926+
Given an ``about_path`` path string to an About file,
927+
get CodebaseResource objects for the companion license
928+
and notice files.
929+
"""
930+
about_file_resource = self.about_resources_by_path.get(about_path)
931+
about_file_extra_data = self.about_pkgdata_by_path.get(about_path).get(
932+
"extra_data"
933+
)
934+
935+
about_file_companion_names = [
936+
about_file_extra_data.get("license_file"),
937+
about_file_extra_data.get("notice_file"),
938+
]
939+
about_file_companions = about_file_resource.siblings().filter(
940+
name__in=about_file_companion_names
941+
)
942+
return about_file_companions
943+
924944
def create_about_packages_relations(self, project):
925945
"""
926946
Create packages using About file package data, if the About file
@@ -932,11 +952,11 @@ def create_about_packages_relations(self, project):
932952

933953
for about_path, mapped_resources in self.mapped_resources_by_aboutpath.items():
934954
about_file_resource = self.about_resources_by_path[about_path]
935-
package_data = self.about_pkgdata_by_path[about_file_resource.path]
955+
package_data = self.about_pkgdata_by_path[about_path]
936956

937957
if not mapped_resources:
938958
error_message_details = {
939-
"path": about_file_resource.path,
959+
"path": about_path,
940960
"package_data": package_data,
941961
}
942962
project.add_warning(
@@ -968,12 +988,7 @@ def create_about_packages_relations(self, project):
968988

969989
about_file_resource.update(status=flag.ABOUT_MAPPED)
970990

971-
for about_file_resource in self.about_resources_by_path.values():
972-
about_file_companions = (
973-
about_file_resource.siblings()
974-
.filter(name__startswith=about_file_resource.name_without_extension)
975-
.filter(extension__in=[".LICENSE", ".NOTICE"])
976-
)
991+
about_file_companions = self.get_about_file_companions(about_path)
977992
about_file_companions.update(status=flag.ABOUT_MAPPED)
978993

979994
return about_purls, mapped_about_resources

scanpipe/pipes/resolve.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,15 @@ def resolve_about_package(input_location):
8686
if value:
8787
package_data[field_name] = value
8888

89+
package_data["extra_data"] = {}
90+
8991
if about_resource := about_data.get("about_resource"):
9092
package_data["filename"] = list(about_resource.keys())[0]
9193

9294
if ignored_resources := about_data.get("ignored_resources"):
93-
extra_data = {"ignored_resources": list(ignored_resources.keys())}
94-
package_data["extra_data"] = extra_data
95-
96-
if license_expression := about_data.get("license_expression"):
97-
package_data["declared_license_expression"] = license_expression
95+
package_data["extra_data"]["ignored_resources"] = list(ignored_resources.keys())
9896

99-
if notice_dict := about_data.get("notice_file"):
100-
package_data["notice_text"] = list(notice_dict.values())[0]
97+
populate_license_notice_fields_about(package_data, about_data)
10198

10299
for field_name, value in about_data.items():
103100
if field_name.startswith("checksum_"):
@@ -107,6 +104,23 @@ def resolve_about_package(input_location):
107104
return package_data
108105

109106

107+
def populate_license_notice_fields_about(package_data, about_data):
108+
"""
109+
Populate ``package_data`` with license and notice attributes
110+
from ``about_data``.
111+
"""
112+
if license_expression := about_data.get("license_expression"):
113+
package_data["declared_license_expression"] = license_expression
114+
115+
if notice_dict := about_data.get("notice_file"):
116+
package_data["notice_text"] = list(notice_dict.values())[0]
117+
package_data["extra_data"]["notice_file"] = list(notice_dict.keys())[0]
118+
119+
if license_dict := about_data.get("license_file"):
120+
package_data["extra_data"]["license_file"] = list(license_dict.keys())[0]
121+
package_data["extracted_license_statement"] = list(license_dict.values())[0]
122+
123+
110124
def resolve_about_packages(input_location):
111125
"""
112126
Wrap ``resolve_about_package`` to return a list as expected by the

scanpipe/tests/data/d2d/about_files/expected.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@
111111
"other_license_expression": "",
112112
"other_license_expression_spdx": "",
113113
"other_license_detections": [],
114-
"extracted_license_statement": "",
114+
"extracted_license_statement": " Apache License\n Version 2.0, January 2004\n http://www.apache.org/licenses/\n\n ",
115115
"notice_text": "notice",
116116
"source_packages": [],
117117
"extra_data": {
118+
"notice_file": "flume-ng-node-1.9.0-sources.NOTICE",
119+
"license_file": "flume-ng-node-1.9.0-sources.LICENSE",
118120
"ignored_resources": [
119121
"*flume-ng-node-*.jar-extract/org/apache/flume/node/ConfigurationProvider.class"
120122
]

scanpipe/tests/pipes/test_resolve.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def test_scanpipe_pipes_resolve_resolve_packages(self):
9292
"filename": "Django-4.0.8-py3-none-any.whl",
9393
"download_url": "https://python.org/Django-4.0.8-py3-none-any.whl",
9494
"declared_license_expression": "bsd-new",
95+
"extra_data": {"license_file": "bsd-new.LICENSE"},
96+
"extracted_license_statement": None,
9597
"md5": "386349753c386e574dceca5067e2788a",
9698
"name": "django",
9799
"sha1": "4cc6f7abda928a0b12cd1f1cd8ad3677519ca04e",
@@ -114,6 +116,8 @@ def test_scanpipe_pipes_resolve_resolve_about_packages(self):
114116
"filename": "Django-4.0.8-py3-none-any.whl",
115117
"download_url": "https://python.org/Django-4.0.8-py3-none-any.whl",
116118
"declared_license_expression": "bsd-new",
119+
"extra_data": {"license_file": "bsd-new.LICENSE"},
120+
"extracted_license_statement": None,
117121
"md5": "386349753c386e574dceca5067e2788a",
118122
"name": "django",
119123
"sha1": "4cc6f7abda928a0b12cd1f1cd8ad3677519ca04e",
@@ -124,7 +128,7 @@ def test_scanpipe_pipes_resolve_resolve_about_packages(self):
124128

125129
input_location = self.manifest_location / "poor_values.ABOUT"
126130
package = resolve.resolve_about_packages(str(input_location))
127-
expected = {"name": "project"}
131+
expected = {"extra_data": {}, "name": "project"}
128132
self.assertEqual([expected], package)
129133

130134
def test_scanpipe_pipes_resolve_spdx_package_to_discovered_package_data(self):

0 commit comments

Comments
 (0)