提交 2dc9d6ca 编写于 作者: A Alexander Pantyukhin 提交者: Shuah Khan

kunit: kunit.py extract handlers

The main function contains a wide if-elif block that handles different
subcommands. It's possible to make code refactoring to extract
subcommands handlers.

Fixed commit summary line.
Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: NAlexander Pantyukhin <apantykhin@gmail.com>
Reviewed-by: NDavid Gow <davidgow@google.com>
Signed-off-by: NShuah Khan <skhan@linuxfoundation.org>
上级 1fdc6f4f
...@@ -386,50 +386,7 @@ def tree_from_args(cli_args: argparse.Namespace) -> kunit_kernel.LinuxSourceTree ...@@ -386,50 +386,7 @@ def tree_from_args(cli_args: argparse.Namespace) -> kunit_kernel.LinuxSourceTree
extra_qemu_args=qemu_args) extra_qemu_args=qemu_args)
def main(argv): def run_handler(cli_args):
parser = argparse.ArgumentParser(
description='Helps writing and running KUnit tests.')
subparser = parser.add_subparsers(dest='subcommand')
# The 'run' command will config, build, exec, and parse in one go.
run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
add_common_opts(run_parser)
add_build_opts(run_parser)
add_exec_opts(run_parser)
add_parse_opts(run_parser)
config_parser = subparser.add_parser('config',
help='Ensures that .config contains all of '
'the options in .kunitconfig')
add_common_opts(config_parser)
build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests')
add_common_opts(build_parser)
add_build_opts(build_parser)
exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests')
add_common_opts(exec_parser)
add_exec_opts(exec_parser)
add_parse_opts(exec_parser)
# The 'parse' option is special, as it doesn't need the kernel source
# (therefore there is no need for a build_dir, hence no add_common_opts)
# and the '--file' argument is not relevant to 'run', so isn't in
# add_parse_opts()
parse_parser = subparser.add_parser('parse',
help='Parses KUnit results from a file, '
'and parses formatted results.')
add_parse_opts(parse_parser)
parse_parser.add_argument('file',
help='Specifies the file to read results from.',
type=str, nargs='?', metavar='input_file')
cli_args = parser.parse_args(massage_argv(argv))
if get_kernel_root_path():
os.chdir(get_kernel_root_path())
if cli_args.subcommand == 'run':
if not os.path.exists(cli_args.build_dir): if not os.path.exists(cli_args.build_dir):
os.mkdir(cli_args.build_dir) os.mkdir(cli_args.build_dir)
...@@ -446,7 +403,9 @@ def main(argv): ...@@ -446,7 +403,9 @@ def main(argv):
result = run_tests(linux, request) result = run_tests(linux, request)
if result.status != KunitStatus.SUCCESS: if result.status != KunitStatus.SUCCESS:
sys.exit(1) sys.exit(1)
elif cli_args.subcommand == 'config':
def config_handler(cli_args):
if cli_args.build_dir and ( if cli_args.build_dir and (
not os.path.exists(cli_args.build_dir)): not os.path.exists(cli_args.build_dir)):
os.mkdir(cli_args.build_dir) os.mkdir(cli_args.build_dir)
...@@ -460,7 +419,9 @@ def main(argv): ...@@ -460,7 +419,9 @@ def main(argv):
result.elapsed_time)) result.elapsed_time))
if result.status != KunitStatus.SUCCESS: if result.status != KunitStatus.SUCCESS:
sys.exit(1) sys.exit(1)
elif cli_args.subcommand == 'build':
def build_handler(cli_args):
linux = tree_from_args(cli_args) linux = tree_from_args(cli_args)
request = KunitBuildRequest(build_dir=cli_args.build_dir, request = KunitBuildRequest(build_dir=cli_args.build_dir,
make_options=cli_args.make_options, make_options=cli_args.make_options,
...@@ -471,7 +432,9 @@ def main(argv): ...@@ -471,7 +432,9 @@ def main(argv):
result.elapsed_time)) result.elapsed_time))
if result.status != KunitStatus.SUCCESS: if result.status != KunitStatus.SUCCESS:
sys.exit(1) sys.exit(1)
elif cli_args.subcommand == 'exec':
def exec_handler(cli_args):
linux = tree_from_args(cli_args) linux = tree_from_args(cli_args)
exec_request = KunitExecRequest(raw_output=cli_args.raw_output, exec_request = KunitExecRequest(raw_output=cli_args.raw_output,
build_dir=cli_args.build_dir, build_dir=cli_args.build_dir,
...@@ -485,7 +448,9 @@ def main(argv): ...@@ -485,7 +448,9 @@ def main(argv):
'Elapsed time: %.3fs\n') % (result.elapsed_time)) 'Elapsed time: %.3fs\n') % (result.elapsed_time))
if result.status != KunitStatus.SUCCESS: if result.status != KunitStatus.SUCCESS:
sys.exit(1) sys.exit(1)
elif cli_args.subcommand == 'parse':
def parse_handler(cli_args):
if cli_args.file is None: if cli_args.file is None:
sys.stdin.reconfigure(errors='backslashreplace') # pytype: disable=attribute-error sys.stdin.reconfigure(errors='backslashreplace') # pytype: disable=attribute-error
kunit_output = sys.stdin kunit_output = sys.stdin
...@@ -499,8 +464,68 @@ def main(argv): ...@@ -499,8 +464,68 @@ def main(argv):
result, _ = parse_tests(request, metadata, kunit_output) result, _ = parse_tests(request, metadata, kunit_output)
if result.status != KunitStatus.SUCCESS: if result.status != KunitStatus.SUCCESS:
sys.exit(1) sys.exit(1)
else:
subcommand_handlers_map = {
'run': run_handler,
'config': config_handler,
'build': build_handler,
'exec': exec_handler,
'parse': parse_handler
}
def main(argv):
parser = argparse.ArgumentParser(
description='Helps writing and running KUnit tests.')
subparser = parser.add_subparsers(dest='subcommand')
# The 'run' command will config, build, exec, and parse in one go.
run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
add_common_opts(run_parser)
add_build_opts(run_parser)
add_exec_opts(run_parser)
add_parse_opts(run_parser)
config_parser = subparser.add_parser('config',
help='Ensures that .config contains all of '
'the options in .kunitconfig')
add_common_opts(config_parser)
build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests')
add_common_opts(build_parser)
add_build_opts(build_parser)
exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests')
add_common_opts(exec_parser)
add_exec_opts(exec_parser)
add_parse_opts(exec_parser)
# The 'parse' option is special, as it doesn't need the kernel source
# (therefore there is no need for a build_dir, hence no add_common_opts)
# and the '--file' argument is not relevant to 'run', so isn't in
# add_parse_opts()
parse_parser = subparser.add_parser('parse',
help='Parses KUnit results from a file, '
'and parses formatted results.')
add_parse_opts(parse_parser)
parse_parser.add_argument('file',
help='Specifies the file to read results from.',
type=str, nargs='?', metavar='input_file')
cli_args = parser.parse_args(massage_argv(argv))
if get_kernel_root_path():
os.chdir(get_kernel_root_path())
subcomand_handler = subcommand_handlers_map.get(cli_args.subcommand, None)
if subcomand_handler is None:
parser.print_help() parser.print_help()
return
subcomand_handler(cli_args)
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv[1:]) main(sys.argv[1:])
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册