start-coin-daemons.py 2.4 KB

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