Skip to content

Commit

Permalink
Merge pull request #330 from AlbertDeFusco/progress
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertDeFusco committed Jun 30, 2021
2 parents dc5fae3 + 9faaedf commit 3394ac6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions anaconda_project/internal/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from tornado import httpclient
from tornado import gen
from tqdm import tqdm

import anaconda_project.internal.makedirs as makedirs
import anaconda_project.internal.rename as rename
Expand All @@ -29,6 +30,7 @@ def __init__(self, url, filename, hash_algorithm=None):
self._hash = None
self._client = None
self._errors = []
self._progress = None

@gen.coroutine
def run(self):
Expand Down Expand Up @@ -83,22 +85,35 @@ def writer(chunk):

try:
_file.write(chunk)
self._progress.update(len(chunk) / 1024 / 1024)
except EnvironmentError as e:
# we can't actually throw this error or Tornado freaks out, so instead
# we ignore all future chunks once we have an error, which does mean
# we continue to download bytes that we don't use. yuck.
self._errors.append("Failed to write to %s: %s" % (tmp_filename, e))

def read_header(line):
if 'content-length' in line.lower():
file_size = int(line.split(':')[1]) / 1024 / 1024
self._progress = tqdm(unit='MiB',
total=file_size,
unit_scale=True,
desc=os.path.basename(self._filename))

try:
timeout_in_seconds = 60 * 10 # pretty long because we could be dealing with huge files
request = httpclient.HTTPRequest(url=self._url,
header_callback=read_header,
streaming_callback=writer,
request_timeout=timeout_in_seconds)
try:
response = yield self._client.fetch(request)
except Exception as e:
self._errors.append("Failed download to %s: %s" % (self._filename, str(e)))
raise gen.Return(None)
finally:
if self._progress is not None:
self._progress.close()

# assert fetch() was supposed to throw the error, not leave it here unthrown
assert response.error is None
Expand Down
1 change: 1 addition & 0 deletions conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ requirements:
- ruamel_yaml
- tornado >=4.2
- jinja2
- tqdm

test:
requires:
Expand Down

0 comments on commit 3394ac6

Please sign in to comment.