1
2
3 import sys
4 import imp
5 import cPickle
6 from gmisclib import g_pipe
7 from gmisclib import load_mod
8 import traceback
9 from gmisclib import die
10
11 PROTOCOL = cPickle.HIGHEST_PROTOCOL
12
16
18 return RemoteException("Remote Exception=", (mod, name),
19 "args=", [str(q) for q in args],
20 "traceback=", trtext
21 )
22
23
25 ty, ob, trb = sys.exc_info()
26 tmp = tuple(traceback.format_tb(trb))
27 del trb
28 log( "DWAE, construcing remote exception" )
29 return make_remote_exception(ty.__name__, ty.__module__,
30 ob.args, tmp
31 )
32
33
35 """This buffers text, transmitting it in units of lines.
36 That's important at the other end if there are multiple
37 connections, because fancyprint adds a prefix at the
38 beginning of each write, assuming it is the beginning
39 of each line."""
40
43
45 self.store.append( s )
46
48 self.store.extend( x )
49
52
55
57 tmp = self.store
58 self.store = []
59 return tmp
60
61
62
65 self.val = val
66 self.out = out
67 self.err = err
68 self.ety = ety
69
70
71 _ffc = {}
73 key = (mname, path, fname)
74 try:
75 return _ffc[ key ]
76 except KeyError:
77 m = load_mod.load(mname, path)
78 if isinstance(fname, tuple):
79 f = m
80 for fc in fname:
81 f = getattr(f, fc)
82 else:
83 f = getattr(m, fname)
84 _ffc[ key ] = f
85 return f
86
87
89 - def __init__(self, path, mname, fname, a, kw):
90 self.path = path
91 self.mname = mname
92 self.fname = fname
93 self.a = a
94 self.kw = kw
95
96
98 f = _find_func(self.mname, self.path, self.fname)
99 ety = False
100 try:
101 v = f(*(self.a), **(self.kw))
102 except:
103 v = deal_with_arbitrary_exception()
104 ety = True
105 return response(ety, v, [], [])
106
107
109 if pa is None:
110 return
111 pth = []
112 imp.acquire_lock()
113 for pc in pa:
114 if pc is None:
115 pth.extend( sys.path)
116 else:
117 pth.append(pc)
118 sys.path = pth
119 imp.release_lock()
120 return pth
121
122
125
126
128 - def __init__(self, connection, pypath, mname, fname):
129 self.connection = connection
130 if pypath is None:
131 self.pypath = pypath
132 else:
133 self.pypath = tuple(pypath)
134 self.mname = mname
135 assert isinstance(fname, str) or isinstance(fname, tuple)
136 if isinstance(fname, str):
137 self.fname = fname
138 else:
139 self.fname = tuple(fname)
140
154
155
158 self.closed = False
159 self.wfd, self.rfd = g_pipe.popen2(execpath, arglist)
160
161 - def call(self, pypath, mname, fname, a, kw):
162
163
164 cPickle.dump(request(pypath, mname, fname, a, kw), self.wfd, PROTOCOL)
165
166 self.wfd.flush()
167
168
169
170 tmp = cPickle.load(self.rfd)
171
172 return tmp
173
174
176 if not self.closed:
177 self.wfd.close()
178 self.rfd.close()
179 self.closed = True
180
183
188
189
191 log('Server starting')
192 out = sys.stdout
193 sys.stdout = sys.stderr
194 while True:
195 log('serverloop')
196 try:
197
198
199
200
201
202 rq = cPickle.load(sys.stdin)
203 log('loaded')
204 except EOFError:
205 log("EOF error")
206 break
207 log('call')
208 back = rq.call()
209 log('return')
210
211 cPickle.dump(back, out, PROTOCOL)
212 out.flush()
213 log('loopend')
214
215
218
220 c = connection(None, ['python', '-c', 'import gyropy; gyropy.g_remote_exec.server()'])
221 f = remote_fcn(c, None, 'math', 'sqrt')
222 g = remote_fcn(c, None, 'gyropy', ('g_remote_exec', 'test_rf2'))
223 for i in range(1000):
224 err = abs(f(i)**2 - i)
225 if err > 0.001:
226 die.die("Wrong answer")
227 try:
228 f(-1)
229 except RemoteException:
230 pass
231 else:
232 die.die("No exception!")
233 f2 = remote_fcn(c, None, "gyropy", ("g_remote_exec", "set_remote_path"))
234 if f2([None, "/tmp"])[-1] != "/tmp":
235 die.die("Bad set remote path")
236 for i in range(98, 1000, 3):
237 if len(g(i)) != i:
238 die.die("Wrong answer")
239 c.close()
240
241
242
243 if __name__ == '__main__':
244 if len(sys.argv)>1 and sys.argv[1] == '-test':
245 for i in range(10):
246 test()
247 print 'OK'
248 sys.exit(0)
249 server()
250