refactoring

This commit is contained in:
Devaev Maxim 2020-05-29 04:16:20 +03:00
parent 1c93f6a562
commit eb13da03be
4 changed files with 58 additions and 27 deletions

48
kvmd/aioproc.py Normal file
View File

@ -0,0 +1,48 @@
# ========================================================================== #
# #
# KVMD - The main Pi-KVM daemon. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# 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 <https://www.gnu.org/licenses/>. #
# #
# ========================================================================== #
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)

View File

@ -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:

View File

@ -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"):

View File

@ -21,7 +21,6 @@
import asyncio
import signal
import pwd
from typing import Dict
@ -30,6 +29,8 @@ from typing import Optional
import pytest
from kvmd import aioproc
from . import get_configured_auth_service
@ -44,7 +45,7 @@ async def _run_process(cmd: str, input: Optional[str]=None) -> None: # pylint:
proc = await asyncio.create_subprocess_exec(
*cmd.split(" "),
stdin=(asyncio.subprocess.PIPE if input is not None else None),
preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)),
preexec_fn=aioproc.preexec_ignore_sigint,
)
await proc.communicate(input.encode() if input is not None else None)
assert proc.returncode == 0