xref: /llvm-project/lldb/test/API/tools/lldb-dap/progress/Progress_emitter.py (revision a939a9fd53d98f33b94f9121646d5906a2b9f598)
1import inspect
2import optparse
3import shlex
4import sys
5import time
6
7import lldb
8
9
10class ProgressTesterCommand:
11    program = "test-progress"
12
13    @classmethod
14    def register_lldb_command(cls, debugger, module_name):
15        parser = cls.create_options()
16        cls.__doc__ = parser.format_help()
17        # Add any commands contained in this module to LLDB
18        command = "command script add -c %s.%s %s" % (
19            module_name,
20            cls.__name__,
21            cls.program,
22        )
23        debugger.HandleCommand(command)
24        print(
25            'The "{0}" command has been installed, type "help {0}" or "{0} '
26            '--help" for detailed help.'.format(cls.program)
27        )
28
29    @classmethod
30    def create_options(cls):
31        usage = "usage: %prog [options]"
32        description = "SBProgress testing tool"
33        # Opt parse is deprecated, but leaving this the way it is because it allows help formating
34        # Additionally all our commands use optparse right now, ideally we migrate them all in one go.
35        parser = optparse.OptionParser(
36            description=description, prog=cls.program, usage=usage
37        )
38
39        parser.add_option(
40            "--total", dest="total", help="Total to count up.", type="int"
41        )
42
43        parser.add_option(
44            "--seconds",
45            dest="seconds",
46            help="Total number of seconds to wait between increments",
47            type="int",
48        )
49
50        return parser
51
52    def get_short_help(self):
53        return "Progress Tester"
54
55    def get_long_help(self):
56        return self.help_string
57
58    def __init__(self, debugger, unused):
59        self.parser = self.create_options()
60        self.help_string = self.parser.format_help()
61
62    def __call__(self, debugger, command, exe_ctx, result):
63        command_args = shlex.split(command)
64        try:
65            (cmd_options, args) = self.parser.parse_args(command_args)
66        except:
67            result.SetError("option parsing failed")
68            return
69
70        total = cmd_options.total
71        progress = lldb.SBProgress("Progress tester", "Detail", total, debugger)
72
73        for i in range(1, total):
74            progress.Increment(1, f"Step {i}")
75            time.sleep(cmd_options.seconds)
76
77
78def __lldb_init_module(debugger, dict):
79    # Register all classes that have a register_lldb_command method
80    for _name, cls in inspect.getmembers(sys.modules[__name__]):
81        if inspect.isclass(cls) and callable(
82            getattr(cls, "register_lldb_command", None)
83        ):
84            cls.register_lldb_command(debugger, __name__)
85