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
|
||||
|
||||
from platformio.exception import APIRequestError
|
||||
from platformio.exception import APIRequestError, InternetIsOffline
|
||||
from platformio.managers.platform import PlatformManager
|
||||
|
||||
|
||||
@ -92,10 +92,13 @@ def _get_boards(installed=False):
|
||||
boards = PlatformManager().get_installed_boards()
|
||||
if not installed:
|
||||
know_boards = ["%s:%s" % (b['platform'], b['id']) for b in boards]
|
||||
for board in PlatformManager().get_registered_boards():
|
||||
key = "%s:%s" % (board['platform'], board['id'])
|
||||
if key not in know_boards:
|
||||
boards.append(board)
|
||||
try:
|
||||
for board in PlatformManager().get_registered_boards():
|
||||
key = "%s:%s" % (board['platform'], board['id'])
|
||||
if key not in know_boards:
|
||||
boards.append(board)
|
||||
except InternetIsOffline:
|
||||
pass
|
||||
return boards
|
||||
|
||||
|
||||
|
@ -151,6 +151,11 @@ class APIRequestError(PlatformioException):
|
||||
MESSAGE = "[API] {0}"
|
||||
|
||||
|
||||
class InternetIsOffline(PlatformioException):
|
||||
|
||||
MESSAGE = "You are not connected to the Internet"
|
||||
|
||||
|
||||
class LibNotFound(PlatformioException):
|
||||
|
||||
MESSAGE = "Library `{0}` has not been found in PlatformIO Registry.\n"\
|
||||
|
@ -209,13 +209,24 @@ class LibraryManager(BasePkgManager):
|
||||
silent=False,
|
||||
trigger_event=True,
|
||||
interactive=False):
|
||||
already_installed = False
|
||||
_name, _requirements, _url = self.parse_pkg_name(name, requirements)
|
||||
if not _url:
|
||||
_name = "id=%d" % self._get_pkg_id_by_name(
|
||||
_name, _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)
|
||||
|
||||
try:
|
||||
if not _url:
|
||||
_name = "id=%d" % self._get_pkg_id_by_name(
|
||||
_name,
|
||||
_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:
|
||||
return
|
||||
|
@ -542,7 +542,9 @@ class BasePkgManager(PkgRepoMixin, PkgInstallerMixin):
|
||||
except exception.PlatformioException:
|
||||
pass
|
||||
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
|
||||
|
||||
up_to_date = False
|
||||
|
@ -258,9 +258,11 @@ def on_event(category, action, label=None, value=None, screen_name=None):
|
||||
|
||||
|
||||
def on_exception(e):
|
||||
if any([isinstance(e, cls)
|
||||
for cls in (IOError, exception.AbortedByUser,
|
||||
exception.NotGlobalLibDir)]):
|
||||
skip = any(
|
||||
[isinstance(e, cls)
|
||||
for cls in (IOError, exception.AbortedByUser,
|
||||
exception.NotGlobalLibDir, exception.InternetIsOffline)])
|
||||
if skip:
|
||||
return
|
||||
is_crash = any([
|
||||
not isinstance(e, exception.PlatformioException),
|
||||
|
@ -17,6 +17,7 @@ import functools
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import socket
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
@ -452,6 +453,8 @@ def get_api_result(path, params=None, data=None, cache_valid=None):
|
||||
return result
|
||||
except (requests.exceptions.ConnectionError,
|
||||
requests.exceptions.Timeout) as e:
|
||||
if not internet_on():
|
||||
raise exception.InternetIsOffline()
|
||||
from platformio.maintenance import in_silence
|
||||
total += 1
|
||||
if not in_silence():
|
||||
@ -466,6 +469,17 @@ def get_api_result(path, params=None, data=None, cache_valid=None):
|
||||
"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():
|
||||
return os.environ.get("PYTHONEXEPATH", normpath(sys.executable))
|
||||
|
||||
|
Reference in New Issue
Block a user