Skip to content

Commit 97cb24e

Browse files
authored
Merge pull request #22 from nexB/21-scan-download
Add support for downloading both package scan results and summary #21
2 parents 2bdb96d + f0bdd6d commit 97cb24e

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

component_catalog/tests/test_views.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -2888,8 +2888,10 @@ def test_delete_scan_view(self, mock_fetch_scan_list, mock_delete_scan):
28882888
response = self.client.get(delete_url)
28892889
self.assertEqual(404, response.status_code)
28902890

2891-
@mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.stream_scan_data")
2892-
def test_send_scan_data_as_file_view(self, mock_stream_scan_data):
2891+
@mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.fetch_scan_data")
2892+
def test_send_scan_data_as_file_view(self, mock_fetch_scan_data):
2893+
mock_fetch_scan_data.return_value = {}
2894+
28932895
project_uuid = "348df847-f48f-4ac7-b864-5785b44c65e2"
28942896
url = reverse(
28952897
"component_catalog:scan_data_as_file", args=[project_uuid, self.package1.filename]
@@ -2906,9 +2908,9 @@ def test_send_scan_data_as_file_view(self, mock_stream_scan_data):
29062908
self.dataspace.save()
29072909
response = self.client.get(url)
29082910
self.assertEqual(200, response.status_code)
2909-
self.assertEqual("application/json", response["content-type"])
2911+
self.assertEqual("application/zip", response["content-type"])
29102912
self.assertEqual(
2911-
'attachment; filename="package1_scan.json"', response["content-disposition"]
2913+
'attachment; filename="package1_scan.zip"', response["content-disposition"]
29122914
)
29132915

29142916
@mock.patch("requests.head")

component_catalog/views.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
# See https://aboutcode.org for more information about AboutCode FOSS projects.
77
#
88

9+
import io
910
import json
11+
import zipfile
1012
from collections import Counter
1113
from operator import itemgetter
1214
from urllib.parse import quote_plus
@@ -1651,10 +1653,18 @@ def send_scan_data_as_file_view(request, project_uuid, filename):
16511653

16521654
scancodeio = ScanCodeIO(request.user)
16531655
scan_results_url = scancodeio.get_scan_results_url(project_uuid)
1654-
data_stream = scancodeio.stream_scan_data(scan_results_url)
1655-
1656-
response = FileResponse(data_stream.iter_lines(), content_type="application/json")
1657-
response["Content-Disposition"] = f'attachment; filename="{filename}_scan.json"'
1656+
scan_results = scancodeio.fetch_scan_data(scan_results_url)
1657+
scan_summary_url = scancodeio.get_scan_summary_url(project_uuid)
1658+
scan_summary = scancodeio.fetch_scan_data(scan_summary_url)
1659+
1660+
in_memory_zip = io.BytesIO()
1661+
with zipfile.ZipFile(in_memory_zip, "a", zipfile.ZIP_DEFLATED, False) as zipf:
1662+
zipf.writestr(f"{filename}_scan.json", json.dumps(scan_results, indent=2))
1663+
zipf.writestr(f"{filename}_summary.json", json.dumps(scan_summary, indent=2))
1664+
1665+
in_memory_zip.seek(0)
1666+
response = FileResponse(in_memory_zip, content_type="application/zip")
1667+
response["Content-Disposition"] = f'attachment; filename="{filename}_scan.zip"'
16581668
return response
16591669

16601670

dejacode_toolkit/scancodeio.py

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def get_scan_results_url(self, project_uuid):
3939
detail_url = self.get_scan_detail_url(project_uuid)
4040
return f"{detail_url}results/"
4141

42+
def get_scan_summary_url(self, project_uuid):
43+
detail_url = self.get_scan_detail_url(project_uuid)
44+
return f"{detail_url}summary/"
45+
4246
def get_project_packages_url(self, project_uuid):
4347
return f"{self.project_api_url}{project_uuid}/packages/"
4448

0 commit comments

Comments
 (0)