jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/401199 )

Change subject: download_dump: Add a progress bar
......................................................................


download_dump: Add a progress bar

Adds a progress bar when downloading files using
scripts/maintenance/download_dump.py. This is helpful when downloading
large files, as it allows the user to see how much is done when downloading
extremely large files. Progress bar also shows number of bytes in
downloaded file and how many bytes have been downloaded.

Bug: T183664
Change-Id: Iacfc4ffec3f7b928fa245879c154775a9f692b8a
---
M scripts/maintenance/download_dump.py
1 file changed, 40 insertions(+), 0 deletions(-)

Approvals:
  Framawiki: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/scripts/maintenance/download_dump.py 
b/scripts/maintenance/download_dump.py
index 853d33f..91c2394 100644
--- a/scripts/maintenance/download_dump.py
+++ b/scripts/maintenance/download_dump.py
@@ -80,6 +80,14 @@
 
     def run(self):
         """Run bot."""
+        def convert_from_bytes(bytes):
+            for unit in ['B', 'K', 'M', 'G', 'T']:
+                if abs(bytes) < 1024:
+                    return str(bytes) + unit
+                bytes = float(format(
+                    bytes / 1024.0, '.2f'))
+            return str(bytes) + 'P'
+
         pywikibot.output('Downloading dump from ' + self.getOption('wikiname'))
 
         download_filename = '{wiki_name}-{revision}-{filename}'.format(
@@ -119,8 +127,40 @@
                     response = fetch(url, stream=True)
                     if response.status == 200:
                         with open(file_current_storepath, 'wb') as result_file:
+                            try:
+                                total = int(response.response_headers[
+                                    'content-length'])
+                            except KeyError:
+                                pywikibot.exception()
+                                total = -1
+                            downloaded = 0
+                            parts = 50
+                            display_string = ''
+
+                            pywikibot.output('')
                             for data in response.data.iter_content(100 * 1024):
                                 result_file.write(data)
+
+                                if total > 0:
+                                    downloaded += len(data)
+                                    done = int(parts * downloaded / total)
+                                    display = map(convert_from_bytes,
+                                                  (downloaded, total))
+                                    prior_display = display_string
+                                    display_string = ('\r|{0}{1}|' +
+                                                      ' ' * 5 +
+                                                      '{2}/{3}').format(
+                                        '=' * done,
+                                        '-' * (parts - done),
+                                        *display)
+                                    # Add whitespace to cover up prior bar
+                                    display_string += ' ' * (
+                                        len(prior_display.rstrip()) -
+                                        len(display_string.rstrip()))
+
+                                    pywikibot.output(display_string,
+                                                     newline=False)
+                            pywikibot.output('')
                     elif response.status == 404:
                         pywikibot.output(
                             'File with name "{filename}", '

-- 
To view, visit https://gerrit.wikimedia.org/r/401199
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iacfc4ffec3f7b928fa245879c154775a9f692b8a
Gerrit-PatchSet: 12
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Ryan10145 <chang.ryan10...@gmail.com>
Gerrit-Reviewer: Eflyjason <eflyja...@gmail.com>
Gerrit-Reviewer: Framawiki <framaw...@tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Ryan10145 <chang.ryan10...@gmail.com>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1...@gmail.com>
Gerrit-Reviewer: Zoranzoki21 <zorandori4...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to