From 52d5cf27aba2ea35cf9832282add823a4706358d Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 29 Nov 2010 19:51:17 -0200 Subject: added g_octave.checksum module, with tests --- g_octave/checksum.py | 30 ++++++++++++++++++++++++++++++ tests/test_checksum.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 g_octave/checksum.py create mode 100644 tests/test_checksum.py diff --git a/g_octave/checksum.py b/g_octave/checksum.py new file mode 100644 index 0000000..79fe969 --- /dev/null +++ b/g_octave/checksum.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +""" + checksum.py + ~~~~~~~~~~~ + + This module implements functions for compute/generate SHA1 checksums + for files. + + :copyright: (c) 2010 by Rafael Goncalves Martins + :license: GPL-2, see LICENSE for more details. +""" + +from __future__ import absolute_import + +__all__ = [ + 'sha1_compute', + 'sha1_check', +] + +from .compat import open +from hashlib import sha1 + + +def sha1_compute(filename): + with open(filename) as fp: + return sha1(fp.read()).hexdigest() + +def sha1_check(filename, checksum): + return sha1_compute(filename) == checksum diff --git a/tests/test_checksum.py b/tests/test_checksum.py new file mode 100644 index 0000000..66cd96b --- /dev/null +++ b/tests/test_checksum.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + test_checksum.py + ~~~~~~~~~~~~~~~~ + + test suite for the *g_octave.checksum* module + + :copyright: (c) 2010 by Rafael Goncalves Martins + :license: GPL-2, see LICENSE for more details. +""" + +import os +import tempfile +import unittest + +from g_octave import checksum + + +class TestChecksum(unittest.TestCase): + + def setUp(self): + self._tempfile = tempfile.mkstemp()[1] + with open(self._tempfile, 'w') as fp: + # SHA1 checksum: 8aa49f56d049193b183cb2918f8fb59e0caf1283 + fp.write("I'm the walrus\n") + + def test_checksum(self): + my_checksum = checksum.sha1_compute(self._tempfile) + self.assertEqual(my_checksum, '8aa49f56d049193b183cb2918f8fb59e0caf1283') + self.assertTrue(checksum.sha1_check(self._tempfile, my_checksum)) + + def tearDown(self): + os.unlink(self._tempfile) + +def suite(): + suite = unittest.TestSuite() + suite.addTest(TestChecksum('test_checksum')) + return suite -- cgit v1.2.3-65-gdbad