Adds error handling for the paths

This commit is contained in:
2025-12-30 17:56:03 +01:00
parent 4f6f48247d
commit 86a6c8acce

View File

@@ -9,6 +9,8 @@ import logging
from pathlib import Path from pathlib import Path
from errors import RemoteMountedError, InvalidMountError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class Synchroniser: class Synchroniser:
@@ -175,16 +177,24 @@ class Synchroniser:
Mount the remote directory to make the local links work. Mount the remote directory to make the local links work.
This is achieved using sshfs. This is achieved using sshfs.
Raise: Raise:
- RemoteMountedError: The .data directory is already a mount point
- InvalidMountError: .data is either not a directory or not empty
- subprocess.CalledProcessError: An error occured with sshfs - subprocess.CalledProcessError: An error occured with sshfs
""" """
# Get the absolute path to the correct .data directory resolving symlinks
path_to_mount:PosixPath = Path(f"{self.local}/../.data").resolve()
if path_to_mount.is_mount():
raise RemoteMountedError
# Check if it is an empty directory
if not path_to_mount.is_dir() or any(path_to_mount.iterdir()):
raise InvalidMountError
command = [ command = [
"/usr/bin/sshfs", "/usr/bin/sshfs",
"-o", "ControlPath={self.control_path}", "-o", "ControlPath={self.control_path}",
"-o", "ServerAliveInterval=15", "-o", "ServerAliveInterval=15",
"-p", str(self.remote_port), "-p", str(self.remote_port),
f"{self.remote_user}@{self.remote_ip}:{self.remote_dir}/.data", f"{self.remote_user}@{self.remote_ip}:{self.remote_dir}/.data",
# Get the absolute path to the correct .data directory resolving symlinks str(path_to_mount)
str(Path(f"{self.local}/../.data").resolve())
] ]
completed_process = subprocess.run(command) completed_process = subprocess.run(command)
completed_process.check_returncode() completed_process.check_returncode()