From 5ec43f916633e4984f5c837dcb31e4f67fa27c7b Mon Sep 17 00:00:00 2001 From: furtest Date: Tue, 20 Jan 2026 10:33:13 +0100 Subject: [PATCH] synchroniser : move backup options to init Moves the backup options from sync_files to init. The options are needed in links (to ignore the backup folders) so it is way easier to have them as attributes. To do this we move everything related to backup into __init__. Also remove the option from the runner. --- src/unisync/runners.py | 2 +- src/unisync/synchroniser.py | 65 ++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/unisync/runners.py b/src/unisync/runners.py index e36704d..af51ded 100644 --- a/src/unisync/runners.py +++ b/src/unisync/runners.py @@ -11,7 +11,7 @@ def unisync_sync(synchroniser:Synchroniser, paths_manager:PathsManager, config: return 1 print("Connected to the remote.") - synchroniser.sync_files(paths_manager.get_paths_to_sync(), backup=config.backup) + synchroniser.sync_files(paths_manager.get_paths_to_sync()) synchroniser.sync_links(paths_manager.get_paths_to_sync()) # TODO check the config options and do or don't do the following diff --git a/src/unisync/synchroniser.py b/src/unisync/synchroniser.py index f3a6c2c..7d0e912 100644 --- a/src/unisync/synchroniser.py +++ b/src/unisync/synchroniser.py @@ -49,8 +49,10 @@ class Synchroniser: Currently unused. """ - def __init__(self, remote:str, local:str, user:str, ip:str, - port:int=22, args_bool:list=[], args_value:dict={}, ssh_settings:dict={}): + def __init__(self, remote:str, local:str, user:str, ip:str, port:int=22, + args_bool:list=[], args_value:dict={}, ssh_settings:dict={}, + backup:BackupConfig | None = None + ): """Initialises an instance of Synchroniser. """ self.remote_dir:str = remote @@ -61,6 +63,34 @@ class Synchroniser: self.remote_user:str = user self.remote_ip:str = ip self.remote_port:int = port + self.files_extra:list = list() + self.links_extra:list = list() + + if(backup != None and backup.enabled): + backup = cast(BackupConfig, backup) + self.files_extra.append("-backup") + if(backup.selection != ""): + self.files_extra.append(backup.selection) + else: + self.files_extra.append("Name *") + + self.files_extra.extend([ + "-backuploc", + backup.location, + "-maxbackups", + str(backup.max_backups), + "-backupsuffix", + backup.backupsuffix, + "-backupprefix", + backup.backupprefix, + "-ignore", + f"Name {backup.backupprefix[:-1]}" + ]) + + self.links_extra.extend([ + "-ignore", + f"Name {backup.backupprefix[:-1]}" + ]) def create_ssh_master_connection(self, control_path:str="~/.ssh/control_%C", connection_timeout:int=60) -> int: """Creates an ssh master connection. @@ -119,9 +149,7 @@ class Synchroniser: close = subprocess.Popen(command) return close.wait() - def sync_files(self, paths:list, force:bool=False, - backup:BackupConfig | None = None - ) -> int: + def sync_files(self, paths:list, force:bool=False) -> int: """Synchronises the files. Args: @@ -131,35 +159,13 @@ class Synchroniser: Returns: The return code of sync. """ - other = list() - if(backup != None and backup.enabled): - backup = cast(BackupConfig, backup) - - other.append("-backup") - if(backup.selection != ""): - other.append(backup.selection) - else: - other.append("Name *") - - other.extend([ - "-backuploc", - backup.location, - "-maxbackups", - str(backup.max_backups), - "-backupsuffix", - backup.backupsuffix, - "-backupprefix", - backup.backupprefix, - "-ignore", - backup.backupprefix - ]) return self.sync( f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/.data", self.local, paths=paths, force=force, - other=other + other=self.files_extra ) def sync_links(self, ignore:list) -> int: @@ -174,7 +180,8 @@ class Synchroniser: return self.sync( f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/links", self.local, - ignore=ignore + ignore=ignore, + other=self.links_extra ) def sync(self, remote_root:str, local_root:str,