-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpt.py
executable file
·689 lines (561 loc) · 18.9 KB
/
mpt.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Shell environment for MPT.
create connection to MPT plotting process
run Shell Interpreter for message passing
to attached user processes
"""
import cmd
import os
import sys
import socket
from struct import pack
from glob import glob
from array import array
from tempfile import gettempdir
from subprocess import Popen, PIPE
MESSAGE_OUTPUT = 0x0
MESSAGE_COMMAND = 0x4
MESSAGE_SET = 0x6
MESSAGE_VALUES = 0x8
MESSAGE_DEST = 0xd
VALUES_UNSIGNED = 0x20
VALUES_FLOAT = 0x40
VALUES_INTEGER = 0x60
if sys.byteorder == "little":
VALUES_NATIVE = -0x80
else:
VALUES_NATIVE = 0
SEARCH_TERMINAL = [
['x-terminal-emulator', '-e'],
['konsole', '-e'],
['xterm', '-e']
]
rundir = os.getenv('MPT_TMP')
if not rundir:
if os.name != 'posix':
rundir = os.path.join(gettempdir(), 'mpt')
else:
rundir = os.path.join(gettempdir(), 'mpt-' + str(os.getuid()))
def encode_command(msg):
if isinstance(msg, str):
msg = bytearray(msg, 'utf-8')
try:
msg.index(b"\x00")
raise ValueError("inline zero byte")
except Exception:
pass
return msg + b"\x00"
def encode_cobs(msg):
""" simple COBS encoder """
code = 0x1
ret = bytearray()
ret.append(code)
if isinstance(msg, str):
msg = bytearray(msg, 'utf-8')
for b in msg:
if b != 0:
if code >= 254:
ret.append(b)
ret[len(ret) - code] = code + 1
code = 1
continue
ret.append(b)
code = code + 1
else:
if code != 1:
ret[len(ret) - code] = code
code = 1
ret.append(code)
ret[len(ret) - code] = code
ret.append(0)
return ret
class Output(object):
""" output buffer """
_encode = encode_command
_buf = None
def __init__(self, name=None):
""" append data to output """
self._encode = Output._encode
self._chan = None if name is None else open(name, 'wb')
def push(self, msg):
""" send data to output """
if not self._buf:
self._buf = bytearray()
if isinstance(msg, tuple) or isinstance(msg, list):
""" serialize message data """
for t in msg:
self._buf = self._buf + t
elif isinstance(msg, bytes):
self._buf = self._buf + msg
elif isinstance(msg, str):
self._buf = self._buf + bytes(bytearray(msg, 'utf-8'))
else:
raise TypeError("invalid output data")
def flush(self):
""" flush output data """
if not self._chan or self._buf is None:
return
buf = self._buf if self._encode is None else self._encode(self._buf)
ret = 0
if len(buf):
if hasattr(self._chan, 'sendall'):
ret = self._chan.sendall(buf)
else:
ret = self._chan.write(buf)
if hasattr(self._chan, 'flush'):
self._chan.flush()
self._buf = None
return ret
def make_header(cmd=MESSAGE_COMMAND, arg=0, _id=None):
""" serialize header """
if _id is None:
return pack('>bb', cmd, arg)
return pack('>Hbb', _id, cmd, arg)
def make_destination(dest):
""" serialize destination encoding """
lay = grf = wld = dim = cyc = off = None
if isinstance(dest, tuple) or isinstance(dest, list):
if len(dest) > 5:
off = dest[5]
if len(dest) > 4:
cyc = dest[4]
if len(dest) > 3:
dim = dest[3]
if len(dest) > 2:
wld = dest[2]
if len(dest) > 1:
grf = dest[1]
if len(dest) > 0:
lay = dest[0]
elif isinstance(dest, dict):
lay = dest.get('lay')
if not lay:
lay = dest.get('layout')
grf = dest.get('grf')
if not grf:
grf = dest.get('graph')
wld = dest.get('wld')
if not wld:
wld = dest.get('world')
dim = dest.get('dim')
if not dim:
dim = dest.get('dimension')
cyc = dest.get('cyc')
if not cyc:
cyc = dest.get('cycle')
off = dest.get('off')
if not off:
off = dest.get('offset')
if lay is None:
lay = 1
if grf is None:
grf = 1
if wld is None:
wld = 1
if dim is None:
dim = 0
if cyc is None:
cyc = 0
if off is None:
off = 0
return pack('>BBBBII', lay, grf, wld, dim, cyc, off)
def make_message(msg, dest=None, messageID=None):
""" create serialized message elements """
if isinstance(msg, array):
if 'df'.find(msg.typecode) >= 0:
arg = VALUES_NATIVE + VALUES_FLOAT + (msg.itemsize - 1)
elif 'LIHBC'.find(msg.typecode) >= 0:
arg = VALUES_NATIVE + VALUES_UNSIGNED + (msg.itemsize - 1)
elif 'lihbc'.find(msg.typecode) >= 0:
arg = VALUES_NATIVE + VALUES_INTEGER + (msg.itemsize - 1)
else:
raise TypeError("invalid value type")
hdr = make_header(MESSAGE_DEST, arg, messageID)
return (hdr + make_destination(dest), msg.tostring())
if isinstance(msg, str):
msg = bytes(bytearray(msg, 'utf-8'))
return (make_header(MESSAGE_COMMAND, ord(" "), messageID), msg)
if isinstance(msg, bytes):
if dest:
fmt = VALUES_NATIVE + VALUES_FLOAT + (8 - 1)
hdr = make_header(MESSAGE_VALUES, fmt, messageID)
else:
hdr = make_header(MESSAGE_OUTPUT, ord(" "), messageID)
return (hdr, msg)
class Graphic(Output):
""" start or connect to graphic output process """
DEFAULT_PORT = 16565
# instance is first argument
def nextID(self):
return None
def __init__(self, dest=None, exe='mptplot'):
""" create or connect to graphic process """
super(Graphic, self).__init__()
self._encode = encode_cobs
if isinstance(dest, tuple):
port = Graphic.DEFAULT_PORT
if len(dest) > 1:
dest, port = dest
elif len(dest) > 0:
dest = dest[0]
else:
dest = 'localhost'
dest = (dest, port)
# connect to existing graphic process
if exe is None:
if dest is None:
return
if isinstance(dest, str):
chan = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
elif isinstance(dest, tuple):
chan = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
raise TypeError("invalid destination")
chan.connect(dest)
self._chan = chan
# default to non-return ID
self.nextID = lambda: 0
return
# use global output process
if exe[0] != '/':
try:
exe = os.path.join(os.environ['MPT_PREFIX'], 'bin', exe)
except KeyError:
exe = which(exe)
# create graphic process arguments
if dest is None:
argv = [exe]
elif isinstance(dest, str):
self.name = 'Unix:' + dest
argv = [exe, '-s', self.name]
elif isinstance(dest, tuple):
argv = [exe, '-s', 'Ip:' + dest[0] + ':' + str(dest[1])]
self.name = 'Ip:localhost:' + str(dest[1])
else:
raise TypeError("invalid destination")
# new environment for graphic process
environ = os.environ.copy()
environ['MPT_GRAPHIC_PIPE'] = 'cobs'
# start new graphic process
self.process = Popen(args=argv, executable=exe,
preexec_fn=os.setpgrp, stdin=PIPE, env=environ)
self._chan = self.process.stdin
def __del__(self):
""" close graphic process if startet for this connection """
if not hasattr(self, 'process'):
return
if self.process.poll() is not None:
return
self.send('close')
def __str__(self):
if not hasattr(self, 'name'):
return ''
return self.name
def send(self, msg, dest=None, onreturn=None):
""" send message data to graphic process """
if onreturn:
onreturn(None)
self.push(make_message(msg, dest, self.nextID()))
self.flush()
def which(program):
""" resolve program name in $PATH """
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ['PATH'].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def error(*err):
""" print message and newline to standard error """
msg = ""
for i in err:
msg = msg + str(i) + " "
sys.stderr.write(msg + os.linesep)
def tolist(arg):
""" convert argument to list """
if isinstance(arg, list):
return arg
if isinstance(arg, tuple):
return list(arg)
if isinstance(arg, str):
return [arg]
raise TypeError("require string, tuple or list")
def getterm():
""" get terminal program """
term = os.getenv('MPT_TERMINAL')
if term is not None:
exe = which(term)
if exe:
return [exe]
error("invalid terminal: " + term)
return None
for term in SEARCH_TERMINAL:
exe = which(term[0])
if exe is not None:
return [exe] + term[1:]
return None
def setup(process, wpath='', cpath=None):
""" setup MPT environment """
control = []
env = os.environ.copy()
if not cpath:
cpath = os.getcwd()
env['MPT_CWD'] = cpath
for idx, p in enumerate(process):
sname = 'mpt.' + str(os.getpid()) + '.ctl' + str(idx)
try:
sname = os.path.join(wpath, sname)
os.mkfifo(sname, 0o600)
env['MPT_CONNECT'] = 'r:' + sname
cl = p.get('client')
if cl is not None:
env['MPT_CLIENT'] = cl
Popen(args=p['command'], env=env, stderr=PIPE,
close_fds=True, preexec_fn=os.setpgrp)
control = control + [open(sname, 'wb')]
except OSError:
cleanup(control)
raise
return control
def cleanup(control):
""" delete control pipes """
for c in control:
if not c:
continue
try:
os.unlink(c.name)
except OSError:
pass
def close(control):
""" send close command to control pipes """
for c in control:
if not c or c.closed:
continue
try:
c.write(encode_command(b'close'))
c.flush()
c.close()
except IOError:
pass
class Shell(cmd.Cmd):
""" MPT shell interpreter and user process controller
Create shell object for user processes
i=Shell() # create shell instance
i.add('ode') # add client program
i.run() # start interpreter
"""
intro = "MPT control shell"
prompt = "(mpt) "
terminal = None
wrapper = [sys.executable, os.path.abspath(__file__)]
def do_start(self, args):
""" send start command to first user process """
return self.send('start ' + args)
def complete_start(self, text, line, begidx, endidx):
return glob(text + '*')
def do_cont(self, args):
""" send continue command to first user process """
return self.send('cont ' + args)
def do_stop(self, args):
""" send stop command to first user process """
return self.send('stop ' + args)
def do_read(self, args):
""" send read command to first user process """
return self.send('read ' + args)
def complete_read(self, text, line, begidx, endidx):
return glob(text + '*')
def do_init(self, args):
""" send initialisation command to first user process """
return self.send('init ' + args)
def do_prep(self, args):
""" send prepare command to first user process """
return self.send('prep ' + args)
def do_step(self, args):
""" send single step command to first user process """
return self.send('step ' + args)
def do_set(self, args):
""" send assignment command to first user process """
return self.send('set ' + args)
def do_clear(self, args):
""" send clear command to first user process """
return self.send('clear ' + args)
def do_graphic(self, args):
""" graphic command for first user process """
if hasattr(self, 'graphic') and hasattr(self.graphic, 'send'):
return self.graphic.send(args)
self.error('no graphic connection')
def do_close(self, args):
""" close shell and user processes """
if hasattr(self, 'graphic') and hasattr(self.graphic, 'process'):
del self.graphic.process
raise SystemExit
def do_EOF(self, args):
""" clean exit on 'C-d' """
sys.stdout.write(os.linesep)
raise SystemExit
def do_exit(self, line=""):
""" exit command interpreter and clean environment """
close(self.control)
raise SystemExit
def do_mesg(self, line):
""" send command to (first) user process """
s = line.split(' ')[0]
try:
chan = int(s)
return self.send(line[len(s):], chan)
except ValueError:
return self.send(line)
def do_shell(self, args):
""" execute command via standard shell """
return os.system(args)
def complete_shell(self, text, line, begidx, endidx):
return glob(text + '*')
def default(self, line=""):
""" default error message """
self.error('command "' + str.split(line, ' ')[0] + '" not found')
def send(self, args, chan=0):
""" send command to user processes """
if chan < 0:
self.error('bad control slot: ' + str(chan))
return
if not hasattr(self, 'control') or chan >= len(self.control):
self.error('control slot ' + str(chan) + ' not available')
return
c = self.control[chan]
if not c or c.closed:
self.error('control slot ' + str(chan) + ' inactive')
return
try:
m = bytearray()
for d in make_message(args):
m = m + d
c.write(encode_cobs(m))
c.flush()
except Exception:
c.close()
def error(self, *args):
error(*args)
def add(self, cmd):
""" append user process """
if self.terminal is None:
if Shell.terminal is None:
Shell.terminal = getterm()
self.terminal = Shell.terminal
if not hasattr(self, 'process'):
self.process = []
# set new process parameters
proc = {
'command': tolist(self.terminal) + tolist(self.wrapper),
'client': cmd
}
self.process = self.process + [proc]
def run(self, wpath=rundir):
""" setup and run shell interpreter """
if wpath != '':
try:
os.mkdir(wpath, 0o700)
except OSError:
pass
# create client processes
self.control = setup(self.process, wpath)
# connect clients to graphic
if hasattr(self, 'control') and hasattr(self, 'graphic'):
cmd = bytearray()
cmd = cmd + make_header(MESSAGE_SET, 2)
cmd = cmd + bytearray("mpt\x00graphic\x00", 'utf-8')
cmd = cmd + bytearray(self.graphic.name, 'utf-8')
cmd = encode_cobs(cmd)
for p in self.control:
p.write(cmd)
p.flush()
# run command interpreter
while 1:
try:
self.cmdloop()
except KeyboardInterrupt:
self.intro = ' '
except SystemExit:
break
except Exception:
# avoid graphic close on error
if hasattr(self, 'graphic'):
if hasattr(self.graphic, 'process'):
del self.graphic.process
raise
# remove control pipes
cleanup(self.control)
del self.control
# remove temporary path
if wpath == '':
return
try:
os.rmdir(wpath)
except OSError:
pass
def client(args):
""" wrap programs to avoid close of termial on error """
p = None
try:
p = Popen(args).wait()
if p == 0:
return p
if p < 0:
error("Terminated by signal " + str(-p))
else:
error("Exited with code " + str(p))
except KeyboardInterrupt:
error("Keyboard Interrupt")
except OSError:
error("File not accessable")
error("Hit Enter to continue")
sys.stdin.readline()
return p
def run(args):
""" run Shell environment """
for i in args:
if not os.path.exists(i):
raise SystemExit("client program not found: " + i)
if not os.access(i, os.X_OK):
raise SystemExit("client program not executable: " + i)
i = Shell()
for arg in args:
i.add(arg)
g = os.path.join(rundir, 'mpt.' + str(os.getpid()) + '.graphic')
i.graphic = Graphic(dest=g)
i.run()
if __name__ == '__main__':
cl = os.getenv('MPT_CLIENT')
if cl is not None:
# change working directory
wdir = os.getenv('MPT_CWD')
if wdir is not None:
os.chdir(wdir)
exe = which(cl)
if exe is None:
exe = './' + cl
os.unsetenv('MPT_CLIENT')
os.environ['MPT_FLAGS'] = 'e'
# start client under debug process
if os.getenv('MPT_DEBUG') is not None:
# enable client setup from environment
dbg = os.getenv('MPT_DEBUG_COMMAND')
if not dbg:
dbg = 'gdb'
client([dbg, exe])
else:
error("Start program '" + '\033[01m' + cl + '\033[00m' + "'")
client(exe)
else:
if len(sys.argv) < 2:
name = os.path.basename(sys.argv[0])
raise SystemExit(sys.argv[0] + ": missing arguments"
+ os.linesep + " " + name # noqa: W503
+ " <client1> [<clientN>]") # noqa: W503
run(sys.argv[1:])