]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
Add a nice progressbar when uploading files to the hub.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2012 10:34:15 +0000 (11:34 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2012 10:34:15 +0000 (11:34 +0100)
python/pakfire/client/base.py
python/pakfire/util.py

index eda836ef2e8bda928210423144d75e2105037fb0..e4574492aa2667444bd9f137c9de7fd04a4cb2ca 100644 (file)
@@ -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."
 
index 4bd40beed1df459ff042631c9405f228558eccc6..79daeaa2cdf7660a97c5a1ec7d4654cef7a13b63 100644 (file)
@@ -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(), "  ",]