hw: check_libraries_available now gets version of incompatible libs

previously we would return early and the user would
just see "missing libraries"
This commit is contained in:
SomberNight 2018-12-06 18:16:34 +01:00
parent 1546d65ebe
commit 8c3920a0db
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
3 changed files with 34 additions and 20 deletions

View file

@ -17,6 +17,7 @@ from electrum.util import print_error, bfh, bh2u, versiontuple, UserFacingExcept
from electrum.base_wizard import ScriptTypeNotSupported from electrum.base_wizard import ScriptTypeNotSupported
from ..hw_wallet import HW_PluginBase from ..hw_wallet import HW_PluginBase
from ..hw_wallet.plugin import LibraryFoundButUnusable
try: try:
import hid import hid
@ -610,7 +611,7 @@ class ColdcardPlugin(HW_PluginBase):
def __init__(self, parent, config, name): def __init__(self, parent, config, name):
HW_PluginBase.__init__(self, parent, config, name) HW_PluginBase.__init__(self, parent, config, name)
self.libraries_available = self.check_libraries_available() and requirements_ok self.libraries_available = self.check_libraries_available()
if not self.libraries_available: if not self.libraries_available:
return return
@ -620,9 +621,13 @@ class ColdcardPlugin(HW_PluginBase):
def get_library_version(self): def get_library_version(self):
import ckcc import ckcc
try: try:
return ckcc.__version__ version = ckcc.__version__
except AttributeError: except AttributeError:
return 'unknown' version = 'unknown'
if requirements_ok:
return version
else:
raise LibraryFoundButUnusable(library_version=version)
def detect_simulator(self): def detect_simulator(self):
# if there is a simulator running on this machine, # if there is a simulator running on this machine,

View file

@ -86,6 +86,7 @@ class HW_PluginBase(BasePlugin):
Returns 'unknown' if library is found but cannot determine version. Returns 'unknown' if library is found but cannot determine version.
Raises 'ImportError' if library is not found. Raises 'ImportError' if library is not found.
Raises 'LibraryFoundButUnusable' if found but there was some problem (includes version num).
""" """
raise NotImplementedError() raise NotImplementedError()
@ -94,23 +95,22 @@ class HW_PluginBase(BasePlugin):
return ".".join(str(i) for i in t) return ".".join(str(i) for i in t)
try: try:
# this might raise ImportError or LibraryFoundButUnusable
library_version = self.get_library_version() library_version = self.get_library_version()
# if no exception so far, we might still raise LibraryFoundButUnusable
if (library_version == 'unknown'
or versiontuple(library_version) < self.minimum_library
or hasattr(self, "maximum_library") and versiontuple(library_version) >= self.maximum_library):
raise LibraryFoundButUnusable(library_version=library_version)
except ImportError: except ImportError:
return False return False
if library_version == 'unknown' or \ except LibraryFoundButUnusable as e:
versiontuple(library_version) < self.minimum_library: library_version = e.library_version
self.libraries_available_message = ( max_version_str = version_str(self.maximum_library) if hasattr(self, "maximum_library") else "inf"
_("Library version for '{}' is too old.").format(self.name)
+ '\nInstalled: {}, Needed: {}'
.format(library_version, version_str(self.minimum_library)))
self.print_stderr(self.libraries_available_message)
return False
elif hasattr(self, "maximum_library") and \
versiontuple(library_version) >= self.maximum_library:
self.libraries_available_message = ( self.libraries_available_message = (
_("Library version for '{}' is incompatible.").format(self.name) _("Library version for '{}' is incompatible.").format(self.name)
+ '\nInstalled: {}, Needed: less than {}' + '\nInstalled: {}, Needed: {} <= x < {}'
.format(library_version, version_str(self.maximum_library))) .format(library_version, version_str(self.minimum_library), max_version_str))
self.print_stderr(self.libraries_available_message) self.print_stderr(self.libraries_available_message)
return False return False
@ -155,3 +155,8 @@ def only_hook_if_libraries_available(func):
if not self.libraries_available: return None if not self.libraries_available: return None
return func(self, *args, **kwargs) return func(self, *args, **kwargs)
return wrapper return wrapper
class LibraryFoundButUnusable(Exception):
def __init__(self, library_version='unknown'):
self.library_version = library_version

View file

@ -12,7 +12,8 @@ from electrum.keystore import Hardware_KeyStore, is_xpubkey, parse_xpubkey
from electrum.base_wizard import ScriptTypeNotSupported, HWD_SETUP_NEW_WALLET from electrum.base_wizard import ScriptTypeNotSupported, HWD_SETUP_NEW_WALLET
from ..hw_wallet import HW_PluginBase from ..hw_wallet import HW_PluginBase
from ..hw_wallet.plugin import is_any_tx_output_on_change_branch, trezor_validate_op_return_output_and_get_data from ..hw_wallet.plugin import (is_any_tx_output_on_change_branch, trezor_validate_op_return_output_and_get_data,
LibraryFoundButUnusable)
try: try:
import trezorlib import trezorlib
@ -112,12 +113,15 @@ class TrezorPlugin(HW_PluginBase):
self.device_manager().register_enumerate_func(self.enumerate) self.device_manager().register_enumerate_func(self.enumerate)
def get_library_version(self): def get_library_version(self):
if not TREZORLIB: import trezorlib
raise ImportError
try: try:
return trezorlib.__version__ version = trezorlib.__version__
except Exception: except Exception:
return 'unknown' version = 'unknown'
if TREZORLIB:
return version
else:
raise LibraryFoundButUnusable(library_version=version)
def enumerate(self): def enumerate(self):
devices = trezorlib.transport.enumerate_devices() devices = trezorlib.transport.enumerate_devices()