[py-svn] r32521 - in py/dist/py/path: . local local/testing

hpk at codespeak.net hpk at codespeak.net
Wed Sep 20 12:40:13 CEST 2006


Author: hpk
Date: Wed Sep 20 12:40:12 2006
New Revision: 32521

Modified:
   py/dist/py/path/common.py
   py/dist/py/path/local/local.py
   py/dist/py/path/local/testing/test_local.py
Log:
add hashing function for local paths


Modified: py/dist/py/path/common.py
==============================================================================
--- py/dist/py/path/common.py	(original)
+++ py/dist/py/path/common.py	Wed Sep 20 12:40:12 2006
@@ -226,6 +226,15 @@
             __tracebackhide__ = True
             raise cls, value
 
+    def _gethashinstance(self, hashtype):
+        if hashtype == "md5": 
+            return py.std.md5.md5()
+        elif hashtype == "sha": 
+            return py.std.sha.sha()
+        else:
+            raise ValueError("unknown hash type: %r" %(hashtype,))
+
+
 class fnmatch:
     def __init__(self, pattern):
         self.pattern = pattern

Modified: py/dist/py/path/local/local.py
==============================================================================
--- py/dist/py/path/local/local.py	(original)
+++ py/dist/py/path/local/local.py	Wed Sep 20 12:40:12 2006
@@ -73,6 +73,19 @@
     def __hash__(self):
         return hash(self.strpath)
 
+    def computehash(self, hashtype="md5", chunksize=524288):
+        """ return hexdigest of hashvalue for this file. """
+        hash = self._gethashinstance(hashtype)
+        f = self.open('rb')
+        try:
+            while 1:
+                buf = f.read(chunksize)
+                if not buf:
+                    return hash.hexdigest()
+                hash.update(buf) 
+        finally:
+            f.close()
+
     def new(self, **kw):
         """ create a modified version of this path.
             the following keyword arguments modify various path parts:

Modified: py/dist/py/path/local/testing/test_local.py
==============================================================================
--- py/dist/py/path/local/testing/test_local.py	(original)
+++ py/dist/py/path/local/testing/test_local.py	Wed Sep 20 12:40:12 2006
@@ -16,6 +16,14 @@
 
 class TestLocalPath(LocalSetup, CommonFSTests):
 
+    def test_gethash(self):
+        import md5
+        import sha
+        fn = self.tmpdir.join("testhashfile")
+        fn.write("hello")
+        assert fn.computehash("md5") == md5.md5("hello").hexdigest()
+        assert fn.computehash("sha") == sha.sha("hello").hexdigest()
+
     def test_remove_removes_readonly_file(self):
         readonly_file = self.tmpdir.join('readonly').ensure()
         readonly_file.chmod(0)



More information about the pytest-commit mailing list