From 08da7cc715c50e808c99d1cfdcef9b1cadcaa59a Mon Sep 17 00:00:00 2001 From: Tristan Van Berkom Date: Tue, 12 Sep 2017 11:36:01 -0400 Subject: utils.py: Added sha256sum utility This little bit of code was being repeated a bunch of times already throughout buildstream, looks sane enough to offer a utility. --- buildstream/utils.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/buildstream/utils.py b/buildstream/utils.py index c923eb795..17b3de41f 100644 --- a/buildstream/utils.py +++ b/buildstream/utils.py @@ -159,6 +159,27 @@ def glob(paths, pattern): yield filename +def sha256sum(filename): + """Calculate the sha256sum of a file + + Args: + filename (str): A path to a file on disk + + Returns: + (str): An sha256 checksum string + + Raises: + OSError: In the case there was an issue opening + or reading `filename` + """ + h = hashlib.sha256() + with open(filename, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + h.update(chunk) + + return h.hexdigest() + + def safe_copy(src, dest, result=None): """Copy a file while preserving attributes -- cgit v1.2.1