inline all messages

This commit is contained in:
timeshifter
2022-12-06 15:15:43 +01:00
parent 28963c3e64
commit 530a1e090a
6 changed files with 13 additions and 30 deletions
+4 -5
View File
@@ -3,7 +3,6 @@ from __future__ import annotations
from pathlib import Path from pathlib import Path
from ._core import CommandInterface from ._core import CommandInterface
from ..messages import MAPPER_ENTRY_ALREADY_EXISTS, CANNOT_FIND_DEVICE_PATH
from ..misc import MAPPER_PATH, DISK_BY_UUID from ..misc import MAPPER_PATH, DISK_BY_UUID
@@ -31,8 +30,7 @@ class Cryptsetup(CommandInterface):
if cls._mapper_entry_in_use(mapper_entry): if cls._mapper_entry_in_use(mapper_entry):
if cls._already_decrypted(path, mapper_entry): if cls._already_decrypted(path, mapper_entry):
return True return True
print(MAPPER_ENTRY_ALREADY_EXISTS) raise FileExistsError("mapper entry already exists")
raise FileExistsError()
cls._run_command(cls._Subcommands.open, str(path), mapper_entry) cls._run_command(cls._Subcommands.open, str(path), mapper_entry)
return False return False
@@ -71,8 +69,9 @@ class Cryptsetup(CommandInterface):
device = Path(line.split(":")[-1].strip()) device = Path(line.split(":")[-1].strip())
break break
else: else:
raise FileNotFoundError(CANNOT_FIND_DEVICE_PATH) raise FileNotFoundError(
# noinspection PyUnboundLocalVariable "Cannot find device path from output of `cryptsetup status`"
)
return device return device
@classmethod @classmethod
+3 -2
View File
@@ -6,7 +6,6 @@ from subprocess import Popen
from .. import Pool from .. import Pool
from ._core import CommandInterface from ._core import CommandInterface
from ..dataset import Dataset from ..dataset import Dataset
from ..messages import SAME_DATASET
from ..misc import assert_type from ..misc import assert_type
from ..snapshot import Snapshot from ..snapshot import Snapshot
from ..zfs_path import ZFSPath from ..zfs_path import ZFSPath
@@ -90,7 +89,9 @@ class ZFS(CommandInterface):
f"Cannot send stream, object is not a Snapshot: {snapshot}", f"Cannot send stream, object is not a Snapshot: {snapshot}",
) )
if old_snapshot.dataset != new_snapshot.dataset: if old_snapshot.dataset != new_snapshot.dataset:
raise ValueError(SAME_DATASET) raise ValueError(
"Cannot send incremental snapshots if start and end snapshot are not based on the same dataset"
)
if old_snapshot.newer_than(new_snapshot): if old_snapshot.newer_than(new_snapshot):
raise ValueError("Old snapshot is newer than new snapshot.") raise ValueError("Old snapshot is newer than new snapshot.")
if old_snapshot == new_snapshot: if old_snapshot == new_snapshot:
+2 -3
View File
@@ -3,7 +3,6 @@ from __future__ import annotations
from pathlib import Path from pathlib import Path
from uuid import UUID from uuid import UUID
from .messages import ALREADY_DECRYPTED, NOT_DECRYPTED
from .commands.cryptsetup import Cryptsetup from .commands.cryptsetup import Cryptsetup
from .misc import DISK_BY_UUID from .misc import DISK_BY_UUID
@@ -18,7 +17,7 @@ class Disk:
def decrypt(self) -> None: def decrypt(self) -> None:
"""Open a LUKS-encrypted disk with `cryptsetup`. Silently pass if it was already decrypted.""" """Open a LUKS-encrypted disk with `cryptsetup`. Silently pass if it was already decrypted."""
if self._decrypted: if self._decrypted:
raise ValueError(ALREADY_DECRYPTED, self._name) raise ValueError("Cannot decrypt, already decrypted", self._name)
self._was_already_decrypted = Cryptsetup.decrypt(self._path, self._mapper_entry) self._was_already_decrypted = Cryptsetup.decrypt(self._path, self._mapper_entry)
self._decrypted = True self._decrypted = True
@@ -35,7 +34,7 @@ class Disk:
def encrypt(self) -> None: def encrypt(self) -> None:
"""Close a LUKS-encrypted container with `cryptsetup`.""" """Close a LUKS-encrypted container with `cryptsetup`."""
if not self._decrypted: if not self._decrypted:
raise ValueError(NOT_DECRYPTED, self._name) raise ValueError("Cannot encrypt, not decrypted", self._name)
if self._was_already_decrypted: if self._was_already_decrypted:
return return
Cryptsetup.encrypt(self._mapper_entry) Cryptsetup.encrypt(self._mapper_entry)
-13
View File
@@ -1,13 +0,0 @@
from __future__ import annotations
ALREADY_DECRYPTED = "Cannot decrypt, already decrypted"
ALREADY_IMPORTED = "Cannot import, already imported"
CANNOT_FIND_DEVICE_PATH = "Cannot find device path from output of `cryptsetup status`"
CANNOT_FIND_BACKUP_DRIVE = "Could not find a backup drive"
DELETE_IN_LOCAL_POOL = "ALERT! Tried to delete in local pool!"
MAPPER_ENTRY_ALREADY_EXISTS = "mapper entry already exists"
NOT_DECRYPTED = "Cannot encrypt, not decrypted"
NOT_IMPORTED = "Cannot export, not imported"
NOT_VALID_ZFS_PATH = "Not a valid ZFSPath"
SAME_DATASET = "Cannot send incremental snapshots if start and end snapshot are not based on the same dataset"
+3 -4
View File
@@ -4,7 +4,6 @@ from datetime import timedelta
from uuid import UUID from uuid import UUID
from .disk import Disk from .disk import Disk
from .messages import CANNOT_FIND_BACKUP_DRIVE, ALREADY_IMPORTED, NOT_IMPORTED
from .misc import MAPPER_PATH, DISK_BY_UUID from .misc import MAPPER_PATH, DISK_BY_UUID
from .commands.zfs import ZFS from .commands.zfs import ZFS
from .dataset import Dataset from .dataset import Dataset
@@ -39,7 +38,7 @@ class ExternalPool(Pool):
def import_(self) -> None: def import_(self) -> None:
"""Import a pool. Raise an error if the pool was previously imported.""" """Import a pool. Raise an error if the pool was previously imported."""
if self._imported: if self._imported:
raise ValueError(ALREADY_IMPORTED, self.name) raise ValueError("Cannot import, already imported", self.name)
self._disk.decrypt() self._disk.decrypt()
ZPool.import_from_directory(self, MAPPER_PATH) ZPool.import_from_directory(self, MAPPER_PATH)
self._imported = True self._imported = True
@@ -47,7 +46,7 @@ class ExternalPool(Pool):
def export(self) -> None: def export(self) -> None:
"""Export a previously imported pool. Raise an error if the pool was not previously imported.""" """Export a previously imported pool. Raise an error if the pool was not previously imported."""
if not self._imported: if not self._imported:
raise ValueError(NOT_IMPORTED, self.name) raise ValueError("Cannot export, not imported", self.name)
ZPool.export(self) ZPool.export(self)
self._disk.encrypt() self._disk.encrypt()
self._imported = False self._imported = False
@@ -73,7 +72,7 @@ class ExternalPool(Pool):
print(f"Found disk {pool_name}, {disk_uuid}") print(f"Found disk {pool_name}, {disk_uuid}")
return cls(pool_name, disk) return cls(pool_name, disk)
else: else:
raise FileNotFoundError(CANNOT_FIND_BACKUP_DRIVE) raise FileNotFoundError("Could not find a backup drive")
def clean_old_snapshots(self) -> None: def clean_old_snapshots(self) -> None:
print("Cleaning old snapshots") print("Cleaning old snapshots")
+1 -3
View File
@@ -1,7 +1,5 @@
from __future__ import annotations from __future__ import annotations
from zfs_backup.messages import NOT_VALID_ZFS_PATH
class ZFSPath: class ZFSPath:
"""Represents a path in ZFS. """Represents a path in ZFS.
@@ -79,4 +77,4 @@ class ZFSPath:
def _sanity_check(elements: list[str]) -> None: def _sanity_check(elements: list[str]) -> None:
for item in elements: for item in elements:
if len(item) == 0: if len(item) == 0:
raise ValueError(NOT_VALID_ZFS_PATH) raise ValueError("Not a valid ZFSPath")