synchroniser : raise error instead of returning a value
Raise a FatalSyncError when the synchronisation fails instead of returning the unison return code.
This commit is contained in:
@@ -16,7 +16,7 @@ import logging
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
from unisync.errors import RemoteMountedError, InvalidMountError, UnknownSSHError
|
from unisync.errors import RemoteMountedError, InvalidMountError, UnknownSSHError, FatalSyncError
|
||||||
from unisync.config import BackupConfig
|
from unisync.config import BackupConfig
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -105,9 +105,6 @@ class Synchroniser:
|
|||||||
connection_timeout:
|
connection_timeout:
|
||||||
Time given to the user to authenticate to the remote server.
|
Time given to the user to authenticate to the remote server.
|
||||||
On slow connections one might want to increase this.
|
On slow connections one might want to increase this.
|
||||||
Returns:
|
|
||||||
An error code (0 success, 1 TimeoutExpired, 2 KeyboardInterrupt).
|
|
||||||
TODO change that to raising the exception.
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
subprocess.TimeoutExpired:
|
subprocess.TimeoutExpired:
|
||||||
@@ -156,18 +153,18 @@ class Synchroniser:
|
|||||||
close = subprocess.Popen(command)
|
close = subprocess.Popen(command)
|
||||||
return close.wait()
|
return close.wait()
|
||||||
|
|
||||||
def sync_files(self, paths:list, force:bool=False) -> int:
|
def sync_files(self, paths:list, force:bool=False) -> None:
|
||||||
"""Synchronises the files.
|
"""Synchronises the files.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
paths: List of paths to synchronise.
|
paths: List of paths to synchronise.
|
||||||
force: Force the changes from remote to local.
|
force: Force the changes from remote to local.
|
||||||
|
|
||||||
Returns:
|
Raises:
|
||||||
The return code of sync.
|
FatalSyncError: A fatal error occured during the synchronisation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self.sync(
|
self.sync(
|
||||||
f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/.data",
|
f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/.data",
|
||||||
self.local,
|
self.local,
|
||||||
paths=paths,
|
paths=paths,
|
||||||
@@ -175,16 +172,16 @@ class Synchroniser:
|
|||||||
other=self.files_extra
|
other=self.files_extra
|
||||||
)
|
)
|
||||||
|
|
||||||
def sync_links(self, ignore:list) -> int:
|
def sync_links(self, ignore:list) -> None:
|
||||||
"""Synchronises the links, they must exist already.
|
"""Synchronises the links, they must exist already.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
ignore: List of paths to ignore.
|
ignore: List of paths to ignore.
|
||||||
|
|
||||||
Returns:
|
Raises:
|
||||||
The return code of sync.
|
FatalSyncError: A fatal error occured during the synchronisation.
|
||||||
"""
|
"""
|
||||||
return self.sync(
|
self.sync(
|
||||||
f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/links",
|
f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/links",
|
||||||
self.local,
|
self.local,
|
||||||
ignore=ignore,
|
ignore=ignore,
|
||||||
@@ -194,7 +191,7 @@ class Synchroniser:
|
|||||||
def sync(self, remote_root:str, local_root:str,
|
def sync(self, remote_root:str, local_root:str,
|
||||||
paths:list=[], ignore:list=[], force:bool=False,
|
paths:list=[], ignore:list=[], force:bool=False,
|
||||||
other:list=[]
|
other:list=[]
|
||||||
) -> int:
|
) -> None:
|
||||||
"""Performs the synchronisation by calling unison.
|
"""Performs the synchronisation by calling unison.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -213,8 +210,11 @@ class Synchroniser:
|
|||||||
They will be added to the command as is no - in front.
|
They will be added to the command as is no - in front.
|
||||||
For exemple backups are implemented using this argument.
|
For exemple backups are implemented using this argument.
|
||||||
|
|
||||||
Returns:
|
Raises:
|
||||||
the unison return code see section 6.11 of the documentation
|
FatalSyncError:
|
||||||
|
If unison returns 3 it means either a fatal error occured or the synchronisation
|
||||||
|
was interrupted.
|
||||||
|
If this happens propagate the error to unisync.
|
||||||
"""
|
"""
|
||||||
command = [ "/usr/bin/unison", "-root", remote_root, "-root", local_root ]
|
command = [ "/usr/bin/unison", "-root", remote_root, "-root", local_root ]
|
||||||
for arg in self.args_bool:
|
for arg in self.args_bool:
|
||||||
@@ -247,7 +247,8 @@ class Synchroniser:
|
|||||||
|
|
||||||
proc = subprocess.Popen(command)
|
proc = subprocess.Popen(command)
|
||||||
ret_code = proc.wait()
|
ret_code = proc.wait()
|
||||||
return ret_code
|
if ret_code == 3:
|
||||||
|
raise FatalSyncError("Synchronisation could not be completed")
|
||||||
|
|
||||||
def update_links(self, background:bool=True):
|
def update_links(self, background:bool=True):
|
||||||
"""Updates the links on the remote.
|
"""Updates the links on the remote.
|
||||||
|
|||||||
Reference in New Issue
Block a user