summaryrefslogtreecommitdiff
path: root/kvmd
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2020-05-29 04:16:20 +0300
committerDevaev Maxim <[email protected]>2020-05-29 04:30:37 +0300
commiteb13da03be9f52954755930def2bfdf57fb6347c (patch)
tree5d1776e93e036610652fc9c42c1aabb2d84c443b /kvmd
parent1c93f6a562d1c2106d148cebb128397989fb84fd (diff)
refactoring
Diffstat (limited to 'kvmd')
-rw-r--r--kvmd/aioproc.py48
-rw-r--r--kvmd/apps/kvmd/streamer.py18
-rw-r--r--kvmd/plugins/msd/otg/helpers.py14
3 files changed, 55 insertions, 25 deletions
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 <[email protected]> #
+# #
+# 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)
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"):