From: Philippe Proulx Date: Fri, 8 Apr 2016 00:11:43 +0000 (-0400) Subject: cli: add optional progress lines to MI mode X-Git-Url: http://git.efficios.com/?p=deliverable%2Flttng-analyses.git;a=commitdiff_plain;h=73f9d00539d371356b89d83c2337933786d6f14c cli: add optional progress lines to MI mode Signed-off-by: Philippe Proulx --- diff --git a/lttnganalyses/cli/command.py b/lttnganalyses/cli/command.py index 89a9333..c277709 100644 --- a/lttnganalyses/cli/command.py +++ b/lttnganalyses/cli/command.py @@ -225,7 +225,12 @@ class Command: ) def _check_lost_events(self): - self._print('Checking the trace for lost events...') + msg = 'Checking the trace for lost events...' + self._print(msg) + + if self._mi_mode and self._args.output_progress: + mi.print_progress(0, msg) + try: subprocess.check_output('babeltrace "%s"' % self._args.path, shell=True) @@ -245,18 +250,39 @@ class Command: self._mi_print() + def _pb_setup(self): + if self._mi_mode: + if self._args.output_progress: + progressbar.mi_progress_setup(self) + else: + progressbar.progressbar_setup(self) + + def _pb_update(self): + if self._mi_mode: + if self._args.output_progress: + progressbar.mi_progress_update(self) + else: + progressbar.progressbar_update(self) + + def _pb_finish(self): + if self._mi_mode: + if self._args.output_progress: + progressbar.mi_progress_finish(self) + else: + progressbar.progressbar_finish(self) + def _run_analysis(self): self._pre_analysis() - progressbar.progressbar_setup(self) + self._pb_setup() for event in self._traces.events: - progressbar.progressbar_update(self) + self._pb_update() self._analysis.process_event(event) if self._analysis.ended: break self._automaton.process_event(event) - progressbar.progressbar_finish(self) + self._pb_finish() self._analysis.end() self._post_analysis() @@ -357,9 +383,6 @@ class Command: args.freq_uniform = True if self._mi_mode: - # force no progress in MI mode - args.no_progress = True - # print MI metadata if required if args.metadata: self._mi_print_metadata() @@ -420,6 +443,8 @@ class Command: help='Show analysis\'s metadata') ap.add_argument('path', metavar='', help='trace path', nargs='*') + ap.add_argument('--output-progress', action='store_true', + help='Print progress indication lines') else: ap.add_argument('--no-progress', action='store_true', help='Don\'t display the progress bar') diff --git a/lttnganalyses/cli/mi.py b/lttnganalyses/cli/mi.py index 5c96984..416eba0 100644 --- a/lttnganalyses/cli/mi.py +++ b/lttnganalyses/cli/mi.py @@ -21,6 +21,7 @@ # SOFTWARE. from collections import namedtuple +import sys class Tags: @@ -538,3 +539,20 @@ def get_error(message, code=None): error['error-code'] = code return error + + +def get_progress(at=None, msg=None): + if at is None: + at = '*' + + add = '' + + if msg is not None: + add = ' {}'.format(msg) + + return '{}{}'.format(at, add) + + +def print_progress(at=None, msg=None): + print(get_progress(at, msg)) + sys.stdout.flush() diff --git a/lttnganalyses/cli/progressbar.py b/lttnganalyses/cli/progressbar.py index 7122c46..cd30d4e 100644 --- a/lttnganalyses/cli/progressbar.py +++ b/lttnganalyses/cli/progressbar.py @@ -22,6 +22,8 @@ import os import sys +from . import mi +from collections import namedtuple try: from progressbar import ETA, Bar, Percentage, ProgressBar @@ -44,18 +46,23 @@ def get_folder_size(folder): return total_size +def _get_maxval(obj): + size = get_folder_size(obj._args.path) + + return size / BYTES_PER_EVENT + + def progressbar_setup(obj): if obj._args.no_progress: obj.pbar = None return if progressbar_available: - size = get_folder_size(obj._args.path) widgets = ['Processing the trace: ', Percentage(), ' ', Bar(marker='#', left='[', right=']'), ' ', ETA(), ' '] # see docs for other options obj.pbar = ProgressBar(widgets=widgets, - maxval=size/BYTES_PER_EVENT) + maxval=_get_maxval(obj)) obj.pbar.start() else: print('Warning: progressbar module not available, ' @@ -80,3 +87,44 @@ def progressbar_finish(obj): if obj._args.no_progress: return obj.pbar.finish() + + +class _MiProgress: + def __init__(self, maxval): + self._maxval = maxval + self._events = 0 + self._step = maxval // 997 + + if self._step == 0: + self._step = 1 + + def init(self): + msg = 'Starting analysis: {} estimated events'.format(round(self._maxval)) + mi.print_progress(0, msg) + + def update(self): + if (self._events % self._step) == 0: + if self._events > self._maxval: + mi.print_progress(1, 'Almost done...') + else: + at = round(self._events / self._maxval, 4) + msg = '{} events processed'.format(self._events) + mi.print_progress(at, msg) + + self._events += 1 + + def finish(self): + mi.print_progress(1, 'Done!') + + +def mi_progress_setup(obj): + obj.pbar = _MiProgress(_get_maxval(obj)) + obj.pbar.init() + + +def mi_progress_update(obj): + obj.pbar.update() + + +def mi_progress_finish(obj): + obj.pbar.finish()