Better handling of VSCode Terminal IOError

This commit is contained in:
Ivan Kravets
2018-04-05 22:10:28 -07:00
parent e0fc44aa42
commit 59fe190f20
3 changed files with 40 additions and 29 deletions

View File

@ -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()

View File

@ -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):

View File

@ -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)