[Treepkg-commits] r354 - in branches/treepkg-status: test treepkg

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Jul 9 14:42:43 CEST 2010


Author: bricks
Date: 2010-07-09 14:42:42 +0200 (Fri, 09 Jul 2010)
New Revision: 354

Modified:
   branches/treepkg-status/test/test_util.py
   branches/treepkg-status/treepkg/util.py
Log:
added md5sum function


Modified: branches/treepkg-status/test/test_util.py
===================================================================
--- branches/treepkg-status/test/test_util.py	2010-07-09 10:20:49 UTC (rev 353)
+++ branches/treepkg-status/test/test_util.py	2010-07-09 12:42:42 UTC (rev 354)
@@ -12,7 +12,7 @@
 
 from filesupport import FileTestMixin
 
-from treepkg.util import replace_in_file, listdir_abs
+from treepkg.util import replace_in_file, listdir_abs, md5sum
 
 
 class TestReplaceInFile(unittest.TestCase, FileTestMixin):
@@ -72,3 +72,13 @@
         self.assertEquals(sorted(listdir_abs(directory, '*.dsc')),
                           [os.path.join(directory, "foo.dsc")])
 
+class TestMd5sum(unittest.TestCase, FileTestMixin):
+
+    content = "this is a test content"
+
+    def setUp(self):
+        self.testfile = self.create_temp_file("testmd5.txt", self.content)
+
+    def test_md5sum(self):
+        sum = md5sum(self.testfile)
+        self.assertEquals("a12511153555c1f0f0a1eda200733a3f", sum)

Modified: branches/treepkg-status/treepkg/util.py
===================================================================
--- branches/treepkg-status/treepkg/util.py	2010-07-09 10:20:49 UTC (rev 353)
+++ branches/treepkg-status/treepkg/util.py	2010-07-09 12:42:42 UTC (rev 354)
@@ -13,6 +13,8 @@
 import shutil
 import fnmatch
 import pwd
+import os.path
+import hashlib
 
 import run
 
@@ -156,3 +158,19 @@
 def getuser():
     """Returns the login name of the current user owning the proccess"""
     return pwd.getpwuid(os.getuid())[0]
+
+def md5sum(filename):
+    """ calculates the md5sum of a file """
+    if not os.path.isfile(filename):
+        raise RuntimeError("Could not create md5sum. File not found: %s"
+                            % filename)
+    f = file(filename, 'rb')
+    m = hashlib.md5()
+    while True:
+        d = f.read(8096)
+        if not d:
+           break
+        m.update(d)
+    f.close()
+    return m.hexdigest()
+



More information about the Treepkg-commits mailing list