Browse Source

minor cleanups

The MMGen Project 6 months ago
parent
commit
9ae8338253
6 changed files with 53 additions and 34 deletions
  1. 9 5
      mmgen/autosign.py
  2. 5 1
      mmgen/led.py
  3. 12 6
      mmgen/main_autosign.py
  4. 13 10
      mmgen/platform/darwin/util.py
  5. 12 10
      test/cmdtest_py_d/ct_autosign.py
  6. 2 2
      test/include/common.py

+ 9 - 5
mmgen/autosign.py

@@ -18,7 +18,7 @@ from pathlib import Path
 from subprocess import run, PIPE, DEVNULL
 
 from .cfg import Config
-from .util import msg,msg_r,ymsg,rmsg,gmsg,bmsg,die,suf,fmt,fmt_list,async_run
+from .util import msg, msg_r, ymsg, rmsg, gmsg, bmsg, die, suf, fmt, fmt_list
 from .color import yellow,red,orange,brown
 from .wallet import Wallet,get_wallet_cls
 from .filename import find_file_in_dir
@@ -394,9 +394,13 @@ class Autosign:
 
 		self.init_fixup()
 
-		if sys.platform == 'darwin': # test suite uses ‘fixed-up’ dir for ramdisk
+		if sys.platform == 'darwin': # test suite uses ‘fixed-up’ shm_dir
 			from .platform.darwin.util import MacOSRamDisk
-			self.ramdisk = MacOSRamDisk(cfg, self.macOS_ramdisk_name, 10, path=self.shm_dir)
+			self.ramdisk = MacOSRamDisk(
+					cfg,
+					self.macOS_ramdisk_name,
+					10,
+					path = self.shm_dir)
 
 		self.keyfile = self.mountpoint / 'autosign.key'
 
@@ -674,8 +678,8 @@ class Autosign:
 					wallets = self.cfg.xmrwallets,  # XMR wallet idxs
 					spec    = None ),
 			)
-			async_run(m.main())
-			async_run(m.stop_wallet_daemon())
+			asyncio.run(m.main())
+			asyncio.run(m.stop_wallet_daemon())
 
 		self.clean_old_files()
 

+ 5 - 1
mmgen/led.py

@@ -169,7 +169,11 @@ class LEDControl:
 		if self.debug:
 			msg(f'Setting LED state to {state!r}')
 
-		self.led_thread = threading.Thread(target=self.led_loop,name='LED loop',args=timings[state])
+		self.led_thread = threading.Thread(
+				target = self.led_loop,
+				name   = 'LED loop',
+				args   = timings[state])
+
 		self.led_thread.start()
 
 	def stop(self):

+ 12 - 6
mmgen/main_autosign.py

@@ -61,10 +61,10 @@ clean     - clean the removable device of unneeded files, removing only non-
 gen_key   - generate the wallet encryption key and copy it to the removable
             device mounted at mountpoint ‘{asi.mountpoint}’ (as currently
             configured)
-setup     - generate both wallet encryption key and temporary signing wallet
-xmr_setup - set up temporary Monero signing wallets.  This operation needn’t
-            be performed by the user directly in most cases, as Monero setup
-            is done by the ‘setup’ command when --xmrwallets is specified
+setup     - full setup: run ‘gen_key’ and create temporary signing wallet(s)
+            for all configured coins
+xmr_setup - set up Monero temporary signing wallet(s).  Not required in normal
+            operation: use ‘setup’ with --xmrwallets instead
 wait      - start in loop mode: wait-mount-sign-unmount-wait
 wipe_key  - wipe the wallet encryption key on the removable device, making
             signing transactions or stealing the user’s seed impossible.
