From 6e0519dc1224a32d040108ffe93f69683c56e40f Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 6 Jan 2022 20:24:22 +0000 Subject: [PATCH] led.py, test.py autosign: improve LED simulation code --- mmgen/led.py | 28 +++++++++------------------- mmgen/main_autosign.py | 4 +++- test/test_py_d/ts_autosign.py | 19 ++++++++++++------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/mmgen/led.py b/mmgen/led.py index 482f9f53..74e2a37e 100755 --- a/mmgen/led.py +++ b/mmgen/led.py @@ -53,7 +53,7 @@ class LEDControl: def __init__(self,enabled,simulate=False,debug=False): self.enabled = enabled - self.debug = debug + self.debug = debug or simulate if not enabled: self.set = self.stop = self.noop @@ -62,11 +62,9 @@ class LEDControl: self.ev = threading.Event() self.led_thread = None - if simulate: - type(self).create_dummy_control_files() - self.debug = True - for board_id,board in self.boards.items(): + if board_id == 'dummy' and not simulate: + continue try: os.stat(board.status) except: pass else: break @@ -82,20 +80,6 @@ class LEDControl: Trigger file: {board.trigger} """,indent=' ',strip_char='\t')) - if board_id == 'dummy' and not simulate: - if g.test_suite: - msg('Warning: no simulation requested but dummy LED control files detected') - self.debug = True - else: - die(1,fmt(f""" - No simulation requested but dummy LED control files detected: - - {board.status} - {board.trigger} - - You may wish to remove them and restart - """,indent=' ',strip_char='\t')) - def check_access(fn,desc,init_val=None): try: if not init_val: @@ -127,6 +111,12 @@ class LEDControl: with open(db.trigger,'w') as fp: fp.write(db.trigger_states[1]+'\n') + @classmethod + def delete_dummy_control_files(cls): + db = cls.boards['dummy'] + os.unlink(db.status) + os.unlink(db.trigger) + def noop(self,*args,**kwargs): pass def ev_sleep(self,secs): diff --git a/mmgen/main_autosign.py b/mmgen/main_autosign.py index 58b7be0e..53fc5cd2 100755 --- a/mmgen/main_autosign.py +++ b/mmgen/main_autosign.py @@ -411,7 +411,9 @@ signal.signal(signal.SIGTERM,handler) signal.signal(signal.SIGINT,handler) from .led import LEDControl -led = LEDControl(enabled=opt.led,simulate=g.test_suite and not os.getenv('MMGEN_TEST_SUITE_AUTOSIGN_LIVE')) +led = LEDControl( + enabled = opt.led, + simulate = os.getenv('MMGEN_TEST_SUITE_AUTOSIGN_LED_SIMULATE') ) led.set('off') async def main(): diff --git a/test/test_py_d/ts_autosign.py b/test/test_py_d/ts_autosign.py index 876c786f..166a49a3 100755 --- a/test/test_py_d/ts_autosign.py +++ b/test/test_py_d/ts_autosign.py @@ -32,6 +32,8 @@ from .ts_base import * from .ts_shared import * from .input import * +from mmgen.led import LEDControl + class TestSuiteAutosign(TestSuiteBase): 'autosigning with BTC, BCH, LTC, ETH and ETC' networks = ('btc',) @@ -249,24 +251,25 @@ class TestSuiteAutosign(TestSuiteBase): ydie(1,f'Directory {txdir} does not exist! Exiting') def init_led(): - from mmgen.led import LEDControl - if simulate: - LEDControl.create_dummy_control_files() try: cf = LEDControl(enabled=True,simulate=simulate) - except: - ydie(2,"'no LED support detected'") - + except Exception as e: + msg(str(e)) + ydie(2,'LEDControl initialization failed') for fn in (cf.board.status,cf.board.trigger): if fn: run(['sudo','chmod','0666',fn],check=True) - os.environ['MMGEN_TEST_SUITE_AUTOSIGN_LIVE'] = '1' # begin execution if simulate and not opt.exact_output: rmsg('This command must be run with --exact-output enabled!') return False + + if simulate or not live: + os.environ['MMGEN_TEST_SUITE_AUTOSIGN_LED_SIMULATE'] = '1' + LEDControl.create_dummy_control_files() + network_ids = [c+'_tn' for c in daemon_coins] + daemon_coins start_test_daemons(*network_ids) @@ -288,6 +291,8 @@ class TestSuiteAutosign(TestSuiteBase): foo = do_autosign(opts,mountpoint,mn_type='bip39',short=True) ret = do_autosign(opts,mountpoint) finally: + if simulate or not live: + LEDControl.delete_dummy_control_files() stop_test_daemons(*[i for i in network_ids if i != 'btc']) return ret