Use io.DEFAULT_BUFFER_SIZE as default chunk size

This commit is contained in:
Ivan Kravets
2020-01-24 22:05:24 +02:00
parent 253e4b13b5
commit decf482367

View File

@ -32,9 +32,6 @@ from platformio.exception import (
class FileDownloader(object): class FileDownloader(object):
CHUNK_SIZE = 1024 * 8
def __init__(self, url, dest_dir=None): def __init__(self, url, dest_dir=None):
self._request = None self._request = None
# make connection # make connection
@ -77,7 +74,7 @@ class FileDownloader(object):
def start(self, with_progress=True, silent=False): def start(self, with_progress=True, silent=False):
label = "Downloading" label = "Downloading"
itercontent = self._request.iter_content(chunk_size=self.CHUNK_SIZE) itercontent = self._request.iter_content(chunk_size=io.DEFAULT_BUFFER_SIZE)
f = open(self._destination, "wb") f = open(self._destination, "wb")
try: try:
if not with_progress or self.get_size() == -1: if not with_progress or self.get_size() == -1:
@ -87,7 +84,7 @@ class FileDownloader(object):
if chunk: if chunk:
f.write(chunk) f.write(chunk)
else: else:
chunks = int(math.ceil(self.get_size() / float(self.CHUNK_SIZE))) chunks = int(math.ceil(self.get_size() / float(io.DEFAULT_BUFFER_SIZE)))
with click.progressbar(length=chunks, label=label) as pb: with click.progressbar(length=chunks, label=label) as pb:
for _ in pb: for _ in pb:
f.write(next(itercontent)) f.write(next(itercontent))
@ -110,7 +107,7 @@ class FileDownloader(object):
checksum = hashlib.sha1() checksum = hashlib.sha1()
with io.open(self._destination, "rb", buffering=0) as fp: with io.open(self._destination, "rb", buffering=0) as fp:
while True: while True:
chunk = fp.read(self.CHUNK_SIZE) chunk = fp.read(io.DEFAULT_BUFFER_SIZE)
if not chunk: if not chunk:
break break
checksum.update(chunk) checksum.update(chunk)