spaceforge 0.1.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
spaceforge/runner.py ADDED
@@ -0,0 +1,115 @@
1
+ """
2
+ Plugin runner for executing hook methods.
3
+ """
4
+
5
+ import importlib.util
6
+ import os
7
+ import sys
8
+ from typing import Optional
9
+
10
+
11
+ class PluginRunner:
12
+ """Runs plugin hook methods."""
13
+
14
+ def __init__(self, plugin_path: str = "plugin.py") -> None:
15
+ """
16
+ Initialize the runner.
17
+
18
+ Args:
19
+ plugin_path: Path to the plugin Python file
20
+ """
21
+ self.plugin_path = plugin_path
22
+ self.plugin_instance: Optional[object] = None
23
+
24
+ def load_plugin(self) -> None:
25
+ """Load the plugin from the specified path."""
26
+ if not os.path.exists(self.plugin_path):
27
+ raise FileNotFoundError(f"Plugin file not found: {self.plugin_path}")
28
+
29
+ # Load the module
30
+ spec = importlib.util.spec_from_file_location("plugin_module", self.plugin_path)
31
+ if spec is None or spec.loader is None:
32
+ raise ImportError(f"Could not load plugin from {self.plugin_path}")
33
+
34
+ plugin_module = importlib.util.module_from_spec(spec)
35
+ spec.loader.exec_module(plugin_module)
36
+
37
+ # Find the plugin class (should inherit from SpaceforgePlugin)
38
+ from .plugin import SpaceforgePlugin
39
+
40
+ plugin_class = None
41
+ for attr_name in dir(plugin_module):
42
+ attr = getattr(plugin_module, attr_name)
43
+ if (
44
+ isinstance(attr, type)
45
+ and issubclass(attr, SpaceforgePlugin)
46
+ and attr != SpaceforgePlugin
47
+ ):
48
+ plugin_class = attr
49
+ break
50
+
51
+ if plugin_class is None:
52
+ raise ValueError("No SpaceforgePlugin subclass found in plugin file")
53
+
54
+ # Instantiate the plugin
55
+ self.plugin_instance = plugin_class()
56
+
57
+ def run_hook(self, hook_name: str) -> None:
58
+ """
59
+ Run a specific hook method.
60
+
61
+ Args:
62
+ hook_name: Name of the hook method to run
63
+ """
64
+ if self.plugin_instance is None:
65
+ self.load_plugin()
66
+
67
+ if not hasattr(self.plugin_instance, hook_name):
68
+ print(f"Hook method '{hook_name}' not found in plugin")
69
+ return
70
+
71
+ method = getattr(self.plugin_instance, hook_name)
72
+ if not callable(method):
73
+ print(f"'{hook_name}' is not a callable method")
74
+ return
75
+
76
+ try:
77
+ print(f"[SPACEPY] Running hook: {hook_name}")
78
+ method()
79
+ print(f"[SPACEPY] Hook completed: {hook_name}")
80
+ except Exception as e:
81
+ print(f"[SPACEPY] Error running hook '{hook_name}': {e}")
82
+ raise
83
+
84
+
85
+ import click
86
+
87
+
88
+ @click.command(name="runner")
89
+ @click.argument("hook_name")
90
+ @click.option(
91
+ "--plugin-file",
92
+ default="plugin.py",
93
+ type=click.Path(exists=True, dir_okay=False, readable=True),
94
+ help="Path to the plugin Python file (default: plugin.py)",
95
+ )
96
+ def runner_command(hook_name: str, plugin_file: str) -> None:
97
+ """Run a specific hook method from a plugin.
98
+
99
+ HOOK_NAME: Name of the hook method to execute (e.g., after_plan, before_apply)
100
+
101
+ This command is typically used internally by Spacelift to execute plugin hooks.
102
+ """
103
+ runner = PluginRunner(plugin_file)
104
+ runner.run_hook(hook_name)
105
+
106
+
107
+ def main() -> None:
108
+ """Legacy main entry point for backward compatibility."""
109
+ if len(sys.argv) != 2:
110
+ print("Usage: python -m spaceforge.runner <hook_name>")
111
+ sys.exit(1)
112
+
113
+ hook_name = sys.argv[1]
114
+ runner = PluginRunner()
115
+ runner.run_hook(hook_name)