mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
Better handling of VSCode Terminal IOError
This commit is contained in:
@ -31,8 +31,15 @@ class FileDownloader(object):
|
|||||||
CHUNK_SIZE = 1024
|
CHUNK_SIZE = 1024
|
||||||
|
|
||||||
def __init__(self, url, dest_dir=None):
|
def __init__(self, url, dest_dir=None):
|
||||||
self.url = url
|
self._request = None
|
||||||
self._request = self.init_request()
|
# 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")
|
disposition = self._request.headers.get("content-disposition")
|
||||||
if disposition and "filename=" in disposition:
|
if disposition and "filename=" in disposition:
|
||||||
@ -48,16 +55,6 @@ class FileDownloader(object):
|
|||||||
self.set_destination(
|
self.set_destination(
|
||||||
join(dest_dir.decode(getfilesystemencoding()), self._fname))
|
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):
|
def set_destination(self, destination):
|
||||||
self._destination = destination
|
self._destination = destination
|
||||||
|
|
||||||
@ -87,16 +84,6 @@ class FileDownloader(object):
|
|||||||
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))
|
||||||
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:
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
self._request.close()
|
self._request.close()
|
||||||
|
@ -177,8 +177,25 @@ class PkgInstallerMixin(object):
|
|||||||
shutil.copy(cache_path, dst_path)
|
shutil.copy(cache_path, dst_path)
|
||||||
return dst_path
|
return dst_path
|
||||||
|
|
||||||
fd = FileDownloader(url, dest_dir)
|
with_progress = not app.is_disabled_progressbar()
|
||||||
fd.start(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:
|
if sha1:
|
||||||
fd.verify(sha1)
|
fd.verify(sha1)
|
||||||
dst_path = fd.get_filepath()
|
dst_path = fd.get_filepath()
|
||||||
@ -194,8 +211,15 @@ class PkgInstallerMixin(object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def unpack(source_path, dest_dir):
|
def unpack(source_path, dest_dir):
|
||||||
with FileUnpacker(source_path) as fu:
|
with_progress = not app.is_disabled_progressbar()
|
||||||
return fu.unpack(dest_dir)
|
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
|
@staticmethod
|
||||||
def parse_semver_spec(value, raise_exception=False):
|
def parse_semver_spec(value, raise_exception=False):
|
||||||
|
@ -20,7 +20,7 @@ from zipfile import ZipFile
|
|||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
from platformio import app, util
|
from platformio import util
|
||||||
from platformio.exception import UnsupportedArchiveType
|
from platformio.exception import UnsupportedArchiveType
|
||||||
|
|
||||||
|
|
||||||
@ -96,9 +96,9 @@ class FileUnpacker(object):
|
|||||||
if self._unpacker:
|
if self._unpacker:
|
||||||
self._unpacker.close()
|
self._unpacker.close()
|
||||||
|
|
||||||
def unpack(self, dest_dir="."):
|
def unpack(self, dest_dir=".", with_progress=True):
|
||||||
assert self._unpacker
|
assert self._unpacker
|
||||||
if app.is_disabled_progressbar():
|
if not with_progress:
|
||||||
click.echo("Unpacking...")
|
click.echo("Unpacking...")
|
||||||
for item in self._unpacker.get_items():
|
for item in self._unpacker.get_items():
|
||||||
self._unpacker.extract_item(item, dest_dir)
|
self._unpacker.extract_item(item, dest_dir)
|
||||||
|
Reference in New Issue
Block a user