]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
machine: Rework run() output capture
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 12 Apr 2022 20:52:10 +0000 (22:52 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 19 Apr 2022 06:59:48 +0000 (08:59 +0200)
Let's not capture output by default. Instead, let's forward it
directly to stdout/stderr to simplify debugging. Similar to the
subprocess.run() function, let's add a capture_output argument
to allow configuring whether to capture the output.

We also remove the debug argument from Machine since logging to
stdout/stderr is now the default.

mkosi/machine.py
tests/test_machine.py

index 336deb9c1f80896655a048863dc114ee0d6a272d..91c97bd15532a199b25c4e0001aac8b706c2440c 100644 (file)
@@ -9,7 +9,7 @@ import subprocess
 import sys
 import unittest
 from textwrap import dedent
-from typing import Any, Iterator, Optional, Sequence
+from typing import Any, Iterator, Optional, Sequence, TextIO, Union
 
 import pexpect  # type: ignore
 
@@ -35,11 +35,10 @@ from .backend import MkosiArgs, MkosiNotSupportedException, Verb, die
 
 
 class Machine:
-    def __init__(self, args: Sequence[str] = [], debug: bool = False) -> None:
+    def __init__(self, args: Sequence[str] = []) -> None:
         # Remains None until image is built and booted, then receives pexpect process.
         self._serial: Optional[pexpect.spawn] = None
         self.exit_code: int = -1
-        self.debug = debug
         self.stack = contextlib.ExitStack()
         self.args: MkosiArgs
 
@@ -139,15 +138,19 @@ class Machine:
             self._serial.expect("#")
             self.stack = stack.pop_all()
 
-    def run(self, commands: Sequence[str], timeout: int = 900, check: bool = True) -> CompletedProcess:
+    def run(
+        self,
+        commands: Sequence[str],
+        timeout: int = 900,
+        check: bool = True,
+        capture_output: bool = False,
+    ) -> CompletedProcess:
         self._ensure_booted()
 
-        process = run_command_image(self.args, commands, timeout, check, subprocess.PIPE, subprocess.PIPE)
-        if self.debug:
-            print(f"Stdout:\n {process.stdout}")
-            print(f"Stderr:\n {process.stderr}")
+        stdout: Union[int, TextIO] = subprocess.PIPE if capture_output else sys.stdout
+        stderr: Union[int, TextIO] = subprocess.PIPE if capture_output else sys.stderr
 
-        return process
+        return run_command_image(self.args, commands, timeout, check, stdout, stderr)
 
     def kill(self) -> None:
         self.__exit__(None, None, None)
index 05132ec139fd86fa04acca712b83039d8235a096..6555bc9bd5c99dd9054e158db33ff8ba2c5f97e0 100644 (file)
@@ -22,7 +22,7 @@ class MkosiMachineTestCase(MkosiMachineTest):
             pytest.xfail("QEMU's CPU does not support the CentOS EPEL image arch when running without KVM")
 
     def test_simple_run(self) -> None:
-        process = self.machine.run(["echo", "This is a test."])
+        process = self.machine.run(["echo", "This is a test."], capture_output=True)
         assert process.stdout.strip("\n") == "This is a test."
 
     def test_wrong_command(self) -> None:
@@ -36,7 +36,7 @@ class MkosiMachineTestCase(MkosiMachineTest):
         result = self.machine.run(["NonExisting", "Command"], check=False)
         assert result.returncode in (1, 127, 203)
 
-        result = self.machine.run(["ls", "-"], check=False)
+        result = self.machine.run(["ls", "-"], check=False, capture_output=True)
         assert result.returncode == 2
         assert "No such file or directory" in result.stderr