From 59fe190f205f4175d45457d039b31b6ce5fa2011 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Thu, 5 Apr 2018 22:10:28 -0700 Subject: [PATCH] Better handling of VSCode Terminal IOError --- platformio/downloader.py | 31 +++++++++---------------------- platformio/managers/package.py | 32 ++++++++++++++++++++++++++++---- platformio/unpacker.py | 6 +++--- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/platformio/downloader.py b/platformio/downloader.py index a712c4c6..34f597b2 100644 --- a/platformio/downloader.py +++ b/platformio/downloader.py @@ -31,8 +31,15 @@ class FileDownloader(object): CHUNK_SIZE = 1024 def __init__(self, url, dest_dir=None): - self.url = url - self._request = self.init_request() + self._request = None + # make connection + self._request = requests.get( + url, + stream=True, + headers=util.get_request_defheaders(), + verify=version_info >= (2, 7, 9)) + if self._request.status_code != 200: + raise FDUnrecognizedStatusCode(self._request.status_code, url) disposition = self._request.headers.get("content-disposition") if disposition and "filename=" in disposition: @@ -48,16 +55,6 @@ class FileDownloader(object): self.set_destination( join(dest_dir.decode(getfilesystemencoding()), self._fname)) - def init_request(self): - r = requests.get( - self.url, - stream=True, - headers=util.get_request_defheaders(), - verify=version_info >= (2, 7, 9)) - if r.status_code != 200: - raise FDUnrecognizedStatusCode(r.status_code, self.url) - return r - def set_destination(self, destination): self._destination = destination @@ -87,16 +84,6 @@ class FileDownloader(object): with click.progressbar(length=chunks, label=label) as pb: for _ in pb: f.write(next(itercontent)) - except IOError as e: - if with_progress: - # reinitialize request - self._request = self.init_request() - return self.start(with_progress=False) - click.secho( - "Error: Please read http://bit.ly/package-manager-ioerror", - fg="red", - err=True) - raise e finally: f.close() self._request.close() diff --git a/platformio/managers/package.py b/platformio/managers/package.py index 83a5ba39..795f0761 100644 --- a/platformio/managers/package.py +++ b/platformio/managers/package.py @@ -177,8 +177,25 @@ class PkgInstallerMixin(object): shutil.copy(cache_path, dst_path) return dst_path - fd = FileDownloader(url, dest_dir) - fd.start(with_progress=not app.is_disabled_progressbar()) + with_progress = not app.is_disabled_progressbar() + try: + fd = FileDownloader(url, dest_dir) + fd.start(with_progress=with_progress) + except IOError as e: + raise_error = not with_progress + if with_progress: + try: + fd = FileDownloader(url, dest_dir) + fd.start(with_progress=False) + except IOError: + raise_error = True + if raise_error: + click.secho( + "Error: Please read http://bit.ly/package-manager-ioerror", + fg="red", + err=True) + raise e + if sha1: fd.verify(sha1) dst_path = fd.get_filepath() @@ -194,8 +211,15 @@ class PkgInstallerMixin(object): @staticmethod def unpack(source_path, dest_dir): - with FileUnpacker(source_path) as fu: - return fu.unpack(dest_dir) + with_progress = not app.is_disabled_progressbar() + try: + with FileUnpacker(source_path) as fu: + return fu.unpack(dest_dir, with_progress=with_progress) + except IOError as e: + if not with_progress: + raise e + with FileUnpacker(source_path) as fu: + return fu.unpack(dest_dir, with_progress=False) @staticmethod def parse_semver_spec(value, raise_exception=False): diff --git a/platformio/unpacker.py b/platformio/unpacker.py index 4e789d1c..38165c40 100644 --- a/platformio/unpacker.py +++ b/platformio/unpacker.py @@ -20,7 +20,7 @@ from zipfile import ZipFile import click -from platformio import app, util +from platformio import util from platformio.exception import UnsupportedArchiveType @@ -96,9 +96,9 @@ class FileUnpacker(object): if self._unpacker: self._unpacker.close() - def unpack(self, dest_dir="."): + def unpack(self, dest_dir=".", with_progress=True): assert self._unpacker - if app.is_disabled_progressbar(): + if not with_progress: click.echo("Unpacking...") for item in self._unpacker.get_items(): self._unpacker.extract_item(item, dest_dir)