From: Michael Tremer Date: Thu, 26 May 2022 09:57:02 +0000 (+0000) Subject: hub: Show progress bar on file uploads X-Git-Tag: 0.9.28~752 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dec08a5c62112c3875f70e9761972e818ace2667;p=pakfire.git hub: Show progress bar on file uploads Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/hub.py b/src/pakfire/hub.py index 0b5c689e1..e47a1db88 100644 --- a/src/pakfire/hub.py +++ b/src/pakfire/hub.py @@ -27,7 +27,9 @@ import os.path import tornado.httpclient import urllib.parse +from . import _pakfire from .constants import * +from .i18n import _ # Setup logging log = logging.getLogger("pakfire.hub") @@ -131,7 +133,7 @@ class Hub(object): # File uploads - async def upload(self, path, filename=None): + async def upload(self, path, filename=None, show_progress=True): """ Uploads the file to the hub returning the upload ID """ @@ -144,11 +146,24 @@ class Hub(object): # Determine the filesize size = os.path.getsize(path) + # Make progressbar + if show_progress: + p = _pakfire.Progressbar() + p.add_string(_("Uploading %s") % filename) + p.add_percentage() + p.add_bar() + p.add_transfer_speed() + p.add_string("|") + p.add_bytes_transferred() + p.add_eta() + else: + p = None + # Compute a digest digest = self._compute_digest("blake2b", path) # Prepare the file for streaming - body_producer = functools.partial(self._stream_file, path) + body_producer = functools.partial(self._stream_file, path, size, p) # Perform upload response = await self._request("PUT", "/upload", @@ -160,14 +175,28 @@ class Hub(object): return response.get("id") @staticmethod - def _stream_file(path, write): - with open(path, "rb") as f: - while True: - buf = f.read(64 * 1024) - if not buf: - break - - write(buf) + def _stream_file(path, size, p, write): + # Start the progressbar + if p: + p.start(size) + + try: + with open(path, "rb") as f: + while True: + buf = f.read(64 * 1024) + if not buf: + break + + # Update progressbar + if p: + l = len(buf) + p.increment(l) + + write(buf) + finally: + # Finish the progressbar + if p: + p.finish() @staticmethod def _compute_digest(algo, path):