diff --git a/sacro/views.py b/sacro/views.py index e089aab..2377203 100644 --- a/sacro/views.py +++ b/sacro/views.py @@ -1,4 +1,5 @@ import getpass +import hashlib import html import json import logging @@ -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(): @@ -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 { diff --git a/tests/test_views.py b/tests/test_views.py index 8689e4c..2ba9074 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -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)})