-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_python_util.py
More file actions
92 lines (82 loc) · 3.3 KB
/
my_python_util.py
File metadata and controls
92 lines (82 loc) · 3.3 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
import pexpect
import os
import struct, fcntl, termios, signal, sys
def general_expect(child, expect_list, intent_desc, eof_ok=0, print_output=0, timeout=0, timeout_ok=0):
''' Wrapper on top of pexpect's child.expect([expect-list])
intent_desc is a string, that will printed if expect throws a exception.
eof_ok if non-0, returns len(expect_list)+1 when eof is hit. Otherwise eof is bad
print_output is whether to emit child's stdout in the good-case condition
O/p is always stored into general_expect_failure on error
timeout_ok if non-0, then timeout doesn't raise Exception. You will get len(expect_list)+2 as result.
timeout if non-0, is passed to expect, otherwise Not. You can wait infinitely with this wrapper. Sorry.
Note that child.before and child.after are still available to caller to consume
'''
try:
error = ""
if timeout:
result = child.expect(expect_list, timeout)
else:
result = child.expect(expect_list)
if result >= len(expect_list):
#huh!
error = "Got none of the expected result!"
except pexpect.EOF:
if eof_ok:
result = len(expect_list)
else:
error="Eof hit:\n"
except pexpect.TIMEOUT:
if timeout_ok:
result = len(expect_list)+1
else:
error="Timeout out without matches:\n"
if print_output:
print (str(child.before)+str(child.after))
if error:
err_str = "Error while doing:" + intent_desc + "\n" + "Error:" + error + "\n"
if isinstance(expect_list, str):
expected=expect_list
else:
expected = ""
for i in expect_list:
expected += i + "\n"
err_str += "Expected:\n" + expected + "\n"
open("general_expect_failure","w").write(err_str+str(child.before))
raise Exception(err_str);
return result
def get_parent_win_size():
with open(os.ctermid(), 'r') as fd:
packed = fcntl.ioctl(fd, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0))
rows, cols, h_pixels, v_pixels = struct.unpack('HHHH', packed)
return (rows,cols)
return (24,80)
def spawn_child_later_for_interaction(command):
child = pexpect.spawn(command, encoding='utf-8')
if not child:
raise Exception("no child for command:{}".format(command))
def sigwinch_passthrough (sig, discard_arg):
s = struct.pack("HHHH", 0, 0, 0, 0)
a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ , s))
child.setwinsize(a[0],a[1])
(r,c) = get_parent_win_size()
signal.signal(signal.SIGWINCH, sigwinch_passthrough)
child.setwinsize(r,c)
return child
def execute_cmd(cmd, print_cmd=False, error_ok=True, shouldErrBeEmpty=True, print_op=False, dry_run=False, shellChoice=False):
if print_cmd:
if shellChoice:
print ("Executing :{}".format(cmd))
else:
print ("Executing :%s"%' '.join(cmd))
if dry_run:
return ""
a=subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shellChoice)
output,err=a.communicate()
errcode = a.wait()
if (errcode != 0) or (shouldErrBeEmpty and not err):
if not error_ok:
print("got a error,err:%s, errcode:%d"%(err,errcode))
sys.exit(1)
if print_op:
print("Got:\n%s"%output)
return (errcode, output, err)