bootstrap/template.py: add sha1sum support
authorJoe Guo <joeg@catalyst.net.nz>
Tue, 19 Mar 2019 23:26:48 +0000 (12:26 +1300)
committerStefan Metzmacher <metze@samba.org>
Thu, 18 Apr 2019 12:09:34 +0000 (12:09 +0000)
1. calc sha1sum for files under bootstrap/ (except README.md
   and *.pyc files) after render and write to bootstrap/sha1sum.txt file.
2. add a new option to print sha1sum, so we can use it to compare in ci

Pair-Programmed-With: Stefan Metzmacher <metze@samba.org>

Signed-off-by: Joe Guo <joeg@catalyst.net.nz>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
bootstrap/template.py

index 1e101c8f0e05c9f478f4ba8feb57fba236f0636f..e12e2e0dc828dacb6cb0927aa4759963ba975abb 100755 (executable)
@@ -25,14 +25,65 @@ Author: Joe Guo <joeg@catalyst.net.nz>
 
 import io
 import os
 
 import io
 import os
+import hashlib
 import logging
 import argparse
 from config import DISTS, VAGRANTFILE, OUT
 
 import logging
 import argparse
 from config import DISTS, VAGRANTFILE, OUT
 
+HERE = os.path.abspath(os.path.dirname(__file__))
+SHA1SUM_FILE_PATH = os.path.join(HERE, 'sha1sum.txt')
+README_FILE_PATH = os.path.join(HERE, 'READMD.md')
+
 logging.basicConfig(level='INFO')
 log = logging.getLogger(__file__)
 
 
 logging.basicConfig(level='INFO')
 log = logging.getLogger(__file__)
 
 
+def get_files(path):
+    """Get all files recursively in path as a list"""
+    filepaths = []
+    for root, dirnames, filenames in os.walk(path):
+        for filename in filenames:
+            filepath = os.path.join(root, filename)
+            filepaths.append(filepath)
+    return filepaths
+
+
+def get_sha1sum(debug=False):
+    """Get sha1sum for dists + .gitlab-ci.yml"""
+    filepaths = get_files(HERE)
+    m = hashlib.sha1()
+    i = 0
+    for filepath in sorted(list(filepaths)):
+        _filepath = os.path.relpath(filepath)
+        i += 1
+        if filepath == SHA1SUM_FILE_PATH:
+            d = "skip                                    "
+            if debug:
+                print("%s: %s: %s" % (i, d, _filepath))
+            continue
+        if filepath == README_FILE_PATH:
+            d = "skip                                    "
+            if debug:
+                print("%s: %s: %s" % (i, d, _filepath))
+            continue
+        if filepath.endswith('.pyc'):
+            d = "skip                                    "
+            if debug:
+                print("%s: %s: %s" % (i, d, _filepath))
+            continue
+        with io.open(filepath, mode='rb') as _file:
+            _bytes = _file.read()
+
+            m1 = hashlib.sha1()
+            m1.update(_bytes)
+            d = m1.hexdigest()
+            if debug:
+                print("%s: %s: %s" % (i, d, _filepath))
+
+            m.update(_bytes)
+    return m.hexdigest()
+
+
 def render(dists):
     """Render files for all dists"""
     for dist, config in dists.items():
 def render(dists):
     """Render files for all dists"""
     for dist, config in dists.items():
@@ -52,6 +103,12 @@ def render(dists):
     with io.open(path, mode='wt', encoding='utf8') as fp:
         fp.write(VAGRANTFILE)
 
     with io.open(path, mode='wt', encoding='utf8') as fp:
         fp.write(VAGRANTFILE)
 
+    # always calc sha1sum after render
+    sha1sum = get_sha1sum()
+    log.info('write sha1sum to %s: %s', SHA1SUM_FILE_PATH, sha1sum)
+    with io.open(SHA1SUM_FILE_PATH, mode='wt', encoding='utf8') as fp:
+        fp.write(sha1sum + "\n")
+
 
 def main():
     parser = argparse.ArgumentParser(
 
 def main():
     parser = argparse.ArgumentParser(
@@ -62,11 +119,22 @@ def main():
     parser.add_argument(
         '-r', '--render', action='store_true', help='Render templates')
 
     parser.add_argument(
         '-r', '--render', action='store_true', help='Render templates')
 
+    parser.add_argument(
+        '-s', '--sha1sum', action='store_true', help='Print sha1sum')
+    parser.add_argument(
+        '-d', '--debug', action='store_true', help='Debug sha1sum')
+
     args = parser.parse_args()
     args = parser.parse_args()
+    need_help = True
 
     if args.render:
         render(DISTS)
 
     if args.render:
         render(DISTS)
-    else:
+        need_help = False
+    if args.sha1sum:
+        # we will use the output to check sha1sum in ci
+        print(get_sha1sum(args.debug))
+        need_help = False
+    if need_help:
         parser.print_help()
 
 
         parser.print_help()