transport: fix all_transports when required modules are missing (#232)

This lets the library work without libusb or hidapi (`--disable-libusb`, `--disable-hidapi`).
This commit is contained in:
matejcik 2018-03-09 10:58:24 +01:00 committed by GitHub
parent 89eac8f157
commit 5edcea9ba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 6 deletions

View File

@ -1,6 +1,6 @@
[tox] [tox]
envlist = envlist =
py27, py33,
py34, py34,
py35, py35,
py36, py36,
@ -9,6 +9,7 @@ envlist =
deps = deps =
-rrequirements.txt -rrequirements.txt
pytest pytest
mock
commands = commands =
python -m compileall trezorlib/ python -m compileall trezorlib/
python trezorctl --help python trezorctl --help

View File

@ -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)

View File

@ -64,11 +64,32 @@ class Transport(object):
def all_transports(): def all_transports():
from .bridge import BridgeTransport transports = []
from .hid import HidTransport try:
from .udp import UdpTransport from .bridge import BridgeTransport
from .webusb import WebUsbTransport transports.append(BridgeTransport)
return (BridgeTransport, HidTransport, UdpTransport, WebUsbTransport) 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(): def enumerate_devices():