PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/compress/versioning/hash/__init__.py

http://trespams.googlecode.com/
Python | 47 lines | 39 code | 7 blank | 1 comment | 4 complexity | 8c82bd1608beaf8192ee3a0cf9ce00b1 MD5 | raw file
  1. import cStringIO
  2. from hashlib import md5, sha1
  3. import os
  4. from compress.conf import settings
  5. from compress.utils import concat, get_output_filename
  6. from compress.versioning.base import VersioningBase
  7. class HashVersioningBase(VersioningBase):
  8. def __init__(self, hash_method):
  9. self.hash_method = hash_method
  10. def needs_update(self, output_file, source_files, version):
  11. output_file_name = get_output_filename(output_file, version)
  12. ph = settings.COMPRESS_VERSION_PLACEHOLDER
  13. of = output_file
  14. try:
  15. phi = of.index(ph)
  16. old_version = output_file_name[phi:phi+len(ph)-len(of)]
  17. return (version != old_version), version
  18. except ValueError:
  19. # no placeholder found, do not update, manual update if needed
  20. return False, version
  21. def get_version(self, source_files):
  22. buf = concat(source_files)
  23. s = cStringIO.StringIO(buf)
  24. version = self.get_hash(s)
  25. s.close()
  26. return version
  27. def get_hash(self, f, CHUNK=2**16):
  28. m = self.hash_method()
  29. while 1:
  30. chunk = f.read(CHUNK)
  31. if not chunk:
  32. break
  33. m.update(chunk)
  34. return m.hexdigest()
  35. class MD5Versioning(HashVersioningBase):
  36. def __init__(self):
  37. super(MD5Versioning, self).__init__(md5)
  38. class SHA1Versioning(HashVersioningBase):
  39. def __init__(self):
  40. super(SHA1Versioning, self).__init__(sha1)