Browse Source

led.py, test.py autosign: improve LED simulation code

The MMGen Project 3 years ago
parent
commit
6e0519dc12
3 changed files with 24 additions and 27 deletions
  1. 9 19
      mmgen/led.py
  2. 3 1
      mmgen/main_autosign.py
  3. 12 7
      test/test_py_d/ts_autosign.py

+ 9 - 19
mmgen/led.py

@@ -53,7 +53,7 @@ class LEDControl:
 	def __init__(self,enabled,simulate=False,debug=False):
 	def __init__(self,enabled,simulate=False,debug=False):
 
 
 		self.enabled = enabled
 		self.enabled = enabled
-		self.debug = debug
+		self.debug = debug or simulate
 
 
 		if not enabled:
 		if not enabled:
 			self.set = self.stop = self.noop
 			self.set = self.stop = self.noop
@@ -62,11 +62,9 @@ class LEDControl:
 		self.ev = threading.Event()
 		self.ev = threading.Event()
 		self.led_thread = None
 		self.led_thread = None
 
 
-		if simulate:
-			type(self).create_dummy_control_files()
-			self.debug = True
-
 		for board_id,board in self.boards.items():
 		for board_id,board in self.boards.items():
+			if board_id == 'dummy' and not simulate:
+				continue
 			try: os.stat(board.status)
 			try: os.stat(board.status)
 			except: pass
 			except: pass
 			else: break
 			else: break
@@ -82,20 +80,6 @@ class LEDControl:
 			Trigger file: {board.trigger}
 			Trigger file: {board.trigger}
 			""",indent='  ',strip_char='\t'))
 			""",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):
 		def check_access(fn,desc,init_val=None):
 			try:
 			try:
 				if not init_val:
 				if not init_val:
@@ -127,6 +111,12 @@ class LEDControl:
 		with open(db.trigger,'w') as fp:
 		with open(db.trigger,'w') as fp:
 			fp.write(db.trigger_states[1]+'\n')
 			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 noop(self,*args,**kwargs): pass
 
 
 	def ev_sleep(self,secs):
 	def ev_sleep(self,secs):

+ 3 - 1
mmgen/main_autosign.py

@@ -411,7 +411,9 @@ signal.signal(signal.SIGTERM,handler)
 signal.signal(signal.SIGINT,handler)
 signal.signal(signal.SIGINT,handler)
 
 
 from .led import LEDControl
 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')
 led.set('off')
 
 
 async def main():
 async def main():

+ 12 - 7
test/test_py_d/ts_autosign.py

@@ -32,6 +32,8 @@ from .ts_base import *
 from .ts_shared import *
 from .ts_shared import *
 from .input import *
 from .input import *
 
 
+from mmgen.led import LEDControl
+
 class TestSuiteAutosign(TestSuiteBase):
 class TestSuiteAutosign(TestSuiteBase):
 	'autosigning with BTC, BCH, LTC, ETH and ETC'
 	'autosigning with BTC, BCH, LTC, ETH and ETC'
 	networks = ('btc',)
 	networks = ('btc',)
@@ -249,24 +251,25 @@ class TestSuiteAutosign(TestSuiteBase):
 				ydie(1,f'Directory {txdir} does not exist!  Exiting')
 				ydie(1,f'Directory {txdir} does not exist!  Exiting')
 
 
 		def init_led():
 		def init_led():
-			from mmgen.led import LEDControl
-			if simulate:
-				LEDControl.create_dummy_control_files()
 			try:
 			try:
 				cf = LEDControl(enabled=True,simulate=simulate)
 				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):
 			for fn in (cf.board.status,cf.board.trigger):
 				if fn:
 				if fn:
 					run(['sudo','chmod','0666',fn],check=True)
 					run(['sudo','chmod','0666',fn],check=True)
-			os.environ['MMGEN_TEST_SUITE_AUTOSIGN_LIVE'] = '1'
 
 
 		# begin execution
 		# begin execution
 
 
 		if simulate and not opt.exact_output:
 		if simulate and not opt.exact_output:
 			rmsg('This command must be run with --exact-output enabled!')
 			rmsg('This command must be run with --exact-output enabled!')
 			return False
 			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
 		network_ids = [c+'_tn' for c in daemon_coins] + daemon_coins
 		start_test_daemons(*network_ids)
 		start_test_daemons(*network_ids)
 
 
@@ -288,6 +291,8 @@ class TestSuiteAutosign(TestSuiteBase):
 				foo = do_autosign(opts,mountpoint,mn_type='bip39',short=True)
 				foo = do_autosign(opts,mountpoint,mn_type='bip39',short=True)
 				ret = do_autosign(opts,mountpoint)
 				ret = do_autosign(opts,mountpoint)
 		finally:
 		finally:
+			if simulate or not live:
+				LEDControl.delete_dummy_control_files()
 			stop_test_daemons(*[i for i in network_ids if i != 'btc'])
 			stop_test_daemons(*[i for i in network_ids if i != 'btc'])
 
 
 		return ret
 		return ret