2016-01-15 18:15:24 +00:00
|
|
|
# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
# Logic to spawn a sub-process and interact with its stdio.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import pty
|
|
|
|
import signal
|
|
|
|
import select
|
|
|
|
import time
|
|
|
|
|
|
|
|
class Timeout(Exception):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""An exception sub-class that indicates that a timeout occurred."""
|
2016-01-15 18:15:24 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
class Spawn(object):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Represents the stdio of a freshly created sub-process. Commands may be
|
2016-01-15 18:15:24 +00:00
|
|
|
sent to the process, and responses waited for.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
def __init__(self, args):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Spawn (fork/exec) the sub-process.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
args: array of processs arguments. argv[0] is the command to execute.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self.waited = False
|
|
|
|
self.buf = ''
|
|
|
|
self.logfile_read = None
|
|
|
|
self.before = ''
|
|
|
|
self.after = ''
|
|
|
|
self.timeout = None
|
|
|
|
|
|
|
|
(self.pid, self.fd) = pty.fork()
|
|
|
|
if self.pid == 0:
|
|
|
|
try:
|
|
|
|
# For some reason, SIGHUP is set to SIG_IGN at this point when
|
|
|
|
# run under "go" (www.go.cd). Perhaps this happens under any
|
|
|
|
# background (non-interactive) system?
|
|
|
|
signal.signal(signal.SIGHUP, signal.SIG_DFL)
|
|
|
|
os.execvp(args[0], args)
|
|
|
|
except:
|
|
|
|
print 'CHILD EXECEPTION:'
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
finally:
|
|
|
|
os._exit(255)
|
|
|
|
|
|
|
|
self.poll = select.poll()
|
|
|
|
self.poll.register(self.fd, select.POLLIN | select.POLLPRI | select.POLLERR | select.POLLHUP | select.POLLNVAL)
|
|
|
|
|
|
|
|
def kill(self, sig):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Send unix signal "sig" to the child process.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
sig: The signal number to send.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
os.kill(self.pid, sig)
|
|
|
|
|
|
|
|
def isalive(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Determine whether the child process is still running.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Boolean indicating whether process is alive.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
if self.waited:
|
|
|
|
return False
|
|
|
|
|
|
|
|
w = os.waitpid(self.pid, os.WNOHANG)
|
|
|
|
if w[0] == 0:
|
|
|
|
return True
|
|
|
|
|
|
|
|
self.waited = True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def send(self, data):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Send data to the sub-process's stdin.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
data: The data to send to the process.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
os.write(self.fd, data)
|
|
|
|
|
|
|
|
def expect(self, patterns):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Wait for the sub-process to emit specific data.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
This function waits for the process to emit one pattern from the
|
|
|
|
supplied list of patterns, or for a timeout to occur.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
patterns: A list of strings or regex objects that we expect to
|
|
|
|
see in the sub-process' stdout.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The index within the patterns array of the pattern the process
|
|
|
|
emitted.
|
|
|
|
|
|
|
|
Notable exceptions:
|
|
|
|
Timeout, if the process did not emit any of the patterns within
|
|
|
|
the expected time.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
for pi in xrange(len(patterns)):
|
|
|
|
if type(patterns[pi]) == type(''):
|
|
|
|
patterns[pi] = re.compile(patterns[pi])
|
|
|
|
|
2016-01-22 19:30:07 +00:00
|
|
|
tstart_s = time.time()
|
2016-01-15 18:15:24 +00:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
earliest_m = None
|
|
|
|
earliest_pi = None
|
|
|
|
for pi in xrange(len(patterns)):
|
|
|
|
pattern = patterns[pi]
|
|
|
|
m = pattern.search(self.buf)
|
|
|
|
if not m:
|
|
|
|
continue
|
2016-01-28 06:57:47 +00:00
|
|
|
if earliest_m and m.start() >= earliest_m.start():
|
2016-01-15 18:15:24 +00:00
|
|
|
continue
|
|
|
|
earliest_m = m
|
|
|
|
earliest_pi = pi
|
|
|
|
if earliest_m:
|
|
|
|
pos = earliest_m.start()
|
|
|
|
posafter = earliest_m.end() + 1
|
|
|
|
self.before = self.buf[:pos]
|
|
|
|
self.after = self.buf[pos:posafter]
|
|
|
|
self.buf = self.buf[posafter:]
|
|
|
|
return earliest_pi
|
2016-01-22 19:30:07 +00:00
|
|
|
tnow_s = time.time()
|
|
|
|
tdelta_ms = (tnow_s - tstart_s) * 1000
|
|
|
|
if tdelta_ms > self.timeout:
|
|
|
|
raise Timeout()
|
|
|
|
events = self.poll.poll(self.timeout - tdelta_ms)
|
2016-01-15 18:15:24 +00:00
|
|
|
if not events:
|
|
|
|
raise Timeout()
|
|
|
|
c = os.read(self.fd, 1024)
|
|
|
|
if not c:
|
|
|
|
raise EOFError()
|
|
|
|
if self.logfile_read:
|
|
|
|
self.logfile_read.write(c)
|
|
|
|
self.buf += c
|
|
|
|
finally:
|
|
|
|
if self.logfile_read:
|
|
|
|
self.logfile_read.flush()
|
|
|
|
|
|
|
|
def close(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Close the stdio connection to the sub-process.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
This also waits a reasonable time for the sub-process to stop running.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
os.close(self.fd)
|
|
|
|
for i in xrange(100):
|
|
|
|
if not self.isalive():
|
|
|
|
break
|
|
|
|
time.sleep(0.1)
|