testing and fixing the sanity checker

This commit is contained in:
jonaswinkler
2020-12-02 01:18:11 +01:00
parent f3f5227776
commit a4f60c48ea
2 changed files with 103 additions and 12 deletions

View File

@@ -47,7 +47,7 @@ def check_sanity():
present_files.append(os.path.normpath(os.path.join(root, f)))
for doc in Document.objects.all():
# Check thumbnail
# Check sanity of the thumbnail
if not os.path.isfile(doc.thumbnail_path):
messages.append(SanityError(
f"Thumbnail of document {doc.pk} does not exist."))
@@ -61,7 +61,8 @@ def check_sanity():
f"Cannot read thumbnail file of document {doc.pk}: {e}"
))
# Check document
# Check sanity of the original file
# TODO: extract method
if not os.path.isfile(doc.source_path):
messages.append(SanityError(
f"Original of document {doc.pk} does not exist."))
@@ -80,22 +81,29 @@ def check_sanity():
f"Stored: {doc.checksum}, actual: {checksum}."
))
if os.path.isfile(doc.archive_path):
present_files.remove(os.path.normpath(doc.archive_path))
try:
with doc.archive_file as f:
checksum = hashlib.md5(f.read()).hexdigest()
except OSError as e:
# Check sanity of the archive file.
if doc.archive_checksum:
if not os.path.isfile(doc.archive_path):
messages.append(SanityError(
f"Cannot read archive file of document {doc.pk}: {e}"
f"Archived version of document {doc.pk} does not exist."
))
else:
if not checksum == doc.archive_checksum:
present_files.remove(os.path.normpath(doc.archive_path))
try:
with doc.archive_file as f:
checksum = hashlib.md5(f.read()).hexdigest()
except OSError as e:
messages.append(SanityError(
f"Checksum mismatch of archive {doc.pk}. "
f"Stored: {doc.checksum}, actual: {checksum}."
f"Cannot read archive file of document {doc.pk}: {e}"
))
else:
if not checksum == doc.archive_checksum:
messages.append(SanityError(
f"Checksum mismatch of archive {doc.pk}. "
f"Stored: {doc.checksum}, actual: {checksum}."
))
# other document checks
if not doc.content:
messages.append(SanityWarning(
f"Document {doc.pk} has no content."