From 371e22887da5bc0fe05bb742b74e0f91911024a4 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 1 Jul 2023 15:51:36 +0000 Subject: [PATCH] autosign,daemon: minor cleanups --- mmgen/autosign.py | 4 +-- mmgen/daemon.py | 47 ++++++++++++++++------------- mmgen/opts.py | 2 +- mmgen/proto/xmr/daemon.py | 8 +++-- test/include/coin_daemon_control.py | 9 +++--- 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/mmgen/autosign.py b/mmgen/autosign.py index 74892567..ae3a845e 100755 --- a/mmgen/autosign.py +++ b/mmgen/autosign.py @@ -493,8 +493,8 @@ class Autosign: self._xmrwallet_cfg = Config({ '_clone': self.cfg, 'coin': 'xmr', - 'wallet_rpc_user': 'autosigner', - 'wallet_rpc_password': 'my very secret password', + 'wallet_rpc_user': 'autosign', + 'wallet_rpc_password': 'autosign password', 'wallet_rpc_port': 23232 if self.cfg.test_suite_xmr_autosign else None, 'wallet_dir': str(self.wallet_dir), 'autosign': True, diff --git a/mmgen/daemon.py b/mmgen/daemon.py index 94dd5704..12ee1a9b 100755 --- a/mmgen/daemon.py +++ b/mmgen/daemon.py @@ -25,9 +25,10 @@ from subprocess import run,PIPE,CompletedProcess from collections import namedtuple from .cfg import gc +from .base_obj import Lockable from .color import set_vt100 -from .util import msg,Msg_r,ymsg,die,remove_dups,oneshot_warning -from .flags import * +from .util import msg,Msg_r,ymsg,die,remove_dups,oneshot_warning,fmt_list +from .flags import ClassFlags,ClassOpts _dd = namedtuple('daemon_data',['coind_name','coind_version','coind_version_str']) # latest tested version _nw = namedtuple('coin_networks',['mainnet','testnet','regtest']) @@ -48,6 +49,7 @@ class Daemon(Lockable): avail_opts = () avail_flags = () # like opts, but can be set or unset after instantiation _reset_ok = ('debug','wait','pids') + version_info_arg = '--version' def __init__(self,cfg,opts=None,flags=None): @@ -95,6 +97,9 @@ class Daemon(Lockable): if self.debug or (is_daemon and not silent): msg(f'Starting {self.desc} on port {self.bind_port}') + if self.debug: + msg(f'\nExecuting:\n{fmt_list(cmd,fmt="col",indent=" ")}\n') + if self.use_threads and is_daemon and not self.opt.no_daemonize: ret = self.exec_cmd_thread(cmd) else: @@ -162,14 +167,15 @@ class Daemon(Lockable): return self.run_cmd(self.cli_cmd(*cmds),silent=silent) def state_msg(self,extra_text=None): - extra_text = f'{extra_text} ' if extra_text else '' + extra_text = 'not ' if self.state == 'stopped' else f'{extra_text} ' if extra_text else '' return '{:{w}} {:10} {}'.format( f'{self.desc} {extra_text}running', 'pid N/A' if self.pid is None or self.pids else f'pid {self.pid}', f'port {self.bind_port}', - w = 52 + len(extra_text) ) + w = 60 ) - def pre_start(self): pass + def pre_start(self): + pass def start(self,quiet=False,silent=False): if self.state == 'ready': @@ -231,12 +237,25 @@ class Daemon(Lockable): else: die(2,f'Wait for state {req_state!r} timeout exceeded for {self.desc} (port {self.bind_port})') + @classmethod + def get_exec_version_str(cls): + try: + cp = run([cls.exec_fn,cls.version_info_arg],stdout=PIPE,stderr=PIPE,check=True) + except Exception as e: + die(2,f'{e}\nUnable to execute {cls.exec_fn}') + + if cp.returncode: + die(2,f'Unable to execute {cls.exec_fn}') + else: + res = cp.stdout.decode().splitlines() + return ( res[0] if len(res) == 1 else [s for s in res if 'ersion' in s][0] ).strip() + class RPCDaemon(Daemon): avail_opts = ('no_daemonize',) - def __init__(self,cfg): - super().__init__(cfg) + def __init__(self,cfg,opts=None,flags=None): + super().__init__(cfg,opts=opts,flags=flags) self.desc = '{} {} {}RPC daemon'.format( self.rpc_type, getattr(self.proto.network_names,self.proto.network), @@ -257,7 +276,6 @@ class CoinDaemon(Daemon): test_suite_port_shift = 1237 rpc_user = None rpc_password = None - version_info_arg = '--version' _cd = namedtuple('coins_data',['daemon_ids']) coins = { @@ -313,19 +331,6 @@ class CoinDaemon(Daemon): yield CoinProtocol.Base.create_network_id(coin,network) return remove_dups(list(gen()),quiet=True) - @classmethod - def get_exec_version_str(cls): - try: - cp = run([cls.exec_fn,cls.version_info_arg],stdout=PIPE,stderr=PIPE,check=True) - except: - die(2,f'Unable to execute {cls.exec_fn}') - - if cp.returncode: - die(2,f'Unable to execute {cls.exec_fn}') - else: - res = cp.stdout.decode().splitlines() - return ( res[0] if len(res) == 1 else [s for s in res if 'ersion' in s][0] ).strip() - def __new__(cls, cfg, network_id = None, diff --git a/mmgen/opts.py b/mmgen/opts.py index 46cc6f8d..5b35e4aa 100755 --- a/mmgen/opts.py +++ b/mmgen/opts.py @@ -79,7 +79,7 @@ long_opts_data = { opts_data_dfl = { 'text': { 'desc': '', - 'usage':'', + 'usage':'[options]', 'options': """ -h, --help Print this help message --, --longhelp Print help message for long (common) options diff --git a/mmgen/proto/xmr/daemon.py b/mmgen/proto/xmr/daemon.py index 75116999..61582905 100755 --- a/mmgen/proto/xmr/daemon.py +++ b/mmgen/proto/xmr/daemon.py @@ -103,12 +103,14 @@ class MoneroWalletDaemon(RPCDaemon): port_shift = None, datadir = None, trust_monerod = False, - test_monerod = False ): + test_monerod = False, + opts = None, + flags = None ): self.proto = proto self.test_suite = test_suite - super().__init__(cfg) + super().__init__(cfg,opts=opts,flags=flags) self.network = proto.network self.wallet_dir = wallet_dir @@ -119,7 +121,7 @@ class MoneroWalletDaemon(RPCDaemon): self.rpc_port += port_shift id_str = f'{self.exec_fn}-{self.bind_port}' - self.datadir = datadir or (self.test_suite_datadir if test_suite else self.exec_fn) + self.datadir = datadir or (self.test_suite_datadir if test_suite else self.exec_fn + '.d') self.pidfile = os.path.join(self.datadir,id_str+'.pid') self.logfile = os.path.join(self.datadir,id_str+'.log') diff --git a/test/include/coin_daemon_control.py b/test/include/coin_daemon_control.py index 10f1ac14..485c50aa 100755 --- a/test/include/coin_daemon_control.py +++ b/test/include/coin_daemon_control.py @@ -14,6 +14,7 @@ test.include.coin_daemon_control: Start and stop daemons for the MMGen test suit from .tests_header import repo_root from mmgen.common import * +from mmgen.protocol import init_proto action = gc.prog_name.split('-')[0] @@ -51,13 +52,13 @@ Valid network IDs: {nid}, all, or no_xmr cfg = Config(opts_data=opts_data) -from mmgen.daemon import * +from mmgen.daemon import CoinDaemon class warn_missing_exec(oneshot_warning): color = 'nocolor' message = 'daemon executable {!r} not found on this system!' -def run(network_id=None,proto=None,daemon_id=None,missing_exec_ok=True): +def run(network_id=None,proto=None,daemon_id=None,missing_exec_ok=False): d = CoinDaemon( cfg, @@ -78,8 +79,9 @@ def run(network_id=None,proto=None,daemon_id=None,missing_exec_ok=True): if missing_exec_ok: try: d.get_exec_version_str() - except: + except Exception as e: if not cfg.quiet: + msg(str(e)) warn_missing_exec( div=d.exec_fn, fmt_args=(d.exec_fn,) ) return if cfg.print_version: @@ -100,7 +102,6 @@ if cfg.daemon_ids: elif 'all' in cfg._args or 'no_xmr' in cfg._args: if len(cfg._args) != 1: die(1,"'all' or 'no_xmr' must be the sole argument") - from mmgen.protocol import init_proto for coin in CoinDaemon.coins: if coin == 'XMR' and cfg._args[0] == 'no_xmr': continue