summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2017-09-12 11:36:01 -0400
committerTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2017-09-12 11:36:01 -0400
commit08da7cc715c50e808c99d1cfdcef9b1cadcaa59a (patch)
treee57d877cc79904dbf34dd77bd0760301d8a3141a
parentf9e6fadee05c14b83fe75cf107f271dc09f60044 (diff)
downloadbuildstream-08da7cc715c50e808c99d1cfdcef9b1cadcaa59a.tar.gz
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.
-rw-r--r--buildstream/utils.py21
1 files changed, 21 insertions, 0 deletions
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