1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
4 # Logic to spawn a sub-process and interact with its stdio.
13 class Timeout(Exception):
14 """An exception sub-class that indicates that a timeout occurred."""
18 """Represents the stdio of a freshly created sub-process. Commands may be
19 sent to the process, and responses waited for.
22 output: accumulated output from expect()
25 def __init__(self, args, cwd=None):
26 """Spawn (fork/exec) the sub-process.
29 args: array of processs arguments. argv[0] is the command to
31 cwd: the directory to run the process in, or None for no change.
40 self.logfile_read = None
44 # http://stackoverflow.com/questions/7857352/python-regex-to-match-vt100-escape-sequences
45 # Note that re.I doesn't seem to work with this regex (or perhaps the
46 # version of Python in Ubuntu 14.04), hence the inclusion of a-z inside
48 self.re_vt100 = re.compile('(\x1b\[|\x9b)[^@-_a-z]*[@-_a-z]|\x1b[@-_a-z]')
50 (self.pid, self.fd) = pty.fork()
53 # For some reason, SIGHUP is set to SIG_IGN at this point when
54 # run under "go" (www.go.cd). Perhaps this happens under any
55 # background (non-interactive) system?
56 signal.signal(signal.SIGHUP, signal.SIG_DFL)
59 os.execvp(args[0], args)
61 print('CHILD EXECEPTION:')
68 self.poll = select.poll()
69 self.poll.register(self.fd, select.POLLIN | select.POLLPRI | select.POLLERR | select.POLLHUP | select.POLLNVAL)
75 """Send unix signal "sig" to the child process.
78 sig: The signal number to send.
84 os.kill(self.pid, sig)
87 """Determine whether the child process is still running.
93 Boolean indicating whether process is alive.
99 w = os.waitpid(self.pid, os.WNOHANG)
106 def send(self, data):
107 """Send data to the sub-process's stdin.
110 data: The data to send to the process.
116 os.write(self.fd, data)
118 def expect(self, patterns):
119 """Wait for the sub-process to emit specific data.
121 This function waits for the process to emit one pattern from the
122 supplied list of patterns, or for a timeout to occur.
125 patterns: A list of strings or regex objects that we expect to
126 see in the sub-process' stdout.
129 The index within the patterns array of the pattern the process
133 Timeout, if the process did not emit any of the patterns within
137 for pi in range(len(patterns)):
138 if type(patterns[pi]) == type(''):
139 patterns[pi] = re.compile(patterns[pi])
141 tstart_s = time.time()
146 for pi in range(len(patterns)):
147 pattern = patterns[pi]
148 m = pattern.search(self.buf)
151 if earliest_m and m.start() >= earliest_m.start():
156 pos = earliest_m.start()
157 posafter = earliest_m.end()
158 self.before = self.buf[:pos]
159 self.after = self.buf[pos:posafter]
160 self.output += self.buf[:posafter]
161 self.buf = self.buf[posafter:]
165 tdelta_ms = (tnow_s - tstart_s) * 1000
166 poll_maxwait = self.timeout - tdelta_ms
167 if tdelta_ms > self.timeout:
171 events = self.poll.poll(poll_maxwait)
174 c = os.read(self.fd, 1024)
177 if self.logfile_read:
178 self.logfile_read.write(c)
180 # count=0 is supposed to be the default, which indicates
181 # unlimited substitutions, but in practice the version of
182 # Python in Ubuntu 14.04 appears to default to count=2!
183 self.buf = self.re_vt100.sub('', self.buf, count=1000000)
185 if self.logfile_read:
186 self.logfile_read.flush()
189 """Close the stdio connection to the sub-process.
191 This also waits a reasonable time for the sub-process to stop running.
202 if not self.isalive():
206 def get_expect_output(self):
207 """Return the output read by expect()
210 The output processed by expect(), as a string.