From 4c49a6e59c6ce9852b02f45bee048a066c97d88e Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 12 Dec 2023 10:19:51 +0000 Subject: [PATCH] launch test and helper scripts with `main.launch()` --- scripts/compute-file-chksum.py | 22 +++++++++++++--------- scripts/create-token.py | 8 ++++++-- test/cmdtest_py_d/ct_ethdev.py | 1 - test/gentest.py | 3 ++- test/hashfunc.py | 5 ++++- test/scrambletest.py | 5 ++++- test/start-coin-daemons.py | 3 ++- test/stop-coin-daemons.py | 3 ++- test/tooltest.py | 8 ++++---- test/tooltest2.py | 7 +++---- test/unit_tests.py | 7 ++++--- 11 files changed, 44 insertions(+), 28 deletions(-) diff --git a/scripts/compute-file-chksum.py b/scripts/compute-file-chksum.py index 25e7eaf0..a6b6750f 100755 --- a/scripts/compute-file-chksum.py +++ b/scripts/compute-file-chksum.py @@ -3,6 +3,7 @@ import sys,os import script_init +from mmgen.main import launch from mmgen.cfg import Config from mmgen.util import msg,Msg,make_chksum_6 from mmgen.fileutil import get_lines_from_file @@ -20,12 +21,15 @@ opts_data = { cfg = Config(opts_data=opts_data) -lines = get_lines_from_file( cfg, cfg._args[0] ) -start = (1,0)[bool(cfg.include_first_line)] -a = make_chksum_6(' '.join(lines[start:]).encode()) -if start == 1: - b = lines[0] - msg( - 'Checksum in file OK' if a == b else - f"Checksum in file ({b}) doesn't match computed value!") -Msg(a) +def main(): + lines = get_lines_from_file(cfg, cfg._args[0]) + start = (1,0)[bool(cfg.include_first_line)] + a = make_chksum_6(' '.join(lines[start:]).encode()) + if start == 1: + b = lines[0] + msg( + 'Checksum in file OK' if a == b else + f"Checksum in file ({b}) doesn't match computed value!") + Msg(a) + +launch(func=main) diff --git a/scripts/create-token.py b/scripts/create-token.py index 6e295e0a..1702adeb 100755 --- a/scripts/create-token.py +++ b/scripts/create-token.py @@ -25,6 +25,7 @@ from subprocess import run,PIPE from collections import namedtuple import script_init +from mmgen.main import launch from mmgen.cfg import Config from mmgen.util import Msg,msg,rmsg,ymsg,die @@ -263,8 +264,7 @@ def compile_code(cfg,code): else: cfg._util.vmsg(out) -if __name__ == '__main__': - +def main(): cfg = Config(opts_data=opts_data) if cfg.check_solc_version: @@ -288,3 +288,7 @@ if __name__ == '__main__': print(json.dumps(out)) msg('Contract successfully compiled') + +if __name__ == '__main__': + + launch(func=main) diff --git a/test/cmdtest_py_d/ct_ethdev.py b/test/cmdtest_py_d/ct_ethdev.py index bafd0989..545cb4cd 100755 --- a/test/cmdtest_py_d/ct_ethdev.py +++ b/test/cmdtest_py_d/ct_ethdev.py @@ -958,7 +958,6 @@ class CmdTestEthdev(CmdTestBase,CmdTestShared): pass cmd = [ 'python3', - 'scripts/exec_wrapper.py', 'scripts/create-token.py', '--coin=' + self.proto.coin, '--outdir=' + odir diff --git a/test/gentest.py b/test/gentest.py index f4932b86..bf51b3f2 100755 --- a/test/gentest.py +++ b/test/gentest.py @@ -586,4 +586,5 @@ if proto.coin == 'XMR': load_cryptodomex() if __name__ == '__main__': - main() + from mmgen.main import launch + launch(func=main) diff --git a/test/hashfunc.py b/test/hashfunc.py index 8813ea7d..6d000ff8 100755 --- a/test/hashfunc.py +++ b/test/hashfunc.py @@ -165,8 +165,9 @@ class TestSha512(TestSha2): from test.include.common import getrand,set_globals from mmgen.cfg import Config +from mmgen.main import launch -if __name__ == '__main__': +def main(): if len(sys.argv) not in (2,3): die(1,'Test takes 1 or 2 arguments: test name, plus optional rounds count') @@ -184,3 +185,5 @@ if __name__ == '__main__': t.test_constants() t.test_ref() t.test_random(random_rounds) + +launch(func=main) diff --git a/test/scrambletest.py b/test/scrambletest.py index 47be986a..7eb84cb8 100755 --- a/test/scrambletest.py +++ b/test/scrambletest.py @@ -153,7 +153,7 @@ def do_passwd_tests(): s = fs.format('','') do_test(cmd,tdata,s+' '*(40-len(s)),'password') -if __name__ == '__main__': +def main(): start_time = int(time.time()) cmds = cfg._args or ('coin','pw') @@ -161,3 +161,6 @@ if __name__ == '__main__': {'coin': do_coin_tests, 'pw': do_passwd_tests }[cmd]() end_msg(int(time.time()) - start_time) + +from mmgen.main import launch +launch(func=main) diff --git a/test/start-coin-daemons.py b/test/start-coin-daemons.py index 35229566..e4ceefde 100755 --- a/test/start-coin-daemons.py +++ b/test/start-coin-daemons.py @@ -9,4 +9,5 @@ try: except ImportError: from test.include.coin_daemon_control import main -main() +from mmgen.main import launch +launch(func=main) diff --git a/test/stop-coin-daemons.py b/test/stop-coin-daemons.py index f725c1cc..07db95b2 100755 --- a/test/stop-coin-daemons.py +++ b/test/stop-coin-daemons.py @@ -9,4 +9,5 @@ try: except ImportError: from test.include.coin_daemon_control import main -main() +from mmgen.main import launch +launch(func=main) diff --git a/test/tooltest.py b/test/tooltest.py index 63dcc92a..63d4cade 100755 --- a/test/tooltest.py +++ b/test/tooltest.py @@ -486,7 +486,7 @@ def do_cmds(cmd_group): cmdline = [cmd] + [os.path.join(tcfg['tmpdir'],fn) for fn in fns] getattr(tc,cmd)(*cmdline) -try: +def main(): if cfg._args: if len(cfg._args) != 1: die(1,'Only one command may be specified') @@ -507,7 +507,7 @@ try: do_cmds(cmd) if cmd is not list(cmd_data.keys())[-1]: msg('') -except KeyboardInterrupt: - die(1,green('\nExiting at user request')) + end_msg(int(time.time()) - start_time) -end_msg(int(time.time()) - start_time) +from mmgen.main import launch +launch(func=main) diff --git a/test/tooltest2.py b/test/tooltest2.py index 657b34ba..942d6269 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -342,8 +342,7 @@ if cfg.fork: else: tool_cmd_preargs = ['python3','scripts/exec_wrapper.py'] +from mmgen.main import launch start_time = int(time.time()) - -if __name__ == '__main__': - async_run(main()) - end_msg(int(time.time()) - start_time) +launch(func = lambda: async_run(main())) +end_msg(int(time.time()) - start_time) diff --git a/test/unit_tests.py b/test/unit_tests.py index cfa54cfa..ab0ac4fe 100755 --- a/test/unit_tests.py +++ b/test/unit_tests.py @@ -207,7 +207,7 @@ def run_test(test,subtest=None): if not mod.unit_test().run_test(test,UnitTestHelpers(test)): die(4,'Unit test {test!r} failed') -try: +def main(): for test in (cfg._args or all_tests): if '.' in test: test,subtest = test.split('.') @@ -218,5 +218,6 @@ try: if test not in exclude: run_test(test,subtest=subtest) end_msg(int(time.time()) - start_time) -except KeyboardInterrupt: - die(1,green('\nExiting at user request')) + +from mmgen.main import launch +launch(func=main)