-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngFECSendCommand.py
More file actions
112 lines (103 loc) · 4.62 KB
/
ngFECSendCommand.py
File metadata and controls
112 lines (103 loc) · 4.62 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
import pexpect
from re import escape
import signal
import time
class DelayedKeyboardInterrupt(object):
def __enter__(self):
self.signal_received = False
self.old_handler = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, self.handler)
def handler(self, sig, frame):
self.signal_received = (sig, frame)
logging.debug('SIGINT received. Delaying KeyboardInterrupt.')
def __exit__(self, type, value, traceback):
signal.signal(signal.SIGINT, self.old_handler)
if self.signal_received:
self.old_handler(*self.signal_received)
def send_commands(port, control_hub, cmds, script=False, raw=False, time_out=30):
#print cmds
# Arguments and variables
output = []
raw_output = ""
if control_hub != None and port: # Potential bug if "port=0" ... (Control_hub should be allowed to be None.)
## Parse commands:
if isinstance(cmds, str):
cmds = [cmds]
if not script:
pass
else:
cmds = [c for c in cmds if c != "quit"] # "quit" can't be in a ngFEC script.
#print cmds
cmds_str = ""
for c in cmds:
cmds_str += "{0}\n".format(c)
file_script = "ngfec_script"
with open(file_script, "w") as out:
out.write(cmds_str)
# Prepare the ngfec arguments:
ngfec_cmd = 'ngFEC.exe -t -c -p {0}'.format(port)
if not script:
ngfec_cmd = 'ngFEC.exe -t -z -c -p {0}'.format(port)
if control_hub != None:
ngfec_cmd += " -H {0}".format(control_hub)
#print ngfec_cmd
# Send the ngfec commands:
p = pexpect.spawn(ngfec_cmd, timeout=time_out)
try:
with DelayedKeyboardInterrupt():
if not script:
for i, c in enumerate(cmds):
p.sendline(c)
if c != "quit":
t0 = time.time()
p.expect("{0}\s?#((\s|E)[^\r^\n]*)".format(escape(c)))
t1 = time.time()
output.append({
"cmd": c,
"result": p.match.group(1).replace('# _','').strip().replace("'", ""),
"times": [t0, t1],
})
raw_output += p.before + p.after
else:
p.sendline("< {0}".format(file_script))
for i, c in enumerate(cmds):
# Deterimine how long to wait until the first result is expected:
if i == 0:
timeout = max([time_out, int(0.075*len(cmds))])
else:
timeout = time_out # pexpect default
# Send commands:
t0 = time.time()
#print i, c, "{0}\s?#((\s|E)[^\r^\n]*)".format(escape(c))
#p.expect("{0}\s?#((\s|E)[^\r^\n]*)".format(escape(c)), timeout=timeout)
p.expect("{0}\s?#((\s|E)[^\r^\n]*)\r\n".format(escape(c)), timeout=timeout)
t1 = time.time()
output.append({
"cmd": c,
"result": p.match.group(1).strip().replace("'", ""),
"times": [t0, t1],
})
raw_output += p.before + p.after
except pexpect.TIMEOUT:
print "PExpect Timeout! Probably ngCCM server not found!!!"
raise
except KeyboardInterrupt:
print "KeyboardInterrupt detected: closing ngFEC client connection"
raise
finally:
with DelayedKeyboardInterrupt():
try:
p.sendline("quit")
p.expect(pexpect.EOF)
except pexpect.TIMEOUT:
print "well, guess the client (or server??) just crashed, but the show must go on so client process is being killed"
if not p.terminate(True):
print "Client termination failed, please comense manual process cleanup!"
else:
p.read() #Take care of any unfinished business so the process can rest in peace
raw_output += p.before
p.close()
if raw:
return (raw_output, output)
else:
return output