# HG changeset patch # User cmlenz # Date 1186476664 0 # Node ID 8ba7bf659f710427baa514c225977ef6eebedc3f # Parent 9e702a1f8df3e4070e1e37fe58b5467b25bfdbc5 Remove MD5 checksum code that is no longer needed. diff --git a/bitten/util/md5sum.py b/bitten/util/md5sum.py deleted file mode 100644 --- a/bitten/util/md5sum.py +++ /dev/null @@ -1,91 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2005 Christopher Lenz -# All rights reserved. -# -# This software is licensed as described in the file COPYING, which -# you should have received as part of this distribution. The terms -# are also available at http://bitten.cmlenz.net/wiki/License. - -"""Convenience functions for creating and validating MD5 checksums for files.""" - -import md5 -import os - - -class IntegrityError(Exception): - """Exception raised when checksum validation fails.""" - - -def generate(filename): - """Generate an MD5 checksum for the specified file. - - @param filename: the absolute path to the file - @return: string containing the checksum - """ - checksum = md5.new() - fileobj = file(filename, 'rb') - try: - while True: - chunk = fileobj.read(4096) - if not chunk: - break - checksum.update(chunk) - finally: - fileobj.close() - return checksum.hexdigest() - -def write(filename, md5file=None): - """Write an MD5 checksum file for the specified file. - - @param filename: absolute path to the file - @param md5file: absolute path to the MD5 checksum file to create (optional) - @return: the absolute path to the created checksum file - - If the `md5file` parameter is omitted, this function will write the checksum - to a file alongside the orignal file, with an added `.md5` extension. - """ - if md5file is None: - md5file = filename + '.md5' - - fileobj = file(md5file, 'w') - try: - fileobj.write(generate(filename) + ' ' + os.path.basename(filename)) - finally: - fileobj.close() - return md5file - -def validate(filename, checksum=None): - """Check the integrity of a specified file against an MD5 checksum. - - @param filename: the absolute path to the file - @param checksum: string containing the checksum (optional) - - If the second parameter is omitted, this function will look for a file with - an `.md5` extension alongside the original file, and try to read the - checksum from that file. If no such file is found, an `IntegrityError` is - raised. - - If the file does not match the checksum, an `IntegrityError` is raised. - """ - if checksum is None: - md5file = filename + '.md5' - if not os.path.isfile(md5file): - md5file = os.path.splitext(filename)[0] + '.md5' - if not os.path.isfile(md5file): - raise IntegrityError('Checksum file not found') - fileobj = file(md5file, 'r') - try: - content = fileobj.read() - finally: - fileobj.close() - try: - checksum, path = content.split(' ') - except ValueError: - raise IntegrityError('Checksum file invalid') - if path != os.path.basename(filename): - raise IntegrityError('Checksum for a different file') - - expected = generate(filename) - if expected != checksum: - raise IntegrityError('Checksum does not match') diff --git a/bitten/util/tests/__init__.py b/bitten/util/tests/__init__.py --- a/bitten/util/tests/__init__.py +++ b/bitten/util/tests/__init__.py @@ -11,11 +11,9 @@ import unittest from bitten.util import xmlio -from bitten.util.tests import md5sum def suite(): suite = unittest.TestSuite() - suite.addTest(md5sum.suite()) suite.addTest(doctest.DocTestSuite(xmlio)) return suite diff --git a/bitten/util/tests/md5sum.py b/bitten/util/tests/md5sum.py deleted file mode 100644 --- a/bitten/util/tests/md5sum.py +++ /dev/null @@ -1,97 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2005 Christopher Lenz -# All rights reserved. -# -# This software is licensed as described in the file COPYING, which -# you should have received as part of this distribution. The terms -# are also available at http://bitten.cmlenz.net/wiki/License. - -import md5 -import os -import shutil -import tempfile -import unittest - -from bitten.util import md5sum - - -class Md5sumTestCase(unittest.TestCase): - - def setUp(self): - self.tempdir = os.path.realpath(tempfile.mkdtemp(suffix='bitten_test')) - - def tearDown(self): - shutil.rmtree(self.tempdir) - - def _create_file(self, name, content=None): - filename = os.path.join(self.tempdir, name) - fd = file(filename, 'w') - if content: - fd.write(content) - fd.close() - return filename - - def test_generate(self): - filename = self._create_file('test.xyz', 'Foo bar') - checksum = md5sum.generate(filename) - self.assertEqual(md5.new('Foo bar').hexdigest(), checksum) - - def test_write(self): - filename = self._create_file('test.xyz', 'Foo bar') - md5file = md5sum.write(filename) - self.assertEqual(filename + '.md5', md5file) - fileobj = file(md5file, 'r') - try: - checksum, path = fileobj.read().split(' ') - finally: - fileobj.close() - self.assertEqual(md5.new('Foo bar').hexdigest(), checksum) - self.assertEqual('test.xyz', path) - - def test_write_with_md5file(self): - filename = self._create_file('test.xyz', 'Foo bar') - md5file = os.path.join(self.tempdir, 'test.md5') - self.assertEqual(md5file, md5sum.write(filename, md5file=md5file)) - fileobj = file(md5file, 'r') - try: - checksum, path = fileobj.read().split(' ') - finally: - fileobj.close() - self.assertEqual(md5.new('Foo bar').hexdigest(), checksum) - self.assertEqual('test.xyz', path) - - def test_validate_missing(self): - filename = self._create_file('test.xyz', 'Foo bar') - self.assertRaises(md5sum.IntegrityError, md5sum.validate, filename) - - def test_validate_incorrect_digest(self): - filename = self._create_file('test.xyz', 'Foo bar') - checksum = md5.new('Foo baz').hexdigest() + ' ' + filename - md5file = self._create_file('test.xyz.md5', checksum) - self.assertRaises(md5sum.IntegrityError, md5sum.validate, filename) - - def test_validate_invalid_format(self): - filename = self._create_file('test.xyz', 'Foo bar') - checksum = md5.new('Foo bar').hexdigest() + ',' + filename - md5file = self._create_file('test.xyz.md5', checksum) - self.assertRaises(md5sum.IntegrityError, md5sum.validate, filename) - - def test_validate_incorrect_path(self): - filename = self._create_file('test.xyz', 'Foo bar') - checksum = md5.new('Foo bar').hexdigest() + ' ' + '/etc/test' - md5file = self._create_file('test.xyz.md5', checksum) - self.assertRaises(md5sum.IntegrityError, md5sum.validate, filename) - - def test_validate_with_checksum(self): - filename = self._create_file('test.xyz', 'Foo bar') - md5sum.validate(filename, md5.new('Foo bar').hexdigest()) - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(Md5sumTestCase, 'test')) - return suite - -if __name__ == '__main__': - unittest.main(defaultTest='suite')