@ -1,6 +1,7 @@
import datetime
import o s
import sy s
import time
from subprocess import run
class Device :
@ -8,14 +9,28 @@ class Device:
self . uhub_location = uhub_location
self . device_port = device_port
def run_trezorctl ( self , cmd : str ) :
@staticmethod
def log ( msg ) :
print ( msg , flush = True , file = sys . stderr )
def run_trezorctl ( self , cmd : str , * * kwargs ) :
full_cmd = " trezorctl "
full_cmd + = cmd
print ( " [software/trezorctl] Running ' {} ' " . format ( full_cmd ) )
os . system ( full_cmd )
self . log ( " [software/trezorctl] Running ' {} ' " . format ( full_cmd ) )
return run ( full_cmd , shell = True , check = True , * * kwargs )
def check_version ( self ) :
def check_model ( self , model = None ) :
res = self . run_trezorctl ( " list " , capture_output = True , text = True ) . stdout
self . log ( res )
self . run_trezorctl ( " get-features | grep version " )
lines = res . splitlines ( )
if len ( lines ) != 1 :
raise RuntimeError ( " {} trezors connected " . format ( len ( lines ) ) )
if model and model not in lines [ 0 ] :
raise RuntimeError (
" invalid trezor model connected (expected {} ) " . format ( model )
)
return lines [ 0 ] . split ( ) [ 0 ]
def reboot ( self ) :
self . power_off ( )
@ -23,19 +38,23 @@ class Device:
def power_on ( self ) :
self . now ( )
print ( " [hardware/usb] Turning power on... " )
os . system (
" uhubctl -l {} -p {} -a on " . format ( self . uhub_location , self . device_port )
self . log ( " [hardware/usb] Turning power on... " )
run (
" uhubctl -l {} -p {} -a on " . format ( self . uhub_location , self . device_port ) ,
shell = True ,
check = True ,
)
self . wait ( 3 )
def power_off ( self ) :
self . now ( )
print ( " [hardware/usb] Turning power off... " )
os. system (
self . log ( " [hardware/usb] Turning power off... " )
run (
" uhubctl -l {} -p {} -r 100 -a off " . format (
self . uhub_location , self . device_port
)
) ,
shell = True ,
check = True ,
)
self . wait ( 3 )
@ -45,9 +64,9 @@ class Device:
@staticmethod
def wait ( seconds ) :
Device . now ( )
print ( " [software] Waiting for {} seconds... " . format ( seconds ) )
Device . log ( " [software] Waiting for {} seconds... " . format ( seconds ) )
time . sleep ( seconds )
@staticmethod
def now ( ) :
print ( " \n [timestamp] {} " . format ( datetime . datetime . now ( ) ) )
Device . log ( " \n [timestamp] {} " . format ( datetime . datetime . now ( ) ) )