aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2010-11-29 19:51:17 -0200
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2010-11-29 19:51:17 -0200
commit52d5cf27aba2ea35cf9832282add823a4706358d (patch)
treea92a781eabefa917e19e872215492651f116d34c
parentpreparing for the release (diff)
downloadg-octave-52d5cf27aba2ea35cf9832282add823a4706358d.tar.gz
g-octave-52d5cf27aba2ea35cf9832282add823a4706358d.tar.bz2
g-octave-52d5cf27aba2ea35cf9832282add823a4706358d.zip
added g_octave.checksum module, with tests
-rw-r--r--g_octave/checksum.py30
-rw-r--r--tests/test_checksum.py40
2 files changed, 70 insertions, 0 deletions
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