Skip to content

Commit

Permalink
Merge pull request #22 from nexB/21-scan-download
Browse files Browse the repository at this point in the history
Add support for downloading both package scan results and summary #21
  • Loading branch information
tdruez authored Dec 13, 2023
2 parents 2bdb96d + f0bdd6d commit 97cb24e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
10 changes: 6 additions & 4 deletions component_catalog/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2888,8 +2888,10 @@ def test_delete_scan_view(self, mock_fetch_scan_list, mock_delete_scan):
response = self.client.get(delete_url)
self.assertEqual(404, response.status_code)

@mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.stream_scan_data")
def test_send_scan_data_as_file_view(self, mock_stream_scan_data):
@mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.fetch_scan_data")
def test_send_scan_data_as_file_view(self, mock_fetch_scan_data):
mock_fetch_scan_data.return_value = {}

project_uuid = "348df847-f48f-4ac7-b864-5785b44c65e2"
url = reverse(
"component_catalog:scan_data_as_file", args=[project_uuid, self.package1.filename]
Expand All @@ -2906,9 +2908,9 @@ def test_send_scan_data_as_file_view(self, mock_stream_scan_data):
self.dataspace.save()
response = self.client.get(url)
self.assertEqual(200, response.status_code)
self.assertEqual("application/json", response["content-type"])
self.assertEqual("application/zip", response["content-type"])
self.assertEqual(
'attachment; filename="package1_scan.json"', response["content-disposition"]
'attachment; filename="package1_scan.zip"', response["content-disposition"]
)

@mock.patch("requests.head")
Expand Down
18 changes: 14 additions & 4 deletions component_catalog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
# See https://aboutcode.org for more information about AboutCode FOSS projects.
#

import io
import json
import zipfile
from collections import Counter
from operator import itemgetter
from urllib.parse import quote_plus
Expand Down Expand Up @@ -1651,10 +1653,18 @@ def send_scan_data_as_file_view(request, project_uuid, filename):

scancodeio = ScanCodeIO(request.user)
scan_results_url = scancodeio.get_scan_results_url(project_uuid)
data_stream = scancodeio.stream_scan_data(scan_results_url)

response = FileResponse(data_stream.iter_lines(), content_type="application/json")
response["Content-Disposition"] = f'attachment; filename="{filename}_scan.json"'
scan_results = scancodeio.fetch_scan_data(scan_results_url)
scan_summary_url = scancodeio.get_scan_summary_url(project_uuid)
scan_summary = scancodeio.fetch_scan_data(scan_summary_url)

in_memory_zip = io.BytesIO()
with zipfile.ZipFile(in_memory_zip, "a", zipfile.ZIP_DEFLATED, False) as zipf:
zipf.writestr(f"{filename}_scan.json", json.dumps(scan_results, indent=2))
zipf.writestr(f"{filename}_summary.json", json.dumps(scan_summary, indent=2))

in_memory_zip.seek(0)
response = FileResponse(in_memory_zip, content_type="application/zip")
response["Content-Disposition"] = f'attachment; filename="{filename}_scan.zip"'
return response


Expand Down
4 changes: 4 additions & 0 deletions dejacode_toolkit/scancodeio.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def get_scan_results_url(self, project_uuid):
detail_url = self.get_scan_detail_url(project_uuid)
return f"{detail_url}results/"

def get_scan_summary_url(self, project_uuid):
detail_url = self.get_scan_detail_url(project_uuid)
return f"{detail_url}summary/"

def get_project_packages_url(self, project_uuid):
return f"{self.project_api_url}{project_uuid}/packages/"

Expand Down

0 comments on commit 97cb24e

Please sign in to comment.