From: Jan Kiszka <
jan.k...@siemens.com>
While run_cmd_async was dumping stderr on fail=True, run_cmd was not.
This aligns both which specifically helps debugging failing
init-build-env scripts.
It furthermore makes the error dump more compact. Error reports are
already clearly marked by not carrying the prefixes of kas-generated
outputs.
Closes #166
Signed-off-by: Jan Kiszka <
jan.k...@siemens.com>
---
Changes in v2:
- keep the header line as CommandExecError may be caught and
differently reported in some cases
- move cmdstr generation around to avoid needless execution
kas/libkas.py | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)
diff --git a/kas/libkas.py b/kas/libkas.py
index 89113fa..95afd20 100644
--- a/kas/libkas.py
+++ b/kas/libkas.py
@@ -116,6 +116,14 @@ def _filter_stderr(capture_stderr, ret, out, err=None):
return (ret, out)
+def _report_cmd_error(ret, cwd, cmd, stderr):
+ if stderr:
+ cmdstr = ' '.join(cmd)
+ msg = f'Command "{cwd}$ {cmdstr}" failed:\n{stderr}'
+ logging.error(msg.rstrip('\n'))
+ raise CommandExecError(cmd, ret)
+
+
async def run_cmd_async(cmd, cwd, env=None, fail=True, liveupdate=False,
capture_stderr=False):
"""
@@ -123,7 +131,6 @@ async def run_cmd_async(cmd, cwd, env=None, fail=True, liveupdate=False,
"""
env = env or get_context().environ
- cmdstr = ' '.join(cmd)
logging.debug('%s$ %s', cwd, cmdstr)
logo = LogOutput(liveupdate)
@@ -165,18 +172,13 @@ async def run_cmd_async(cmd, cwd, env=None, fail=True, liveupdate=False,
ret = await asyncio.shield(process.wait())
except asyncio.CancelledError:
process.terminate()
+ cmdstr = ' '.join(cmd)
logging.debug('Command "%s" cancelled', cmdstr)
await process.wait()
raise
if ret and fail:
- msg = f'Command "{cwd}$ {cmdstr}" failed'
- if logo.stderr:
- msg += '\n--- Error summary ---\n'
- for line in logo.stderr:
- msg += line
- logging.error(msg)
- raise CommandExecError(cmd, ret)
+ _report_cmd_error(ret, cwd, cmd, logo.stderr)
return _filter_stderr(capture_stderr, ret,
''.join(logo.stdout), ''.join(logo.stderr))
@@ -193,7 +195,8 @@ def run_cmd(cmd, cwd, env=None, fail=True, capture_stderr=False):
try:
ret = subprocess_run(cmd, env=env, cwd=cwd, stdout=PIPE, stderr=PIPE)
if ret.returncode and fail:
- raise CommandExecError(cmd, ret.returncode)
+ _report_cmd_error(ret.returncode, cwd, cmd,
+ ret.stderr.decode('utf-8'))
except FileNotFoundError as ex:
if fail:
raise ex
--
2.51.0