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