]> git.ozlabs.org Git - patchwork/commitdiff
Eliminate hashlib requirement
authorNate Case <ncase@xes-inc.com>
Fri, 22 Aug 2008 20:58:02 +0000 (15:58 -0500)
committerJeremy Kerr <jk@ozlabs.org>
Sat, 23 Aug 2008 02:51:05 +0000 (10:51 +0800)
If the hashlib module does not exist, use sha and md5 modules instead
to support Python 2.4.  The hashlib module was added to Python 2.5.

Signed-off-by: Nate Case <ncase@xes-inc.com>
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
apps/patchwork/models.py
apps/patchwork/parser.py

index a40931aa1c3af6f7079dcc761f310bf2df9b8c91..eef4b69ba6ca7db46965e827a5854ef871c2a082 100644 (file)
@@ -28,7 +28,6 @@ import re
 import datetime, time
 import string
 import random
-import hashlib
 from email.mime.text import MIMEText
 import email.utils
 
@@ -167,10 +166,27 @@ class HashField(models.Field):
 
     def __init__(self, algorithm = 'sha1', *args, **kwargs):
         self.algorithm = algorithm
+        try:
+            import hashlib
+            self.hashlib = True
+        except ImportError:
+            self.hashlib = False
+            if algorithm == 'sha1':
+                import sha
+                self.hash_constructor = sha.new
+            elif algorithm == 'md5':
+                import md5
+                self.hash_constructor = md5.new
+            else:
+                raise NameError("Unknown algorithm '%s'" % algorithm)
+            
         super(HashField, self).__init__(*args, **kwargs)
 
     def db_type(self):
-        n_bytes = len(hashlib.new(self.algorithm).digest())
+        if self.hashlib:
+            n_bytes = len(hashlib.new(self.algorithm).digest())
+        else:
+            n_bytes = len(self.hash_constructor().digest())
        if settings.DATABASE_ENGINE == 'postgresql':
            return 'bytea'
        elif settings.DATABASE_ENGINE == 'mysql':
index ecc1d4b0f1e5ce962f4d37a3afcea23867769d90..bb6fce022a98a4bb61b734ac2a5189878e1c6491 100644 (file)
 
 
 import re
-import hashlib
+try:
+    import hashlib
+    sha1_hash = hashlib.sha1
+except ImportError:
+    import sha
+    sha1_hash = sha.sha
 
 _hunk_re = re.compile('^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
 _filename_re = re.compile('^(---|\+\+\+) (\S+)')
@@ -158,7 +163,7 @@ def patch_hash(str):
     lines = str.split('\n')
 
     prefixes = ['-', '+', ' ']
-    hash = hashlib.sha1()
+    hash = sha1_hash()
 
     for line in str.split('\n'):