Browse Source

buildkit.downloads: Simplify progress reporting and re-add round()

See new code comments for rationale.

See #652
Eloston 5 years ago
parent
commit
47c6164c3b
1 changed files with 11 additions and 4 deletions
  1. 11 4
      buildkit/downloads.py

+ 11 - 4
buildkit/downloads.py

@@ -38,11 +38,18 @@ class _UrlRetrieveReportHook: #pylint: disable=too-few-public-methods
         self._last_percentage = None
 
     def __call__(self, block_count, block_size, total_size):
-        if total_size > 0:
-            total_blocks = (total_size + (block_size - total_size % block_size)) / block_size
-            percentage = block_count / total_blocks
+        # Use total_blocks to handle case total_size < block_size
+        # total_blocks is ceiling of total_size / block_size
+        # Ceiling division from: https://stackoverflow.com/a/17511341
+        total_blocks = -(-total_size // block_size)
+        if total_blocks > 0:
+            # Do not needlessly update the console. Since the console is
+            # updated synchronously, we don't want updating the console to
+            # bottleneck downloading. Thus, only refresh the output when the
+            # displayed value should change.
+            percentage = round(block_count / total_blocks, ndigits=3)
             if percentage == self._last_percentage:
-                return # Do not needlessly update the console
+                return
             self._last_percentage = percentage
             print('\r' + ' ' * self._max_len_printed, end='')
             status_line = 'Progress: {:.1%} of {:,d} B'.format(percentage, total_size)