From eb13da03be9f52954755930def2bfdf57fb6347c Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Fri, 29 May 2020 04:16:20 +0300 Subject: refactoring --- kvmd/aioproc.py | 48 +++++++++++++++++++++++++++++++++++++++++ kvmd/apps/kvmd/streamer.py | 18 ++++------------ kvmd/plugins/msd/otg/helpers.py | 14 +++--------- 3 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 kvmd/aioproc.py (limited to 'kvmd') diff --git a/kvmd/aioproc.py b/kvmd/aioproc.py new file mode 100644 index 00000000..a778141d --- /dev/null +++ b/kvmd/aioproc.py @@ -0,0 +1,48 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +# ========================================================================== # + + +import asyncio +import asyncio.subprocess +import signal + +from typing import Tuple +from typing import List + + +# ===== +async def run_process(cmd: List[str], err_to_null: bool=False) -> asyncio.subprocess.Process: # pylint: disable=no-member + return (await asyncio.create_subprocess_exec( + *cmd, + stdout=asyncio.subprocess.PIPE, + stderr=(asyncio.subprocess.DEVNULL if err_to_null else asyncio.subprocess.STDOUT), + preexec_fn=preexec_ignore_sigint, + )) + + +async def read_process(cmd: List[str], err_to_null: bool=False) -> Tuple[asyncio.subprocess.Process, str]: # pylint: disable=no-member + proc = await run_process(cmd, err_to_null) + (stdout, _) = await proc.communicate() + return (proc, stdout.decode(errors="ignore").strip()) + + +def preexec_ignore_sigint() -> None: + signal.signal(signal.SIGINT, signal.SIG_IGN) diff --git a/kvmd/apps/kvmd/streamer.py b/kvmd/apps/kvmd/streamer.py index 357a46bb..7f527f69 100644 --- a/kvmd/apps/kvmd/streamer.py +++ b/kvmd/apps/kvmd/streamer.py @@ -36,6 +36,7 @@ import aiohttp from ...logging import get_logger from ... import aiotools +from ... import aioproc from ... import htclient from ... import gpio @@ -217,16 +218,10 @@ class Streamer: # pylint: disable=too-many-instance-attributes waiter_task = None async def get_info(self) -> Dict: - proc = await asyncio.create_subprocess_exec( - *[self.__cmd[0], "--version"], - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.DEVNULL, - preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)), - ) - (stdout, _) = await proc.communicate() + version = (await aioproc.read_process([self.__cmd[0], "--version"], err_to_null=True))[1] return { "app": os.path.basename(self.__cmd[0]), - "version": stdout.decode(errors="ignore").strip(), + "version": version, } @aiotools.atomic @@ -329,12 +324,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes ) for part in self.__cmd ] - self.__streamer_proc = await asyncio.create_subprocess_exec( - *cmd, - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.STDOUT, - preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)), - ) + self.__streamer_proc = await aioproc.run_process(cmd) get_logger(0).info("Started streamer pid=%d: %s", self.__streamer_proc.pid, cmd) async def __kill_streamer_proc(self) -> None: diff --git a/kvmd/plugins/msd/otg/helpers.py b/kvmd/plugins/msd/otg/helpers.py index ee759ef0..df004c6d 100644 --- a/kvmd/plugins/msd/otg/helpers.py +++ b/kvmd/plugins/msd/otg/helpers.py @@ -20,14 +20,12 @@ # ========================================================================== # -import signal -import asyncio -import asyncio.subprocess - from typing import List from ....logging import get_logger +from .... import aioproc + from .. import MsdError @@ -62,14 +60,8 @@ async def _run_helper(cmd: List[str]) -> None: logger = get_logger(0) logger.info("Executing helper %s ...", cmd) - proc = await asyncio.create_subprocess_exec( - *cmd, - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.STDOUT, - preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)), - ) + (proc, stdout) = await aioproc.read_process(cmd) - stdout = (await proc.communicate())[0].decode(errors="ignore").strip() if stdout: log = (logger.info if proc.returncode == 0 else logger.error) for line in stdout.split("\n"): -- cgit v1.2.3