aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
authorBonfaceKilz2021-02-12 15:18:21 +0300
committerBonfaceKilz2021-02-12 15:36:41 +0300
commit21a4a847456fde5fcc6072df0d0fc36992da283d (patch)
treef6921e2e740f3ce3f528258044aad1dbf938bd1e /gn3
parentedd18bffdb179db75769b5a47b9258e5eded5aaf (diff)
downloadgenenetwork3-21a4a847456fde5fcc6072df0d0fc36992da283d.tar.gz
Add function for computing the hash of a directory
Diffstat (limited to 'gn3')
-rw-r--r--gn3/__init__.py0
-rw-r--r--gn3/file_utils.py20
2 files changed, 20 insertions, 0 deletions
diff --git a/gn3/__init__.py b/gn3/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gn3/__init__.py
diff --git a/gn3/file_utils.py b/gn3/file_utils.py
new file mode 100644
index 0000000..8e342c9
--- /dev/null
+++ b/gn3/file_utils.py
@@ -0,0 +1,20 @@
+"""Procedures that operate on files/ directories"""
+import hashlib
+import os
+
+from functools import partial
+
+
+def get_dir_hash(directory: str) -> str:
+ """Return the hash of a DIRECTORY"""
+ md5hash = hashlib.md5()
+ if not os.path.exists(directory):
+ raise FileNotFoundError
+ for root, _, files in os.walk(directory):
+ for names in files:
+ file_path = os.path.join(root, names)
+ with open(file_path, "rb") as file_:
+ for buf in iter(partial(file_.read, 4096), b''):
+ md5hash.update(bytearray(hashlib.md5(buf).hexdigest(),
+ "utf-8"))
+ return md5hash.hexdigest()