summaryrefslogtreecommitdiff
path: root/buildtools/wafadmin/py3kfixes.py
blob: 2f3c9c29776548171dd2eae6a16ffb0e6a79e0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2009 (ita)

"""
Fixes for py3k go here
"""

import os

all_modifs = {}

def modif(dir, name, fun):
	if name == '*':
		lst = []
		for y in '. Tools 3rdparty'.split():
			for x in os.listdir(os.path.join(dir, y)):
				if x.endswith('.py'):
					lst.append(y + os.sep + x)
		#lst = [y + os.sep + x for x in os.listdir(os.path.join(dir, y)) for y in '. Tools 3rdparty'.split() if x.endswith('.py')]
		for x in lst:
			modif(dir, x, fun)
		return

	filename = os.path.join(dir, name)
	f = open(filename, 'r')
	txt = f.read()
	f.close()

	txt = fun(txt)

	f = open(filename, 'w')
	f.write(txt)
	f.close()

def subst(filename):
	def do_subst(fun):
		global all_modifs
		try:
			all_modifs[filename] += fun
		except KeyError:
			all_modifs[filename] = [fun]
		return fun
	return do_subst

@subst('Constants.py')
def r1(code):
	code = code.replace("'iluvcuteoverload'", "b'iluvcuteoverload'")
	code = code.replace("ABI=7", "ABI=37")
	return code

@subst('Tools/ccroot.py')
def r2(code):
	code = code.replace("p.stdin.write('\\n')", "p.stdin.write(b'\\n')")
	code = code.replace('p.communicate()[0]', 'p.communicate()[0].decode("utf-8")')
	return code

@subst('Utils.py')
def r3(code):
	code = code.replace("m.update(str(lst))", "m.update(str(lst).encode())")
	code = code.replace('p.communicate()[0]', 'p.communicate()[0].decode("utf-8")')
	return code

@subst('ansiterm.py')
def r33(code):
	code = code.replace('unicode', 'str')
	return code

@subst('Task.py')
def r4(code):
	code = code.replace("up(self.__class__.__name__)", "up(self.__class__.__name__.encode())")
	code = code.replace("up(self.env.variant())", "up(self.env.variant().encode())")
	code = code.replace("up(x.parent.abspath())", "up(x.parent.abspath().encode())")
	code = code.replace("up(x.name)", "up(x.name.encode())")
	code = code.replace('class TaskBase(object):\n\t__metaclass__=store_task_type', 'import binascii\n\nclass TaskBase(object, metaclass=store_task_type):')
	code = code.replace('keys=self.cstr_groups.keys()', 'keys=list(self.cstr_groups.keys())')
	code = code.replace("sig.encode('hex')", 'binascii.hexlify(sig)')
	code = code.replace("os.path.join(Options.cache_global,ssig)", "os.path.join(Options.cache_global,ssig.decode())")
	return code

@subst('Build.py')
def r5(code):
	code = code.replace("cPickle.dump(data,file,-1)", "cPickle.dump(data,file)")
	code = code.replace('for node in src_dir_node.childs.values():', 'for node in list(src_dir_node.childs.values()):')
	return code

@subst('*')
def r6(code):
	code = code.replace('xrange', 'range')
	code = code.replace('iteritems', 'items')
	code = code.replace('maxint', 'maxsize')
	code = code.replace('iterkeys', 'keys')
	code = code.replace('Error,e:', 'Error as e:')
	code = code.replace('Exception,e:', 'Exception as e:')
	return code

@subst('TaskGen.py')
def r7(code):
	code = code.replace('class task_gen(object):\n\t__metaclass__=register_obj', 'class task_gen(object, metaclass=register_obj):')
	return code

@subst('Tools/python.py')
def r8(code):
	code = code.replace('proc.communicate()[0]', 'proc.communicate()[0].decode("utf-8")')
	return code

@subst('Tools/glib2.py')
def r9(code):
	code = code.replace('f.write(c)', 'f.write(c.encode("utf-8"))')
	return code

@subst('Tools/config_c.py')
def r10(code):
	code = code.replace("key=kw['success']", "key=kw['success']\n\t\t\t\ttry:\n\t\t\t\t\tkey=key.decode('utf-8')\n\t\t\t\texcept:\n\t\t\t\t\tpass")
	code = code.replace('out=str(out)','out=out.decode("utf-8")')
	code = code.replace('err=str(err)','err=err.decode("utf-8")')
	return code

@subst('Tools/d.py')
def r11(code):
	code = code.replace('ret.strip()', 'ret.strip().decode("utf-8")')
	return code

def fixdir(dir):
	global all_modifs
	for k in all_modifs:
		for v in all_modifs[k]:
			modif(os.path.join(dir, 'wafadmin'), k, v)
	#print('substitutions finished')