1# Copyright (c) 2024 Antmicro <www.antmicro.com>
2#
3# SPDX-License-Identifier: Apache-2.0
4
5'''Runner stub for Renode.'''
6
7import subprocess
8from runners.core import ZephyrBinaryRunner, RunnerCaps
9
10
11class RenodeRunner(ZephyrBinaryRunner):
12    '''Place-holder for Renode runner customizations.'''
13
14    def __init__(self, cfg, args):
15        super().__init__(cfg)
16        self.renode_arg = args.renode_arg
17        self.renode_command = args.renode_command
18        self.renode_help = args.renode_help
19
20    @classmethod
21    def name(cls):
22        return 'renode'
23
24    @classmethod
25    def capabilities(cls):
26        return RunnerCaps(commands={'simulate'}, hide_load_files=True)
27
28    @classmethod
29    def do_add_parser(cls, parser):
30        parser.add_argument('--renode-arg',
31                            metavar='ARG',
32                            action='append',
33                            help='additional argument passed to Renode; `--help` will print all possible arguments')
34        parser.add_argument('--renode-command',
35                            metavar='COMMAND',
36                            action='append',
37                            help='additional command passed to Renode\'s simulation')
38        parser.add_argument('--renode-help',
39                            default=False,
40                            action='store_true',
41                            help='print all possible `Renode` arguments')
42
43    @classmethod
44    def do_create(cls, cfg, args):
45        return RenodeRunner(cfg, args)
46
47    def do_run(self, command, **kwargs):
48        self.run_test(**kwargs)
49
50    def run_test(self, **kwargs):
51        cmd = ['renode']
52        if self.renode_help is True:
53            cmd.append('--help')
54        else:
55            if self.renode_arg is not None:
56                for arg in self.renode_arg:
57                    cmd.append(arg)
58            if self.renode_command is not None:
59                for command in self.renode_command:
60                    cmd.append('-e')
61                    cmd.append(command)
62        subprocess.run(cmd, check=True)
63