fhash.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. Modification to handle folders as if they were files.
  5. Usually, the target folders are created automatically (Node.find_or_declare)
  6. for files that need them so this is not really necessary.
  7. This modification incurs a performance penalty (computing hashes,
  8. creating additional tasks, checking if the folders are there
  9. vs just creating the folders if missing), and can conceal serious
  10. errors (confusing files and folders for example).
  11. The build order will not look at the parent folder relationships,
  12. we will need a testcase for this (overriding the function
  13. Task.set_file_constraints is trivial)
  14. """
  15. import stat, os
  16. from waflib import Utils, Task
  17. from waflib.TaskGen import feature
  18. def h_file(filename):
  19. """now folders can have a signature too"""
  20. st = os.stat(filename)
  21. if stat.S_ISDIR(st[stat.ST_MODE]):
  22. return Utils.md5(filename).digest()
  23. m = Utils.md5()
  24. m.update(str(st.st_mtime))
  25. m.update(str(st.st_size))
  26. m.update(filename)
  27. return m.digest()
  28. Utils.h_file = h_file
  29. @feature('mkdir')
  30. def make_target_folder(self):
  31. """code provided as an example"""
  32. try:
  33. node = self.target
  34. except AttributeError:
  35. raise self.bld.errors.WafError('Missing target attribute on task generator %r' % self)
  36. self.create_task('mkdir', [], node)
  37. class mkdir(Task.Task):
  38. """calling node.mkdir() will be more efficient than creating folders"""
  39. def run(self):
  40. self.outputs[0].mkdir()