diff options
author | Maxim Devaev <[email protected]> | 2023-03-22 04:44:07 +0200 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2023-03-22 04:44:07 +0200 |
commit | 22db176ef05df0b49a9e40e3c31c886dfcb710b5 (patch) | |
tree | 49e3b409e66706dd327604a72dcaac0583628e38 /kvmd | |
parent | 6c0f5cccb94fc8092a49d90eca89fe89e9cf923a (diff) |
removed aiofs
Diffstat (limited to 'kvmd')
-rw-r--r-- | kvmd/aiofs.py | 39 | ||||
-rw-r--r-- | kvmd/aiotools.py | 9 | ||||
-rw-r--r-- | kvmd/apps/kvmd/info/hw.py | 6 | ||||
-rw-r--r-- | kvmd/apps/vnc/vncauth.py | 5 | ||||
-rw-r--r-- | kvmd/plugins/msd/__init__.py | 10 |
5 files changed, 20 insertions, 49 deletions
diff --git a/kvmd/aiofs.py b/kvmd/aiofs.py deleted file mode 100644 index cad72ecf..00000000 --- a/kvmd/aiofs.py +++ /dev/null @@ -1,39 +0,0 @@ -# ========================================================================== # -# # -# KVMD - The main PiKVM daemon. # -# # -# Copyright (C) 2018-2022 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 os - -import aiofiles -import aiofiles.base - -from . import aiotools - - -# ===== -async def read(path: str) -> str: - async with aiofiles.open(path) as afile: # type: ignore - return (await afile.read()) - - -async def afile_sync(afile: aiofiles.base.AiofilesContextManager) -> None: - await afile.flush() # type: ignore - await aiotools.run_async(os.fsync, afile.fileno()) # type: ignore diff --git a/kvmd/aiotools.py b/kvmd/aiotools.py index a5a28fde..c87e8268 100644 --- a/kvmd/aiotools.py +++ b/kvmd/aiotools.py @@ -26,7 +26,6 @@ import asyncio import ssl import functools import types - import typing from typing import Callable @@ -35,10 +34,18 @@ from typing import Coroutine from typing import TypeVar from typing import Any +import aiofiles + from .logging import get_logger # ===== +async def read_file(path: str) -> str: + async with aiofiles.open(path) as file: + return (await file.read()) + + +# ===== def run(coro: Coroutine, final: (Coroutine | None)=None) -> None: # https://github.com/aio-libs/aiohttp/blob/a1d4dac1d/aiohttp/web.py#L515 diff --git a/kvmd/apps/kvmd/info/hw.py b/kvmd/apps/kvmd/info/hw.py index 86efd761..881f54aa 100644 --- a/kvmd/apps/kvmd/info/hw.py +++ b/kvmd/apps/kvmd/info/hw.py @@ -31,7 +31,7 @@ from ....logging import get_logger from .... import env from .... import tools -from .... import aiofs +from .... import aiotools from .... import aioproc from .base import BaseInfoSubmanager @@ -90,7 +90,7 @@ class HwInfoSubmanager(BaseInfoSubmanager): if name not in self.__dt_cache: path = os.path.join(f"{env.PROCFS_PREFIX}/proc/device-tree", name) try: - self.__dt_cache[name] = (await aiofs.read(path)).strip(" \t\r\n\0") + self.__dt_cache[name] = (await aiotools.read_file(path)).strip(" \t\r\n\0") except Exception as err: get_logger(0).error("Can't read DT %s from %s: %s", name, path, err) return None @@ -99,7 +99,7 @@ class HwInfoSubmanager(BaseInfoSubmanager): async def __get_cpu_temp(self) -> (float | None): temp_path = f"{env.SYSFS_PREFIX}/sys/class/thermal/thermal_zone0/temp" try: - return int((await aiofs.read(temp_path)).strip()) / 1000 + return int((await aiotools.read_file(temp_path)).strip()) / 1000 except Exception as err: get_logger(0).error("Can't read CPU temp from %s: %s", temp_path, err) return None diff --git a/kvmd/apps/vnc/vncauth.py b/kvmd/apps/vnc/vncauth.py index c7c04864..ebda9ef4 100644 --- a/kvmd/apps/vnc/vncauth.py +++ b/kvmd/apps/vnc/vncauth.py @@ -24,7 +24,7 @@ import dataclasses from ...logging import get_logger -from ... import aiofs +from ... import aiotools # ===== @@ -61,8 +61,7 @@ class VncAuthManager: return ({}, (not self.__enabled)) async def __inner_read_credentials(self) -> dict[str, VncAuthKvmdCredentials]: - lines = (await aiofs.read(self.__path)).split("\n") - + lines = (await aiotools.read_file(self.__path)).split("\n") credentials: dict[str, VncAuthKvmdCredentials] = {} for (lineno, line) in enumerate(lines): if len(line.strip()) == 0 or line.lstrip().startswith("#"): diff --git a/kvmd/plugins/msd/__init__.py b/kvmd/plugins/msd/__init__.py index 65da192c..69be26bc 100644 --- a/kvmd/plugins/msd/__init__.py +++ b/kvmd/plugins/msd/__init__.py @@ -36,7 +36,6 @@ from ...errors import OperationError from ...errors import IsBusyError from ... import aiotools -from ... import aiofs from .. import BasePlugin from .. import get_plugin_class @@ -254,7 +253,7 @@ class MsdFileWriter(BaseMsdWriter): # pylint: disable=too-many-instance-attribu self.__unsynced += len(chunk) if self.__unsynced >= self.__sync_size: - await aiofs.afile_sync(self.__file) + await self.__sync() self.__unsynced = 0 now = time.monotonic() @@ -287,12 +286,17 @@ class MsdFileWriter(BaseMsdWriter): # pylint: disable=too-many-instance-attribu (log, result) = (logger.warning, "OVERFLOW") log("Written %d of %d bytes to MSD image %r: %s", self.__written, self.__file_size, self.__name, result) try: - await aiofs.afile_sync(self.__file) + await self.__sync() finally: await self.__file.close() # type: ignore except Exception: logger.exception("Can't close image writer") + async def __sync(self) -> None: + assert self.__file is not None + await self.__file.flush() # type: ignore + await aiotools.run_async(os.fsync, self.__file.fileno()) # type: ignore + # ===== def get_msd_class(name: str) -> type[BaseMsd]: |