forked from platformio/platformio-core
Improve a work in off-line mode
This commit is contained in:
@ -16,7 +16,7 @@ import json
|
|||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
from platformio.exception import APIRequestError
|
from platformio.exception import APIRequestError, InternetIsOffline
|
||||||
from platformio.managers.platform import PlatformManager
|
from platformio.managers.platform import PlatformManager
|
||||||
|
|
||||||
|
|
||||||
@ -92,10 +92,13 @@ def _get_boards(installed=False):
|
|||||||
boards = PlatformManager().get_installed_boards()
|
boards = PlatformManager().get_installed_boards()
|
||||||
if not installed:
|
if not installed:
|
||||||
know_boards = ["%s:%s" % (b['platform'], b['id']) for b in boards]
|
know_boards = ["%s:%s" % (b['platform'], b['id']) for b in boards]
|
||||||
for board in PlatformManager().get_registered_boards():
|
try:
|
||||||
key = "%s:%s" % (board['platform'], board['id'])
|
for board in PlatformManager().get_registered_boards():
|
||||||
if key not in know_boards:
|
key = "%s:%s" % (board['platform'], board['id'])
|
||||||
boards.append(board)
|
if key not in know_boards:
|
||||||
|
boards.append(board)
|
||||||
|
except InternetIsOffline:
|
||||||
|
pass
|
||||||
return boards
|
return boards
|
||||||
|
|
||||||
|
|
||||||
|
@ -151,6 +151,11 @@ class APIRequestError(PlatformioException):
|
|||||||
MESSAGE = "[API] {0}"
|
MESSAGE = "[API] {0}"
|
||||||
|
|
||||||
|
|
||||||
|
class InternetIsOffline(PlatformioException):
|
||||||
|
|
||||||
|
MESSAGE = "You are not connected to the Internet"
|
||||||
|
|
||||||
|
|
||||||
class LibNotFound(PlatformioException):
|
class LibNotFound(PlatformioException):
|
||||||
|
|
||||||
MESSAGE = "Library `{0}` has not been found in PlatformIO Registry.\n"\
|
MESSAGE = "Library `{0}` has not been found in PlatformIO Registry.\n"\
|
||||||
|
@ -209,13 +209,24 @@ class LibraryManager(BasePkgManager):
|
|||||||
silent=False,
|
silent=False,
|
||||||
trigger_event=True,
|
trigger_event=True,
|
||||||
interactive=False):
|
interactive=False):
|
||||||
|
already_installed = False
|
||||||
_name, _requirements, _url = self.parse_pkg_name(name, requirements)
|
_name, _requirements, _url = self.parse_pkg_name(name, requirements)
|
||||||
if not _url:
|
|
||||||
_name = "id=%d" % self._get_pkg_id_by_name(
|
try:
|
||||||
_name, _requirements, silent=silent, interactive=interactive)
|
if not _url:
|
||||||
already_installed = self.get_package(_name, _requirements, _url)
|
_name = "id=%d" % self._get_pkg_id_by_name(
|
||||||
pkg_dir = BasePkgManager.install(self, _name if not _url else name,
|
_name,
|
||||||
_requirements, silent, trigger_event)
|
_requirements,
|
||||||
|
silent=silent,
|
||||||
|
interactive=interactive)
|
||||||
|
already_installed = self.get_package(_name, _requirements, _url)
|
||||||
|
pkg_dir = BasePkgManager.install(
|
||||||
|
self, _name
|
||||||
|
if not _url else name, _requirements, silent, trigger_event)
|
||||||
|
except exception.InternetIsOffline as e:
|
||||||
|
if not silent:
|
||||||
|
click.secho(str(e), fg="yellow")
|
||||||
|
return
|
||||||
|
|
||||||
if already_installed:
|
if already_installed:
|
||||||
return
|
return
|
||||||
|
@ -542,7 +542,9 @@ class BasePkgManager(PkgRepoMixin, PkgInstallerMixin):
|
|||||||
except exception.PlatformioException:
|
except exception.PlatformioException:
|
||||||
pass
|
pass
|
||||||
if not latest_version:
|
if not latest_version:
|
||||||
click.echo("[%s]" % (click.style("Unknown", fg="yellow")))
|
click.echo("[%s]" % (click.style(
|
||||||
|
"Off-line" if not util.internet_on() else "Unknown",
|
||||||
|
fg="yellow")))
|
||||||
return
|
return
|
||||||
|
|
||||||
up_to_date = False
|
up_to_date = False
|
||||||
|
@ -258,9 +258,11 @@ def on_event(category, action, label=None, value=None, screen_name=None):
|
|||||||
|
|
||||||
|
|
||||||
def on_exception(e):
|
def on_exception(e):
|
||||||
if any([isinstance(e, cls)
|
skip = any(
|
||||||
for cls in (IOError, exception.AbortedByUser,
|
[isinstance(e, cls)
|
||||||
exception.NotGlobalLibDir)]):
|
for cls in (IOError, exception.AbortedByUser,
|
||||||
|
exception.NotGlobalLibDir, exception.InternetIsOffline)])
|
||||||
|
if skip:
|
||||||
return
|
return
|
||||||
is_crash = any([
|
is_crash = any([
|
||||||
not isinstance(e, exception.PlatformioException),
|
not isinstance(e, exception.PlatformioException),
|
||||||
|
@ -17,6 +17,7 @@ import functools
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import socket
|
||||||
import stat
|
import stat
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
@ -452,6 +453,8 @@ def get_api_result(path, params=None, data=None, cache_valid=None):
|
|||||||
return result
|
return result
|
||||||
except (requests.exceptions.ConnectionError,
|
except (requests.exceptions.ConnectionError,
|
||||||
requests.exceptions.Timeout) as e:
|
requests.exceptions.Timeout) as e:
|
||||||
|
if not internet_on():
|
||||||
|
raise exception.InternetIsOffline()
|
||||||
from platformio.maintenance import in_silence
|
from platformio.maintenance import in_silence
|
||||||
total += 1
|
total += 1
|
||||||
if not in_silence():
|
if not in_silence():
|
||||||
@ -466,6 +469,17 @@ def get_api_result(path, params=None, data=None, cache_valid=None):
|
|||||||
"Please try later.")
|
"Please try later.")
|
||||||
|
|
||||||
|
|
||||||
|
def internet_on(timeout=3):
|
||||||
|
host = "8.8.8.8"
|
||||||
|
port = 53
|
||||||
|
try:
|
||||||
|
socket.setdefaulttimeout(timeout)
|
||||||
|
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
|
||||||
|
return True
|
||||||
|
except: # pylint: disable=bare-except
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_pythonexe_path():
|
def get_pythonexe_path():
|
||||||
return os.environ.get("PYTHONEXEPATH", normpath(sys.executable))
|
return os.environ.get("PYTHONEXEPATH", normpath(sys.executable))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user