coin_daemon_control.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class warn_missing_exec(oneshot_warning):
  39. color = 'nocolor'
  40. message = 'daemon executable {!r} not found on this system!'
  41. def run(network_id=None,proto=None,daemon_id=None,missing_exec_ok=True):
  42. d = CoinDaemon(
  43. network_id = network_id,
  44. proto = proto,
  45. test_suite = not opt.usermode,
  46. opts = ['no_daemonize'] if opt.no_daemonize else None,
  47. port_shift = int(opt.port_shift or 0),
  48. datadir = opt.datadir,
  49. daemon_id = daemon_id )
  50. if opt.mainnet_only and d.network != 'mainnet':
  51. return
  52. d.debug = d.debug or opt.debug
  53. d.wait = not opt.no_wait
  54. if missing_exec_ok:
  55. try:
  56. d.get_exec_version_str()
  57. except:
  58. if not opt.quiet:
  59. warn_missing_exec( div=d.exec_fn, fmt_args=(d.exec_fn,) )
  60. return
  61. if opt.print_version:
  62. msg('{:16} {}'.format( d.exec_fn+':', d.get_exec_version_str() ))
  63. elif opt.get_state:
  64. print(d.state_msg())
  65. elif opt.testing:
  66. for cmd in d.start_cmds if action == 'start' else [d.stop_cmd]:
  67. print(' '.join(cmd))
  68. else:
  69. if action == 'stop' and hasattr(d,'rpc'):
  70. run_session(d.rpc.stop_daemon(quiet=opt.quiet))
  71. else:
  72. d.cmd(action,quiet=opt.quiet)
  73. if opt.daemon_ids:
  74. print('\n'.join(CoinDaemon.all_daemon_ids()))
  75. elif 'all' in cmd_args or 'no_xmr' in cmd_args:
  76. if len(cmd_args) != 1:
  77. die(1,"'all' or 'no_xmr' must be the sole argument")
  78. from mmgen.protocol import init_proto
  79. for coin in CoinDaemon.coins:
  80. if coin == 'XMR' and cmd_args[0] == 'no_xmr':
  81. continue
  82. for daemon_id in CoinDaemon.get_daemon_ids(coin):
  83. for network in CoinDaemon.get_daemon(coin,daemon_id).networks:
  84. run(
  85. proto = init_proto(coin=coin,network=network),
  86. daemon_id = daemon_id,
  87. missing_exec_ok = True )
  88. else:
  89. ids = cmd_args
  90. network_ids = CoinDaemon.get_network_ids()
  91. if not ids:
  92. opts.usage()
  93. for i in ids:
  94. if i not in network_ids:
  95. die(1,f'{i!r}: invalid network ID')
  96. for network_id in ids:
  97. run(network_id=network_id.lower())