Add sha1sum and sha256sum
authorlihongsx <lihongx.sun@intel.com>
Mon, 4 Aug 2014 15:23:47 +0000 (23:23 +0800)
committeradmin <yuhuan.yang@samsung.com>
Thu, 4 Feb 2016 10:21:06 +0000 (18:21 +0800)
Fixes: #1956

Change-Id: I9eca40309e4a7eabb1cbc46424266312b3f2b10c

mic/imager/baseimager.py
mic/utils/misc.py

index 63ac3b9..dc7807d 100644 (file)
@@ -1309,22 +1309,32 @@ class BaseImageCreator(object):
             os.rename(_rpath(f), _rpath(newf))
             outimages.append(_rpath(newf))
 
-        # generate MD5SUMS
-        with open(_rpath("MD5SUMS"), "w") as wf:
-            for f in os.listdir(destdir):
-                if f == "MD5SUMS":
-                    continue
-
-                if os.path.isdir(os.path.join(destdir, f)):
-                    continue
-
-                md5sum = misc.get_md5sum(_rpath(f))
-                # There needs to be two spaces between the sum and
-                # filepath to match the syntax with md5sum.
-                # This way also md5sum -c MD5SUMS can be used by users
-                wf.write("%s  %s\n" % (md5sum, f))
-
-        outimages.append("%s/MD5SUMS" % destdir)
+        # generate MD5SUMS SHA1SUMS SHA256SUMS
+        def generate_hashsum(hash_name, hash_method):
+            with open(_rpath(hash_name), "w") as wf:
+                for f in os.listdir(destdir):
+                    if f.endswith('SUMS'):
+                        continue
+
+                    if os.path.isdir(os.path.join(destdir, f)):
+                        continue
+
+                    hash_value = hash_method(_rpath(f))
+                    # There needs to be two spaces between the sum and
+                    # filepath to match the syntax with md5sum,sha1sum,
+                    # sha256sum. This way also *sum -c *SUMS can be used.
+                    wf.write("%s  %s\n" % (hash_value, f))
+
+            outimages.append("%s/%s" % (destdir, hash_name))
+
+        hash_dict = {
+                     'MD5SUMS'    : misc.get_md5sum,
+                     'SHA1SUMS'   : misc.get_sha1sum,
+                     'SHA256SUMS' : misc.get_sha256sum
+                    }
+
+        for k, v in hash_dict.items():
+            generate_hashsum(k, v)
 
         # Filter out the nonexist file
         for fp in outimages[:]:
index cd3e01a..bf377b4 100755 (executable)
@@ -261,6 +261,11 @@ def calc_hashes(file_path, hash_names, start = 0, end = None):
 def get_md5sum(fpath):
     return calc_hashes(fpath, ('md5', ))[0]
 
+def get_sha1sum(fpath):
+    return calc_hashes(fpath, ('sha1', ))[0]
+
+def get_sha256sum(fpath):
+    return calc_hashes(fpath, ('sha256', ))[0]
 
 def normalize_ksfile(ksconf, release, arch):
     '''