@@ -171,7 +171,6 @@ from .autosign import Autosign
 cfg = Config(
 	opts_data = opts_data,
 	init_opts = {
-		'quiet': True,
 		'out_fmt': 'wallet',
 		'usr_randchars': 0,
 		'hash_preset': '1',
@@ -181,7 +180,14 @@ cfg = Config(
 
 cmd = cfg._args[0] if len(cfg._args) == 1 else 'sign' if not cfg._args else cfg._opts.usage()
 
-valid_cmds = ('gen_key', 'setup', 'xmr_setup', 'sign', 'wait', 'clean', 'wipe_key')
+valid_cmds = (
+	'gen_key',
+	'setup',
+	'xmr_setup',
+	'sign',
+	'wait',
+	'clean',
+	'wipe_key')
 
 if cmd not in valid_cmds:
 	die(1,f'‘{cmd}’: unrecognized command')

+ 13 - 10
mmgen/platform/darwin/util.py

@@ -15,12 +15,11 @@ platform.darwin.util: utilities for the macOS platform
 from pathlib import Path
 from subprocess import run, PIPE, DEVNULL
 
-from ...color import cyan
 from ...obj import MMGenLabel
 
-def get_device_size(fn):
+def get_device_size(path_or_label):
 	import re
-	cp = run(['diskutil', 'info', fn], text=True, stdout=PIPE, check=True)
+	cp = run(['diskutil', 'info', path_or_label], text=True, stdout=PIPE, check=True)
 	res = [e for e in cp.stdout.splitlines() if 'Disk Size' in e]
 	errmsg = '‘diskutil info’ output could not be parsed for device size'
 	assert len(res) == 1, f'{errmsg}:\n{cp.stdout}'
@@ -34,23 +33,27 @@ class RamDiskLabel(MMGenLabel):
 
 class MacOSRamDisk:
 
-	desc = 'macOS ramdisk'
+	desc = 'ramdisk'
 
-	def __init__(self, cfg, label, size_in_MB, path=None):
+	def __init__(self, cfg, label, size, path=None):
 		self.cfg = cfg
 		self.label = RamDiskLabel(label)
-		self.size_in_MB  = size_in_MB
+		self.size = size # size in MiB
 		self.dfl_path = Path('/Volumes') / self.label
 		self.path = Path(path) if path else self.dfl_path
 
+	def exists(self):
+		return self.path.is_mount()
+
 	def create(self, quiet=False):
 		redir = DEVNULL if quiet else None
-		if self.path.exists():
+		if self.exists():
 			self.cfg._util.qmsg('{} {} [{}] already exists'.format(self.desc, self.label.hl(), self.path))
 			return
-		cp = run(['hdiutil', 'attach', '-nomount', f'ram://{2048 * self.size_in_MB}'], stdout=PIPE, check=True)
+		self.cfg._util.qmsg(f'Creating {self.desc} {self.label.hl()} of size {self.size}MB')
+		cp = run(['hdiutil', 'attach', '-nomount', f'ram://{2048 * self.size}'], stdout=PIPE, check=True)
 		self.dev_name = cp.stdout.decode().strip()
-		self.cfg._util.qmsg('{} {} [{}]'.format(cyan(f'Created {self.desc}'), self.label.hl(), self.dev_name))
+		self.cfg._util.qmsg(f'Created {self.desc} {self.label.hl()} [{self.dev_name}]')
 		run(['diskutil', 'eraseVolume', 'APFS', self.label, self.dev_name], stdout=redir, check=True)
 		if self.path != self.dfl_path:
 			run(['diskutil', 'umount', self.label], stdout=redir, check=True)
@@ -61,4 +64,4 @@ class MacOSRamDisk:
 		redir = DEVNULL if quiet else None
 		run(['diskutil', 'eject', self.label], stdout=redir, check=True)
 		if not quiet:
-			self.cfg._util.qmsg('{} {} [{}]'.format(cyan(f'Destroyed {self.desc}'), self.label.hl(), self.path))
+			self.cfg._util.qmsg(f'Destroyed {self.desc} {self.label.hl()} at {self.path}')

+ 12 - 10
test/cmdtest_py_d/ct_autosign.py

@@ -776,18 +776,26 @@ class CmdTestAutosign(CmdTestAutosignBase):
 		self.remove_device()
 		return 'ok'
 
-	def do_sign(self, args=[], have_msg=False, exc_exit_val=None):
+	def do_sign(self, args=[], have_msg=False, exc_exit_val=None, expect_str=None):
 
 		tx_desc = Signable.transaction.desc
 		self.insert_device()
 
+		def do_exit():
+			if expect_str:
+				t.expect(expect_str)
+			t.read()
+			self.remove_device()
+			imsg('')
+			return t
+
 		t = self.spawn(
 				'mmgen-autosign',
 				self.opts + args,
 				exit_val = exc_exit_val or (1 if self.bad_tx_count or (have_msg and self.bad_msg_count) else None))
 
 		if exc_exit_val:
-			return t
+			return do_exit()
 
 		t.expect(
 			f'{self.tx_count} {tx_desc}{suf(self.tx_count)} signed' if self.tx_count else
@@ -807,11 +815,7 @@ class CmdTestAutosign(CmdTestAutosignBase):
 					f'{self.bad_msg_count} message file{suf(self.bad_msg_count)}{{0,1}} failed to sign',
 					regex = True)
 
-		t.read()
-		self.remove_device()
-
-		imsg('')
-		return t
+		return do_exit()
 
 	def sign_quiet(self):
 		return self.do_sign(['--quiet'])
@@ -832,9 +836,7 @@ class CmdTestAutosign(CmdTestAutosignBase):
 		return self.do_sign(['--full-summary'], have_msg=True)
 
 	def sign_bad_no_daemon(self):
-		t = self.do_sign(exc_exit_val=2)
-		t.expect('listening on the correct port')
-		return t
+		return self.do_sign(exc_exit_val=2, expect_str='listening on the correct port')
 
 	def sign_no_unsigned(self):
 		return self._sign_no_unsigned(

+ 2 - 2
test/include/common.py

@@ -389,7 +389,7 @@ class VirtBlockDeviceBase:
 	def detach(self, silent=False):
 		dev = self.dev
 		if not silent:
-			imsg(f'Detaching device {dev!r}')
+			imsg(f'Detaching {dev!r}')
 		self.do_detach(dev)
 		if hasattr(self, 'dev_mode_orig'):
 			if not silent:
@@ -447,7 +447,7 @@ class VirtBlockDeviceMacOS(VirtBlockDeviceBase):
 		do_run(['hdiutil', 'create', '-size', size, '-layout', 'NONE', str(path)])
 
 	def do_attach(self, path, dev=None):
-		do_run(['hdiutil', 'attach', '-mount', 'suppressed', str(path)])
+		do_run(['hdiutil', 'attach', '-nomount', str(path)])
 
 	def do_detach(self, dev, check=True):
 		do_run(['hdiutil', 'detach', dev], check=check)