sim-cli-core 0.2.4__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.
- sim/__init__.py +11 -0
- sim/__main__.py +24 -0
- sim/_plugin_install.py +451 -0
- sim/_timeout.py +115 -0
- sim/cli.py +1353 -0
- sim/compat.py +411 -0
- sim/config.py +380 -0
- sim/describe.py +346 -0
- sim/driver.py +190 -0
- sim/drivers/__init__.py +161 -0
- sim/drivers/coolprop/__init__.py +3 -0
- sim/drivers/coolprop/driver.py +183 -0
- sim/drivers/openfoam/__init__.py +4 -0
- sim/drivers/openfoam/compatibility.yaml +37 -0
- sim/drivers/openfoam/driver.py +432 -0
- sim/gui/LICENSES/ATTRIBUTION.md +24 -0
- sim/gui/__init__.py +230 -0
- sim/gui/_pywinauto_tools.py +452 -0
- sim/gui/_win32_dialog.py +148 -0
- sim/history.py +170 -0
- sim/inspect.py +1088 -0
- sim/plugins.py +468 -0
- sim/runner.py +195 -0
- sim/server.py +547 -0
- sim/session.py +224 -0
- sim/testing/__init__.py +30 -0
- sim/testing/protocol_conformance.py +264 -0
- sim_cli_core-0.2.4.dist-info/METADATA +309 -0
- sim_cli_core-0.2.4.dist-info/RECORD +33 -0
- sim_cli_core-0.2.4.dist-info/WHEEL +4 -0
- sim_cli_core-0.2.4.dist-info/entry_points.txt +2 -0
- sim_cli_core-0.2.4.dist-info/licenses/LICENSE +190 -0
- sim_cli_core-0.2.4.dist-info/licenses/NOTICE +72 -0
sim/plugins.py
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
"""Plugin discovery, metadata, and doctor — the substrate of ``sim plugin``.
|
|
2
|
+
|
|
3
|
+
Plugins register themselves with sim-cli via three Python entry-point groups:
|
|
4
|
+
|
|
5
|
+
* ``sim.drivers`` — required. The driver class implementing
|
|
6
|
+
:class:`sim.driver.DriverProtocol`. Already consumed by
|
|
7
|
+
:mod:`sim.drivers`; this module does not duplicate that work.
|
|
8
|
+
* ``sim.skills`` — optional. A :class:`importlib.resources.abc.Traversable`
|
|
9
|
+
pointing at the plugin's bundled ``_skills/`` directory. Used by
|
|
10
|
+
``sim plugin sync-skills``.
|
|
11
|
+
* ``sim.plugins`` — optional but recommended. A lightweight metadata dict
|
|
12
|
+
(``plugin_info``) that lets ``sim plugin list`` show one row per plugin
|
|
13
|
+
without importing the driver class.
|
|
14
|
+
|
|
15
|
+
This module exposes:
|
|
16
|
+
|
|
17
|
+
list_installed_plugins() -> list[InstalledPlugin]
|
|
18
|
+
doctor(name, deep=False) -> DoctorReport
|
|
19
|
+
doctor_all(deep=False) -> list[DoctorReport]
|
|
20
|
+
plugin_info_for(name) -> dict | None
|
|
21
|
+
skills_dir_for(name) -> Traversable | None
|
|
22
|
+
|
|
23
|
+
It deliberately does **not** install / uninstall plugins — that's
|
|
24
|
+
``sim plugin install``'s concern, in :mod:`sim._plugin_install`. Discovery
|
|
25
|
+
is read-only and side-effect-free.
|
|
26
|
+
"""
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
from dataclasses import dataclass, field
|
|
30
|
+
from importlib import resources as _resources
|
|
31
|
+
from importlib.metadata import EntryPoint, entry_points
|
|
32
|
+
from typing import Any
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# ── Entry-point groups ──────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
_GROUP_DRIVERS = "sim.drivers"
|
|
38
|
+
_GROUP_SKILLS = "sim.skills"
|
|
39
|
+
_GROUP_PLUGINS = "sim.plugins"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# ── Data shapes ─────────────────────────────────────────────────────────────
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass(frozen=True)
|
|
46
|
+
class InstalledPlugin:
|
|
47
|
+
"""One row of ``sim plugin list``."""
|
|
48
|
+
name: str # driver name, e.g. "coolprop"
|
|
49
|
+
package: str # PyPI distribution, e.g. "sim-plugin-coolprop"
|
|
50
|
+
version: str | None # package version, e.g. "0.1.0"
|
|
51
|
+
summary: str = "" # plugin_info.summary or ""
|
|
52
|
+
homepage: str = "" # plugin_info.homepage or ""
|
|
53
|
+
license_class: str = "" # "oss" / "commercial" / ""
|
|
54
|
+
solver_name: str = "" # plugin_info.solver_name or ""
|
|
55
|
+
has_skills: bool = False # whether sim.skills entry-point present
|
|
56
|
+
builtin: bool = False # True if shipped with sim-cli (registry _BUILTIN_REGISTRY)
|
|
57
|
+
driver_module: str = "" # module path for the driver, from the registry spec
|
|
58
|
+
|
|
59
|
+
def to_dict(self) -> dict[str, Any]:
|
|
60
|
+
return {
|
|
61
|
+
"name": self.name,
|
|
62
|
+
"package": self.package,
|
|
63
|
+
"version": self.version,
|
|
64
|
+
"summary": self.summary,
|
|
65
|
+
"homepage": self.homepage,
|
|
66
|
+
"license_class": self.license_class,
|
|
67
|
+
"solver_name": self.solver_name,
|
|
68
|
+
"has_skills": self.has_skills,
|
|
69
|
+
"builtin": self.builtin,
|
|
70
|
+
"driver_module": self.driver_module,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass(frozen=True)
|
|
75
|
+
class DoctorCheck:
|
|
76
|
+
"""One step in a doctor report."""
|
|
77
|
+
label: str
|
|
78
|
+
status: str # "ok", "warn", "fail", "info"
|
|
79
|
+
message: str = ""
|
|
80
|
+
|
|
81
|
+
def to_dict(self) -> dict[str, Any]:
|
|
82
|
+
return {"label": self.label, "status": self.status, "message": self.message}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass
|
|
86
|
+
class DoctorReport:
|
|
87
|
+
"""The full doctor result for one plugin."""
|
|
88
|
+
name: str
|
|
89
|
+
plugin: InstalledPlugin | None
|
|
90
|
+
checks: list[DoctorCheck] = field(default_factory=list)
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def fail_count(self) -> int:
|
|
94
|
+
return sum(1 for c in self.checks if c.status == "fail")
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def warn_count(self) -> int:
|
|
98
|
+
return sum(1 for c in self.checks if c.status == "warn")
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def ok(self) -> bool:
|
|
102
|
+
return self.fail_count == 0
|
|
103
|
+
|
|
104
|
+
def to_dict(self) -> dict[str, Any]:
|
|
105
|
+
return {
|
|
106
|
+
"name": self.name,
|
|
107
|
+
"ok": self.ok,
|
|
108
|
+
"fail_count": self.fail_count,
|
|
109
|
+
"warn_count": self.warn_count,
|
|
110
|
+
"plugin": self.plugin.to_dict() if self.plugin else None,
|
|
111
|
+
"checks": [c.to_dict() for c in self.checks],
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
# ── Discovery ───────────────────────────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _entry_points_in_group(group: str) -> list[EntryPoint]:
|
|
119
|
+
"""Defensive wrapper: entry-point machinery sometimes raises in odd envs."""
|
|
120
|
+
try:
|
|
121
|
+
return list(entry_points(group=group))
|
|
122
|
+
except Exception: # noqa: BLE001 — degrade gracefully on weird metadata states
|
|
123
|
+
return []
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _ep_distribution_info(ep: EntryPoint) -> tuple[str, str | None]:
|
|
127
|
+
"""Return ``(package_name, version)`` for an entry point, defensively.
|
|
128
|
+
|
|
129
|
+
The entry-point's ``.dist`` attribute may be None for in-tree dev
|
|
130
|
+
installs in some Python envs; fall back to ``("", None)`` then.
|
|
131
|
+
"""
|
|
132
|
+
dist = getattr(ep, "dist", None)
|
|
133
|
+
if dist is None:
|
|
134
|
+
return "", None
|
|
135
|
+
name = getattr(dist, "name", None) or getattr(dist, "metadata", {}).get("Name", "")
|
|
136
|
+
version = getattr(dist, "version", None)
|
|
137
|
+
return (name or "", version)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _skills_eps_by_name() -> dict[str, EntryPoint]:
|
|
141
|
+
return {ep.name: ep for ep in _entry_points_in_group(_GROUP_SKILLS)}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _plugin_info_eps_by_name() -> dict[str, EntryPoint]:
|
|
145
|
+
return {ep.name: ep for ep in _entry_points_in_group(_GROUP_PLUGINS)}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def plugin_info_for(name: str) -> dict[str, Any] | None:
|
|
149
|
+
"""Load and return the ``plugin_info`` dict for one plugin, or None.
|
|
150
|
+
|
|
151
|
+
Catches every exception during load — ``plugin_info`` is metadata, not
|
|
152
|
+
code, so a misformed one shouldn't crash the listing.
|
|
153
|
+
"""
|
|
154
|
+
eps = _plugin_info_eps_by_name()
|
|
155
|
+
ep = eps.get(name)
|
|
156
|
+
if ep is None:
|
|
157
|
+
return None
|
|
158
|
+
try:
|
|
159
|
+
info = ep.load()
|
|
160
|
+
except Exception: # noqa: BLE001 — see docstring
|
|
161
|
+
return None
|
|
162
|
+
if not isinstance(info, dict):
|
|
163
|
+
return None
|
|
164
|
+
# Normalize: every key is a string; fill in expected keys with "".
|
|
165
|
+
out = {
|
|
166
|
+
"name": str(info.get("name", name)),
|
|
167
|
+
"summary": str(info.get("summary", "")),
|
|
168
|
+
"homepage": str(info.get("homepage", "")),
|
|
169
|
+
"license_class": str(info.get("license_class", "")),
|
|
170
|
+
"solver_name": str(info.get("solver_name", "")),
|
|
171
|
+
}
|
|
172
|
+
return out
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def skills_dir_for(name: str):
|
|
176
|
+
"""Load and return the bundled ``_skills/<name>`` Traversable, or None.
|
|
177
|
+
|
|
178
|
+
The ``sim.skills`` entry-point should resolve to a Traversable already
|
|
179
|
+
pointing at the plugin's ``_skills/`` directory. We then descend one
|
|
180
|
+
level to the per-driver subdir using the driver name.
|
|
181
|
+
"""
|
|
182
|
+
eps = _skills_eps_by_name()
|
|
183
|
+
ep = eps.get(name)
|
|
184
|
+
if ep is None:
|
|
185
|
+
return None
|
|
186
|
+
try:
|
|
187
|
+
traversable = ep.load()
|
|
188
|
+
except Exception: # noqa: BLE001
|
|
189
|
+
return None
|
|
190
|
+
# If the entry-point already points at <pkg>/_skills/, descend to <name>.
|
|
191
|
+
candidate = traversable.joinpath(name) if hasattr(traversable, "joinpath") else None
|
|
192
|
+
if candidate is not None and getattr(candidate, "is_dir", lambda: False)():
|
|
193
|
+
return candidate
|
|
194
|
+
# Otherwise treat it as the leaf already.
|
|
195
|
+
return traversable
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def list_installed_plugins() -> list[InstalledPlugin]:
|
|
199
|
+
"""Enumerate every driver currently registered, with metadata where present.
|
|
200
|
+
|
|
201
|
+
Walks the ``sim.drivers`` registry (built-ins + externally discovered
|
|
202
|
+
entry-points). For each entry, looks up matching ``sim.plugins`` and
|
|
203
|
+
``sim.skills`` entries. Returns one row per registered driver.
|
|
204
|
+
"""
|
|
205
|
+
from sim.drivers import _BUILTIN_REGISTRY, _REGISTRY # local — see compat.py
|
|
206
|
+
|
|
207
|
+
builtin_names = {n for n, _ in _BUILTIN_REGISTRY}
|
|
208
|
+
skills_eps = _skills_eps_by_name()
|
|
209
|
+
info_eps = _plugin_info_eps_by_name()
|
|
210
|
+
drivers_eps = {ep.name: ep for ep in _entry_points_in_group(_GROUP_DRIVERS)}
|
|
211
|
+
|
|
212
|
+
rows: list[InstalledPlugin] = []
|
|
213
|
+
for driver_name, spec in _REGISTRY:
|
|
214
|
+
module_path, _, _cls = spec.rpartition(":")
|
|
215
|
+
is_builtin = driver_name in builtin_names
|
|
216
|
+
|
|
217
|
+
package, version = "", None
|
|
218
|
+
ep_info: dict[str, Any] | None = None
|
|
219
|
+
|
|
220
|
+
if not is_builtin and driver_name in drivers_eps:
|
|
221
|
+
package, version = _ep_distribution_info(drivers_eps[driver_name])
|
|
222
|
+
if is_builtin:
|
|
223
|
+
# The host package itself ships the built-in drivers.
|
|
224
|
+
package, version = _ep_distribution_info_for_host()
|
|
225
|
+
|
|
226
|
+
if driver_name in info_eps:
|
|
227
|
+
ep_info = plugin_info_for(driver_name)
|
|
228
|
+
|
|
229
|
+
rows.append(InstalledPlugin(
|
|
230
|
+
name=driver_name,
|
|
231
|
+
package=package,
|
|
232
|
+
version=version,
|
|
233
|
+
summary=(ep_info or {}).get("summary", ""),
|
|
234
|
+
homepage=(ep_info or {}).get("homepage", ""),
|
|
235
|
+
license_class=(ep_info or {}).get("license_class", ""),
|
|
236
|
+
solver_name=(ep_info or {}).get("solver_name", ""),
|
|
237
|
+
has_skills=driver_name in skills_eps,
|
|
238
|
+
builtin=is_builtin,
|
|
239
|
+
driver_module=module_path,
|
|
240
|
+
))
|
|
241
|
+
return rows
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _ep_distribution_info_for_host() -> tuple[str, str | None]:
|
|
245
|
+
"""Look up the host package (sim-cli itself) — used for built-in driver rows."""
|
|
246
|
+
try:
|
|
247
|
+
from importlib.metadata import distribution
|
|
248
|
+
# sim-cli-core is the canonical PyPI dist name; sim-runtime is kept
|
|
249
|
+
# as a fallback for editable installs predating the Phase 4 rename.
|
|
250
|
+
for candidate in ("sim-cli-core", "sim-runtime"):
|
|
251
|
+
try:
|
|
252
|
+
dist = distribution(candidate)
|
|
253
|
+
return dist.name or candidate, dist.version
|
|
254
|
+
except Exception: # noqa: BLE001
|
|
255
|
+
continue
|
|
256
|
+
except Exception: # noqa: BLE001
|
|
257
|
+
pass
|
|
258
|
+
return "", None
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
# ── Doctor ──────────────────────────────────────────────────────────────────
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def doctor(name: str, deep: bool = False) -> DoctorReport:
|
|
265
|
+
"""Validate one plugin end-to-end. Never raises; everything turns into checks."""
|
|
266
|
+
plugins = {p.name: p for p in list_installed_plugins()}
|
|
267
|
+
plugin = plugins.get(name)
|
|
268
|
+
report = DoctorReport(name=name, plugin=plugin)
|
|
269
|
+
|
|
270
|
+
if plugin is None:
|
|
271
|
+
report.checks.append(DoctorCheck(
|
|
272
|
+
label="registered",
|
|
273
|
+
status="fail",
|
|
274
|
+
message=f"no driver registered as {name!r}",
|
|
275
|
+
))
|
|
276
|
+
return report
|
|
277
|
+
|
|
278
|
+
report.checks.append(DoctorCheck(
|
|
279
|
+
label="registered",
|
|
280
|
+
status="ok",
|
|
281
|
+
message=f"{plugin.driver_module}",
|
|
282
|
+
))
|
|
283
|
+
|
|
284
|
+
# 2. Driver class importable.
|
|
285
|
+
try:
|
|
286
|
+
from sim.drivers import _resolve as _resolve_driver
|
|
287
|
+
driver = _resolve_driver(name)
|
|
288
|
+
except Exception as e: # noqa: BLE001 — surface any import failure
|
|
289
|
+
report.checks.append(DoctorCheck(
|
|
290
|
+
label="driver_imports",
|
|
291
|
+
status="fail",
|
|
292
|
+
message=f"{type(e).__name__}: {e}",
|
|
293
|
+
))
|
|
294
|
+
return report
|
|
295
|
+
report.checks.append(DoctorCheck(
|
|
296
|
+
label="driver_imports",
|
|
297
|
+
status="ok",
|
|
298
|
+
))
|
|
299
|
+
|
|
300
|
+
# 3. Structural protocol conformance.
|
|
301
|
+
from sim.driver import DriverProtocol
|
|
302
|
+
if isinstance(driver, DriverProtocol):
|
|
303
|
+
report.checks.append(DoctorCheck(label="protocol_conforms", status="ok"))
|
|
304
|
+
else:
|
|
305
|
+
report.checks.append(DoctorCheck(
|
|
306
|
+
label="protocol_conforms",
|
|
307
|
+
status="warn",
|
|
308
|
+
message="instance does not match DriverProtocol structurally",
|
|
309
|
+
))
|
|
310
|
+
|
|
311
|
+
# 4. Skills bundle.
|
|
312
|
+
if plugin.has_skills:
|
|
313
|
+
skills = skills_dir_for(name)
|
|
314
|
+
if skills is None:
|
|
315
|
+
report.checks.append(DoctorCheck(
|
|
316
|
+
label="skills_bundle",
|
|
317
|
+
status="warn",
|
|
318
|
+
message="sim.skills entry-point present but failed to load",
|
|
319
|
+
))
|
|
320
|
+
else:
|
|
321
|
+
has_skill_md = False
|
|
322
|
+
try:
|
|
323
|
+
has_skill_md = skills.joinpath("SKILL.md").is_file()
|
|
324
|
+
except Exception: # noqa: BLE001
|
|
325
|
+
pass
|
|
326
|
+
if has_skill_md:
|
|
327
|
+
report.checks.append(DoctorCheck(label="skills_bundle", status="ok"))
|
|
328
|
+
else:
|
|
329
|
+
report.checks.append(DoctorCheck(
|
|
330
|
+
label="skills_bundle",
|
|
331
|
+
status="warn",
|
|
332
|
+
message="bundled _skills/<name>/ has no SKILL.md",
|
|
333
|
+
))
|
|
334
|
+
else:
|
|
335
|
+
# Built-ins typically don't have skills bundled; not a regression.
|
|
336
|
+
if plugin.builtin:
|
|
337
|
+
report.checks.append(DoctorCheck(
|
|
338
|
+
label="skills_bundle",
|
|
339
|
+
status="info",
|
|
340
|
+
message="built-in driver; no bundled skills (expected)",
|
|
341
|
+
))
|
|
342
|
+
else:
|
|
343
|
+
report.checks.append(DoctorCheck(
|
|
344
|
+
label="skills_bundle",
|
|
345
|
+
status="warn",
|
|
346
|
+
message="external plugin has no sim.skills entry-point",
|
|
347
|
+
))
|
|
348
|
+
|
|
349
|
+
# 5. Compatibility.yaml present and parses.
|
|
350
|
+
from sim.compat import load_compatibility_by_name
|
|
351
|
+
compat = load_compatibility_by_name(name)
|
|
352
|
+
if compat is None:
|
|
353
|
+
# Many drivers legitimately have no compat.yaml (SDK-less / version-insensitive).
|
|
354
|
+
report.checks.append(DoctorCheck(
|
|
355
|
+
label="compatibility_yaml",
|
|
356
|
+
status="info",
|
|
357
|
+
message="no compatibility.yaml (driver is SDK-less or version-insensitive)",
|
|
358
|
+
))
|
|
359
|
+
else:
|
|
360
|
+
report.checks.append(DoctorCheck(
|
|
361
|
+
label="compatibility_yaml",
|
|
362
|
+
status="ok",
|
|
363
|
+
message=f"{len(compat.profiles)} profile(s)",
|
|
364
|
+
))
|
|
365
|
+
|
|
366
|
+
# 6. Optional deep check: detect_installed.
|
|
367
|
+
if deep:
|
|
368
|
+
try:
|
|
369
|
+
from sim.compat import safe_detect_installed
|
|
370
|
+
installs = safe_detect_installed(driver)
|
|
371
|
+
if installs:
|
|
372
|
+
report.checks.append(DoctorCheck(
|
|
373
|
+
label="solver_detected",
|
|
374
|
+
status="ok",
|
|
375
|
+
message=f"{len(installs)} install(s)",
|
|
376
|
+
))
|
|
377
|
+
else:
|
|
378
|
+
report.checks.append(DoctorCheck(
|
|
379
|
+
label="solver_detected",
|
|
380
|
+
status="info",
|
|
381
|
+
message="no installation found on this host",
|
|
382
|
+
))
|
|
383
|
+
except Exception as e: # noqa: BLE001
|
|
384
|
+
report.checks.append(DoctorCheck(
|
|
385
|
+
label="solver_detected",
|
|
386
|
+
status="warn",
|
|
387
|
+
message=f"detect_installed raised {type(e).__name__}: {e}",
|
|
388
|
+
))
|
|
389
|
+
|
|
390
|
+
return report
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def doctor_all(deep: bool = False) -> list[DoctorReport]:
|
|
394
|
+
"""Run doctor on every registered driver. Order matches the registry."""
|
|
395
|
+
return [doctor(p.name, deep=deep) for p in list_installed_plugins()]
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
# ── Skills sync ─────────────────────────────────────────────────────────────
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def sync_skills_to(target_dir, *, copy: bool = False) -> dict[str, Any]:
|
|
402
|
+
"""Materialize every installed plugin's ``_skills/<name>/`` under ``target_dir``.
|
|
403
|
+
|
|
404
|
+
Default mode is symlink (idempotent, near-free); ``copy=True`` falls
|
|
405
|
+
back to recursive copy for environments where symlinks are blocked
|
|
406
|
+
(Windows without developer mode).
|
|
407
|
+
|
|
408
|
+
Returns ``{"ok": bool, "linked": [...], "copied": [...], "skipped": [...]}``.
|
|
409
|
+
Skips drivers without a ``sim.skills`` entry-point (built-ins, OSS plugins
|
|
410
|
+
that haven't shipped skills yet).
|
|
411
|
+
"""
|
|
412
|
+
from pathlib import Path
|
|
413
|
+
|
|
414
|
+
target = Path(target_dir)
|
|
415
|
+
target.mkdir(parents=True, exist_ok=True)
|
|
416
|
+
|
|
417
|
+
linked: list[str] = []
|
|
418
|
+
copied: list[str] = []
|
|
419
|
+
skipped: list[str] = []
|
|
420
|
+
|
|
421
|
+
for plugin in list_installed_plugins():
|
|
422
|
+
if not plugin.has_skills:
|
|
423
|
+
skipped.append(plugin.name)
|
|
424
|
+
continue
|
|
425
|
+
skills = skills_dir_for(plugin.name)
|
|
426
|
+
if skills is None:
|
|
427
|
+
skipped.append(plugin.name)
|
|
428
|
+
continue
|
|
429
|
+
|
|
430
|
+
# Resolve the Traversable to a real filesystem path. importlib.resources
|
|
431
|
+
# returns a MultiplexedPath / PosixPath / WindowsPath depending on the
|
|
432
|
+
# install type. _resources.as_file() yields a real path.
|
|
433
|
+
with _resources.as_file(skills) as src_path:
|
|
434
|
+
dest = target / plugin.name
|
|
435
|
+
if dest.is_symlink() or dest.exists():
|
|
436
|
+
try:
|
|
437
|
+
if dest.is_symlink():
|
|
438
|
+
dest.unlink()
|
|
439
|
+
elif dest.is_dir():
|
|
440
|
+
import shutil
|
|
441
|
+
shutil.rmtree(dest)
|
|
442
|
+
else:
|
|
443
|
+
dest.unlink()
|
|
444
|
+
except OSError:
|
|
445
|
+
skipped.append(plugin.name)
|
|
446
|
+
continue
|
|
447
|
+
|
|
448
|
+
if copy:
|
|
449
|
+
import shutil
|
|
450
|
+
shutil.copytree(src_path, dest)
|
|
451
|
+
copied.append(plugin.name)
|
|
452
|
+
else:
|
|
453
|
+
try:
|
|
454
|
+
dest.symlink_to(src_path, target_is_directory=True)
|
|
455
|
+
linked.append(plugin.name)
|
|
456
|
+
except (OSError, NotImplementedError):
|
|
457
|
+
# Windows / no privilege — fall back to copy.
|
|
458
|
+
import shutil
|
|
459
|
+
shutil.copytree(src_path, dest)
|
|
460
|
+
copied.append(plugin.name)
|
|
461
|
+
|
|
462
|
+
return {
|
|
463
|
+
"ok": True,
|
|
464
|
+
"target": str(target),
|
|
465
|
+
"linked": linked,
|
|
466
|
+
"copied": copied,
|
|
467
|
+
"skipped": skipped,
|
|
468
|
+
}
|
sim/runner.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""Execution helpers for sim one-shot runs."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
import subprocess
|
|
6
|
+
import sys
|
|
7
|
+
import time
|
|
8
|
+
from datetime import datetime, timezone
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from sim.driver import RunResult
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# Generic error patterns that indicate failure regardless of exit code.
|
|
15
|
+
# Each driver can add solver-specific patterns on top.
|
|
16
|
+
_GENERIC_ERROR_PATTERNS = [
|
|
17
|
+
re.compile(r"^Traceback \(most recent call last\):", re.MULTILINE),
|
|
18
|
+
re.compile(r"^(Error|ERROR|Fatal error|FATAL):", re.MULTILINE),
|
|
19
|
+
re.compile(r"(?:Exception|Error): .+", re.MULTILINE),
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def detect_output_errors(stdout: str, stderr: str) -> list[str]:
|
|
24
|
+
"""Scan stdout and stderr for generic error patterns.
|
|
25
|
+
|
|
26
|
+
Returns a list of human-readable error descriptions found.
|
|
27
|
+
Drivers should call this first, then append solver-specific checks.
|
|
28
|
+
"""
|
|
29
|
+
errors: list[str] = []
|
|
30
|
+
for text, source in [(stderr, "stderr"), (stdout, "stdout")]:
|
|
31
|
+
if not text:
|
|
32
|
+
continue
|
|
33
|
+
for pat in _GENERIC_ERROR_PATTERNS:
|
|
34
|
+
m = pat.search(text)
|
|
35
|
+
if m:
|
|
36
|
+
# Extract the matching line plus context
|
|
37
|
+
line = m.group(0)[:200]
|
|
38
|
+
errors.append(f"[{source}] {line}")
|
|
39
|
+
return errors
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def run_subprocess(
|
|
43
|
+
command: list[str],
|
|
44
|
+
*,
|
|
45
|
+
script: Path,
|
|
46
|
+
solver: str,
|
|
47
|
+
) -> RunResult:
|
|
48
|
+
"""Execute a subprocess and capture a RunResult."""
|
|
49
|
+
start = time.monotonic()
|
|
50
|
+
proc = subprocess.run(
|
|
51
|
+
command,
|
|
52
|
+
capture_output=True,
|
|
53
|
+
text=True,
|
|
54
|
+
)
|
|
55
|
+
duration = time.monotonic() - start
|
|
56
|
+
|
|
57
|
+
stdout = proc.stdout.strip()
|
|
58
|
+
stderr = proc.stderr.strip()
|
|
59
|
+
errors = detect_output_errors(stdout, stderr)
|
|
60
|
+
|
|
61
|
+
# If exit code is 0 but errors detected in output, override to 1
|
|
62
|
+
exit_code = proc.returncode
|
|
63
|
+
if exit_code == 0 and errors:
|
|
64
|
+
exit_code = 1
|
|
65
|
+
|
|
66
|
+
return RunResult(
|
|
67
|
+
exit_code=exit_code,
|
|
68
|
+
stdout=stdout,
|
|
69
|
+
stderr=stderr,
|
|
70
|
+
duration_s=round(duration, 3),
|
|
71
|
+
script=str(script),
|
|
72
|
+
solver=solver,
|
|
73
|
+
timestamp=datetime.now(timezone.utc).isoformat(),
|
|
74
|
+
errors=errors,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def execute_script(
|
|
79
|
+
script: Path,
|
|
80
|
+
python: str | None = None,
|
|
81
|
+
solver: str = "unknown",
|
|
82
|
+
driver=None,
|
|
83
|
+
) -> RunResult:
|
|
84
|
+
"""Execute a script, delegating to the solver driver when available.
|
|
85
|
+
|
|
86
|
+
Wraps the actual run with a workspace mtime snapshot so the resulting
|
|
87
|
+
``RunResult.workspace_delta`` lists files added/modified under cwd
|
|
88
|
+
during the run. Solver-neutral: works for any subprocess that writes
|
|
89
|
+
to disk (CFD log files, postProcessing dirs, mesh output, etc.).
|
|
90
|
+
"""
|
|
91
|
+
cwd = Path.cwd()
|
|
92
|
+
before = _snapshot_workspace(cwd)
|
|
93
|
+
|
|
94
|
+
if driver is not None:
|
|
95
|
+
result = driver.run_file(script)
|
|
96
|
+
else:
|
|
97
|
+
if python is None:
|
|
98
|
+
python = sys.executable
|
|
99
|
+
result = run_subprocess(
|
|
100
|
+
[python, str(script)],
|
|
101
|
+
script=script,
|
|
102
|
+
solver=solver,
|
|
103
|
+
)
|
|
104
|
+
_attach_probes(result, solver)
|
|
105
|
+
|
|
106
|
+
after = _snapshot_workspace(cwd)
|
|
107
|
+
result.workspace_delta = _diff_workspace(before, after)
|
|
108
|
+
return result
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# ── Workspace observation ──────────────────────────────────────────────
|
|
112
|
+
# Solver-neutral capture of "what did this run do to the filesystem".
|
|
113
|
+
# Snapshots cwd recursively before+after the subprocess and reports new /
|
|
114
|
+
# modified files. Any solver that writes to disk (every CFD/CAE solver
|
|
115
|
+
# does) gets visibility for free; sim doesn't have to know per-solver
|
|
116
|
+
# file conventions.
|
|
117
|
+
|
|
118
|
+
_WORKSPACE_MAX_FILES = 50000 # cap to keep snapshot bounded on huge dirs
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _snapshot_workspace(root: Path) -> dict[str, tuple[float, int]]:
|
|
122
|
+
"""Recursively walk ``root``; return {path: (mtime, size)}.
|
|
123
|
+
|
|
124
|
+
Bounded by ``_WORKSPACE_MAX_FILES``; on overflow we silently stop
|
|
125
|
+
enumerating — better partial visibility than blocking the run.
|
|
126
|
+
Inaccessible files are skipped (not raised).
|
|
127
|
+
"""
|
|
128
|
+
out: dict[str, tuple[float, int]] = {}
|
|
129
|
+
if not root.is_dir():
|
|
130
|
+
return out
|
|
131
|
+
try:
|
|
132
|
+
for p in root.rglob("*"):
|
|
133
|
+
if not p.is_file():
|
|
134
|
+
continue
|
|
135
|
+
try:
|
|
136
|
+
st = p.stat()
|
|
137
|
+
except OSError:
|
|
138
|
+
continue
|
|
139
|
+
out[str(p)] = (st.st_mtime, st.st_size)
|
|
140
|
+
if len(out) >= _WORKSPACE_MAX_FILES:
|
|
141
|
+
break
|
|
142
|
+
except OSError:
|
|
143
|
+
pass
|
|
144
|
+
return out
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _diff_workspace(
|
|
148
|
+
before: dict[str, tuple[float, int]],
|
|
149
|
+
after: dict[str, tuple[float, int]],
|
|
150
|
+
) -> list[dict]:
|
|
151
|
+
"""Return list of new/modified files. Stable files don't appear."""
|
|
152
|
+
delta: list[dict] = []
|
|
153
|
+
for path, (mt, sz) in after.items():
|
|
154
|
+
old = before.get(path)
|
|
155
|
+
if old is None:
|
|
156
|
+
delta.append({"path": path, "kind": "added", "size": sz})
|
|
157
|
+
elif old[0] != mt or old[1] != sz:
|
|
158
|
+
delta.append({"path": path, "kind": "modified", "size": sz})
|
|
159
|
+
# Sort by size desc so the agent sees the biggest writes first
|
|
160
|
+
# (typically the most informative — solver logs, mesh output, etc.).
|
|
161
|
+
delta.sort(key=lambda d: -d["size"])
|
|
162
|
+
return delta
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _attach_probes(result: RunResult, solver: str) -> None:
|
|
166
|
+
"""Run generic probes on a completed one-shot RunResult (in-place).
|
|
167
|
+
|
|
168
|
+
Probes applicable to subprocess one-shot runs:
|
|
169
|
+
#1 ProcessMetaProbe — exit_code + wall_time (from result fields)
|
|
170
|
+
#3 StdoutJsonTailProbe — last JSON line on stdout
|
|
171
|
+
#3+ PythonTracebackProbe — tracebacks in stderr
|
|
172
|
+
#5 DomainExceptionMapProbe — upgrades python.* exception codes
|
|
173
|
+
|
|
174
|
+
Not applicable to one-shot runs (no live session, no workdir baseline):
|
|
175
|
+
#1+ RuntimeTimeoutProbe — no hung-snippet detection for subprocesses
|
|
176
|
+
#4 SdkAttributeProbe — no live session namespace
|
|
177
|
+
#9 WorkdirDiffProbe — skipped (workdir_before=None → applies()=False)
|
|
178
|
+
"""
|
|
179
|
+
try:
|
|
180
|
+
from sim.inspect import InspectCtx, collect_diagnostics, generic_probes
|
|
181
|
+
ctx = InspectCtx(
|
|
182
|
+
stdout=result.stdout,
|
|
183
|
+
stderr=result.stderr,
|
|
184
|
+
workdir=str(Path(result.script).parent),
|
|
185
|
+
wall_time_s=result.duration_s,
|
|
186
|
+
exit_code=result.exit_code,
|
|
187
|
+
driver_name=solver,
|
|
188
|
+
session_ns={},
|
|
189
|
+
workdir_before=None, # no baseline → WorkdirDiffProbe skipped
|
|
190
|
+
)
|
|
191
|
+
diags, arts = collect_diagnostics(generic_probes(), ctx)
|
|
192
|
+
result.diagnostics = [d.to_dict() for d in diags]
|
|
193
|
+
result.artifacts = [a.to_dict() for a in arts]
|
|
194
|
+
except Exception:
|
|
195
|
+
pass # probes must never break the run path
|