]> glassweightruler.freedombox.rocks Git - waydroid.git/commitdiff
upgrade: show progress
authorNbiba Bedis <bedisnbiba@gmail.com>
Sat, 25 Sep 2021 17:56:44 +0000 (18:56 +0100)
committerErfan Abdi <erfangplus@gmail.com>
Tue, 28 Sep 2021 06:37:17 +0000 (10:07 +0330)
tools/helpers/http.py

index c63d665719c93cb14a0b81f8297a3ba4c4b6cae2..77ff719171987f21026f9d82478ce872f0cfe33f 100644 (file)
@@ -4,9 +4,11 @@ import hashlib
 import logging
 import os
 import shutil
 import logging
 import os
 import shutil
+import threading
 import urllib.request
 
 import tools.helpers.run
 import urllib.request
 
 import tools.helpers.run
+import time
 
 
 def download(args, url, prefix, cache=True, loglevel=logging.INFO,
 
 
 def download(args, url, prefix, cache=True, loglevel=logging.INFO,
@@ -25,6 +27,14 @@ def download(args, url, prefix, cache=True, loglevel=logging.INFO,
                           with a 404 Not Found error. Only display a warning on
                           stdout (no matter if loglevel is changed).
         :returns: path to the downloaded file in the cache or None on 404 """
                           with a 404 Not Found error. Only display a warning on
                           stdout (no matter if loglevel is changed).
         :returns: path to the downloaded file in the cache or None on 404 """
+
+    # Show progress while downloading
+    downloadEnded = False
+    def progress(totalSize, destinationPath):
+        while not downloadEnded:
+            print("[Downloading] {}/{}".format(os.path.getsize(destinationPath), totalSize), end='\r')
+            time.sleep(.01)
+
     # Create cache folder
     if not os.path.exists(args.work + "/cache_http"):
         tools.helpers.run.user(args, ["mkdir", "-p", args.work + "/cache_http"])
     # Create cache folder
     if not os.path.exists(args.work + "/cache_http"):
         tools.helpers.run.user(args, ["mkdir", "-p", args.work + "/cache_http"])
@@ -43,6 +53,7 @@ def download(args, url, prefix, cache=True, loglevel=logging.INFO,
     try:
         with urllib.request.urlopen(url) as response:
             with open(path, "wb") as handle:
     try:
         with urllib.request.urlopen(url) as response:
             with open(path, "wb") as handle:
+                threading.Thread(target=progress, args=(response.headers.get('content-length'), path)).start()
                 shutil.copyfileobj(response, handle)
     # Handle 404
     except urllib.error.HTTPError as e:
                 shutil.copyfileobj(response, handle)
     # Handle 404
     except urllib.error.HTTPError as e:
@@ -50,6 +61,7 @@ def download(args, url, prefix, cache=True, loglevel=logging.INFO,
             logging.warning("WARNING: file not found: " + url)
             return None
         raise
             logging.warning("WARNING: file not found: " + url)
             return None
         raise
+    downloadEnded = True
 
     # Return path in cache
     return path
 
     # Return path in cache
     return path