diff options
author | Nate Case <ncase@xes-inc.com> | 2008-08-22 15:58:02 -0500 |
---|---|---|
committer | Jeremy Kerr <jk@ozlabs.org> | 2008-08-23 10:51:05 +0800 |
commit | 97d8d152a5f832f6266a33622e25f4ae5f7dc068 (patch) | |
tree | 71de5d5cf6ec9aafadc384a2fd5bd3bf77bb36ae /apps/patchwork/models.py | |
parent | 8252a61e73126c2b89c97e4195c635bc3d60a3e9 (diff) | |
download | patchwork-97d8d152a5f832f6266a33622e25f4ae5f7dc068.tar.bz2 patchwork-97d8d152a5f832f6266a33622e25f4ae5f7dc068.tar.xz |
Eliminate hashlib requirement
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>
Diffstat (limited to 'apps/patchwork/models.py')
-rw-r--r-- | apps/patchwork/models.py | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py index a40931a..eef4b69 100644 --- a/apps/patchwork/models.py +++ b/apps/patchwork/models.py @@ -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': |