Simone Tiraboschi has uploaded a new change for review.

Change subject: upload: use low level copy and print a progress bar
......................................................................

upload: use low level copy and print a progress bar

Using python code to copy the file measuring its progress
and printing a progress bar.

Change-Id: I2a28947cd607be83c51b25bfcdb5ef50a58afbbb
Bug-Url: https://bugzilla.redhat.com/1077235
Signed-off-by: Simone Tiraboschi <[email protected]>
---
M src/__main__.py
1 file changed, 34 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-image-uploader 
refs/changes/92/37292/1

diff --git a/src/__main__.py b/src/__main__.py
index 747ed08..1b5d12d 100644
--- a/src/__main__.py
+++ b/src/__main__.py
@@ -806,18 +806,25 @@
         else:
             return (False, dir_size)
 
-    def copyfileobj_sparse(
+    def copyfileobj_sparse_progress(
             self,
             fsrc,
             fdst,
             length=16*1024,
-            make_sparse=True
+            make_sparse=True,
+            bar_length=40,
+            quiet=True,
     ):
         """
         copy data from file-like object fsrc to file-like object fdst
         like shutils.copyfileobj does but supporting also
-        sparse file
+        sparse file. It can print also a progress bar
         """
+        i = 0
+        fsrc.seek(0, 2)  # move the cursor to the end of the file
+        end_val = fsrc.tell()
+        fsrc.seek(0, 0)  # move back the cursor to the start of the file
+        old_ipercent = -1
         while 1:
             buf = fsrc.read(length)
             if not buf:
@@ -826,9 +833,28 @@
                 fdst.seek(len(buf), os.SEEK_CUR)
             else:
                 fdst.write(buf)
+            i += length
+            percent = float(i) / end_val
+            ipercent = int(round(percent * 100))
+            if not quiet and ipercent > old_ipercent:
+                old_ipercent = ipercent
+                hashes = '#' * int(round(percent * bar_length))
+                spaces = ' ' * (bar_length - len(hashes))
+                sys.stdout.write(
+                    _(
+                        "\rUploading: [{h}] {n}%".format(
+                            h=hashes + spaces,
+                            n=ipercent,
+                        )
+                    )
+                )
+                sys.stdout.flush()
         if make_sparse:
             # Make sure the file ends where it should, even if padded out.
             fdst.truncate()
+        if not quiet:
+            sys.stdout.write('\n')
+            sys.stdout.flush()
 
     def copy_file_nfs(self, src_file_name, dest_file_name, uid, gid):
         """
@@ -846,7 +872,11 @@
             os.setegid(gid)
             os.seteuid(uid)
             dest = open(dest_file_name, 'wb')
-            self.copyfileobj_sparse(src, dest)
+            self.copyfileobj_sparse_progress(
+                fsrc=src,
+                fdst=dest,
+                quiet=self.configuration.options.quiet,
+            )
         except Exception, e:
             retVal = False
             logging.error(


-- 
To view, visit http://gerrit.ovirt.org/37292
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2a28947cd607be83c51b25bfcdb5ef50a58afbbb
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-image-uploader
Gerrit-Branch: master
Gerrit-Owner: Simone Tiraboschi <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to