summaryrefslogtreecommitdiffstats
path: root/src/utils/fs.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/fs.py')
-rw-r--r--src/utils/fs.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/utils/fs.py b/src/utils/fs.py
index d6cf36a..8446f08 100644
--- a/src/utils/fs.py
+++ b/src/utils/fs.py
@@ -8,6 +8,7 @@
import logging
import os
+import hashlib
import subprocess
import shlex
from src.log import OgError
@@ -340,3 +341,19 @@ def _extend_ntfsresize(partdev):
proc = subprocess.run(cmd, input=b'y')
if proc.returncode != 0:
raise OgError(f'Error growing ntfs filesystem at {partdev}')
+
+def compute_md5(path, bs=2**20):
+ if not os.path.exists(path):
+ raise OgError(f"Failed to calculate checksum, image file {path} does not exist")
+
+ m = hashlib.md5()
+ try:
+ with open(path, 'rb') as f:
+ while True:
+ buf = f.read(bs)
+ if not buf:
+ break
+ m.update(buf)
+ except Exception as e:
+ raise OgError(f'Failed to calculate checksum for {path}: {e}') from e
+ return m.hexdigest()