fixpy2.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2010-2018 (ita)
  4. from __future__ import with_statement
  5. import os
  6. all_modifs = {}
  7. def fixdir(dir):
  8. """Call all substitution functions on Waf folders"""
  9. for k in all_modifs:
  10. for v in all_modifs[k]:
  11. modif(os.path.join(dir, 'waflib'), k, v)
  12. def modif(dir, name, fun):
  13. """Call a substitution function"""
  14. if name == '*':
  15. lst = []
  16. for y in '. Tools extras'.split():
  17. for x in os.listdir(os.path.join(dir, y)):
  18. if x.endswith('.py'):
  19. lst.append(y + os.sep + x)
  20. for x in lst:
  21. modif(dir, x, fun)
  22. return
  23. filename = os.path.join(dir, name)
  24. with open(filename, 'r') as f:
  25. txt = f.read()
  26. txt = fun(txt)
  27. with open(filename, 'w') as f:
  28. f.write(txt)
  29. def subst(*k):
  30. """register a substitution function"""
  31. def do_subst(fun):
  32. for x in k:
  33. try:
  34. all_modifs[x].append(fun)
  35. except KeyError:
  36. all_modifs[x] = [fun]
  37. return fun
  38. return do_subst
  39. @subst('*')
  40. def r1(code):
  41. "utf-8 fixes for python < 2.6"
  42. code = code.replace('as e:', ',e:')
  43. code = code.replace(".decode(sys.stdout.encoding or'latin-1',errors='replace')", '')
  44. return code.replace('.encode()', '')
  45. @subst('Runner.py')
  46. def r4(code):
  47. "generator syntax"
  48. return code.replace('next(self.biter)', 'self.biter.next()')
  49. @subst('Context.py')
  50. def r5(code):
  51. return code.replace("('Execution failure: %s'%str(e),ex=e)", "('Execution failure: %s'%str(e),ex=e),None,sys.exc_info()[2]")