From c6de3ebea06d9bb351b974dbe88c6886234fb5bd Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 20 Aug 2019 16:49:18 +0300 Subject: [PATCH] Refactor "humanize_duration_time" to "00:00:00.000" format --- platformio/commands/run/command.py | 16 ++++++++-------- platformio/commands/test/command.py | 18 +++++++++--------- platformio/util.py | 22 +++++++++------------- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/platformio/commands/run/command.py b/platformio/commands/run/command.py index e3a22f9243..7b5ca50c0d 100644 --- a/platformio/commands/run/command.py +++ b/platformio/commands/run/command.py @@ -132,8 +132,8 @@ def process_env(ctx, name, config, environments, targets, upload_port, silent, ep = EnvironmentProcessor(ctx, name, config, targets, upload_port, silent, verbose, jobs) - result = {"env": name, "elapsed": time(), "succeeded": ep.process()} - result['elapsed'] = time() - result['elapsed'] + result = {"env": name, "duration": time(), "succeeded": ep.process()} + result['duration'] = time() - result['duration'] # print footer on error or when is not unit testing if not is_test_running and (not silent or not result['succeeded']): @@ -164,7 +164,7 @@ def print_processing_footer(result): util.print_labeled_bar( "[%s] Took %.2f seconds" % ((click.style("FAILED", fg="red", bold=True) if is_failed else - click.style("SUCCESS", fg="green", bold=True)), result['elapsed']), + click.style("SUCCESS", fg="green", bold=True)), result['duration']), is_error=is_failed) @@ -172,10 +172,10 @@ def print_processing_summary(results): tabular_data = [] succeeded_nums = 0 failed_nums = 0 - elapsed = 0 + duration = 0 for result in results: - elapsed += result.get("elapsed", 0) + duration += result.get("duration", 0) if result.get("succeeded") is False: failed_nums += 1 status_str = click.style("FAILED", fg="red") @@ -187,19 +187,19 @@ def print_processing_summary(results): tabular_data.append( (click.style(result['env'], fg="cyan"), status_str, - util.humanize_elapsed_time(result.get("elapsed")))) + util.humanize_duration_time(result.get("duration")))) click.echo() click.echo(tabulate(tabular_data, headers=[ click.style(s, bold=True) - for s in ("Environment", "Status", "Time") + for s in ("Environment", "Status", "Duration") ]), err=failed_nums) util.print_labeled_bar( "%s%d succeeded in %s" % ("%d failed, " % failed_nums if failed_nums else "", succeeded_nums, - util.humanize_elapsed_time(elapsed)), + util.humanize_duration_time(duration)), is_error=failed_nums, fg="red" if failed_nums else "green") diff --git a/platformio/commands/test/command.py b/platformio/commands/test/command.py index bf4b0253ed..ce50b5d988 100644 --- a/platformio/commands/test/command.py +++ b/platformio/commands/test/command.py @@ -139,10 +139,10 @@ def cli( # pylint: disable=redefined-builtin result = { "env": envname, "test": testname, - "elapsed": time(), + "duration": time(), "succeeded": tp.process() } - result['elapsed'] = time() - result['elapsed'] + result['duration'] = time() - result['duration'] results.append(result) print_processing_footer(result) @@ -179,21 +179,20 @@ def print_processing_footer(result): util.print_labeled_bar( "[%s] Took %.2f seconds" % ((click.style("FAILED", fg="red", bold=True) if is_failed else - click.style("PASSED", fg="green", bold=True)), result['elapsed']), + click.style("PASSED", fg="green", bold=True)), result['duration']), is_error=is_failed) def print_testing_summary(results): click.echo() - # util.print_labeled_bar("SUMMARY") tabular_data = [] succeeded_nums = 0 failed_nums = 0 - elapsed = 0 + duration = 0 for result in results: - elapsed += result.get("elapsed", 0) + duration += result.get("duration", 0) if result.get("succeeded") is False: failed_nums += 1 status_str = click.style("FAILED", fg="red") @@ -205,18 +204,19 @@ def print_testing_summary(results): tabular_data.append( (result['test'], click.style(result['env'], fg="cyan"), status_str, - util.humanize_elapsed_time(result.get("elapsed")))) + util.humanize_duration_time(result.get("duration")))) click.echo(tabulate(tabular_data, headers=[ click.style(s, bold=True) - for s in ("Test", "Environment", "Status", "Time") + for s in ("Test", "Environment", "Status", + "Duration") ]), err=failed_nums) util.print_labeled_bar( "%s%d succeeded in %s" % ("%d failed, " % failed_nums if failed_nums else "", succeeded_nums, - util.humanize_elapsed_time(elapsed)), + util.humanize_duration_time(duration)), is_error=failed_nums, fg="red" if failed_nums else "green") diff --git a/platformio/util.py b/platformio/util.py index 4b16bf3afd..1903f04cd0 100644 --- a/platformio/util.py +++ b/platformio/util.py @@ -455,20 +455,16 @@ def print_labeled_bar(label, is_error=False, fg=None): err=is_error) -def humanize_elapsed_time(total): - total = total or 0 - constants = ((3600 * 24, "day"), (3600, "hour"), (60, "minute"), - (1, "second")) +def humanize_duration_time(duration): + if duration is None: + return duration + duration = duration * 1000 tokens = [] - for coef, name in constants: - t = (math.floor if total > 60 else round)(total / coef) - if t == 0: - continue - tokens.append("%d %s%s" % (t, name, "s" if t > 1 else "")) - total -= t * coef - if len(tokens) > 1: - tokens[-1] = "and %s" % tokens[-1] - return ", ".join(tokens) + for multiplier in (3600000, 60000, 1000, 1): + fraction = math.floor(duration / multiplier) + tokens.append(int(round(duration) if multiplier == 1 else fraction)) + duration -= fraction * multiplier + return "{:02d}:{:02d}:{:02d}.{:03d}".format(*tokens) def get_original_version(version):