[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 07/25] python/utils: add VerboseProcessError
From: |
Hanna Reitz |
Subject: |
[PULL 07/25] python/utils: add VerboseProcessError |
Date: |
Tue, 22 Mar 2022 12:56:29 +0100 |
From: John Snow <jsnow@redhat.com>
This adds an Exception that extends the Python stdlib
subprocess.CalledProcessError.
The difference is that the str() method of this exception also adds the
stdout/stderr logs. In effect, if this exception goes unhandled, Python
will print the output in a visually distinct wrapper to the terminal so
that it's easy to spot in a sea of traceback information.
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Message-Id: <20220321201618.903471-3-jsnow@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
---
python/qemu/utils/__init__.py | 39 +++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git a/python/qemu/utils/__init__.py b/python/qemu/utils/__init__.py
index b84c86d004..9fb273b13d 100644
--- a/python/qemu/utils/__init__.py
+++ b/python/qemu/utils/__init__.py
@@ -18,6 +18,7 @@
import os
import re
import shutil
+from subprocess import CalledProcessError
import textwrap
from typing import Optional
@@ -26,6 +27,7 @@
__all__ = (
+ 'VerboseProcessError',
'add_visual_margin',
'get_info_usernet_hostfwd_port',
'kvm_available',
@@ -121,3 +123,40 @@ def _wrap(line: str) -> str:
os.linesep.join(_wrap(line) for line in content.splitlines()),
_bar(None, top=False),
))
+
+
+class VerboseProcessError(CalledProcessError):
+ """
+ The same as CalledProcessError, but more verbose.
+
+ This is useful for debugging failed calls during test executions.
+ The return code, signal (if any), and terminal output will be displayed
+ on unhandled exceptions.
+ """
+ def summary(self) -> str:
+ """Return the normal CalledProcessError str() output."""
+ return super().__str__()
+
+ def __str__(self) -> str:
+ lmargin = ' '
+ width = -len(lmargin)
+ sections = []
+
+ # Does self.stdout contain both stdout and stderr?
+ has_combined_output = self.stderr is None
+
+ name = 'output' if has_combined_output else 'stdout'
+ if self.stdout:
+ sections.append(add_visual_margin(self.stdout, width, name))
+ else:
+ sections.append(f"{name}: N/A")
+
+ if self.stderr:
+ sections.append(add_visual_margin(self.stderr, width, 'stderr'))
+ elif not has_combined_output:
+ sections.append("stderr: N/A")
+
+ return os.linesep.join((
+ self.summary(),
+ textwrap.indent(os.linesep.join(sections), prefix=lmargin),
+ ))
--
2.35.1
- [PULL 00/25] Block patches for 7.0-rc1, Hanna Reitz, 2022/03/22
- [PULL 02/25] block/rbd: fix write zeroes with growing images, Hanna Reitz, 2022/03/22
- [PULL 03/25] tests/qemu-iotests: Use GNU sed in two more spots where it is necessary, Hanna Reitz, 2022/03/22
- [PULL 01/25] tests: add (riscv virt) machine mapping to testenv, Hanna Reitz, 2022/03/22
- [PULL 04/25] tests: Do not treat the iotests as separate meson test target anymore, Hanna Reitz, 2022/03/22
- [PULL 05/25] tests/qemu-iotests/testrunner: Supply a test plan in TAP mode, Hanna Reitz, 2022/03/22
- [PULL 07/25] python/utils: add VerboseProcessError,
Hanna Reitz <=
- [PULL 06/25] python/utils: add add_visual_margin() text decoration utility, Hanna Reitz, 2022/03/22
- [PULL 08/25] iotests: Remove explicit checks for qemu_img() == 0, Hanna Reitz, 2022/03/22
- [PULL 09/25] iotests: make qemu_img raise on non-zero rc by default, Hanna Reitz, 2022/03/22
- [PULL 11/25] iotests: add qemu_img_json(), Hanna Reitz, 2022/03/22
- [PULL 10/25] iotests: fortify compare_images() against crashes, Hanna Reitz, 2022/03/22
- [PULL 12/25] iotests: use qemu_img_json() when applicable, Hanna Reitz, 2022/03/22
- [PULL 13/25] iotests: add qemu_img_info(), Hanna Reitz, 2022/03/22
- [PULL 14/25] iotests/remove-bitmap-from-backing: use qemu_img_info(), Hanna Reitz, 2022/03/22
- [PULL 16/25] iotests: change supports_quorum to use qemu_img, Hanna Reitz, 2022/03/22
- [PULL 17/25] iotests: replace unchecked calls to qemu_img_pipe(), Hanna Reitz, 2022/03/22