in lisa/executable.py [0:0]
def __getitem__(self, tool_type: Union[Type[T], CustomScriptBuilder, str]) -> T:
"""
return a typed subclass of tool or script builder.
for example,
echo_tool = node.tools[Echo]
echo_tool.run("hello")
"""
if tool_type is CustomScriptBuilder:
raise LisaException(
"CustomScriptBuilder should call build to create a script instance"
)
if isinstance(tool_type, CustomScriptBuilder):
tool_key = tool_type.name
elif isinstance(tool_type, str):
tool_key = tool_type.lower()
else:
tool_key = tool_type.__name__.lower()
tool = self._cache.get(tool_key)
if tool is None:
# the Tool is not installed on current node, try to install it.
tool_log = get_logger("tool", tool_key, self._node.log)
tool_log.debug(f"initializing tool [{tool_key}]")
if isinstance(tool_type, CustomScriptBuilder):
tool = tool_type.build(self._node)
elif isinstance(tool_type, str):
raise LisaException(
f"{tool_type} cannot be found. "
f"short usage need to get with type before get with name."
)
else:
cast_tool_type = cast(Type[Tool], tool_type)
tool = cast_tool_type.create(self._node)
tool.initialize()
if not tool.exists:
tool_log.debug(f"'{tool.name}' not installed")
if tool.can_install:
tool_log.debug(f"{tool.name} is installing")
timer = create_timer()
is_success = tool.install()
if not is_success:
raise LisaException(
f"install '{tool.name}' failed. After installed, "
f"it cannot be detected."
)
tool_log.debug(f"installed in {timer}")
else:
raise LisaException(
f"cannot find [{tool.name}] on [{self._node.name}], "
f"{self._node.os.__class__.__name__}, "
f"Remote({self._node.is_remote}) "
f"and installation of [{tool.name}] isn't enabled in lisa."
)
else:
tool_log.debug("installed already")
self._cache[tool_key] = tool
return cast(T, tool)