diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-10-02 14:50:14 +0200 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-10-02 14:50:14 +0200 |
commit | 0f167cf29fd51b9e95146c4e93dd0f046e82de44 (patch) | |
tree | 510d00f8f5b024c0e6c397d888c96b16f8188d5b /src/utils/fs.py | |
parent | cc70274079a0de78497011e2c665838522b6e1f5 (diff) |
src: consolidate compute_md5 functions
Add compute_md5 function in src/utils/fs.py
Remove identical md5 functions from src/live/ogOperations.py and
src/utils/tiptorrent.py
Move error checks from ogOperations.py into compute_md5 function in
src/utils/fs.py
Diffstat (limited to 'src/utils/fs.py')
-rw-r--r-- | src/utils/fs.py | 17 |
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() |