improve md5sum generation
authorGui Chen <gui.chen@intel.com>
Fri, 23 Dec 2011 06:28:34 +0000 (14:28 +0800)
committerGui Chen <gui.chen@intel.com>
Mon, 26 Dec 2011 02:51:17 +0000 (10:51 +0800)
Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/imager/baseimager.py
mic/utils/misc.py

index ff9e873..edeefec 100644 (file)
@@ -1009,18 +1009,10 @@ class BaseImageCreator(object):
         if not self._genchecksum:
             return
 
-        """ Generate md5sum if /usr/bin/md5sum is available """
-        if os.path.exists("/usr/bin/md5sum"):
-            (rc, md5sum) = runner.runtool(["/usr/bin/md5sum", "-b", image_name])
-            if rc != 0:
-                msger.warning("Can't generate md5sum for image %s" % image_name)
-            else:
-                pattern = re.compile("\*.*$")
-                md5sum = pattern.sub("*" + os.path.basename(image_name), md5sum)
-                fd = open(image_name + ".md5sum", "w")
-                fd.write(md5sum)
-                fd.close()
-                self.outimage.append(image_name+".md5sum")
+        md5sum = misc.get_md5sum(image_name)
+        with open(image_name + ".md5sum", "w") as f:
+            f.write("%s %s" % (md5sum, os.path.basename(image_name)))
+        self.outimage.append(image_name+".md5sum")
 
     def package(self, destdir = "."):
         """Prepares the created image for final delivery.
@@ -1160,22 +1152,16 @@ class BaseImageCreator(object):
 
         # generate MANIFEST
         with open(_rpath("MANIFEST"), "w") as wf:
-            if os.path.exists("/usr/bin/md5sum"):
-                for f in os.listdir(destdir):
-                    if f == "MANIFEST": continue
-                    if os.path.isdir(os.path.join(destdir,f)):
-                        continue
-
-                    rc, md5sum = runner.runtool(["/usr/bin/md5sum", "-b", _rpath(f)])
-                    if rc != 0:
-                        msger.warning("Failed to generate md5sum for file %s" \
-                                      % _rpath(f))
-                    else:
-                        md5sum = md5sum.lstrip().split()[0]
-                        wf.write(md5sum+" "+ f +"\n")
-            else:
-                msger.warning('no md5sum tool found, no checksum string in MANIFEST')
-                wf.writelines(os.listdir(destdir))
+            for f in os.listdir(destdir):
+                if f == "MANIFEST":
+                    continue
+
+                if os.path.isdir(os.path.join(destdir, f)):
+                    continue
+
+                md5sum = misc.get_md5sum(_rpath(f))
+                wf.write("%s %s\n" % (md5sum, f))
+
         outimages.append("%s/MANIFEST" % destdir)
 
         # Filter out the nonexist file
index 89d8f40..3951730 100644 (file)
@@ -26,6 +26,11 @@ import hashlib
 import rpmmisc
 
 try:
+    from hashlib import md5
+except ImportError:
+    from md5 import md5
+
+try:
     import sqlite3 as sqlite
 except ImportError:
     import sqlite
@@ -44,6 +49,16 @@ import runner
 
 from mic import msger
 
+def get_md5sum(fpath):
+    md5sum = md5()
+    with open(fpath, 'rb') as f:
+        while True:
+            data = f.read(1024)
+            if not data:
+                break
+            md5sum.update(data)
+    return md5sum.hexdigest()
+
 def save_ksconf_file(ksconf, release="latest", arch="ia32"):
     if not os.path.exists(ksconf):
         return