Skip to content

Commit dee8f47

Browse files
committed
[Dexter] Normalise the "tools directory" into a list of tools
AFAIUI the original rationale behind having tools abstracted behind the directory that they live is was to ease development -- create a new tool by creating a new directory. However it also puts in awkward abstractions whereby you can't know what's going to be run without first loading a module. There's also effectively no authoritative list of tools, because you can load one from anywhere. And the tool names are affected by pythons normalisation of module names! Instead: move the tools into named files in the tools directory, have a list of them, and create the tool object from that list when needed. No module wrangling requried at all, and there can be certainty about what's going on. We can also delete some name-normalisation that happens now that it's not subject to module-loading-normalisation.
1 parent 6de5d1e commit dee8f47

File tree

15 files changed

+36
-92
lines changed

15 files changed

+36
-92
lines changed

cross-project-tests/debuginfo-tests/dexter/dex/debugger/Debuggers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _get_potential_debuggers(): # noqa
4747

4848

4949
def _warn_meaningless_option(context, option):
50-
if hasattr(context.options, "list_debuggers"):
50+
if hasattr(context.options, "list-debuggers"):
5151
return
5252

5353
context.logger.warning(

cross-project-tests/debuginfo-tests/dexter/dex/tools/Main.py

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,6 @@ def _output_bug_report_message(context):
5454
)
5555

5656

57-
def get_tools_directory():
58-
"""Returns directory path where DExTer tool imports can be
59-
found.
60-
"""
61-
tools_directory = os.path.join(get_root_directory(), "tools")
62-
assert os.path.isdir(tools_directory), tools_directory
63-
return tools_directory
64-
65-
66-
def get_tool_names():
67-
"""Returns a list of expected DExTer Tools"""
68-
return [
69-
"help",
70-
"list-debuggers",
71-
"no-tool-",
72-
"run-debugger-internal-",
73-
"test",
74-
"view",
75-
]
76-
77-
7857
def _set_auto_highlights(context):
7958
"""Flag some strings for auto-highlighting."""
8059
context.o.auto_reds.extend(
@@ -118,6 +97,7 @@ def _is_valid_tool_name(tool_name):
11897
"""check tool name matches a tool directory within the dexter tools
11998
directory.
12099
"""
100+
from . import get_tool_names
121101
valid_tools = get_tool_names()
122102
if tool_name not in valid_tools:
123103
raise Error(
@@ -127,17 +107,6 @@ def _is_valid_tool_name(tool_name):
127107
)
128108

129109

130-
def _import_tool_module(tool_name):
131-
"""Imports the python module at the tool directory specificed by
132-
tool_name.
133-
"""
134-
# format tool argument to reflect tool directory form.
135-
tool_name = tool_name.replace("-", "_")
136-
137-
tools_directory = get_tools_directory()
138-
return load_module(tool_name, tools_directory)
139-
140-
141110
def tool_main(context, tool, args):
142111
with Timer(tool.name):
143112
options, defaults = tool.parse_command_line(args)
@@ -190,6 +159,7 @@ def __init__(self):
190159

191160

192161
def main() -> ReturnCode:
162+
from . import get_tools
193163
context = Context()
194164
with PrettyOutput() as context.o:
195165
context.logger = Logger(context.o)
@@ -200,8 +170,8 @@ def main() -> ReturnCode:
200170
options, args = _get_options_and_args(context)
201171
# raises 'Error' if command line tool is invalid.
202172
tool_name = _get_tool_name(options)
203-
module = _import_tool_module(tool_name)
204-
return tool_main(context, module.Tool(context), args)
173+
T = get_tools()[tool_name]
174+
return tool_main(context, T(context), args)
205175
except Error as e:
206176
context.logger.error(str(e))
207177
try:

cross-project-tests/debuginfo-tests/dexter/dex/tools/__init__.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@
55
# See https://llvm.org/LICENSE.txt for license information.
66
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77

8-
from dex.tools.Main import Context, get_tool_names, get_tools_directory, main, tool_main
8+
from dex.tools.Main import Context, main, tool_main
99
from dex.tools.TestToolBase import TestToolBase
1010
from dex.tools.ToolBase import ToolBase
11+
12+
def get_tool_names():
13+
"""Returns a list of expected DExTer Tools"""
14+
return ["help", "list-debuggers", "no-tool-", "run-debugger-internal-", "test", "view"]
15+
16+
def get_tools():
17+
"""Returns a dictionary of expected DExTer Tools"""
18+
return _the_tools
19+
20+
21+
from .help import Tool as help_tool
22+
from .list_debuggers import Tool as list_debuggers_tool
23+
from .no_tool_ import Tool as no_tool_tool
24+
from .run_debugger_internal_ import Tool as run_debugger_internal_tool
25+
from .test import Tool as test_tool
26+
from .view import Tool as view_tool
27+
28+
_the_tools = {
29+
"help" : help_tool,
30+
"list-debuggers" : list_debuggers_tool,
31+
"no_tool_" : no_tool_tool,
32+
"run-debugger-internal-" : run_debugger_internal_tool,
33+
"test" : test_tool,
34+
"view" : view_tool
35+
}
36+

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import textwrap
1010

11-
from dex.tools import ToolBase, get_tool_names, get_tools_directory, tool_main
11+
from dex.tools import ToolBase, get_tool_names, get_tools, tool_main
1212
from dex.utils.Imports import load_module
1313
from dex.utils.ReturnCode import ReturnCode
1414

@@ -36,10 +36,8 @@ def handle_options(self, defaults):
3636
@property
3737
def _default_text(self):
3838
s = "\n<b>The following subtools are available:</>\n\n"
39-
tools_directory = get_tools_directory()
4039
for tool_name in sorted(self._visible_tool_names):
41-
internal_name = tool_name.replace("-", "_")
42-
tool_doc = load_module(internal_name, tools_directory).Tool.__doc__
40+
tool_doc = get_tools()[tool_name].__doc__
4341
tool_doc = tool_doc.strip() if tool_doc else ""
4442
tool_doc = textwrap.fill(" ".join(tool_doc.split()), 80)
4543
s += "<g>{}</>\n{}\n\n".format(tool_name, tool_doc)
@@ -50,7 +48,5 @@ def go(self) -> ReturnCode:
5048
self.context.o.auto(self._default_text)
5149
return ReturnCode.OK
5250

53-
tool_name = self.context.options.tool.replace("-", "_")
54-
tools_directory = get_tools_directory()
55-
module = load_module(tool_name, tools_directory)
56-
return tool_main(self.context, module.Tool(self.context), ["--help"])
51+
T = get_tools()[tool_name]
52+
return tool_main(self.context, T(self.context), ["--help"])

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

Lines changed: 0 additions & 8 deletions
This file was deleted.

cross-project-tests/debuginfo-tests/dexter/dex/tools/list_debuggers/__init__.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

cross-project-tests/debuginfo-tests/dexter/dex/tools/no_tool_/__init__.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)