Repository navigation
Expand file tree
/
Copy pathplugin_thread.py
More file actions
134 lines (107 loc) · 4.37 KB
/
Copy pathplugin_thread.py
File metadata and controls
134 lines (107 loc) · 4.37 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
130
131
132
133
134
import os
import pprint
import time
import traceback
from sys import exc_info
from threading import Thread
from types import MethodType
class PluginThread(Thread):
"""
abstract base class for thread types.
"""
# ----------------------------------------------------------------------
def __init__(self, manager):
"""
Constructor.
"""
super().__init__()
self.active = False
self.daemon = True
self.pyload = manager.pyload
self._ = manager._
self.m = self.manager = manager #: thread manager
def write_debug_report(self, pyfile):
"""
writes a debug report.
:return:``
"""
date = time.strftime("%Y-%m-%d_%H-%M-%S")
dump_name = f"debug_{pyfile.pluginname}_{date}.zip"
dump_filename = os.path.join(self.pyload.tempdir, dump_name)
dump = self.get_debug_dump(pyfile)
try:
import zipfile
with zipfile.ZipFile(dump_filename, "w") as zip:
try:
for entry in os.listdir(
os.path.join(self.pyload.tempdir, pyfile.pluginname)
):
try:
# avoid encoding errors
zip.write(
os.path.join(self.pyload.tempdir, pyfile.pluginname, entry),
os.path.join(pyfile.pluginname, entry),
)
except Exception:
pass
except OSError:
pass
info = zipfile.ZipInfo(
os.path.join(pyfile.pluginname, "debug_Report.txt"), time.gmtime()
)
info.external_attr = 0o644 << 16 #: change permissions
zip.writestr(info, dump)
if not os.stat(dump_filename).st_size:
raise Exception("Empty Zipfile")
except Exception as exc:
self.pyload.log.debug(f"Error creating zip file: {exc}")
dump_filename = dump_filename.replace(".zip", ".txt")
with open(dump_filename, mode="w") as fp:
fp.write(dump)
self.pyload.log.info(self._("Debug Report written to {}").format(dump_filename))
def get_debug_dump(self, pyfile):
version = self.pyload.api.get_server_version()
dump = f"pyLoad {version} Debug Report of {pyfile.pluginname} {pyfile.plugin.__version__} \n\nTRACEBACK:\n {traceback.format_exc()} \n\nFRAMESTACK:\n"
tb = exc_info()[2]
stack = []
while tb:
stack.append(tb.tb_frame)
tb = tb.tb_next
for frame in stack[1:]:
dump += f"\n_frame {frame.f_code.co_name} in {frame.f_code.co_filename} at line {frame.f_lineno}\n"
for key, value in frame.f_locals.items():
dump += f"\t{key:20} = "
try:
dump += pprint.pformat(value) + "\n"
except Exception as exc:
dump += f"<ERROR WHILE PRINTING VALUE> {exc}\n"
del frame
del stack #: delete it just to be sure...
dump += "\n\n_PLUGIN OBJECT DUMP: \n\n"
for name in dir(pyfile.plugin):
attr = getattr(pyfile.plugin, name)
if not name.endswith("__") and not isinstance(attr, MethodType):
dump += f"\t{name:20} = "
try:
dump += pprint.pformat(attr) + "\n"
except Exception as exc:
dump += f"<ERROR WHILE PRINTING VALUE> {exc}\n"
dump += "\n_PYFILE OBJECT DUMP: \n\n"
for name in dir(pyfile):
attr = getattr(pyfile, name)
if not name.endswith("__") and not isinstance(attr, MethodType):
dump += f"\t{name:20} = "
try:
dump += pprint.pformat(attr) + "\n"
except Exception as exc:
dump += f"<ERROR WHILE PRINTING VALUE> {exc}\n"
if pyfile.pluginname in self.pyload.config.plugin:
dump += "\n\nCONFIG: \n\n"
dump += pprint.pformat(self.pyload.config.plugin[pyfile.pluginname]) + "\n"
return dump
def clean(self, pyfile):
"""
set thread as inactive and release pyfile.
"""
self.active = False
pyfile.release()