changeset 394:bfd1c6a155fa treepkg-status

added md5sum function
author Bjoern Ricks <bricks@intevation.de>
date Fri, 09 Jul 2010 12:42:42 +0000
parents 5fe26e7f6e2d
children 0ba451c4a856
files test/test_util.py treepkg/util.py
diffstat 2 files changed, 29 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/test/test_util.py	Fri Jul 09 10:20:49 2010 +0000
+++ b/test/test_util.py	Fri Jul 09 12:42:42 2010 +0000
@@ -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)
--- a/treepkg/util.py	Fri Jul 09 10:20:49 2010 +0000
+++ b/treepkg/util.py	Fri Jul 09 12:42:42 2010 +0000
@@ -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()
+
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)