md5_tstamp.py 963 B

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