From 5edcea9ba6cca37f8265b47fb8fb8ba84ec3d39a Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 9 Mar 2018 10:58:24 +0100 Subject: [PATCH] transport: fix all_transports when required modules are missing (#232) This lets the library work without libusb or hidapi (`--disable-libusb`, `--disable-hidapi`). --- tox.ini | 3 +- trezorlib/tests/unit_tests/test_transport.py | 13 ++++++++ trezorlib/transport/__init__.py | 31 ++++++++++++++++---- 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 trezorlib/tests/unit_tests/test_transport.py diff --git a/tox.ini b/tox.ini index d50be28a92..d2578fd9ab 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] envlist = - py27, + py33, py34, py35, py36, @@ -9,6 +9,7 @@ envlist = deps = -rrequirements.txt pytest + mock commands = python -m compileall trezorlib/ python trezorctl --help diff --git a/trezorlib/tests/unit_tests/test_transport.py b/trezorlib/tests/unit_tests/test_transport.py new file mode 100644 index 0000000000..9b5c31ab93 --- /dev/null +++ b/trezorlib/tests/unit_tests/test_transport.py @@ -0,0 +1,13 @@ +import mock + +from trezorlib.transport import all_transports + + +def test_all_transports_without_hid(): + # import all transports, assume this doesn't fail + transports_ref = all_transports() + # also shouldn't fail when bridge transport is missing + with mock.patch.dict('sys.modules', {'trezorlib.transport.bridge': None}): + transports = all_transports() + # there should now be less transports + assert len(transports_ref) > len(transports) diff --git a/trezorlib/transport/__init__.py b/trezorlib/transport/__init__.py index fb6d4aa32b..b720c2e3d4 100644 --- a/trezorlib/transport/__init__.py +++ b/trezorlib/transport/__init__.py @@ -64,11 +64,32 @@ class Transport(object): def all_transports(): - from .bridge import BridgeTransport - from .hid import HidTransport - from .udp import UdpTransport - from .webusb import WebUsbTransport - return (BridgeTransport, HidTransport, UdpTransport, WebUsbTransport) + transports = [] + try: + from .bridge import BridgeTransport + transports.append(BridgeTransport) + except: + pass + + try: + from .hid import HidTransport + transports.append(HidTransport) + except: + pass + + try: + from .udp import UdpTransport + transports.append(UdpTransport) + except: + pass + + try: + from .webusb import WebUsbTransport + transports.append(WebUsbTransport) + except: + pass + + return transports def enumerate_devices():