group_mgr.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. test.include.group_mgr: Command group manager for the MMGen Wallet cmdtest suite
  12. """
  13. import sys, os, time
  14. from mmgen.color import yellow, green, cyan
  15. from mmgen.util import Msg, die
  16. from .cfg import cfgs, cmd_groups_dfl, cmd_groups_extra
  17. class CmdGroupMgr:
  18. dpy_data = None
  19. cmd_groups = cmd_groups_dfl.copy()
  20. cmd_groups.update(cmd_groups_extra)
  21. def __init__(self, cfg):
  22. self.cfg = cfg
  23. self.network_id = cfg.coin.lower() + ('_tn' if cfg.testnet else '')
  24. self.name = type(self).__name__
  25. def create_cmd_group(self, cls, sg_name=None):
  26. cmd_group_in = dict(cls.cmd_group_in)
  27. if sg_name and 'subgroup.' + sg_name not in cmd_group_in:
  28. die(1, f'{sg_name!r}: no such subgroup in test group {cls.__name__}')
  29. def add_entries(key, add_deps=True, added_subgroups=[]):
  30. if add_deps:
  31. for dep in cmd_group_in['subgroup.'+key]:
  32. yield from add_entries(dep)
  33. assert isinstance(cls.cmd_subgroups[key][0], str), f'header for subgroup {key!r} missing!'
  34. if not key in added_subgroups:
  35. yield from cls.cmd_subgroups[key][1:]
  36. added_subgroups.append(key)
  37. def gen():
  38. for name, data in cls.cmd_group_in:
  39. if name.startswith('subgroup.'):
  40. sg_key = name.removeprefix('subgroup.')
  41. if sg_name in (None, sg_key):
  42. yield from add_entries(
  43. sg_key,
  44. add_deps = sg_name and not self.cfg.skipping_deps,
  45. added_subgroups = [sg_name] if self.cfg.deps_only else [])
  46. if self.cfg.deps_only and sg_key == sg_name:
  47. return
  48. elif not self.cfg.skipping_deps:
  49. yield (name, data)
  50. return tuple(gen())
  51. def load_mod(self, gname, modname=None):
  52. clsname, kwargs = self.cmd_groups[gname]
  53. if modname is None and 'modname' in kwargs:
  54. modname = kwargs['modname']
  55. import importlib
  56. modpath = f'test.cmdtest_d.ct_{modname or gname}'
  57. return getattr(importlib.import_module(modpath), clsname)
  58. def create_group(self, gname, sg_name, full_data=False, modname=None, is3seed=False, add_dpy=False):
  59. """
  60. Initializes the list 'cmd_list' and dict 'dpy_data' from module's cmd_group data.
  61. Alternatively, if called with 'add_dpy=True', updates 'dpy_data' from module data
  62. without touching 'cmd_list'
  63. """
  64. cls = self.load_mod(gname, modname)
  65. cdata = []
  66. def get_shared_deps(cmdname, tmpdir_idx):
  67. """
  68. shared_deps are "implied" dependencies for all cmds in cmd_group that don't appear in
  69. the cmd_group data or cmds' argument lists. Supported only for 3seed tests at present.
  70. """
  71. if not hasattr(cls, 'shared_deps'):
  72. return []
  73. return [k for k, v in cfgs[str(tmpdir_idx)]['dep_generators'].items()
  74. if k in cls.shared_deps and v != cmdname]
  75. if not hasattr(cls, 'cmd_group'):
  76. cls.cmd_group = self.create_cmd_group(cls, sg_name)
  77. for a, b in cls.cmd_group:
  78. if is3seed:
  79. for n, (i, j) in enumerate(zip(cls.tmpdir_nums, (128, 192, 256))):
  80. k = f'{a}_{n+1}'
  81. if hasattr(cls, 'skip_cmds') and k in cls.skip_cmds:
  82. continue
  83. sdeps = get_shared_deps(k, i)
  84. if isinstance(b, str):
  85. cdata.append((k, (i, f'{b} ({j}-bit)', [[[]+sdeps, i]])))
  86. else:
  87. cdata.append((k, (i, f'{b[1]} ({j}-bit)', [[b[0]+sdeps, i]])))
  88. else:
  89. cdata.append((a, b if full_data else (cls.tmpdir_nums[0], b, [[[], cls.tmpdir_nums[0]]])))
  90. if add_dpy:
  91. self.dpy_data.update(dict(cdata))
  92. else:
  93. self.cmd_list = tuple(e[0] for e in cdata)
  94. self.dpy_data = dict(cdata)
  95. return cls
  96. def gm_init_group(self, trunner, gname, sg_name, spawn_prog):
  97. kwargs = self.cmd_groups[gname][1]
  98. cls = self.create_group(gname, sg_name, **kwargs)
  99. cls.group_name = gname
  100. return cls(trunner, cfgs, spawn_prog)
  101. def get_cls_by_gname(self, gname):
  102. return self.load_mod(gname, self.cmd_groups[gname][1].get('modname'))
  103. def list_cmd_groups(self):
  104. ginfo = []
  105. for gname in self.cmd_groups:
  106. ginfo.append((gname, self.get_cls_by_gname(gname)))
  107. if self.cfg.list_current_cmd_groups:
  108. exclude = (self.cfg.exclude_groups or '').split(',')
  109. ginfo = [g for g in ginfo
  110. if self.network_id in g[1].networks
  111. and not g[0] in exclude
  112. and g[0] in tuple(self.cmd_groups_dfl) + tuple(self.cfg._args)]
  113. desc = 'CONFIGURED'
  114. else:
  115. desc = 'AVAILABLE'
  116. def gen_output():
  117. yield green(f'{desc} COMMAND GROUPS AND SUBGROUPS:')
  118. yield ''
  119. for name, cls in ginfo:
  120. yield ' {} - {}'.format(
  121. yellow(name.ljust(13)),
  122. (cls.__doc__.strip() if cls.__doc__ else cls.__name__))
  123. if 'cmd_subgroups' in cls.__dict__:
  124. subgroups = {k:v for k, v in cls.cmd_subgroups.items() if not k.startswith('_')}
  125. max_w = max(len(k) for k in subgroups)
  126. for k, v in subgroups.items():
  127. yield ' + {} · {}'.format(cyan(k.ljust(max_w+1)), v[0])
  128. from mmgen.ui import do_pager
  129. do_pager('\n'.join(gen_output()))
  130. Msg('\n' + ' '.join(e[0] for e in ginfo))
  131. sys.exit(0)
  132. def find_cmd_in_groups(self, cmd, group=None):
  133. """
  134. Search for a test command in specified group or all configured command groups
  135. and return it as a string. Loads modules but alters no global variables.
  136. """
  137. if group:
  138. if not group in [e[0] for e in self.cmd_groups]:
  139. die(1, f'{group!r}: unrecognized group')
  140. groups = [self.cmd_groups[group]]
  141. else:
  142. groups = self.cmd_groups
  143. for gname in groups:
  144. cls = self.get_cls_by_gname(gname)
  145. if not hasattr(cls, 'cmd_group'):
  146. cls.cmd_group = self.create_cmd_group(cls)
  147. if cmd in cls.cmd_group: # first search the class
  148. return gname
  149. if cmd in dir(cls(None, None, None)): # then a throwaway instance
  150. return gname # cmd might exist in more than one group - we'll go with the first
  151. return None