Debian Codegraph @

cross-project-tests/debuginfo-tests/dexter/dex/tools/help/Tool.py

raw
1 # DExTer : Debugging Experience Tester
2 # ~~~~~~ ~ ~~ ~ ~~
3 #
4 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 # See https://llvm.org/LICENSE.txt for license information.
6 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 """Help tool."""
8
9 import imp
10 import textwrap
11
12 from dex.tools import ToolBase, get_tool_names, get_tools_directory, tool_main
13 from dex.utils.ReturnCode import ReturnCode
14
15
16 class Tool(ToolBase):
17 """Provides help info on subtools."""
18
19 @property
20 def name(self):
21 return "DExTer help"
22
23 @property
24 def _visible_tool_names(self):
25 return [t for t in get_tool_names() if not t.endswith("-")]
26
27 def add_tool_arguments(self, parser, defaults):
28 parser.description = Tool.__doc__
29 parser.add_argument(
30 "tool", choices=self._visible_tool_names, nargs="?", help="name of subtool"
31 )
32
33 def handle_options(self, defaults):
34 pass
35
36 @property
37 def _default_text(self):
38 s = "\n<b>The following subtools are available:</>\n\n"
39 tools_directory = get_tools_directory()
40 for tool_name in sorted(self._visible_tool_names):
41 internal_name = tool_name.replace("-", "_")
42 module_info = imp.find_module(internal_name, [tools_directory])
43 tool_doc = imp.load_module(internal_name, *module_info).Tool.__doc__
44 tool_doc = tool_doc.strip() if tool_doc else ""
45 tool_doc = textwrap.fill(" ".join(tool_doc.split()), 80)
46 s += "<g>{}</>\n{}\n\n".format(tool_name, tool_doc)
47 return s
48
49 def go(self) -> ReturnCode:
50 if self.context.options.tool is None:
51 self.context.o.auto(self._default_text)
52 return ReturnCode.OK
53
54 tool_name = self.context.options.tool.replace("-", "_")
55 tools_directory = get_tools_directory()
56 module_info = imp.find_module(tool_name, [tools_directory])
57 module = imp.load_module(tool_name, *module_info)
58 return tool_main(self.context, module.Tool(self.context), ["--help"])