diff options
author | Maxim Devaev <[email protected]> | 2023-03-06 18:31:49 +0200 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2023-03-06 18:31:49 +0200 |
commit | cd3c28e97f9a5159148efa90d8e07c55edc86aae (patch) | |
tree | 0fb902431bf048e8b5c8cbfefcde5aed7a04c4d6 /kvmd/plugins/msd/otg | |
parent | 7667834b6de23ce71ea2e805f6a50eb679444b9e (diff) |
refactoring
Diffstat (limited to 'kvmd/plugins/msd/otg')
-rw-r--r-- | kvmd/plugins/msd/otg/__init__.py | 2 | ||||
-rw-r--r-- | kvmd/plugins/msd/otg/storage.py | 47 |
2 files changed, 25 insertions, 24 deletions
diff --git a/kvmd/plugins/msd/otg/__init__.py b/kvmd/plugins/msd/otg/__init__.py index ce43d3d8..4155a89e 100644 --- a/kvmd/plugins/msd/otg/__init__.py +++ b/kvmd/plugins/msd/otg/__init__.py @@ -179,7 +179,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes for name in list(storage["images"]): del storage["images"][name]["path"] del storage["images"][name]["in_storage"] - del storage["images"][name]["storage"] storage["downloading"] = (self.__reader.get_state() if self.__reader else None) @@ -197,7 +196,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes vd = dataclasses.asdict(self.__state.vd) if vd["image"]: del vd["image"]["path"] - del vd["image"]["storage"] return { "enabled": True, diff --git a/kvmd/plugins/msd/otg/storage.py b/kvmd/plugins/msd/otg/storage.py index 80e6fa87..051ef007 100644 --- a/kvmd/plugins/msd/otg/storage.py +++ b/kvmd/plugins/msd/otg/storage.py @@ -38,24 +38,30 @@ from .. import MsdError class _Image: name: str path: str - storage: Optional["Storage"] = dataclasses.field(compare=False) - in_storage: bool = dataclasses.field(init=False) - complete: bool = dataclasses.field(init=False, compare=False) size: int = dataclasses.field(init=False, compare=False) mod_ts: float = dataclasses.field(init=False, compare=False) class Image(_Image): + def __init__(self, name: str, path: str, storage: Optional["Storage"]) -> None: + super().__init__(name, path) + self.__storage = storage + self.__complete_path = os.path.join( + os.path.dirname(path), + ".__" + os.path.basename(path) + ".complete", + ) + self.__adopted = (storage._is_adopted(self) if storage else True) + @property def in_storage(self) -> bool: - return (self.storage is not None) + return bool(self.__storage) @property def complete(self) -> bool: - if self.storage is not None: - return os.path.exists(self.__get_complete_path()) + if self.__storage: + return os.path.exists(self.__complete_path) return True @property @@ -76,12 +82,12 @@ class Image(_Image): return os.path.exists(self.path) async def remount_rw(self, rw: bool, fatal: bool=True) -> None: - assert self.storage - if self.storage._is_mounted(self): # pylint: disable=protected-access - await self.storage.remount_rw(rw, fatal) + assert self.__storage + if not self.__adopted: + await self.__storage.remount_rw(rw, fatal) def remove(self, fatal: bool) -> None: - assert self.storage is not None + assert self.__storage try: os.remove(self.path) except FileNotFoundError: @@ -92,22 +98,15 @@ class Image(_Image): self.set_complete(False) def set_complete(self, flag: bool) -> None: - assert self.storage is not None - path = self.__get_complete_path() + assert self.__storage if flag: - open(path, "w").close() # pylint: disable=consider-using-with + open(self.__complete_path, "w").close() # pylint: disable=consider-using-with else: try: - os.remove(path) + os.remove(self.__complete_path) except FileNotFoundError: pass - def __get_complete_path(self) -> str: - return os.path.join( - os.path.dirname(self.path), - ".__" + os.path.basename(self.path) + ".complete", - ) - @dataclasses.dataclass(frozen=True) class StorageSpace: @@ -173,11 +172,15 @@ class Storage: free=(st.f_bavail * st.f_frsize), ) - def _is_mounted(self, image: Image) -> bool: + def _is_adopted(self, image: Image) -> bool: + # True, если образ находится вне хранилища + # или в другой точке монтирования под ним + if not image.in_storage: + return True path = image.path while not os.path.ismount(path): path = os.path.dirname(path) - return (path == self.__path) + return (self.__path != path) async def remount_rw(self, rw: bool, fatal: bool=True) -> None: if not (await aiohelpers.remount("MSD", self.__remount_cmd, rw)): |