brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.4 KiB · 0bf785e Raw
112 lines · python
1import inspect2import optparse3import shlex4import sys5import time6 7import lldb8 9 10class ProgressTesterCommand:11    program = "test-progress"12 13    @classmethod14    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 LLDB18        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    @classmethod30    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 formating34        # 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=usage37        )38 39        parser.add_option(40            "--total",41            dest="total",42            help="Total items in this progress object. When this option is not specified, this will be an indeterminate progress.",43            type="int",44            default=None,45        )46 47        parser.add_option(48            "--seconds",49            dest="seconds",50            help="Total number of seconds to wait between increments",51            type="int",52        )53 54        parser.add_option(55            "--no-details",56            dest="no_details",57            help="Do not display details",58            action="store_true",59            default=False,60        )61 62        return parser63 64    def get_short_help(self):65        return "Progress Tester"66 67    def get_long_help(self):68        return self.help_string69 70    def __init__(self, debugger, unused):71        self.parser = self.create_options()72        self.help_string = self.parser.format_help()73 74    def __call__(self, debugger, command, exe_ctx, result):75        command_args = shlex.split(command)76        try:77            (cmd_options, args) = self.parser.parse_args(command_args)78        except:79            result.SetError("option parsing failed")80            return81 82        total = cmd_options.total83        if total is None:84            progress = lldb.SBProgress(85                "Progress tester", "Initial Indeterminate Detail", debugger86            )87        else:88            progress = lldb.SBProgress(89                "Progress tester", "Initial Detail", total, debugger90            )91        # Check to see if total is set to None to indicate an indeterminate92        # progress then default to 3 steps.93        with progress:94            if total is None:95                total = 396 97            for i in range(1, total):98                if cmd_options.no_details:99                    progress.Increment(1)100                else:101                    progress.Increment(1, f"Step {i}")102                time.sleep(cmd_options.seconds)103 104 105def __lldb_init_module(debugger, dict):106    # Register all classes that have a register_lldb_command method107    for _name, cls in inspect.getmembers(sys.modules[__name__]):108        if inspect.isclass(cls) and callable(109            getattr(cls, "register_lldb_command", None)110        ):111            cls.register_lldb_command(debugger, __name__)112