aiohelpers

This commit is contained in:
Maxim Devaev 2022-04-10 08:08:05 +03:00
parent c92d17dea2
commit 7c44d733a9
2 changed files with 19 additions and 22 deletions

View File

@ -20,35 +20,31 @@
# ========================================================================== # # ========================================================================== #
import subprocess
from typing import List from typing import List
from ....logging import get_logger from .logging import get_logger
from .... import aioproc from . import tools
from . import aioproc
from .. import MsdError
# ===== # =====
async def remount_storage(base_cmd: List[str], rw: bool) -> None: async def remount(name: str, base_cmd: List[str], rw: bool) -> bool:
logger = get_logger(0) logger = get_logger(1)
mode = ("rw" if rw else "ro") mode = ("rw" if rw else "ro")
cmd = [ cmd = [
part.format(mode=mode) part.format(mode=mode)
for part in base_cmd for part in base_cmd
] ]
logger.info("Remounting internal storage to %s ...", mode.upper()) logger.info("Remounting %s storage to %s: %s ...", name, mode.upper(), cmd)
try: try:
await _run_helper(cmd) proc = await aioproc.log_process(cmd, logger)
except Exception: if proc.returncode != 0:
logger.error("Can't remount internal storage") assert proc.returncode is not None
raise raise subprocess.CalledProcessError(proc.returncode, cmd)
except Exception as err:
logger.error("Can't remount %s storage: %s", name, tools.efmt(err))
# ===== return False
async def _run_helper(cmd: List[str]) -> None: return True
logger = get_logger(0)
logger.info("Executing helper %s ...", cmd)
proc = await aioproc.log_process(cmd, logger)
if proc.returncode != 0:
raise MsdError(f"Error while helper execution: pid={proc.pid}; retcode={proc.returncode}")

View File

@ -46,6 +46,7 @@ from ....validators.os import valid_printable_filename
from ....validators.os import valid_command from ....validators.os import valid_command
from .... import aiotools from .... import aiotools
from .... import aiohelpers
from .. import MsdError from .. import MsdError
from .. import MsdIsBusyError from .. import MsdIsBusyError
@ -59,7 +60,6 @@ from .. import BaseMsd
from .. import MsdImageWriter from .. import MsdImageWriter
from . import fs from . import fs
from . import helpers
from .drive import Drive from .drive import Drive
@ -538,4 +538,5 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
# ===== # =====
async def __remount_storage(self, rw: bool) -> None: async def __remount_storage(self, rw: bool) -> None:
await helpers.remount_storage(self.__remount_cmd, rw) if not (await aiohelpers.remount("MSD", self.__remount_cmd, rw)):
raise MsdError("Can't execute remount helper")