-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·288 lines (219 loc) · 8.16 KB
/
main.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
# Author: Gerrit Addiks <[email protected]>
import os
import sys
import gi
import csv
from os.path import expanduser
from tkinter import Tk # from tkinter import Tk for Python 3.x
from tkinter.filedialog import askopenfilename
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, Pango, GObject
class TraceEntry:
__level = 0
__number = 0
__begin = 0.0
__end = 0.0
__duration = 0.0
__memory = 0
__function = "NONE"
__file = "NONE"
__arguments = ""
__children = []
__row_iter = None
__row_path = None
__tree_store = None
__tree_view = None
def __init__(
self,
level,
number,
begin,
memory,
function,
fileName,
arguments
):
self.__level = level
self.__number = number
self.__begin = begin
self.__memory = memory
self.__function = function
self.__file = fileName
self.__arguments = arguments
self.__children = []
self.__visible = True
def addChild(self, child):
self.__children.append(child);
def close(self, end):
self.__end = end
self.__duration = self.__end - self.__begin
def asListStoreEntry(self):
return [
self.__level,
self.__number,
self.__begin,
self.__end,
self.__duration,
self.__memory,
self.__function,
self.__file,
self.__arguments,
self.__visible
]
def addRowToTreeStore(self, treeStore, treeView, treeStoreFilter, parentRowIter):
rowIter = treeStore.append(parentRowIter, self.asListStoreEntry())
self.__row_path = treeStore.get_path(rowIter)
self.__tree_store = treeStore
self.__tree_store_filter = treeStoreFilter
self.__tree_view = treeView
def addChildrenToTreeStore(self, treeStore, treeView, treeStoreFilter, parentRowIter):
self.__tree_store = treeStore
self.__tree_store_filter = treeStoreFilter
self.__tree_view = treeView
if len(self.__children) > 0:
for child in self.__children:
child.addRowToTreeStore(
self.__tree_store,
treeView,
self.__tree_store_filter,
self.__tree_store.get_iter(self.__row_path)
)
treeView.connect("row-expanded", self.onRowExpanded);
def onRowExpanded(self, treeView, treeIter, treePath):
treeIter = self.__tree_store_filter.convert_iter_to_child_iter(treeIter)
rowPath = self.__tree_store.get_path(treeIter)
if self.__row_path != rowPath:
return
for child in self.__children:
child.addChildrenToTreeStore(
self.__tree_store,
self.__tree_view,
self.__tree_store_filter,
self.__tree_store.get_iter(rowPath)
)
print("*row expand*")
def children(self):
return self.__children
def num(self):
return self.__number
def duration(self):
return self.__duration
def descr(self):
return self.__file + ":" + self.__function
def onFilterChanged(self, durationLimit):
self.__visible = self.__duration > durationLimit
if self.__tree_store != None:
self.__tree_store.set_value(
self.__tree_store.get_iter(self.__row_path),
9,
self.__visible
)
for child in self.__children:
child.onFilterChanged(durationLimit)
class TraceFile:
__entries = []
__highest_duration = 0.0
def __init__(self, file_handle):
with file_handle as handle:
print(handle.readline(), end="") # Version: 3.3.2
print(handle.readline(), end="") # File format: 4
if 'TRACE START' not in handle.readline():
print('Invalid format for the trace. Did you forget to change the trace format to be machine readable?')
print('Try adding: xdebug.trace_format=1 to the config')
reader = csv.reader(handle, delimiter="\t")
level = 0
entryStack = []
for row in reader:
currentEntry = None
if len(row) < 3:
continue
if row[2] == '0': # Entry
# print('ENTRY')
# The format can be found here: https://xdebug.org/docs/trace#trace_format
[ level, function_number, zero, begin_time, memory, function_name, user_defined, included_file, filename, line_number, *args ] = row
if len(args) != 0:
arguments = "TODO"
else:
arguments = "Feature disabled. Set xdebug.collect_params=1"
newEntry = TraceEntry(
int(level), # Level
int(function_number), # Number
float(begin_time), # Begin
int(memory), # Memory
function_name, # Function
f"{filename}:{line_number}", # FileName
arguments
)
if len(entryStack) > 0:
parent = entryStack[-1]
parent.addChild(newEntry)
else:
self.__entries.append(newEntry)
entryStack.append(newEntry)
if row[2] == '1': # Exit
currentEntry = entryStack.pop()
currentEntry.close(
float(row[3]) # End
)
duration = currentEntry.duration()
if duration > self.__highest_duration:
self.__highest_duration = duration
if row[2] == 'R': # Return
print('RETURN')
def attachToStore(self, treeStore, treeView, treeStoreFilter):
for root in self.__entries:
root.addRowToTreeStore(treeStore, treeView, treeStoreFilter, None)
root.addChildrenToTreeStore(treeStore, treeView, treeStoreFilter, None)
def highestDuration(self):
return self.__highest_duration
def onFilterChanged(self, durationLimit):
for root in self.__entries:
root.onFilterChanged(durationLimit)
class Handler:
def __init__(self, traceFile):
self.__traceFile = traceFile
def onDeleteWindow(self, *args):
Gtk.main_quit(*args)
def onRowExpanded(self, treeView, treeIter, treePath):
pass
def onRowCollapsed(self, *args):
pass
def onFilterChanged(self, *args):
filterEntry = builder.get_object("duration_filter_value")
traceFile.onFilterChanged(filterEntry.get_value())
treeStoreFilter = builder.get_object("traces_filtered")
treeStoreFilter.refilter()
basedir = os.path.dirname(os.path.abspath(__file__))
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
if len(sys.argv) == 2 and os.path.exists(sys.argv[1]):
filename = sys.argv[1]
else:
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
if filename.endswith('.gz'):
import gzip, io
file_handle = io.TextIOWrapper(gzip.open(filename, 'rb'))
else:
file_handle = open(filename)
traceFile = TraceFile(file_handle)
handler = Handler(traceFile)
builder = Gtk.Builder()
handler.builder = builder
builder.add_from_file(basedir + "/ui.glade")
builder.connect_signals(handler)
treeStoreFilter = builder.get_object("traces_filtered")
treeStoreFilter.set_visible_column(9)
traceDataStore = builder.get_object("traces") # GtkTreeStore
print(traceDataStore)
traceFile.attachToStore(
traceDataStore,
builder.get_object("traces_view"),
treeStoreFilter
)
filterScale = builder.get_object("duration_filter_value")
filterScale.set_upper(traceFile.highestDuration())
filterScale.set_step_increment(0.0001)
window = builder.get_object("windowMain")
window.show_all()
Gtk.main()