|
| 1 | +############################################################################### |
| 2 | +# |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (C) 2026 Advanced Micro Devices, Inc. |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | +# |
| 25 | +############################################################################### |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +import argparse |
| 30 | +import logging |
| 31 | +from contextlib import contextmanager |
| 32 | +from contextvars import ContextVar |
| 33 | +from dataclasses import dataclass |
| 34 | +from typing import Iterator, Optional |
| 35 | + |
| 36 | +from nodescraper.models import PluginConfig, SystemInfo |
| 37 | +from nodescraper.models.pluginresult import PluginResult |
| 38 | +from nodescraper.pluginexecutor import PluginExecutor |
| 39 | +from nodescraper.pluginregistry import PluginRegistry |
| 40 | + |
| 41 | +_plugin_run_invocation_ctx: ContextVar[Optional["PluginRunInvocation"]] = ContextVar( |
| 42 | + "nodescraper_plugin_run_invocation", default=None |
| 43 | +) |
| 44 | + |
| 45 | + |
| 46 | +def get_plugin_run_invocation() -> Optional[PluginRunInvocation]: |
| 47 | + """Return the active invocation while run_plugin_queue_with_invocation is running, if any.""" |
| 48 | + return _plugin_run_invocation_ctx.get() |
| 49 | + |
| 50 | + |
| 51 | +@contextmanager |
| 52 | +def plugin_run_invocation_scope(inv: PluginRunInvocation) -> Iterator[None]: |
| 53 | + """Bind *inv* for nested code (connection managers, plugins) for the scope of the context.""" |
| 54 | + token = _plugin_run_invocation_ctx.set(inv) |
| 55 | + try: |
| 56 | + yield |
| 57 | + finally: |
| 58 | + _plugin_run_invocation_ctx.reset(token) |
| 59 | + |
| 60 | + |
| 61 | +@dataclass |
| 62 | +class PluginRunInvocation: |
| 63 | + """Recorded inputs for one plugin run; optional host_cli_args for embedded hosts.""" |
| 64 | + |
| 65 | + plugin_reg: PluginRegistry |
| 66 | + parsed_args: argparse.Namespace |
| 67 | + plugin_config_inst_list: list[PluginConfig] |
| 68 | + system_info: SystemInfo |
| 69 | + log_path: Optional[str] |
| 70 | + logger: logging.Logger |
| 71 | + timestamp: str |
| 72 | + sname: str |
| 73 | + host_cli_args: Optional[argparse.Namespace] = None |
| 74 | + |
| 75 | + |
| 76 | +def run_plugin_queue_with_invocation( |
| 77 | + *, |
| 78 | + plugin_reg: PluginRegistry, |
| 79 | + parsed_args: argparse.Namespace, |
| 80 | + plugin_config_inst_list: list[PluginConfig], |
| 81 | + system_info: SystemInfo, |
| 82 | + log_path: Optional[str], |
| 83 | + logger: logging.Logger, |
| 84 | + timestamp: str, |
| 85 | + sname: str, |
| 86 | + host_cli_args: Optional[argparse.Namespace] = None, |
| 87 | +) -> list[PluginResult]: |
| 88 | + """Constructs the plugin executor, binds invocation context, and runs the plugin queue.""" |
| 89 | + inv = PluginRunInvocation( |
| 90 | + plugin_reg=plugin_reg, |
| 91 | + parsed_args=parsed_args, |
| 92 | + plugin_config_inst_list=plugin_config_inst_list, |
| 93 | + system_info=system_info, |
| 94 | + log_path=log_path, |
| 95 | + logger=logger, |
| 96 | + timestamp=timestamp, |
| 97 | + sname=sname, |
| 98 | + host_cli_args=host_cli_args, |
| 99 | + ) |
| 100 | + plugin_executor = PluginExecutor( |
| 101 | + logger=logger, |
| 102 | + plugin_configs=plugin_config_inst_list, |
| 103 | + connections=parsed_args.connection_config, |
| 104 | + system_info=system_info, |
| 105 | + log_path=log_path, |
| 106 | + plugin_registry=plugin_reg, |
| 107 | + ) |
| 108 | + with plugin_run_invocation_scope(inv): |
| 109 | + return plugin_executor.run_queue() |
| 110 | + |
| 111 | + |
| 112 | +__all__ = [ |
| 113 | + "PluginRunInvocation", |
| 114 | + "get_plugin_run_invocation", |
| 115 | + "plugin_run_invocation_scope", |
| 116 | + "run_plugin_queue_with_invocation", |
| 117 | +] |
0 commit comments