coin_daemon_control.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. from .tests_header import repo_root
  3. from mmgen.common import *
  4. action = g.prog_name.split('-')[0]
  5. opts_data = {
  6. 'sets': [('debug',True,'verbose',True)],
  7. 'text': {
  8. 'desc': f'{action.capitalize()} coin daemons for the MMGen test suite',
  9. 'usage':'[opts] <network IDs>',
  10. 'options': """
  11. -h, --help Print this help message
  12. --, --longhelp Print help message for long options (common options)
  13. -D, --debug Produce debugging output (implies --verbose)
  14. -d, --datadir= Override the default datadir
  15. -i, --daemon-ids Print all known daemon IDs
  16. -m, --mainnet-only Perform operations for mainnet daemons only
  17. -n, --no-daemonize Don't fork daemon to background
  18. -p, --port-shift= Shift the RPC port by this number
  19. -s, --get-state Get the state of the daemon(s) and exit
  20. -t, --testing Testing mode. Print commands but don't execute them
  21. -q, --quiet Produce quieter output
  22. -u, --usermode Run the daemon in user (non test-suite) mode
  23. -v, --verbose Produce more verbose output
  24. -V, --print-version Print version strings from exec’ed daemons (not RPC)
  25. -W, --no-wait Don't wait for daemons to change state before exiting
  26. """,
  27. 'notes': """
  28. Valid network IDs: {nid}, all, or no_xmr
  29. """
  30. },
  31. 'code': {
  32. 'options': lambda s: s.format(a=action.capitalize(),pn=g.prog_name),
  33. 'notes': lambda s,help_notes: s.format(nid=help_notes('coin_daemon_network_ids'))
  34. }
  35. }
  36. cmd_args = opts.init(opts_data)
  37. from mmgen.daemon import *
  38. def run(network_id=None,proto=None,daemon_id=None):
  39. d = CoinDaemon(
  40. network_id = network_id,
  41. proto = proto,
  42. test_suite = not opt.usermode,
  43. opts = ['no_daemonize'] if opt.no_daemonize else None,
  44. port_shift = int(opt.port_shift or 0),
  45. datadir = opt.datadir,
  46. daemon_id = daemon_id )
  47. if opt.mainnet_only and d.network != 'mainnet':
  48. return
  49. d.debug = d.debug or opt.debug
  50. d.wait = not opt.no_wait
  51. if opt.print_version:
  52. msg('{:16} {}'.format( d.exec_fn+':', d.get_exec_version_str() ))
  53. elif opt.get_state:
  54. print(d.state_msg())
  55. elif opt.testing:
  56. for cmd in d.start_cmds if action == 'start' else [d.stop_cmd]:
  57. print(' '.join(cmd))
  58. else:
  59. if action == 'stop' and hasattr(d,'rpc'):
  60. run_session(d.rpc.stop_daemon(quiet=opt.quiet))
  61. else:
  62. d.cmd(action,quiet=opt.quiet)
  63. if opt.daemon_ids:
  64. print('\n'.join(CoinDaemon.all_daemon_ids()))
  65. elif 'all' in cmd_args or 'no_xmr' in cmd_args:
  66. if len(cmd_args) != 1:
  67. die(1,"'all' or 'no_xmr' must be the sole argument")
  68. from mmgen.protocol import init_proto
  69. for coin in CoinDaemon.coins:
  70. if coin == 'XMR' and cmd_args[0] == 'no_xmr':
  71. continue
  72. for daemon_id in CoinDaemon.get_daemon_ids(coin):
  73. for network in CoinDaemon.get_daemon(coin,daemon_id).networks:
  74. run(proto=init_proto(coin=coin,network=network),daemon_id=daemon_id)
  75. else:
  76. ids = cmd_args
  77. network_ids = CoinDaemon.get_network_ids()
  78. if not ids:
  79. opts.usage()
  80. for i in ids:
  81. if i not in network_ids:
  82. die(1,f'{i!r}: invalid network ID')
  83. for network_id in ids:
  84. run(network_id=network_id.lower())