Skip to content

Commit 7c922cc

Browse files
committed
Limit output frequency based on configurable value
Adds parameter for output progress frequency, defaulting to 0 to keep current behavior. Add multiline output control to allow for writing the progress report to a new line.
1 parent c522dbd commit 7c922cc

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

awscli/customizations/s3/results.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ class ResultPrinter(BaseResultHandler):
315315
SRC_DEST_TRANSFER_LOCATION_FORMAT = '{src} to {dest}'
316316
SRC_TRANSFER_LOCATION_FORMAT = '{src}'
317317

318-
def __init__(self, result_recorder, out_file=None, error_file=None):
318+
def __init__(
319+
self, result_recorder, out_file=None, error_file=None, frequency=0, oneline=True
320+
):
319321
"""Prints status of ongoing transfer
320322
321323
:type result_recorder: ResultRecorder
@@ -331,6 +333,8 @@ def __init__(self, result_recorder, out_file=None, error_file=None):
331333
"""
332334
self._result_recorder = result_recorder
333335
self._out_file = out_file
336+
self._frequency = frequency
337+
self._first = True
334338
if self._out_file is None:
335339
self._out_file = sys.stdout
336340
self._error_file = error_file
@@ -347,12 +351,31 @@ def __init__(self, result_recorder, out_file=None, error_file=None):
347351
DryRunResult: self._print_dry_run,
348352
FinalTotalSubmissionsResult: self._clear_progress_if_no_more_expected_transfers,
349353
}
354+
self._now = time.time()
355+
self._oneline = oneline
350356

351357
def __call__(self, result):
352358
"""Print the progress of the ongoing transfer based on a result"""
353-
self._result_handler_map.get(type(result), self._print_noop)(
354-
result=result
355-
)
359+
result_handler = self._result_handler_map.get(type(result), self._print_noop)
360+
if type(result) is ProgressResult:
361+
result_handler = self._override_progress_result_handler(
362+
result, result_handler
363+
)
364+
result_handler(result=result)
365+
366+
def _override_progress_result_handler(self, result, result_handler):
367+
if (
368+
type(result) in [ProgressResult]
369+
and (
370+
self._first
371+
or (self._frequency == 0)
372+
or (time.time() - self._now >= self._frequency)
373+
)
374+
):
375+
self._now = time.time()
376+
self._first = False
377+
return result_handler
378+
return self._print_noop
356379

357380
def _print_noop(self, **kwargs):
358381
# If the result does not have a handler, then do nothing with it.
@@ -463,15 +486,19 @@ def _print_progress(self, **kwargs):
463486
if not self._result_recorder.expected_totals_are_final():
464487
progress_statement += self._STILL_CALCULATING_TOTALS
465488

466-
# Make sure that it overrides any previous progress bar.
467-
progress_statement = self._adjust_statement_padding(
468-
progress_statement, ending_char='\r'
469-
)
470-
# We do not want to include the carriage return in this calculation
471-
# as progress length is used for determining whitespace padding.
472-
# So we subtract one off of the length.
473-
self._progress_length = len(progress_statement) - 1
474-
489+
if self._oneline:
490+
# Make sure that it overrides any previous progress bar.
491+
progress_statement = self._adjust_statement_padding(
492+
progress_statement, ending_char='\r'
493+
)
494+
# We do not want to include the carriage return in this calculation
495+
# as progress length is used for determining whitespace padding.
496+
# So we subtract one off of the length.
497+
self._progress_length = len(progress_statement) - 1
498+
else:
499+
progress_statement = self._adjust_statement_padding(
500+
progress_statement, ending_char='\n'
501+
)
475502
# Print the progress out.
476503
self._print_to_out_file(progress_statement)
477504

awscli/customizations/s3/s3handler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ def _add_result_printer(self, result_recorder, result_processor_handlers):
104104
elif not self._cli_params.get('progress'):
105105
result_printer = NoProgressResultPrinter(result_recorder)
106106
else:
107-
result_printer = ResultPrinter(result_recorder)
107+
result_printer = ResultPrinter(
108+
result_recorder,
109+
frequency=self._cli_params.get('progress_frequency'),
110+
oneline=not self._cli_params.get('progress_multiline'),
111+
)
108112
result_processor_handlers.append(result_printer)
109113

110114

awscli/customizations/s3/subcommands.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,28 @@
526526
),
527527
}
528528

529+
PROGRESS_FREQUENCY = {
530+
'name': 'progress-frequency',
531+
'dest': 'progress_frequency',
532+
'cli_type_name': 'integer',
533+
'default': 0,
534+
'help_text': (
535+
'Number of seconds to wait before updating file '
536+
'transfer progress. This flag is only applied when '
537+
'the quiet and only-show-errors flags are not '
538+
'provided.'
539+
),
540+
}
541+
542+
PROGRESS_MULTILINE = {
543+
'name': 'progress-multiline',
544+
'dest': 'progress_multiline',
545+
'action': 'store_true',
546+
'help_text': (
547+
'Show progress on multiple lines.'
548+
),
549+
}
550+
529551

530552
EXPECTED_SIZE = {
531553
'name': 'expected-size',
@@ -669,6 +691,8 @@
669691
SOURCE_REGION,
670692
ONLY_SHOW_ERRORS,
671693
NO_PROGRESS,
694+
PROGRESS_FREQUENCY,
695+
PROGRESS_MULTILINE,
672696
PAGE_SIZE,
673697
IGNORE_GLACIER_WARNINGS,
674698
FORCE_GLACIER_TRANSFER,

0 commit comments

Comments
 (0)