From 9aa9205a9d320b3627dc0392c6773357c3881b65 Mon Sep 17 00:00:00 2001 From: timeshifter Date: Thu, 2 Dec 2021 15:12:49 +0100 Subject: [PATCH] refactor: class Command --- zfs-backup.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/zfs-backup.py b/zfs-backup.py index f6b7b97..a03b8eb 100755 --- a/zfs-backup.py +++ b/zfs-backup.py @@ -192,7 +192,7 @@ class ZFSPath: class ZFS: @staticmethod def get_datasets(name: str) -> list[Dataset]: - datasets_str = get_output_of_command( + datasets_str = Command.get_output( [ config.ZFS, "list", @@ -214,17 +214,17 @@ class ZFS: def import_pool_from_directory(name: str, directory: Path) -> None: # -N: no mount # -d: directory to search the pool in - run_command([config.ZPOOL, "import", "-N", "-d", directory, name]) + Command.run([config.ZPOOL, "import", "-N", "-d", directory, name]) @staticmethod def export_pool(name: str) -> None: - run_command([config.ZPOOL, "export", name]) + Command.run([config.ZPOOL, "export", name]) @staticmethod def get_snapshots(name: str) -> list[Snapshot]: return [ Snapshot(ZFSPath.from_string(name)) - for name in get_output_of_command( + for name in Command.get_output( [ config.ZFS, "list", @@ -251,7 +251,7 @@ class ZFS: if recursive: cmdline.append("-r") cmdline.append(str(snapshot)) - run_command(cmdline) + Command.run(cmdline) @staticmethod def send_incremental(old_snap: Snapshot, new_snap: Snapshot) -> Popen[str]: @@ -283,9 +283,9 @@ class ZFS: if not isinstance(pool, Pool): raise TypeError("Can only scrub pools.") if not cls._scrub_in_progress(pool): - run_command([config.ZPOOL, "scrub", pool.name]) + Command.run([config.ZPOOL, "scrub", pool.name]) if wait_for_finish: - run_command([config.ZPOOL, "wait", "-t", "scrub", pool.name]) + Command.run([config.ZPOOL, "wait", "-t", "scrub", pool.name]) if not cls._healthy(pool): raise IOError(f"Pool {pool.name} is not healthy.") @@ -299,7 +299,7 @@ class ZFS: @staticmethod def _pool_status_output(pool): - return get_output_of_command([config.ZFS, "status", pool.name]) + return Command.get_output([config.ZFS, "status", pool.name]) class Disk: @@ -316,7 +316,7 @@ class Disk: self._decrypted = True def _decrypt_with_cryptsetup(self): - run_command([config.CRYPTSETUP, "open", self._path, self._mapper_entry]) + Command.run([config.CRYPTSETUP, "open", self._path, self._mapper_entry]) def _verify_mapper_entry_not_in_use(self): for item in MAPPER_PATH.iterdir(): @@ -336,8 +336,7 @@ class Disk: def encrypt(self): if not self._decrypted: raise ValueError(NOT_DECRYPTED, self._name) - # TODO use run instead? - get_output_of_command([config.CRYPTSETUP, "close", self._mapper_entry]) + Command.run([config.CRYPTSETUP, "close", self._mapper_entry]) self._decrypted = False @@ -499,15 +498,16 @@ class Snapshot: return Dataset(self._path.dataset) -# TODO class CommandRunner -def run_command(cmdline: list[str]): - call(cmdline) +class Command: + @staticmethod + def run(cmdline: list[str]): + call(cmdline) - -def get_output_of_command(cmdline: str | list[str]) -> str: - if isinstance(cmdline, str): - cmdline = [cmdline] - return check_output(cmdline).strip().decode() + @staticmethod + def get_output(cmdline: str | list[str]) -> str: + if isinstance(cmdline, str): + cmdline = [cmdline] + return check_output(cmdline).strip().decode() def verify_running_as_root():