mirror of
https://github.com/motioneye-project/motioneyeos.git
synced 2025-07-30 22:56:33 +00:00
support/testing: use pexpect.expect directly
When using pexpect there is no need for a helper function. Just use expect() directly everywhere. Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
This commit is contained in:
parent
77b4b948fc
commit
4a622fe4b6
@ -8,7 +8,6 @@ class Emulator(object):
|
|||||||
def __init__(self, builddir, downloaddir, logtofile):
|
def __init__(self, builddir, downloaddir, logtofile):
|
||||||
self.qemu = None
|
self.qemu = None
|
||||||
self.downloaddir = downloaddir
|
self.downloaddir = downloaddir
|
||||||
self.log = ""
|
|
||||||
self.logfile = infra.open_log_file(builddir, "run", logtofile)
|
self.logfile = infra.open_log_file(builddir, "run", logtofile)
|
||||||
|
|
||||||
# Start Qemu to boot the system
|
# Start Qemu to boot the system
|
||||||
@ -66,34 +65,25 @@ class Emulator(object):
|
|||||||
qemu_cmd += ["-append", " ".join(kernel_cmdline)]
|
qemu_cmd += ["-append", " ".join(kernel_cmdline)]
|
||||||
|
|
||||||
self.logfile.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd))
|
self.logfile.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd))
|
||||||
self.qemu = pexpect.spawn(qemu_cmd[0], qemu_cmd[1:])
|
self.qemu = pexpect.spawn(qemu_cmd[0], qemu_cmd[1:], timeout=5)
|
||||||
# We want only stdout into the log to avoid double echo
|
# We want only stdout into the log to avoid double echo
|
||||||
self.qemu.logfile_read = self.logfile
|
self.qemu.logfile_read = self.logfile
|
||||||
|
|
||||||
def __read_until(self, waitstr, timeout=5):
|
|
||||||
index = self.qemu.expect([waitstr, pexpect.TIMEOUT], timeout=timeout)
|
|
||||||
data = self.qemu.before
|
|
||||||
if index == 0:
|
|
||||||
data += self.qemu.after
|
|
||||||
self.log += data
|
|
||||||
# Remove double carriage return from qemu stdout so str.splitlines()
|
|
||||||
# works as expected.
|
|
||||||
return data.replace("\r\r", "\r")
|
|
||||||
|
|
||||||
# Wait for the login prompt to appear, and then login as root with
|
# Wait for the login prompt to appear, and then login as root with
|
||||||
# the provided password, or no password if not specified.
|
# the provided password, or no password if not specified.
|
||||||
def login(self, password=None):
|
def login(self, password=None):
|
||||||
self.__read_until("buildroot login:", 10)
|
index = self.qemu.expect(["buildroot login:", pexpect.TIMEOUT],
|
||||||
if "buildroot login:" not in self.log:
|
timeout=10)
|
||||||
|
if index != 0:
|
||||||
self.logfile.write("==> System does not boot")
|
self.logfile.write("==> System does not boot")
|
||||||
raise SystemError("System does not boot")
|
raise SystemError("System does not boot")
|
||||||
|
|
||||||
self.qemu.sendline("root")
|
self.qemu.sendline("root")
|
||||||
if password:
|
if password:
|
||||||
self.__read_until("Password:")
|
self.qemu.expect("Password:")
|
||||||
self.qemu.sendline(password)
|
self.qemu.sendline(password)
|
||||||
self.__read_until("# ")
|
index = self.qemu.expect(["# ", pexpect.TIMEOUT])
|
||||||
if "# " not in self.log:
|
if index != 0:
|
||||||
raise SystemError("Cannot login")
|
raise SystemError("Cannot login")
|
||||||
self.run("dmesg -n 1")
|
self.run("dmesg -n 1")
|
||||||
|
|
||||||
@ -101,13 +91,14 @@ class Emulator(object):
|
|||||||
# return a tuple (output, exit_code)
|
# return a tuple (output, exit_code)
|
||||||
def run(self, cmd):
|
def run(self, cmd):
|
||||||
self.qemu.sendline(cmd)
|
self.qemu.sendline(cmd)
|
||||||
output = self.__read_until("# ")
|
self.qemu.expect("# ")
|
||||||
output = output.strip().splitlines()
|
# Remove double carriage return from qemu stdout so str.splitlines()
|
||||||
output = output[1:len(output)-1]
|
# works as expected.
|
||||||
|
output = self.qemu.before.replace("\r\r", "\r").splitlines()[1:]
|
||||||
|
|
||||||
self.qemu.sendline("echo $?")
|
self.qemu.sendline("echo $?")
|
||||||
exit_code = self.__read_until("# ")
|
self.qemu.expect("# ")
|
||||||
exit_code = exit_code.strip().splitlines()[1]
|
exit_code = self.qemu.before.splitlines()[2]
|
||||||
exit_code = int(exit_code)
|
exit_code = int(exit_code)
|
||||||
|
|
||||||
return output, exit_code
|
return output, exit_code
|
||||||
|
Loading…
x
Reference in New Issue
Block a user