libkas: Fail run_cmd silently if command is not found

If we only try-execute a command (fail=False), do not bail out with an
exception when starting that command already fails. Catch typical
exceptions and return the corresponding error code along with the
exception string.

This fixes get_root_path in case hg is not installed.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Jan Kiszka 2018-01-25 19:07:39 +01:00 committed by Daniel Wagner
parent 7fa75d94c0
commit ca6b3c05c9

View File

@ -29,6 +29,7 @@ import sys
import logging import logging
import tempfile import tempfile
import asyncio import asyncio
import errno
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
__license__ = 'MIT' __license__ = 'MIT'
@ -95,21 +96,30 @@ def run_cmd_async(cmd, cwd, env=None, fail=True, shell=False, liveupdate=True):
logo = LogOutput(liveupdate) logo = LogOutput(liveupdate)
if shell: try:
process = yield from asyncio.create_subprocess_shell( if shell:
cmd, process = yield from asyncio.create_subprocess_shell(
env=env, cmd,
cwd=cwd, env=env,
universal_newlines=True, cwd=cwd,
stdout=asyncio.subprocess.PIPE, universal_newlines=True,
stderr=asyncio.subprocess.PIPE) stdout=asyncio.subprocess.PIPE,
else: stderr=asyncio.subprocess.PIPE)
process = yield from asyncio.create_subprocess_exec( else:
*cmd, process = yield from asyncio.create_subprocess_exec(
cwd=cwd, *cmd,
env=env, cwd=cwd,
stdout=asyncio.subprocess.PIPE, env=env,
stderr=asyncio.subprocess.PIPE) stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
except FileNotFoundError as ex:
if fail:
raise ex
return (errno.ENOENT, str(ex))
except PermissionError as ex:
if fail:
raise ex
return (errno.EPERM, str(ex))
yield from asyncio.wait([ yield from asyncio.wait([
_read_stream(process.stdout, logo.log_stdout), _read_stream(process.stdout, logo.log_stdout),