diff options
author | Nirbheek Chauhan <nirbheek@centricular.com> | 2021-08-17 15:58:41 +0530 |
---|---|---|
committer | GStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org> | 2021-08-17 10:55:11 +0000 |
commit | 35c69db6555c0d797a76d6f64fc69b4021aa6f43 (patch) | |
tree | 02d7ab87dfaef0b59d6f4cb60bb3da1e57cfdbe7 | |
parent | 0217c0b6793ff1430e261938d2764916b3b6d019 (diff) |
cerbero: Add a dotted progress bar for urllib downloads
Also add a 20s timeout (same as wget) so that we timeout on download
instead of timing out the entire job on CI. The global socket timeout
with CPython is apparently -1, aka never timeout.
Part-of: <https://gitlab.freedesktop.org/gstreamer/cerbero/-/merge_requests/724>
-rw-r--r-- | cerbero/utils/shell.py | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/cerbero/utils/shell.py b/cerbero/utils/shell.py index 166fc2ba..3682e214 100644 --- a/cerbero/utils/shell.py +++ b/cerbero/utils/shell.py @@ -382,6 +382,8 @@ async def download_urllib2(url, destination=None, check_cert=True, overwrite=Fal @param destination: destination where the file will be saved @type destination: str ''' + # 1MiB chunk size (same as dot:giga for wget) + chunk_size = 1024 * 1024 ctx = None if not check_cert: import ssl @@ -392,12 +394,25 @@ async def download_urllib2(url, destination=None, check_cert=True, overwrite=Fal if not destination: destination = os.path.basename(url) + from datetime import datetime + start_time = datetime.now() + try: with open(destination, 'wb') as d: req = urllib.request.Request(url) req.add_header('User-Agent', USER_AGENT) - f = urllib.request.urlopen(req, context=ctx) - d.write(f.read()) + f = urllib.request.urlopen(req, timeout=20, context=ctx) + while True: + chunk = f.read(chunk_size) + if not chunk: + break + d.write(chunk) + print('.', end='', file=logfile, flush=True) + total_time = (datetime.now() - start_time).total_seconds() + size = d.tell() / 1024 + speed = size / total_time + print('\nDownloaded {:2,.2f} KiB in {:2,.2f} seconds at {:2,.2f} KiB/s' + .format(size, total_time, speed), file=logfile, flush=True) except urllib.error.HTTPError as e: if os.path.exists(destination): os.remove(destination) |