From 21a4a847456fde5fcc6072df0d0fc36992da283d Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Fri, 12 Feb 2021 15:18:21 +0300 Subject: Add function for computing the hash of a directory --- gn3/__init__.py | 0 gn3/file_utils.py | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 gn3/__init__.py create mode 100644 gn3/file_utils.py (limited to 'gn3') diff --git a/gn3/__init__.py b/gn3/__init__.py new file mode 100644 index 0000000..e69de29 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() -- cgit v1.2.3