From 2790ae90d333b0ba2d08ba12b86704bc984b4286 Mon Sep 17 00:00:00 2001 From: Kristjan Eimre Date: Thu, 22 Feb 2024 15:02:27 +0200 Subject: [PATCH] use rsync legacy progress for v<3 (default on macos) --- disk_objectstore/backup_utils.py | 37 ++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/disk_objectstore/backup_utils.py b/disk_objectstore/backup_utils.py index 74011c4..1db05a4 100644 --- a/disk_objectstore/backup_utils.py +++ b/disk_objectstore/backup_utils.py @@ -5,6 +5,7 @@ import datetime import logging import random +import re import shutil import sqlite3 import string @@ -115,7 +116,23 @@ def run_cmd( return success, res.stdout - def call_rsync( # pylint: disable=too-many-arguments + def get_rsync_major_version(self): + """ + Get the rsync major version. + """ + result = subprocess.run( + [self.rsync_exe, "--version"], + capture_output=True, + text=True, + check=False, + ) + pattern = r"rsync\s+version\s+(\d+\.\d+\.\d+)" + match = re.search(pattern, result.stdout) + if match: + return int(match.group(1).split(".")[0]) + return None + + def call_rsync( # pylint: disable=too-many-arguments,too-many-branches self, src: Path, dest: Path, @@ -142,8 +159,20 @@ def call_rsync( # pylint: disable=too-many-arguments self.rsync_exe, "-azh", "--no-whole-file", - "--info=progress2,stats1", ] + + capture_output = True + if LOGGER.isEnabledFor(logging.INFO): + capture_output = False + rsync_version = self.get_rsync_major_version() + if rsync_version and rsync_version >= 3: + # These options show progress in a nicer way but + # they're only available for rsync version 3+ + all_args += ["--info=progress2,stats1"] + else: + LOGGER.info("rsync version <3 detected: showing 'legacy' progress.") + all_args += ["--progress"] + if LOGGER.isEnabledFor(logging.DEBUG): all_args += ["-vv"] if extra_args: @@ -171,10 +200,6 @@ def call_rsync( # pylint: disable=too-many-arguments cmd_str = " ".join(all_args) LOGGER.info("Running '%s'", cmd_str) - capture_output = True - if LOGGER.isEnabledFor(logging.INFO): - capture_output = False - res = subprocess.run( all_args, capture_output=capture_output, text=True, check=False )