forked from renpy/renpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renpycoverage.py
262 lines (178 loc) · 6.11 KB
/
renpycoverage.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from __future__ import print_function, unicode_literals, division, absolute_import
import Cython.Coverage
import os
import coverage # @UnresolvedImport
import cPickle
import ast
ROOT = os.path.dirname(os.path.abspath(__file__))
def _find_c_source(base_path):
base_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "module", "gen.coverage", os.path.basename(base_path))
if os.path.exists(base_path + '.c'):
c_file = base_path + '.c'
elif os.path.exists(base_path + '.cpp'):
c_file = base_path + '.cpp'
else:
c_file = None
return c_file
Cython.Coverage._find_c_source = _find_c_source
class FixedCythonReporter(coverage.FileReporter):
def __init__(self, old, fn):
# fn = old.filename
#
# if not os.path.exists(fn):
# new_fn = os.path.join(os.path.dirname(fn), "module", os.path.basename(fn))
# if os.path.exists(new_fn):
# fn = new_fn
super(FixedCythonReporter, self).__init__(fn)
if old._code is None:
self._lines = set()
else:
self._lines = set(old._code)
def lines(self):
return self._lines
class CythonCoverage(Cython.Coverage.Plugin):
def _find_source_files(self, filename):
if not filename.endswith(".pyx"):
return None, None
pyx_base = os.path.basename(filename)
pyx_search = [
"module",
"module/gen.coverage",
"module/pysdlsound",
"renpy",
"renpy/display",
"renpy/gl",
"renpy/styledata",
"renpy/text",
"renpy/audio",
]
for i in pyx_search:
pyx_fn = os.path.join(ROOT, i, pyx_base)
if os.path.exists(pyx_fn):
break
else:
return None, None
c_base = os.path.basename(filename)[:-4] + ".c"
modules = [
"",
"renpy.",
"renpy.display.",
"renpy.gl.",
"renpy.styledata.",
"renpy.text.",
"renpy.audio.",
"pysdlsound."
]
for i in modules:
c_fn = os.path.join(ROOT, "module", "gen.coverage", i + c_base)
if os.path.exists(c_fn):
break
else:
print("Could not find C source for", filename)
return None, None
return c_fn, pyx_fn
def file_tracer(self, filename):
if not filename.endswith(".pyx"):
return None
_, pyx_fn = self._find_source_files(filename)
rv = super(CythonCoverage, self).file_tracer(pyx_fn)
def source_filename():
return pyx_fn
rv.source_filename = source_filename
return rv
def file_reporter(self, filename):
_, pyx_fn = self._find_source_files(filename)
r = super(CythonCoverage, self).file_reporter(filename)
return FixedCythonReporter(r, pyx_fn)
class RenpyTracer(coverage.FileTracer):
def __init__(self, filename):
self.filename = filename
def source_filename(self):
return self.filename
import renpy
renpy_import_all = False
class PycodeVisitor(ast.NodeVisitor):
def __init__(self, lines):
self.lines = lines
def statement(self, n):
self.lines.add(n.lineno)
self.generic_visit(n)
visit_FunctionDef = statement
visit_ClassDef = statement
visit_Return = statement
visit_Delete = statement
visit_Assign = statement
visit_AugAssign = statement
visit_Print = statement
visit_For = statement
visit_While = statement
visit_If = statement
visit_With = statement
visit_Raise = statement
visit_TryExcept = statement
visit_TryFinally = statement
visit_Assert = statement
visit_Import = statement
visit_ImportFrom = statement
# Global
visit_Exec = statement
# pass
visit_Break = statement
visit_Continue = statement
def visit_Expr(self, n):
if not isinstance(n.value, ast.Str):
self.lines.add(n.lineno)
self.generic_visit(n)
class RenpyReporter(coverage.FileReporter):
def __init__(self, filename):
super(RenpyReporter, self).__init__(filename)
global renpy_import_all
if not renpy_import_all:
import pygame_sdl2
pygame_sdl2.import_as_pygame()
renpy.import_all()
renpy_import_all = True
renpy.config.basedir = '/'
renpy.config.renpy_base = '/'
# renpy.game.contexts = [ renpy.execution.Context(False) ]
renpy.game.script = renpy.script.Script()
# stmts = renpy.parser.parse(filename)
with open(filename + "c", "rb") as f:
data = renpy.game.script.read_rpyc_data(f, 1)
try:
stmts = cPickle.loads(data)[1]
except:
stmts = [ ]
print(filename + "c", "failed")
all_stmts = [ ]
for i in stmts:
i.get_children(all_stmts.append)
self._lines = set()
for i in all_stmts:
if not isinstance(i, renpy.ast.Init):
self._lines.add(i.linenumber)
for i in renpy.game.script.all_pycode:
self.pycode_lines(i)
renpy.game.script.all_pycode = [ ]
def pycode_lines(self, pycode):
if pycode.mode != 'exec':
return
nodes = renpy.python.py_compile(pycode.source, pycode.mode, pycode.location[0], pycode.location[1], ast_node=True)
v = PycodeVisitor(self._lines)
for i in nodes:
v.visit(i)
def lines(self):
return self._lines
class RenpyCoverage(coverage.CoveragePlugin):
def file_tracer(self, filename):
for i in [ ".rpy", ".rpym" ]:
if filename.endswith(i):
break
else:
return None
return RenpyTracer(filename)
def file_reporter(self, filename):
return RenpyReporter(filename)
def coverage_init(reg, options):
reg.add_file_tracer(RenpyCoverage())
reg.add_file_tracer(CythonCoverage())