From e999f3ff5178f10d05a3f18443bc602a63fb2f84 Mon Sep 17 00:00:00 2001 From: Abogical Date: Thu, 24 Nov 2016 01:26:29 +0200 Subject: [PATCH 1/2] Add ProgressIndicatorMessage and abstract class ProgressIndicatorBase --- src/borg/helpers.py | 57 ++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/borg/helpers.py b/src/borg/helpers.py index ab2e1271a..d6c71d054 100644 --- a/src/borg/helpers.py +++ b/src/borg/helpers.py @@ -1209,23 +1209,10 @@ def ellipsis_truncate(msg, space): return msg + ' ' * (space - msg_width) -class ProgressIndicatorPercent: +class ProgressIndicatorBase: LOGGER = 'borg.output.progress' - def __init__(self, total=0, step=5, start=0, msg="%3.0f%%"): - """ - Percentage-based progress indicator - - :param total: total amount of items - :param step: step size in percent - :param start: at which percent value to start - :param msg: output message, must contain one %f placeholder for the percentage - """ - self.counter = 0 # 0 .. (total-1) - self.total = total - self.trigger_at = start # output next percentage value when reaching (at least) this - self.step = step - self.msg = msg + def __init__(self): self.handler = None self.logger = logging.getLogger(self.LOGGER) @@ -1247,6 +1234,41 @@ class ProgressIndicatorPercent: self.logger.removeHandler(self.handler) self.handler.close() + +def justify_to_terminal_size(message): + terminal_space = get_terminal_size(fallback=(-1, -1))[0] + # justify only if we are outputting to a terminal + if terminal_space != -1: + return message.ljust(terminal_space) + return message + + +class ProgressIndicatorMessage(ProgressIndicatorBase): + def output(self, msg): + self.logger.info(justify_to_terminal_size(msg)) + + def finish(self): + self.output('') + + +class ProgressIndicatorPercent(ProgressIndicatorBase): + def __init__(self, total=0, step=5, start=0, msg="%3.0f%%"): + """ + Percentage-based progress indicator + + :param total: total amount of items + :param step: step size in percent + :param start: at which percent value to start + :param msg: output message, must contain one %f placeholder for the percentage + """ + self.counter = 0 # 0 .. (total-1) + self.total = total + self.trigger_at = start # output next percentage value when reaching (at least) this + self.step = step + self.msg = msg + + super().__init__() + def progress(self, current=None, increase=1): if current is not None: self.counter = current @@ -1279,10 +1301,7 @@ class ProgressIndicatorPercent: def output(self, message, justify=True): if justify: - terminal_space = get_terminal_size(fallback=(-1, -1))[0] - # no need to ljust if we're not outputing to a terminal - if terminal_space != -1: - message = message.ljust(terminal_space) + message = justify_to_terminal_size(message) self.logger.info(message) def finish(self): From f9b3d28c19e72e339e5c73dfb3a43eccb3eb356b Mon Sep 17 00:00:00 2001 From: Abogical Date: Thu, 24 Nov 2016 01:46:50 +0200 Subject: [PATCH 2/2] Add progress messages for cache.commit --- src/borg/cache.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/borg/cache.py b/src/borg/cache.py index e2ce3651b..a0cce1ecd 100644 --- a/src/borg/cache.py +++ b/src/borg/cache.py @@ -18,6 +18,7 @@ from .helpers import get_cache_dir from .helpers import decode_dict, int_to_bigint, bigint_to_int, bin_to_hex from .helpers import format_file_size from .helpers import yes +from .helpers import ProgressIndicatorMessage from .item import Item, ArchiveItem from .key import PlaintextKey from .locking import Lock @@ -246,7 +247,9 @@ Chunk index: {0.total_unique_chunks:20d} {0.total_chunks:20d}""" """ if not self.txn_active: return + pi = ProgressIndicatorMessage() if self.files is not None: + pi.output('Saving files cache') ttl = int(os.environ.get('BORG_FILES_CACHE_TTL', 20)) with SaveFile(os.path.join(self.path, 'files'), binary=True) as fd: for path_hash, item in self.files.items(): @@ -257,17 +260,20 @@ Chunk index: {0.total_unique_chunks:20d} {0.total_chunks:20d}""" if entry.age == 0 and bigint_to_int(entry.mtime) < self._newest_mtime or \ entry.age > 0 and entry.age < ttl: msgpack.pack((path_hash, entry), fd) + pi.output('Saving cache config') self.config.set('cache', 'manifest', self.manifest.id_str) self.config.set('cache', 'timestamp', self.manifest.timestamp) self.config.set('cache', 'key_type', str(self.key.TYPE)) self.config.set('cache', 'previous_location', self.repository._location.canonical_path()) with SaveFile(os.path.join(self.path, 'config')) as fd: self.config.write(fd) + pi.output('Saving chunks cache') self.chunks.write(os.path.join(self.path, 'chunks').encode('utf-8')) os.rename(os.path.join(self.path, 'txn.active'), os.path.join(self.path, 'txn.tmp')) shutil.rmtree(os.path.join(self.path, 'txn.tmp')) self.txn_active = False + pi.finish() def rollback(self): """Roll back partial and aborted transactions