Improve downloading progress for non-terminal stream

This commit is contained in:
Ivan Kravets
2022-08-03 17:32:40 +03:00
parent 8ada1c2b34
commit 83c4e5f463
2 changed files with 20 additions and 22 deletions

View File

@ -226,12 +226,7 @@ def set_session_var(name, value):
def is_disabled_progressbar(): def is_disabled_progressbar():
return any( return os.getenv("PLATFORMIO_DISABLE_PROGRESSBAR") == "true"
[
proc.is_ci(),
os.getenv("PLATFORMIO_DISABLE_PROGRESSBAR") == "true",
]
)
def get_cid(): def get_cid():

View File

@ -13,8 +13,7 @@
# limitations under the License. # limitations under the License.
import io import io
import math from email.utils import parsedate
from email.utils import parsedate_tz
from os.path import getsize, join from os.path import getsize, join
from time import mktime from time import mktime
@ -72,22 +71,27 @@ class FileDownloader:
def start(self, with_progress=True, silent=False): def start(self, with_progress=True, silent=False):
label = "Downloading" label = "Downloading"
file_size = self.get_size()
itercontent = self._request.iter_content(chunk_size=io.DEFAULT_BUFFER_SIZE) itercontent = self._request.iter_content(chunk_size=io.DEFAULT_BUFFER_SIZE)
fp = open(self._destination, "wb") # pylint: disable=consider-using-with
try: try:
if not with_progress or self.get_size() == -1: with open(self._destination, "wb") as fp:
if not silent: if not with_progress or file_size == -1:
click.echo("%s..." % label) if not silent:
for chunk in itercontent: click.echo("%s..." % label)
if chunk: for chunk in itercontent:
fp.write(chunk) fp.write(chunk)
else: else:
chunks = int(math.ceil(self.get_size() / float(io.DEFAULT_BUFFER_SIZE))) pb = click.progressbar(
with click.progressbar(length=chunks, label=label) as pb: length=file_size,
for _ in pb: iterable=itercontent,
fp.write(next(itercontent)) label=label,
)
pb.update_min_steps = file_size / (5 if pb.is_hidden else 100)
with pb:
for chunk in pb:
pb.update(len(chunk))
fp.write(chunk)
finally: finally:
fp.close()
self._request.close() self._request.close()
if self.get_lmtime(): if self.get_lmtime():
@ -132,8 +136,7 @@ class FileDownloader:
return True return True
def _preserve_filemtime(self, lmdate): def _preserve_filemtime(self, lmdate):
timedata = parsedate_tz(lmdate) lmtime = mktime(parsedate(lmdate))
lmtime = mktime(timedata[:9])
fs.change_filemtime(self._destination, lmtime) fs.change_filemtime(self._destination, lmtime)
def __del__(self): def __del__(self):