From 9ae83382539a0ea8eba3f839c79789f01dbe41d2 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 4 Sep 2024 13:08:50 +0000 Subject: [PATCH] minor cleanups --- mmgen/autosign.py | 14 +++++++++----- mmgen/led.py | 6 +++++- mmgen/main_autosign.py | 18 ++++++++++++------ mmgen/platform/darwin/util.py | 23 +++++++++++++---------- test/cmdtest_py_d/ct_autosign.py | 22 ++++++++++++---------- test/include/common.py | 4 ++-- 6 files changed, 53 insertions(+), 34 deletions(-) diff --git a/mmgen/autosign.py b/mmgen/autosign.py index 7e0107a1..f6b0d5b0 100755 --- a/mmgen/autosign.py +++ b/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() diff --git a/mmgen/led.py b/mmgen/led.py index c2e8fa60..a795e4e6 100755 --- a/mmgen/led.py +++ b/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): diff --git a/mmgen/main_autosign.py b/mmgen/main_autosign.py index a024dafb..76285d81 100755 --- a/mmgen/main_autosign.py +++ b/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') diff --git a/mmgen/platform/darwin/util.py b/mmgen/platform/darwin/util.py index 4500ae8d..ae7662f0 100755 --- a/mmgen/platform/darwin/util.py +++ b/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}') diff --git a/test/cmdtest_py_d/ct_autosign.py b/test/cmdtest_py_d/ct_autosign.py index 10e9bf45..fbd5e1ea 100755 --- a/test/cmdtest_py_d/ct_autosign.py +++ b/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( diff --git a/test/include/common.py b/test/include/common.py index fcb79798..fa53a2c4 100755 --- a/test/include/common.py +++ b/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)