Commit 2c6d0634 by Sander Smits

moved md5 versioning to hash/__init__.py

added sha1 versioning to hash versioning
parent efd2e5d9
import cStringIO import cStringIO
import md5 from hashlib import md5, sha1
import os import os
from compress.conf import settings from compress.conf import settings
from compress.utils import concat, get_output_filename from compress.utils import concat, get_output_filename
from compress.versioning.base import VersioningBase from compress.versioning.base import VersioningBase
def get_md5(f, CHUNK=2**16): class HashVersioningBase(VersioningBase):
m = md5.new() def __init__(self, hash_method):
while 1: self.hash_method = hash_method
chunk = f.read(CHUNK)
if not chunk:
break
m.update(chunk)
return m.hexdigest()
class MD5Versioning(VersioningBase):
def get_version(self, source_files):
buf = concat(source_files)
s = cStringIO.StringIO(buf)
version = get_md5(s)
s.close()
return version
def needs_update(self, output_file, source_files, version): def needs_update(self, output_file, source_files, version):
output_file_name = get_output_filename(output_file, version) output_file_name = get_output_filename(output_file, version)
...@@ -36,3 +21,27 @@ class MD5Versioning(VersioningBase): ...@@ -36,3 +21,27 @@ class MD5Versioning(VersioningBase):
except ValueError: except ValueError:
# no placeholder found, do not update, manual update if needed # no placeholder found, do not update, manual update if needed
return False, version return False, version
def get_version(self, source_files):
buf = concat(source_files)
s = cStringIO.StringIO(buf)
version = self.get_hash(s)
s.close()
return version
def get_hash(self, f, CHUNK=2**16):
m = self.hash_method()
while 1:
chunk = f.read(CHUNK)
if not chunk:
break
m.update(chunk)
return m.hexdigest()
class MD5Versioning(HashVersioningBase):
def __init__(self):
super(MD5Versioning, self).__init__(md5)
class SHA1Versioning(HashVersioningBase):
def __init__(self):
super(SHA1Versioning, self).__init__(sha1)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment