From 8d2a6df1fda2d766d92f554a7ffea499420e4f7a Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Fri, 26 Feb 2021 03:55:44 -0500 Subject: [PATCH] progress: include execution time summary We're already keeping tracking of the start time, so might as well use it to display overall execution time for steps. Change-Id: Ib4cf8b2b0dfcdf7b776a84295d59cc569971bdf5 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/298482 Reviewed-by: Michael Mortensen Tested-by: Mike Frysinger --- progress.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/progress.py b/progress.py index d19fc5b1..ae797488 100644 --- a/progress.py +++ b/progress.py @@ -25,6 +25,22 @@ _NOT_TTY = not os.isatty(2) CSI_ERASE_LINE = '\x1b[2K' +def duration_str(total): + """A less noisy timedelta.__str__. + + The default timedelta stringification contains a lot of leading zeros and + uses microsecond resolution. This makes for noisy output. + """ + hours, rem = divmod(total, 3600) + mins, secs = divmod(rem, 60) + ret = '%.3fs' % (secs,) + if mins: + ret = '%im%s' % (mins, ret) + if hours: + ret = '%ih%s' % (hours, ret) + return ret + + class Progress(object): def __init__(self, title, total=0, units='', print_newline=False): self._title = title @@ -87,18 +103,21 @@ class Progress(object): if _NOT_TTY or IsTrace() or not self._show: return + duration = duration_str(time() - self._start) if self._total <= 0: - sys.stderr.write('%s\r%s: %d, done.\n' % ( + sys.stderr.write('%s\r%s: %d, done in %s\n' % ( CSI_ERASE_LINE, self._title, - self._done)) + self._done, + duration)) sys.stderr.flush() else: p = (100 * self._done) / self._total - sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done.\n' % ( + sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done in %s\n' % ( CSI_ERASE_LINE, self._title, p, self._done, self._units, - self._total, self._units)) + self._total, self._units, + duration)) sys.stderr.flush()