-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbugreport.py
More file actions
132 lines (98 loc) · 3.41 KB
/
bugreport.py
File metadata and controls
132 lines (98 loc) · 3.41 KB
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
import sys
import platform
import os
import datetime
import traceback
import tkinter
import build_info
import locations
# keep those trailing whites!!
TEXT = """
Sorry!
This was unexpected.
We created a Bugreport under: {bugreport}.
It would be great if you could send the bugreport to thinkdownstairs@gmail.com
Thanks for your Help!
"""
TEMPLATE = """
BUGREPORT {timestamp}
ERROR:
// This section contains information about the error.
TYPE : {error_type}
VALUE : {error_value}
BACKTRACE : {error_trace}
BUILD INFO:
// This section contains informations about the system your executable was build on.
TIMESTAMP : {build_timestamp}
SYSTEM : {build_system}
COMMIT : {build_commit}
STATUS : {build_status}
USER : {build_user}
PACKAGES : {build_packages}
SYSTEM INFO:
// This section contains informations about your system.
SYSTEM : {system_system}
COMMAND : {system_command}
"""
INDENT = '\n' + ' ' * 20
def bugreport(exc_info=None) -> str:
typ, value, frames = exc_info if exc_info is not None else sys.exc_info()
trace = []
path_to_strip = locations.get_root()
strip_path = lambda s: s[len(path_to_strip) + 1:] if s.startswith(path_to_strip) else s
for frame in traceback.extract_tb(frames, 32):
fn = strip_path(frame[0])
ln = frame[1]
fu = frame[2]
tx = frame[3]
t = '{}:{} {} {}'.format(fn, ln, fu, tx)
trace.insert(0, t)
def prepare(value):
iterable = None
if isinstance(value, str):
iterable = value.split('\n')
else:
try:
iterable = iter(value)
except:
iterable = [str(value)]
return INDENT.join(filter(None, iterable))
aliases = platform.system_alias(platform.system(), platform.release(), platform.version())
system = '{} {} {}'.format(*aliases)
report = TEMPLATE.format(
timestamp=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
error_type=prepare(typ),
error_value=prepare(value),
error_trace=prepare(trace),
build_timestamp=prepare(build_info.TIMESTAMP),
build_system=prepare(build_info.SYSTEM),
build_commit=prepare(build_info.COMMIT),
build_status=prepare(build_info.STATUS),
build_user=prepare(build_info.USER),
build_packages=prepare(build_info.PACKAGES),
system_system=prepare(system),
system_command=prepare(' '.join(sys.argv)))
report_file = locations.user('bugreport.txt')
# with 'w' we override,... but i dont want to grow the file uncontrolled,
# so maybe i rewrite it later to append,.. but then we need some kind of
# ring buffer.
with open(report_file, 'w') as f:
f.write(report)
try:
root = tkinter.Tk()
root.title('Coder - Unexpected Error')
tkinter.Label(root, text=TEXT.format(bugreport=report_file)).pack(side=tkinter.TOP)
tkinter.Button(root, text='Close', command=lambda: root.quit()).pack(side=tkinter.BOTTOM)
root.mainloop()
except:
pass
if not getattr(sys, 'frozen', False):
print(report)
return report_file
if __name__ == '__main__':
try:
x = 0
y = 1
z = y / x
except:
bugreport()