From 77de8493520c9a58b9bc8d87fc9badc79b5ef9c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= <sybren@stuvel.eu>
Date: Fri, 9 Jun 2017 14:23:23 +0200
Subject: [PATCH] Added SHA256 checksum to "setup.py zip"

---
 setup.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/setup.py b/setup.py
index 2a5a5dab..634e12a1 100755
--- a/setup.py
+++ b/setup.py
@@ -2,6 +2,7 @@
 
 from pathlib import Path
 import collections
+import hashlib
 import setuptools
 import sys
 import zipfile
@@ -66,7 +67,18 @@ class ZipCommand(Command):
                 log.info('    adding %s', this_path)
                 archive.write(str(this_path), str(this_path))
 
-
+        # Compute SHA256 checksum of the produced zip file.
+        hasher = hashlib.sha256()
+        blocksize = 65536
+        with zip_name.open(mode='rb') as infile:
+            buf = infile.read(blocksize)
+            while len(buf) > 0:
+                hasher.update(buf)
+                buf = infile.read(blocksize)
+        checksum_path = zip_name.with_suffix('.sha256')
+        log.info('Writing SHA256 checksum to %s', checksum_path)
+        with checksum_path.open(mode='w') as shafile:
+            print('%s  %s' % (hasher.hexdigest(), zip_name.name), file=shafile)
 
 if __name__ == '__main__':
     setuptools.setup(
-- 
GitLab