From 38ba21f9010334b9eb84d4ac54e5e87c40b7e539 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 11 Feb 2012 11:34:15 +0100 Subject: [PATCH] Add a nice progressbar when uploading files to the hub. --- python/pakfire/client/base.py | 18 +++++++++++++----- python/pakfire/util.py | 8 +++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/python/pakfire/client/base.py b/python/pakfire/client/base.py index eda836ef2..e4574492a 100644 --- a/python/pakfire/client/base.py +++ b/python/pakfire/client/base.py @@ -15,6 +15,7 @@ from pakfire.system import system import transport from pakfire.constants import * +from pakfire.i18n import _ import logging log = logging.getLogger("pakfire.client") @@ -103,9 +104,13 @@ class BuildMixin(object): upload_id = self.conn.upload_create(os.path.basename(filename), size, hash) + # Make a nice progressbar. + pb = pakfire.util.make_progress(os.path.basename(filename), size, speed=True, eta=True) + try: # Calculate the number of chunks. chunks = (size // CHUNK_SIZE) + 1 + transferred = 0 # Cut the file in pieces and upload them one after another. with open(filename) as f: @@ -116,8 +121,9 @@ class BuildMixin(object): break chunk += 1 - log.info("Uploading chunk %s/%s of %s." % (chunk, chunks, - os.path.basename(filename))) + if pb: + transferred += len(data) + pb.update(transferred) data = xmlrpclib.Binary(data) self.conn.upload_chunk(upload_id, data) @@ -132,11 +138,13 @@ class BuildMixin(object): raise + finally: + if pb: + pb.finish() + # If the server sends false, something happened with the upload that # could not be recovered. - if ret: - logging.info("Upload of %s succeeded." % filename) - else: + if not ret: logging.error("Upload of %s was not successful." % filename) raise Exception, "Upload failed." diff --git a/python/pakfire/util.py b/python/pakfire/util.py index 4bd40beed..79daeaa2c 100644 --- a/python/pakfire/util.py +++ b/python/pakfire/util.py @@ -92,7 +92,7 @@ class Bar(progressbar.Bar): bar = (self.left + (m*marked_width).ljust(cwidth) + self.right) return bar -def make_progress(message, maxval, eta=True): +def make_progress(message, maxval, eta=True, speed=False): # Return nothing if stdout is not a terminal. if not sys.stdout.isatty(): return @@ -105,6 +105,12 @@ def make_progress(message, maxval, eta=True): " ", ] + if speed: + widgets += [ + progressbar.Percentage(), " ", + progressbar.FileTransferSpeed(), " " + ] + if eta: widgets += [progressbar.ETA(), " ",] -- 2.39.5