mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 01:00:29 +08:00
refactoring
This commit is contained in:
parent
1c93f6a562
commit
eb13da03be
48
kvmd/aioproc.py
Normal file
48
kvmd/aioproc.py
Normal 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)
|
||||||
@ -36,6 +36,7 @@ import aiohttp
|
|||||||
from ...logging import get_logger
|
from ...logging import get_logger
|
||||||
|
|
||||||
from ... import aiotools
|
from ... import aiotools
|
||||||
|
from ... import aioproc
|
||||||
from ... import htclient
|
from ... import htclient
|
||||||
from ... import gpio
|
from ... import gpio
|
||||||
|
|
||||||
@ -217,16 +218,10 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
waiter_task = None
|
waiter_task = None
|
||||||
|
|
||||||
async def get_info(self) -> Dict:
|
async def get_info(self) -> Dict:
|
||||||
proc = await asyncio.create_subprocess_exec(
|
version = (await aioproc.read_process([self.__cmd[0], "--version"], err_to_null=True))[1]
|
||||||
*[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()
|
|
||||||
return {
|
return {
|
||||||
"app": os.path.basename(self.__cmd[0]),
|
"app": os.path.basename(self.__cmd[0]),
|
||||||
"version": stdout.decode(errors="ignore").strip(),
|
"version": version,
|
||||||
}
|
}
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic
|
||||||
@ -329,12 +324,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
)
|
)
|
||||||
for part in self.__cmd
|
for part in self.__cmd
|
||||||
]
|
]
|
||||||
self.__streamer_proc = await asyncio.create_subprocess_exec(
|
self.__streamer_proc = await aioproc.run_process(cmd)
|
||||||
*cmd,
|
|
||||||
stdout=asyncio.subprocess.PIPE,
|
|
||||||
stderr=asyncio.subprocess.STDOUT,
|
|
||||||
preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)),
|
|
||||||
)
|
|
||||||
get_logger(0).info("Started streamer pid=%d: %s", self.__streamer_proc.pid, cmd)
|
get_logger(0).info("Started streamer pid=%d: %s", self.__streamer_proc.pid, cmd)
|
||||||
|
|
||||||
async def __kill_streamer_proc(self) -> None:
|
async def __kill_streamer_proc(self) -> None:
|
||||||
|
|||||||
@ -20,14 +20,12 @@
|
|||||||
# ========================================================================== #
|
# ========================================================================== #
|
||||||
|
|
||||||
|
|
||||||
import signal
|
|
||||||
import asyncio
|
|
||||||
import asyncio.subprocess
|
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from ....logging import get_logger
|
from ....logging import get_logger
|
||||||
|
|
||||||
|
from .... import aioproc
|
||||||
|
|
||||||
from .. import MsdError
|
from .. import MsdError
|
||||||
|
|
||||||
|
|
||||||
@ -62,14 +60,8 @@ async def _run_helper(cmd: List[str]) -> None:
|
|||||||
logger = get_logger(0)
|
logger = get_logger(0)
|
||||||
logger.info("Executing helper %s ...", cmd)
|
logger.info("Executing helper %s ...", cmd)
|
||||||
|
|
||||||
proc = await asyncio.create_subprocess_exec(
|
(proc, stdout) = await aioproc.read_process(cmd)
|
||||||
*cmd,
|
|
||||||
stdout=asyncio.subprocess.PIPE,
|
|
||||||
stderr=asyncio.subprocess.STDOUT,
|
|
||||||
preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)),
|
|
||||||
)
|
|
||||||
|
|
||||||
stdout = (await proc.communicate())[0].decode(errors="ignore").strip()
|
|
||||||
if stdout:
|
if stdout:
|
||||||
log = (logger.info if proc.returncode == 0 else logger.error)
|
log = (logger.info if proc.returncode == 0 else logger.error)
|
||||||
for line in stdout.split("\n"):
|
for line in stdout.split("\n"):
|
||||||
|
|||||||
@ -21,7 +21,6 @@
|
|||||||
|
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import signal
|
|
||||||
import pwd
|
import pwd
|
||||||
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
@ -30,6 +29,8 @@ from typing import Optional
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from kvmd import aioproc
|
||||||
|
|
||||||
from . import get_configured_auth_service
|
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(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
*cmd.split(" "),
|
*cmd.split(" "),
|
||||||
stdin=(asyncio.subprocess.PIPE if input is not None else None),
|
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)
|
await proc.communicate(input.encode() if input is not None else None)
|
||||||
assert proc.returncode == 0
|
assert proc.returncode == 0
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user