# HG changeset patch # User Bjoern Ricks # Date 1278679362 0 # Node ID bfd1c6a155fac8d5e00fc01577e3b223309e6321 # Parent 5fe26e7f6e2dbefbb772eee0fb12de44305ad2c1 added md5sum function diff -r 5fe26e7f6e2d -r bfd1c6a155fa test/test_util.py --- 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) diff -r 5fe26e7f6e2d -r bfd1c6a155fa treepkg/util.py --- 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() +