removed aiofs

This commit is contained in:
Maxim Devaev 2023-03-22 04:44:07 +02:00
parent 6c0f5cccb9
commit 22db176ef0
5 changed files with 20 additions and 49 deletions

View File

@ -1,39 +0,0 @@
# ========================================================================== #
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2022 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 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

View File

@ -26,7 +26,6 @@ import asyncio
import ssl import ssl
import functools import functools
import types import types
import typing import typing
from typing import Callable from typing import Callable
@ -35,9 +34,17 @@ from typing import Coroutine
from typing import TypeVar from typing import TypeVar
from typing import Any from typing import Any
import aiofiles
from .logging import get_logger 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: def run(coro: Coroutine, final: (Coroutine | None)=None) -> None:
# https://github.com/aio-libs/aiohttp/blob/a1d4dac1d/aiohttp/web.py#L515 # https://github.com/aio-libs/aiohttp/blob/a1d4dac1d/aiohttp/web.py#L515

View File

@ -31,7 +31,7 @@ from ....logging import get_logger
from .... import env from .... import env
from .... import tools from .... import tools
from .... import aiofs from .... import aiotools
from .... import aioproc from .... import aioproc
from .base import BaseInfoSubmanager from .base import BaseInfoSubmanager
@ -90,7 +90,7 @@ class HwInfoSubmanager(BaseInfoSubmanager):
if name not in self.__dt_cache: if name not in self.__dt_cache:
path = os.path.join(f"{env.PROCFS_PREFIX}/proc/device-tree", name) path = os.path.join(f"{env.PROCFS_PREFIX}/proc/device-tree", name)
try: 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: except Exception as err:
get_logger(0).error("Can't read DT %s from %s: %s", name, path, err) get_logger(0).error("Can't read DT %s from %s: %s", name, path, err)
return None return None
@ -99,7 +99,7 @@ class HwInfoSubmanager(BaseInfoSubmanager):
async def __get_cpu_temp(self) -> (float | None): async def __get_cpu_temp(self) -> (float | None):
temp_path = f"{env.SYSFS_PREFIX}/sys/class/thermal/thermal_zone0/temp" temp_path = f"{env.SYSFS_PREFIX}/sys/class/thermal/thermal_zone0/temp"
try: try:
return int((await aiofs.read(temp_path)).strip()) / 1000 return int((await aiotools.read_file(temp_path)).strip()) / 1000
except Exception as err: except Exception as err:
get_logger(0).error("Can't read CPU temp from %s: %s", temp_path, err) get_logger(0).error("Can't read CPU temp from %s: %s", temp_path, err)
return None return None

View File

@ -24,7 +24,7 @@ import dataclasses
from ...logging import get_logger from ...logging import get_logger
from ... import aiofs from ... import aiotools
# ===== # =====
@ -61,8 +61,7 @@ class VncAuthManager:
return ({}, (not self.__enabled)) return ({}, (not self.__enabled))
async def __inner_read_credentials(self) -> dict[str, VncAuthKvmdCredentials]: 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] = {} credentials: dict[str, VncAuthKvmdCredentials] = {}
for (lineno, line) in enumerate(lines): for (lineno, line) in enumerate(lines):
if len(line.strip()) == 0 or line.lstrip().startswith("#"): if len(line.strip()) == 0 or line.lstrip().startswith("#"):

View File

@ -36,7 +36,6 @@ from ...errors import OperationError
from ...errors import IsBusyError from ...errors import IsBusyError
from ... import aiotools from ... import aiotools
from ... import aiofs
from .. import BasePlugin from .. import BasePlugin
from .. import get_plugin_class from .. import get_plugin_class
@ -254,7 +253,7 @@ class MsdFileWriter(BaseMsdWriter): # pylint: disable=too-many-instance-attribu
self.__unsynced += len(chunk) self.__unsynced += len(chunk)
if self.__unsynced >= self.__sync_size: if self.__unsynced >= self.__sync_size:
await aiofs.afile_sync(self.__file) await self.__sync()
self.__unsynced = 0 self.__unsynced = 0
now = time.monotonic() now = time.monotonic()
@ -287,12 +286,17 @@ class MsdFileWriter(BaseMsdWriter): # pylint: disable=too-many-instance-attribu
(log, result) = (logger.warning, "OVERFLOW") (log, result) = (logger.warning, "OVERFLOW")
log("Written %d of %d bytes to MSD image %r: %s", self.__written, self.__file_size, self.__name, result) log("Written %d of %d bytes to MSD image %r: %s", self.__written, self.__file_size, self.__name, result)
try: try:
await aiofs.afile_sync(self.__file) await self.__sync()
finally: finally:
await self.__file.close() # type: ignore await self.__file.close() # type: ignore
except Exception: except Exception:
logger.exception("Can't close image writer") 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]: def get_msd_class(name: str) -> type[BaseMsd]: