[PATCH v2 5/9] oeqa/utils/qemurunner: simplify output parsing and make crlf-compatible

Enrico Jorns
2023-03-31T10:40:48Z

Reduces the nesting depth and replaces the manual newline matching by
built-in splitlines() method.

This makes it compatible with shells that use windows-compatible line
breaks, e.g. for EFI loaders.

More comments and an early return handling should make the code a bit
more readable.

Signed-off-by: Enrico Jorns <ejo@pengutronix.de>
---
meta/lib/oeqa/utils/qemurunner.py | 32 +++++++++++++++----------------
1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/meta/lib/oeqa/utils/qemurunner.py b/meta/lib/oeqa/utils/qemurunner.py
index 95c9e6596c..1e544acc90 100644
--- a/meta/lib/oeqa/utils/qemurunner.py
+++ b/meta/lib/oeqa/utils/qemurunner.py
@@ -672,23 +672,21 @@ class QemuRunner:
return (1, "")
raise Exception("No data on serial console socket, connection closed?")

- if data:
- if raw:
- status = 1
- else:
- # Remove first line (command line) and last line (prompt)
- data = data[data.find('$?\r\n')+4:data.rfind('\r\n')]
- index = data.rfind('\r\n')
- if index == -1:
- status_cmd = data
- data = ""
- else:
- status_cmd = data[index+2:]
- data = data[:index]
- if (status_cmd == "0"):
- status = 1
- return (status, str(data))
-
+ # If we got no data, we assume something went wrong and return 0
+ if not data:
+ return (0, str(None))
+
+ # in raw mode, we cannot check exit status output and thus assume success
+ if raw:
+ return (1, str(data))
+
+ # Split lines into array and remove first line (command line) and last line (prompt)
+ # Also remove empty lines to ease catching results
+ outlines = list(filter(None, data.splitlines()[1:-1]))
+ # Remaining last line contains exit code output
+ if (outlines[-1] == "0"):
+ status = 1
+ return (status, "\n".join(outlines[0:-1]))

def _dump_host(self):
self.host_dumper.create_dir("qemu")
--
2.39.2