summaryrefslogtreecommitdiffstats
path: root/cli/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'cli/utils.py')
-rw-r--r--cli/utils.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/cli/utils.py b/cli/utils.py
index 3825583..380bd5b 100644
--- a/cli/utils.py
+++ b/cli/utils.py
@@ -8,7 +8,9 @@
import unicodedata
import json
import ipaddress
+import hashlib
import re
+import os
def scope_lookup(scope_id, scope_type, d):
if scope_id == d.get('id') and scope_type == d.get('type'):
@@ -69,3 +71,21 @@ def check_mac_address(addr):
def remove_accents(text):
normalized_text = unicodedata.normalize('NFD', text)
return ''.join(c for c in normalized_text if unicodedata.category(c) != 'Mn')
+
+def compute_md5(path, bs=2**20):
+ if not os.path.exists(path):
+ print(f"Failed to calculate checksum, image file {path} does not exist")
+ return None
+
+ 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:
+ print(f'Failed to calculate checksum for {path}: {e}')
+ return None
+ return m.hexdigest()