md5_tstamp.py 935 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. Re-calculate md5 hashes of files only when the file times or the file
  5. size have changed.
  6. The hashes can also reflect either the file contents (STRONGEST=True) or the
  7. file time and file size.
  8. The performance benefits of this module are usually insignificant.
  9. """
  10. import os, stat
  11. from waflib import Utils, Build, Node
  12. STRONGEST = True
  13. Build.SAVED_ATTRS.append('hashes_md5_tstamp')
  14. def h_file(self):
  15. filename = self.abspath()
  16. st = os.stat(filename)
  17. cache = self.ctx.hashes_md5_tstamp
  18. if filename in cache and cache[filename][0] == st.st_mtime:
  19. return cache[filename][1]
  20. if STRONGEST:
  21. ret = Utils.h_file(filename)
  22. else:
  23. if stat.S_ISDIR(st[stat.ST_MODE]):
  24. raise IOError('Not a file')
  25. ret = Utils.md5(str((st.st_mtime, st.st_size)).encode()).digest()
  26. cache[filename] = (st.st_mtime, ret)
  27. return ret
  28. h_file.__doc__ = Node.Node.h_file.__doc__
  29. Node.Node.h_file = h_file