From dfc3e6db921815416b8edc5892b2a7adfc677a25 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Sat, 23 Apr 2022 20:47:19 -0600 Subject: add checksum checking for mod files instead of timestamp --- src/pyssg/utils.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/pyssg/utils.py') diff --git a/src/pyssg/utils.py b/src/pyssg/utils.py index ffaf8ba..a41249a 100644 --- a/src/pyssg/utils.py +++ b/src/pyssg/utils.py @@ -1,6 +1,7 @@ import os import sys import shutil +from hashlib import md5 from logging import Logger, getLogger log: Logger = getLogger(__name__) @@ -54,15 +55,15 @@ def get_dir_structure(path: str, return [o.replace(path, '')[1:] for o in out] -def create_dir(path: str, p: bool=False) -> None: +def create_dir(path: str, p: bool=False, silent=False) -> None: try: if p: os.makedirs(path) else: os.mkdir(path) - log.info('created directory "%s"', path) + if not silent: log.info('created directory "%s"', path) except FileExistsError: - log.info('directory "%s" already exists, ignoring', path) + if not silent: log.info('directory "%s" already exists, ignoring', path) def copy_file(src: str, dst: str) -> None: @@ -78,3 +79,14 @@ def sanity_check_path(path: str) -> None: log.error('"$" character found in path "%s";' ' could be due to non-existant env var.', path) sys.exit(1) + + +# as seen in SO: https://stackoverflow.com/a/1131238 +def get_checksum(path: str) -> str: + log.debug('calculating md5 checksum for "%s"', path) + file_hash = md5() + with open(path, "rb") as f: + while chunk := f.read(4096): + file_hash.update(chunk) + + return file_hash.hexdigest() \ No newline at end of file -- cgit v1.2.3-54-g00ecf