From cc75fbe2bbc9a16a7f43f8b42ef613ee448fb2a0 Mon Sep 17 00:00:00 2001 From: Paula Kammler Date: Tue, 7 May 2024 11:41:40 +0200 Subject: [PATCH] add new argument to not watch the copied folders --- livesync/livesync.py | 3 ++- livesync/sync.py | 31 +++++++++++++++++-------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/livesync/livesync.py b/livesync/livesync.py index 5734f22..6eb340f 100755 --- a/livesync/livesync.py +++ b/livesync/livesync.py @@ -14,12 +14,13 @@ def main(): parser.add_argument('--on-change', type=str, help='command to be executed on remote host after any file change') parser.add_argument('--mutex-interval', type=int, default=10, help='interval in which mutex is updated') parser.add_argument('--ignore-mutex', action='store_true', help='ignore mutex (use with caution)') + parser.add_argument('--no-watch', action='store_true', help='do not watch for changes') parser.add_argument('rsync_args', nargs=argparse.REMAINDER, help='arbitrary rsync parameters after "--"') args = parser.parse_args() folder = Folder(args.source, args.target, ssh_port=args.ssh_port, on_change=args.on_change) folder.rsync_args(' '.join(args.rsync_args)) - sync(folder, mutex_interval=args.mutex_interval, ignore_mutex=args.ignore_mutex) + sync(folder, mutex_interval=args.mutex_interval, ignore_mutex=args.ignore_mutex, watch=not args.no_watch) if __name__ == '__main__': diff --git a/livesync/sync.py b/livesync/sync.py index 84ac9d7..d0c22ab 100644 --- a/livesync/sync.py +++ b/livesync/sync.py @@ -10,7 +10,9 @@ def get_summary(folders: Iterable[Folder]) -> str: return '\n'.join(folder.get_summary() for folder in folders).replace('"', '\'') -async def run_folder_tasks(folders: Iterable[Folder], mutex_interval: float, ignore_mutex: bool = False) -> None: +async def run_folder_tasks( + folders: Iterable[Folder], + mutex_interval: float, ignore_mutex: bool = False, watch: bool = True) -> None: try: if not ignore_mutex: summary = get_summary(folders) @@ -25,23 +27,24 @@ async def run_folder_tasks(folders: Iterable[Folder], mutex_interval: float, ign print(f' {folder.source_path} --> {folder.target}', flush=True) folder.sync() - for folder in folders: - print(f'Watch folder {folder.source_path}', flush=True) - asyncio.create_task(folder.watch()) - - while True: - if not ignore_mutex: - summary = get_summary(folders) - for mutex in mutexes.values(): - if not mutex.set(summary): - break - await asyncio.sleep(mutex_interval) + if watch: + for folder in folders: + print(f'Watch folder {folder.source_path}', flush=True) + asyncio.create_task(folder.watch()) + + while True: + if not ignore_mutex: + summary = get_summary(folders) + for mutex in mutexes.values(): + if not mutex.set(summary): + break + await asyncio.sleep(mutex_interval) except Exception as e: print(e) -def sync(*folders: Folder, mutex_interval: float = 10, ignore_mutex: bool = False) -> None: +def sync(*folders: Folder, mutex_interval: float = 10, ignore_mutex: bool = False, watch: bool = True) -> None: try: - asyncio.run(run_folder_tasks(folders, mutex_interval, ignore_mutex=ignore_mutex)) + asyncio.run(run_folder_tasks(folders, mutex_interval, ignore_mutex=ignore_mutex, watch=watch)) except KeyboardInterrupt: print('Bye!')