/
runmod.py
241 lines (212 loc) · 8.32 KB
/
runmod.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
def __rope_start_everything():
import os
import sys
import socket
try:
import cPickle as pickle
except ImportError:
import pickle
import marshal
import inspect
import types
import threading
import base64
import hashlib
import hmac
class _MessageSender:
def send_data(self, data):
pass
class _SocketSender(_MessageSender):
def __init__(self, port, key):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", port))
self.my_file = s.makefile("wb")
self.key = base64.b64decode(key)
def send_data(self, data):
if not self.my_file.closed:
pickled_data = base64.b64encode(
pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
)
dgst = hmac.new(self.key, pickled_data, hashlib.sha256).digest()
self.my_file.write(base64.b64encode(dgst) + b":" + pickled_data + b"\n")
def close(self):
self.my_file.close()
class _FileSender(_MessageSender):
def __init__(self, file_name):
self.my_file = open(file_name, "wb")
def send_data(self, data):
if not self.my_file.closed:
marshal.dump(data, self.my_file)
def close(self):
self.my_file.close()
def _cached(func):
cache = {}
def newfunc(self, arg):
if arg in cache:
return cache[arg]
result = func(self, arg)
cache[arg] = result
return result
return newfunc
class _FunctionCallDataSender:
def __init__(self, send_info, project_root):
self.project_root = project_root
if send_info[0].isdigit():
port, key = send_info.split(":", 1)
self.sender = _SocketSender(int(port), key)
else:
self.sender = _FileSender(send_info)
def global_trace(frame, event, arg):
# HACK: Ignoring out->in calls
# This might lose some information
if self._is_an_interesting_call(frame):
return self.on_function_call
sys.settrace(global_trace)
threading.settrace(global_trace)
def on_function_call(self, frame, event, arg):
if event != "return":
return
args = []
returned = ("unknown",)
code = frame.f_code
for argname in code.co_varnames[: code.co_argcount]:
try:
argvalue = self._object_to_persisted_form(frame.f_locals[argname])
args.append(argvalue)
except (TypeError, AttributeError):
args.append(("unknown",))
try:
returned = self._object_to_persisted_form(arg)
except (TypeError, AttributeError):
pass
try:
data = (
self._object_to_persisted_form(frame.f_code),
tuple(args),
returned,
)
self.sender.send_data(data)
except (TypeError):
pass
return self.on_function_call
def _is_an_interesting_call(self, frame):
# if frame.f_code.co_name in ['?', '<module>']:
# return False
# return not frame.f_back or
# not self._is_code_inside_project(frame.f_back.f_code)
if not self._is_code_inside_project(frame.f_code) and (
not frame.f_back
or not self._is_code_inside_project(frame.f_back.f_code)
):
return False
return True
def _is_code_inside_project(self, code):
source = self._path(code.co_filename)
return (
source is not None
and os.path.exists(source)
and _realpath(source).startswith(self.project_root)
)
@_cached
def _get_persisted_code(self, object_):
source = self._path(object_.co_filename)
if not os.path.exists(source):
raise TypeError("no source")
return ("defined", _realpath(source), str(object_.co_firstlineno))
@_cached
def _get_persisted_class(self, object_):
try:
return (
"defined",
_realpath(inspect.getsourcefile(object_)),
object_.__name__,
)
except (TypeError, AttributeError):
return ("unknown",)
def _get_persisted_builtin(self, object_):
if isinstance(object_, str):
return ("builtin", "str")
if isinstance(object_, list):
holding = None
if len(object_) > 0:
holding = object_[0]
return ("builtin", "list", self._object_to_persisted_form(holding))
if isinstance(object_, dict):
keys = None
values = None
if len(object_) > 0:
# @todo - fix it properly, why is __locals__ being
# duplicated ?
keys = [key for key in object_.keys() if key != "__locals__"][0]
values = object_[keys]
return (
"builtin",
"dict",
self._object_to_persisted_form(keys),
self._object_to_persisted_form(values),
)
if isinstance(object_, tuple):
objects = []
if len(object_) < 3:
for holding in object_:
objects.append(self._object_to_persisted_form(holding))
else:
objects.append(self._object_to_persisted_form(object_[0]))
return tuple(["builtin", "tuple"] + objects)
if isinstance(object_, set):
holding = None
if len(object_) > 0:
for o in object_:
holding = o
break
return ("builtin", "set", self._object_to_persisted_form(holding))
return ("unknown",)
def _object_to_persisted_form(self, object_):
if object_ is None:
return ("none",)
if isinstance(object_, types.CodeType):
return self._get_persisted_code(object_)
if isinstance(object_, types.FunctionType):
return self._get_persisted_code(object_.__code__)
if isinstance(object_, types.MethodType):
return self._get_persisted_code(object_.__func__.__code__)
if isinstance(object_, types.ModuleType):
return self._get_persisted_module(object_)
if isinstance(object_, (str, list, dict, tuple, set)):
return self._get_persisted_builtin(object_)
if isinstance(object_, type):
return self._get_persisted_class(object_)
return ("instance", self._get_persisted_class(type(object_)))
@_cached
def _get_persisted_module(self, object_):
path = self._path(object_.__file__)
if path and os.path.exists(path):
return ("defined", _realpath(path))
return ("unknown",)
def _path(self, path):
if path.endswith(".pyc"):
path = path[:-1]
if path.endswith(".py"):
return path
def close(self):
self.sender.close()
sys.settrace(None)
def _realpath(path):
return os.path.realpath(os.path.abspath(os.path.expanduser(path)))
send_info = sys.argv[1]
project_root = sys.argv[2]
file_to_run = sys.argv[3]
run_globals = globals()
run_globals.update(
{"__name__": "__main__", "__builtins__": __builtins__, "__file__": file_to_run}
)
if send_info != "-":
data_sender = _FunctionCallDataSender(send_info, project_root)
del sys.argv[1:4]
with open(file_to_run) as f:
code = compile(f.read(), file_to_run, "exec")
exec(code, run_globals)
if send_info != "-":
data_sender.close()
if __name__ == "__main__":
__rope_start_everything()