From 2a706a751a5211867fa3ac6614ffe3fc55c8862f Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 2 Mar 2018 18:24:05 +0100 Subject: [PATCH] update trezorctl to use prefix search correctly first, try exact path second, try prefix search last, fail :) with reporting used path (for people like me who forget to unset TREZOR_PATH --- trezorctl | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/trezorctl b/trezorctl index 7646d44395..ab3d03a2be 100755 --- a/trezorctl +++ b/trezorctl @@ -29,7 +29,7 @@ import os import sys from trezorlib.client import TrezorClient, TrezorClientVerbose, CallException -from trezorlib.device import TrezorDevice +from trezorlib.transport import get_transport, enumerate_devices, TransportException from trezorlib import messages as proto from trezorlib import protobuf from trezorlib.coins import coins_txapi @@ -72,9 +72,24 @@ CHOICE_OUTPUT_SCRIPT_TYPE = ChoiceType({ def cli(ctx, path, verbose, is_json): if ctx.invoked_subcommand != 'list': if verbose: - ctx.obj = lambda: TrezorClientVerbose(TrezorDevice.find_by_path(path)) + cls = TrezorClientVerbose else: - ctx.obj = lambda: TrezorClient(TrezorDevice.find_by_path(path)) + cls = TrezorClient + + def get_device(): + try: + device = get_transport(path, prefix_search=False) + except: + try: + device = get_transport(path, prefix_search=True) + except: + click.echo("Failed to find a TREZOR device.") + if path is not None: + click.echo("Using path: {}".format(path)) + sys.exit(1) + return cls(device) + + ctx.obj = get_device @cli.resultcallback() @@ -106,7 +121,7 @@ def print_result(res, path, verbose, is_json): @cli.command(name='list', help='List connected TREZOR devices.') def ls(): - return TrezorDevice.enumerate() + return enumerate_devices() @cli.command(help='Show version of trezorctl/trezorlib.')