1 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
3 # SPDX-License-Identifier: GPL-2.0
5 # Logic to spawn a sub-process and interact with its stdio.
14 class Timeout(Exception):
15 """An exception sub-class that indicates that a timeout occurred."""
19 """Represents the stdio of a freshly created sub-process. Commands may be
20 sent to the process, and responses waited for.
23 output: accumulated output from expect()
26 def __init__(self, args, cwd=None):
27 """Spawn (fork/exec) the sub-process.
30 args: array of processs arguments. argv[0] is the command to
32 cwd: the directory to run the process in, or None for no change.
41 self.logfile_read = None
45 # http://stackoverflow.com/questions/7857352/python-regex-to-match-vt100-escape-sequences
46 # Note that re.I doesn't seem to work with this regex (or perhaps the
47 # version of Python in Ubuntu 14.04), hence the inclusion of a-z inside
49 self.re_vt100 = re.compile('(\x1b\[|\x9b)[^@-_a-z]*[@-_a-z]|\x1b[@-_a-z]')
51 (self.pid, self.fd) = pty.fork()
54 # For some reason, SIGHUP is set to SIG_IGN at this point when
55 # run under "go" (www.go.cd). Perhaps this happens under any
56 # background (non-interactive) system?
57 signal.signal(signal.SIGHUP, signal.SIG_DFL)
60 os.execvp(args[0], args)
62 print 'CHILD EXECEPTION:'
69 self.poll = select.poll()
70 self.poll.register(self.fd, select.POLLIN | select.POLLPRI | select.POLLERR | select.POLLHUP | select.POLLNVAL)
76 """Send unix signal "sig" to the child process.
79 sig: The signal number to send.
85 os.kill(self.pid, sig)
88 """Determine whether the child process is still running.
94 Boolean indicating whether process is alive.
100 w = os.waitpid(self.pid, os.WNOHANG)
107 def send(self, data):
108 """Send data to the sub-process's stdin.
111 data: The data to send to the process.
117 os.write(self.fd, data)
119 def expect(self, patterns):
120 """Wait for the sub-process to emit specific data.
122 This function waits for the process to emit one pattern from the
123 supplied list of patterns, or for a timeout to occur.
126 patterns: A list of strings or regex objects that we expect to
127 see in the sub-process' stdout.
130 The index within the patterns array of the pattern the process
134 Timeout, if the process did not emit any of the patterns within
138 for pi in xrange(len(patterns)):
139 if type(patterns[pi]) == type(''):
140 patterns[pi] = re.compile(patterns[pi])
142 tstart_s = time.time()
147 for pi in xrange(len(patterns)):
148 pattern = patterns[pi]
149 m = pattern.search(self.buf)
152 if earliest_m and m.start() >= earliest_m.start():
157 pos = earliest_m.start()
158 posafter = earliest_m.end()
159 self.before = self.buf[:pos]
160 self.after = self.buf[pos:posafter]
161 self.output += self.buf[:posafter]
162 self.buf = self.buf[posafter:]
166 tdelta_ms = (tnow_s - tstart_s) * 1000
167 poll_maxwait = self.timeout - tdelta_ms
168 if tdelta_ms > self.timeout:
172 events = self.poll.poll(poll_maxwait)
175 c = os.read(self.fd, 1024)
178 if self.logfile_read:
179 self.logfile_read.write(c)
181 # count=0 is supposed to be the default, which indicates
182 # unlimited substitutions, but in practice the version of
183 # Python in Ubuntu 14.04 appears to default to count=2!
184 self.buf = self.re_vt100.sub('', self.buf, count=1000000)
186 if self.logfile_read:
187 self.logfile_read.flush()
190 """Close the stdio connection to the sub-process.
192 This also waits a reasonable time for the sub-process to stop running.
202 for i in xrange(100):
203 if not self.isalive():
207 def get_expect_output(self):
208 """Return the output read by expect()
211 The output processed by expect(), as a string.