Skip to content

Commit

Permalink
Add checksums into UI JSON, and verify them
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodearnest committed Jul 13, 2023
1 parent 30ee991 commit 155ae8c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sacro/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getpass
import hashlib
import html
import json
import logging
Expand Down Expand Up @@ -44,6 +45,10 @@ def __post_init__(self):

self.version = self.raw_metadata["version"]
self.update(self.raw_metadata["results"])
self.annotate()

def annotate(self):
"""Add various useful annotations to the JSON data"""

# add urls to JSON data
for output, metadata in self.items():
Expand All @@ -57,6 +62,25 @@ def __post_init__(self):
"contents",
)

# add and check checksum data
checksums_dir = self.path.parent / "checksums"
for output, metadata in self.items():
for filedata in metadata["files"]:
filedata["checksum_valid"] = None
filedata["checksum"] = None

path = checksums_dir / (filedata["name"] + ".txt")
if not path.exists():
continue

filedata["checksum"] = path.read_text(encoding="utf8")
actual_file = self.get_file_path(output, filedata["name"])
if not actual_file.exists():
continue

checksum = hashlib.sha256(actual_file.read_bytes()).hexdigest()
filedata["checksum_valid"] = checksum == filedata["checksum"]

def get_file_path(self, output, filename):
"""Return absolute path to output file"""
if filename not in {
Expand Down
22 changes: 22 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ def test_outputs(tmp_path):
return views.Outputs(tmp_path / TEST_PATH.name)


def test_outputs_annotation(test_outputs):
assert test_outputs.version == "0.4.0"
for metadata in test_outputs.values():
for filedata in metadata["files"]:
assert filedata["checksum"] is not None
assert filedata["checksum_valid"] is True
assert filedata["url"].startswith("/contents/?path=")


def test_outputs_annotation_checksum_failed(test_outputs):
first_output = list(test_outputs)[0]
first_file = test_outputs[first_output]["files"][0]["name"]
checksum = test_outputs.path.parent / "checksums" / (first_file + ".txt")
checksum.write_text("bad checksum")

# re-annotate
test_outputs.annotate()

assert test_outputs[first_output]["files"][0]["checksum"] == "bad checksum"
assert test_outputs[first_output]["files"][0]["checksum_valid"] is False


def test_index(test_outputs):
request = RequestFactory().get(path="/", data={"path": str(test_outputs.path)})

Expand Down

0 comments on commit 155ae8c

Please sign in to comment.