summaryrefslogtreecommitdiff
path: root/lib/git/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/git/utils.py')
-rw-r--r--lib/git/utils.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/git/utils.py b/lib/git/utils.py
index 2aa8d33e..cdc7c55b 100644
--- a/lib/git/utils.py
+++ b/lib/git/utils.py
@@ -6,6 +6,58 @@
import os
+try:
+ import hashlib
+except ImportError:
+ import sha
+
+def make_sha(source=''):
+ """
+ A python2.4 workaround for the sha/hashlib module fiasco
+
+ Note
+ From the dulwich project
+ """
+ try:
+ return hashlib.sha1(source)
+ except NameError:
+ sha1 = sha.sha(source)
+ return sha1
+
+
+class SHA1Writer(object):
+ """
+ Wrapper around a file-like object that remembers the SHA1 of
+ the data written to it. It will write a sha when the stream is closed
+ or if the asked for explicitly usign write_sha.
+
+ Note:
+ Based on the dulwich project
+ """
+ __slots__ = ("f", "sha1")
+
+ def __init__(self, f):
+ self.f = f
+ self.sha1 = make_sha("")
+
+ def write(self, data):
+ self.sha1.update(data)
+ self.f.write(data)
+
+ def write_sha(self):
+ sha = self.sha1.digest()
+ self.f.write(sha)
+ return sha
+
+ def close(self):
+ sha = self.write_sha()
+ self.f.close()
+ return sha
+
+ def tell(self):
+ return self.f.tell()
+
+
class LazyMixin(object):
"""
Base class providing an interface to lazily retrieve attribute values upon