Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/emacs-for-python/rope-dist/rope/base/oi/runmod.py
1439 views
1
2
def __rope_start_everything():
3
import os
4
import sys
5
import socket
6
import cPickle as pickle
7
import marshal
8
import inspect
9
import types
10
import threading
11
12
class _MessageSender(object):
13
14
def send_data(self, data):
15
pass
16
17
class _SocketSender(_MessageSender):
18
19
def __init__(self, port):
20
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
21
s.connect(('127.0.0.1', port))
22
self.my_file = s.makefile('w')
23
24
def send_data(self, data):
25
if not self.my_file.closed:
26
pickle.dump(data, self.my_file)
27
28
def close(self):
29
self.my_file.close()
30
31
class _FileSender(_MessageSender):
32
33
def __init__(self, file_name):
34
self.my_file = open(file_name, 'wb')
35
36
def send_data(self, data):
37
if not self.my_file.closed:
38
marshal.dump(data, self.my_file)
39
40
def close(self):
41
self.my_file.close()
42
43
44
def _cached(func):
45
cache = {}
46
def newfunc(self, arg):
47
if arg in cache:
48
return cache[arg]
49
result = func(self, arg)
50
cache[arg] = result
51
return result
52
return newfunc
53
54
class _FunctionCallDataSender(object):
55
56
def __init__(self, send_info, project_root):
57
self.project_root = project_root
58
if send_info.isdigit():
59
self.sender = _SocketSender(int(send_info))
60
else:
61
self.sender = _FileSender(send_info)
62
63
def global_trace(frame, event, arg):
64
# HACK: Ignoring out->in calls
65
# This might lose some information
66
if self._is_an_interesting_call(frame):
67
return self.on_function_call
68
sys.settrace(global_trace)
69
threading.settrace(global_trace)
70
71
def on_function_call(self, frame, event, arg):
72
if event != 'return':
73
return
74
args = []
75
returned = ('unknown',)
76
code = frame.f_code
77
for argname in code.co_varnames[:code.co_argcount]:
78
try:
79
args.append(self._object_to_persisted_form(frame.f_locals[argname]))
80
except (TypeError, AttributeError):
81
args.append(('unknown',))
82
try:
83
returned = self._object_to_persisted_form(arg)
84
except (TypeError, AttributeError):
85
pass
86
try:
87
data = (self._object_to_persisted_form(frame.f_code),
88
tuple(args), returned)
89
self.sender.send_data(data)
90
except (TypeError):
91
pass
92
return self.on_function_call
93
94
def _is_an_interesting_call(self, frame):
95
#if frame.f_code.co_name in ['?', '<module>']:
96
# return False
97
#return not frame.f_back or not self._is_code_inside_project(frame.f_back.f_code)
98
99
if not self._is_code_inside_project(frame.f_code) and \
100
(not frame.f_back or not self._is_code_inside_project(frame.f_back.f_code)):
101
return False
102
return True
103
104
def _is_code_inside_project(self, code):
105
source = self._path(code.co_filename)
106
return source is not None and os.path.exists(source) and \
107
_realpath(source).startswith(self.project_root)
108
109
@_cached
110
def _get_persisted_code(self, object_):
111
source = self._path(object_.co_filename)
112
if not os.path.exists(source):
113
raise TypeError('no source')
114
return ('defined', _realpath(source), str(object_.co_firstlineno))
115
116
@_cached
117
def _get_persisted_class(self, object_):
118
try:
119
return ('defined', _realpath(inspect.getsourcefile(object_)),
120
object_.__name__)
121
except (TypeError, AttributeError):
122
return ('unknown',)
123
124
def _get_persisted_builtin(self, object_):
125
if isinstance(object_, (str, unicode)):
126
return ('builtin', 'str')
127
if isinstance(object_, list):
128
holding = None
129
if len(object_) > 0:
130
holding = object_[0]
131
return ('builtin', 'list', self._object_to_persisted_form(holding))
132
if isinstance(object_, dict):
133
keys = None
134
values = None
135
if len(object_) > 0:
136
keys = object_.keys()[0]
137
values = object_[keys]
138
return ('builtin', 'dict',
139
self._object_to_persisted_form(keys),
140
self._object_to_persisted_form(values))
141
if isinstance(object_, tuple):
142
objects = []
143
if len(object_) < 3:
144
for holding in object_:
145
objects.append(self._object_to_persisted_form(holding))
146
else:
147
objects.append(self._object_to_persisted_form(object_[0]))
148
return tuple(['builtin', 'tuple'] + objects)
149
if isinstance(object_, set):
150
holding = None
151
if len(object_) > 0:
152
for o in object_:
153
holding = o
154
break
155
return ('builtin', 'set', self._object_to_persisted_form(holding))
156
return ('unknown',)
157
158
def _object_to_persisted_form(self, object_):
159
if object_ is None:
160
return ('none',)
161
if isinstance(object_, types.CodeType):
162
return self._get_persisted_code(object_)
163
if isinstance(object_, types.FunctionType):
164
return self._get_persisted_code(object_.func_code)
165
if isinstance(object_, types.MethodType):
166
return self._get_persisted_code(object_.im_func.func_code)
167
if isinstance(object_, types.ModuleType):
168
return self._get_persisted_module(object_)
169
if isinstance(object_, (str, unicode, list, dict, tuple, set)):
170
return self._get_persisted_builtin(object_)
171
if isinstance(object_, (types.TypeType, types.ClassType)):
172
return self._get_persisted_class(object_)
173
return ('instance', self._get_persisted_class(type(object_)))
174
175
@_cached
176
def _get_persisted_module(self, object_):
177
path = self._path(object_.__file__)
178
if path and os.path.exists(path):
179
return ('defined', _realpath(path))
180
return ('unknown',)
181
182
def _path(self, path):
183
if path.endswith('.pyc'):
184
path = path[:-1]
185
if path.endswith('.py'):
186
return path
187
188
def close(self):
189
self.sender.close()
190
191
def _realpath(path):
192
return os.path.realpath(os.path.abspath(os.path.expanduser(path)))
193
194
send_info = sys.argv[1]
195
project_root = sys.argv[2]
196
file_to_run = sys.argv[3]
197
run_globals = globals()
198
run_globals.update({'__name__': '__main__',
199
'__builtins__': __builtins__,
200
'__file__': file_to_run})
201
if send_info != '-':
202
data_sender = _FunctionCallDataSender(send_info, project_root)
203
del sys.argv[1:4]
204
execfile(file_to_run, run_globals)
205
if send_info != '-':
206
data_sender.close()
207
208
209
if __name__ == '__main__':
210
__rope_start_everything()
211
212