extpy.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. import os
  4. from waflib import Errors, Utils
  5. from waflib import Context as mod
  6. class Context(mod.Context):
  7. cmd = 'all'
  8. def recurse(self, dirs, name=None, mandatory=True, once=True):
  9. try:
  10. cache = self.recurse_cache
  11. except:
  12. cache = self.recurse_cache = {}
  13. for d in Utils.to_list(dirs):
  14. if not os.path.isabs(d):
  15. # absolute paths only
  16. d = os.path.join(self.path.abspath(), d)
  17. WSCRIPT = os.path.join(d, 'wscript.py')
  18. WSCRIPT_FUN = 'wscript_' + (name or self.fun) + '.py'
  19. node = self.root.find_node(WSCRIPT_FUN)
  20. if node and (not once or node not in cache):
  21. cache[node] = True
  22. self.pre_recurse(node)
  23. try:
  24. function_code = node.read('rU')
  25. exec(compile(function_code, node.abspath(), 'exec'), self.exec_dict)
  26. finally:
  27. self.post_recurse(node)
  28. elif not node:
  29. node = self.root.find_node(WSCRIPT)
  30. if node and (not once or node not in cache):
  31. cache[node] = True
  32. self.pre_recurse(node)
  33. try:
  34. wscript_module = mod.load_module(node.abspath())
  35. user_function = getattr(wscript_module, (name or self.fun), None)
  36. if not user_function:
  37. if not mandatory:
  38. continue
  39. raise Errors.WafError('No function %s defined in %s' % (name or self.fun, node.abspath()))
  40. user_function(self)
  41. finally:
  42. self.post_recurse(node)
  43. elif not node:
  44. if not mandatory:
  45. continue
  46. raise Errors.WafError('No wscript file in directory %s' % d)
  47. mod.Context = Context
  48. mod.WSCRIPT_FILE = 'wscript.py'