jcmsuite 7.4.2__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.
Potentially problematic release.
This version of jcmsuite might be problematic. Click here for more details.
- jcmsuite/__init__.py +22 -0
- jcmsuite/_build_info.py +4 -0
- jcmsuite/context/__init__.py +44 -0
- jcmsuite/context/interface_metadata.py +158 -0
- jcmsuite/context/models.py +213 -0
- jcmsuite/context/scope.py +210 -0
- jcmsuite/context/solver_context.py +569 -0
- jcmsuite/context/subprocess_tools.py +172 -0
- jcmsuite/contract.py +47 -0
- jcmsuite/daemon/__init__.py +19 -0
- jcmsuite/daemon/handler.py +925 -0
- jcmsuite/daemon/internal/__init__.py +4 -0
- jcmsuite/daemon/internal/bundle_protocol.py +64 -0
- jcmsuite/daemon/internal/handler_helpers.py +368 -0
- jcmsuite/daemon/internal/handler_resource_jobs.py +227 -0
- jcmsuite/daemon/internal/handler_wait.py +564 -0
- jcmsuite/daemon/models.py +349 -0
- jcmsuite/daemon/namespaces.py +250 -0
- jcmsuite/daemon/protocol.py +801 -0
- jcmsuite/daemon/resources.py +407 -0
- jcmsuite/daemon/scope.py +53 -0
- jcmsuite/daemon/transport.py +223 -0
- jcmsuite/editing/__init__.py +10 -0
- jcmsuite/editing/editor.py +274 -0
- jcmsuite/exceptions/__init__.py +32 -0
- jcmsuite/exceptions/errors.py +173 -0
- jcmsuite/geo/__init__.py +10 -0
- jcmsuite/geo/mesh.py +302 -0
- jcmsuite/info/__init__.py +10 -0
- jcmsuite/info/runtime_info.py +45 -0
- jcmsuite/internal/__init__.py +2 -0
- jcmsuite/internal/diagnostics.py +74 -0
- jcmsuite/internal/progress.py +80 -0
- jcmsuite/loaders/__init__.py +18 -0
- jcmsuite/loaders/blob_header.py +315 -0
- jcmsuite/loaders/cartesian_field_reader.py +249 -0
- jcmsuite/loaders/cartesian_fields.py +324 -0
- jcmsuite/loaders/nested_mapping.py +69 -0
- jcmsuite/loaders/powerflux.py +136 -0
- jcmsuite/loaders/table.py +637 -0
- jcmsuite/logging/__init__.py +22 -0
- jcmsuite/logging/runtime.py +210 -0
- jcmsuite/results/__init__.py +126 -0
- jcmsuite/results/artifacts.py +806 -0
- jcmsuite/results/discovery.py +82 -0
- jcmsuite/results/field_exports.py +1062 -0
- jcmsuite/results/field_payloads.py +453 -0
- jcmsuite/results/internal/__init__.py +4 -0
- jcmsuite/results/internal/export/__init__.py +4 -0
- jcmsuite/results/internal/export/common.py +56 -0
- jcmsuite/results/internal/export/pandas/__init__.py +4 -0
- jcmsuite/results/internal/export/pandas/common.py +165 -0
- jcmsuite/results/internal/export/pandas/payload.py +480 -0
- jcmsuite/results/internal/export/pandas/rows.py +484 -0
- jcmsuite/results/internal/export/preview.py +178 -0
- jcmsuite/results/internal/export/xarray/__init__.py +4 -0
- jcmsuite/results/internal/export/xarray/export.py +688 -0
- jcmsuite/results/internal/export/xarray/grouped.py +608 -0
- jcmsuite/results/internal/export/xarray/headers.py +290 -0
- jcmsuite/results/internal/export/xarray/runtime.py +99 -0
- jcmsuite/results/internal/export/xarray/shared.py +246 -0
- jcmsuite/results/internal/export/xarray/tabular.py +304 -0
- jcmsuite/results/internal/export/xarray/types.py +28 -0
- jcmsuite/results/internal/models/__init__.py +4 -0
- jcmsuite/results/internal/models/batch.py +1081 -0
- jcmsuite/results/internal/models/batch_postprocess.py +1728 -0
- jcmsuite/results/internal/models/bundle.py +343 -0
- jcmsuite/results/internal/models/core.py +556 -0
- jcmsuite/results/internal/models/submission.py +1157 -0
- jcmsuite/results/internal/resultbag/__init__.py +4 -0
- jcmsuite/results/internal/resultbag/access.py +489 -0
- jcmsuite/results/internal/resultbag/iteration.py +199 -0
- jcmsuite/results/internal/resultbag/query.py +226 -0
- jcmsuite/results/internal/resultbag/raw.py +407 -0
- jcmsuite/results/internal/resultbag/source_files.py +142 -0
- jcmsuite/results/internal/resultbag/store.py +881 -0
- jcmsuite/results/internal/resultbag/utils.py +258 -0
- jcmsuite/results/models.py +76 -0
- jcmsuite/results/opening.py +55 -0
- jcmsuite/results/pandas_export.py +325 -0
- jcmsuite/results/payloads.py +478 -0
- jcmsuite/results/postprocess.py +97 -0
- jcmsuite/results/postprocess_payloads.py +757 -0
- jcmsuite/results/postprocess_record.py +418 -0
- jcmsuite/results/resultbag.py +279 -0
- jcmsuite/results/resultbag_management.py +433 -0
- jcmsuite/results/structured_derivatives.py +387 -0
- jcmsuite/solve/__init__.py +10 -0
- jcmsuite/solve/daemon_bundle_staging.py +100 -0
- jcmsuite/solve/daemon_bundle_wait.py +89 -0
- jcmsuite/solve/orchestration.py +1576 -0
- jcmsuite/solve/orchestration_execution.py +180 -0
- jcmsuite/solve/orchestration_routing.py +128 -0
- jcmsuite/solve/orchestration_support.py +323 -0
- jcmsuite/solve/path_resolution.py +98 -0
- jcmsuite/solve/progress.py +20 -0
- jcmsuite/solve/solver.py +709 -0
- jcmsuite/solve/staging.py +264 -0
- jcmsuite/solve/types.py +25 -0
- jcmsuite/solve/working_dirs.py +227 -0
- jcmsuite/templates/__init__.py +11 -0
- jcmsuite/templates/converter.py +71 -0
- jcmsuite/templates/formatting.py +335 -0
- jcmsuite/templates/formatting_cli.py +47 -0
- jcmsuite/templates/rendering.py +666 -0
- jcmsuite/torch/__init__.py +25 -0
- jcmsuite/torch/autograd.py +488 -0
- jcmsuite/torch/coordinate_policy.py +82 -0
- jcmsuite/torch/core.py +312 -0
- jcmsuite/torch/results/__init__.py +51 -0
- jcmsuite/torch/types.py +84 -0
- jcmsuite/torch/validation.py +208 -0
- jcmsuite/torch/wrappers.py +4332 -0
- jcmsuite/view/__init__.py +10 -0
- jcmsuite/view/viewer.py +122 -0
- jcmsuite-7.4.2.dist-info/METADATA +180 -0
- jcmsuite-7.4.2.dist-info/RECORD +119 -0
- jcmsuite-7.4.2.dist-info/WHEEL +4 -0
- jcmsuite-7.4.2.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Encode one ordered bundle using the daemon's repeated-ProjectFile contract."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from jcmsuite_data_tree import (
|
|
13
|
+
TreeDir,
|
|
14
|
+
create_tree_dir,
|
|
15
|
+
data_primitives,
|
|
16
|
+
serialize_tree_dir,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
from ...exceptions import InvalidConfigurationError
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def build_bundle_submit_command(
|
|
23
|
+
*,
|
|
24
|
+
project_files: Sequence[Path],
|
|
25
|
+
mode: str,
|
|
26
|
+
resources: Sequence[int] | None,
|
|
27
|
+
log_file: Path | None,
|
|
28
|
+
) -> str:
|
|
29
|
+
"""Serialize an ordered bundle without indexed primitive names."""
|
|
30
|
+
|
|
31
|
+
if not project_files:
|
|
32
|
+
raise InvalidConfigurationError(
|
|
33
|
+
"A daemon submission requires at least one project."
|
|
34
|
+
)
|
|
35
|
+
submit: TreeDir = create_tree_dir("SubmitJob")
|
|
36
|
+
for path in project_files:
|
|
37
|
+
_add_string(submit, "ProjectFile", str(path))
|
|
38
|
+
_add_string(submit, "Mode", mode)
|
|
39
|
+
submit.AddPrimitive(
|
|
40
|
+
"Resource",
|
|
41
|
+
data_primitives.VectorPrimitive.Create(
|
|
42
|
+
[] if resources is None else list(resources), int
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
if log_file is not None:
|
|
46
|
+
_add_string(submit, "LogFile", str(log_file))
|
|
47
|
+
task: TreeDir = create_tree_dir("Task")
|
|
48
|
+
task.AddTreeDir(submit)
|
|
49
|
+
root: TreeDir = create_tree_dir("root")
|
|
50
|
+
root.AddTreeDir(task)
|
|
51
|
+
serialized: str = serialize_tree_dir(root).lstrip("\n")
|
|
52
|
+
# The general tree writer emits ProjectFile(1) for the second path. The
|
|
53
|
+
# daemon wire format repeats ProjectFile instead. Only remove indices on
|
|
54
|
+
# those assignment names; quoted paths and unrelated commands stay intact.
|
|
55
|
+
return re.sub(r"(?m)^(\s*ProjectFile)\(\d+\)(\s*=)", r"\1\2", serialized)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _add_string(tree: TreeDir, name: str, value: str) -> None:
|
|
59
|
+
"""Add a quoted string with the same escaping as single-project commands."""
|
|
60
|
+
|
|
61
|
+
escaped: str = value.replace("\\", "\\\\").replace('"', '\\"')
|
|
62
|
+
tree.AddPrimitive(
|
|
63
|
+
name, data_primitives.StringPrimitive.Create(quotMarks=True, strArg=escaped)
|
|
64
|
+
)
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Internal helper models/utilities for daemon handler orchestration."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
from collections.abc import Iterable, Sequence
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Literal, cast
|
|
14
|
+
|
|
15
|
+
from ...exceptions import BackendUnavailableError, InvalidConfigurationError
|
|
16
|
+
from ...internal import progress as _progress
|
|
17
|
+
from ...results import (
|
|
18
|
+
BatchSolveEntry,
|
|
19
|
+
BatchSolveFailure,
|
|
20
|
+
BatchSolveResult,
|
|
21
|
+
BatchSolveSubmission,
|
|
22
|
+
BundledSolveResult,
|
|
23
|
+
SolveExecutionLog,
|
|
24
|
+
SolveResult,
|
|
25
|
+
SolveSubmission,
|
|
26
|
+
)
|
|
27
|
+
from ..models import DaemonResource, PendingDaemonJob
|
|
28
|
+
from ..protocol import extract_return_value
|
|
29
|
+
|
|
30
|
+
NoopProgressBar = _progress.NoopProgressBar
|
|
31
|
+
ProgressBar = _progress.ProgressBar
|
|
32
|
+
_TrackedTqdmProgressBar = _progress._TrackedTqdmProgressBar
|
|
33
|
+
create_progress_bar = _progress.create_progress_bar
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(slots=True, frozen=True)
|
|
37
|
+
class WaitRequest:
|
|
38
|
+
"""Normalized wait-request payload."""
|
|
39
|
+
|
|
40
|
+
mode: Literal["single_submission", "batch_submission"]
|
|
41
|
+
job_ids: tuple[int, ...]
|
|
42
|
+
single_submission: SolveSubmission | None = None
|
|
43
|
+
batch_submission: BatchSolveSubmission | None = None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def normalize_status_values(status_payload: object) -> list[str]:
|
|
47
|
+
"""Normalize status values."""
|
|
48
|
+
|
|
49
|
+
if not isinstance(status_payload, dict):
|
|
50
|
+
return []
|
|
51
|
+
status_value: object = status_payload.get("Status")
|
|
52
|
+
if isinstance(status_value, list):
|
|
53
|
+
return [str(entry) for entry in status_value]
|
|
54
|
+
if status_value is None:
|
|
55
|
+
return []
|
|
56
|
+
return [str(status_value)]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def coerce_int(value: object, *, field_name: str) -> int:
|
|
60
|
+
"""Coerce integer to expected type."""
|
|
61
|
+
|
|
62
|
+
try:
|
|
63
|
+
if isinstance(value, bool):
|
|
64
|
+
return int(value)
|
|
65
|
+
if isinstance(value, int):
|
|
66
|
+
return value
|
|
67
|
+
if isinstance(value, float):
|
|
68
|
+
return int(value)
|
|
69
|
+
if isinstance(value, str):
|
|
70
|
+
return int(value)
|
|
71
|
+
if isinstance(value, (bytes, bytearray)):
|
|
72
|
+
return int(value)
|
|
73
|
+
except (TypeError, ValueError) as exception:
|
|
74
|
+
raise BackendUnavailableError(
|
|
75
|
+
f"Daemon returned invalid {field_name}: {value!r}"
|
|
76
|
+
) from exception
|
|
77
|
+
raise BackendUnavailableError(f"Daemon returned invalid {field_name}: {value!r}")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def coerce_id_sequence(payload: object) -> tuple[int, ...]:
|
|
81
|
+
"""Normalize daemon id payload to a stable integer tuple."""
|
|
82
|
+
|
|
83
|
+
if payload is None:
|
|
84
|
+
return ()
|
|
85
|
+
if isinstance(payload, (int, float, str, bytes, bytearray, bool)):
|
|
86
|
+
return (coerce_int(payload, field_name="resource id"),)
|
|
87
|
+
if isinstance(payload, Sequence) and not isinstance(
|
|
88
|
+
payload, (str, bytes, bytearray)
|
|
89
|
+
):
|
|
90
|
+
coerced_ids: list[int] = [
|
|
91
|
+
coerce_int(value, field_name="resource id") for value in payload
|
|
92
|
+
]
|
|
93
|
+
return tuple(coerced_ids)
|
|
94
|
+
raise BackendUnavailableError(f"Daemon returned invalid resource ids: {payload!r}")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def flatten_remote_ids(id_sets: Iterable[tuple[int, ...] | None]) -> list[int]:
|
|
98
|
+
"""Flatten optional remote-id tuples into insertion-ordered unique ids."""
|
|
99
|
+
|
|
100
|
+
seen: set[int] = set()
|
|
101
|
+
flattened: list[int] = []
|
|
102
|
+
id_set: tuple[int, ...] | None
|
|
103
|
+
for id_set in id_sets:
|
|
104
|
+
if id_set is None:
|
|
105
|
+
continue
|
|
106
|
+
resource_id: int
|
|
107
|
+
for resource_id in id_set:
|
|
108
|
+
if resource_id in seen:
|
|
109
|
+
continue
|
|
110
|
+
seen.add(resource_id)
|
|
111
|
+
flattened.append(resource_id)
|
|
112
|
+
return flattened
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def resource_hostname_chain_string(resource: DaemonResource) -> str:
|
|
116
|
+
"""Normalize a resource hostname declaration to daemon chain text."""
|
|
117
|
+
|
|
118
|
+
hostname_value: object = resource.hostname
|
|
119
|
+
if isinstance(hostname_value, str):
|
|
120
|
+
return hostname_value
|
|
121
|
+
if isinstance(hostname_value, Sequence):
|
|
122
|
+
return ";".join(str(entry) for entry in hostname_value)
|
|
123
|
+
return str(hostname_value)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def extract_return_value_if_present(response_payload: str) -> object | None:
|
|
127
|
+
"""Extract daemon `ReturnValue` if present; tolerate empty `s0=dict()` payloads."""
|
|
128
|
+
|
|
129
|
+
try:
|
|
130
|
+
return extract_return_value(response_payload)
|
|
131
|
+
except BackendUnavailableError as exception:
|
|
132
|
+
if "does not contain ReturnValue" in str(exception):
|
|
133
|
+
return None
|
|
134
|
+
raise
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def extract_tar_path(payload: object) -> str | None:
|
|
138
|
+
"""Extract `TarPath` from install-remote-environment minify payload."""
|
|
139
|
+
|
|
140
|
+
if not isinstance(payload, dict):
|
|
141
|
+
return None
|
|
142
|
+
tar_path_value: object = payload.get("TarPath")
|
|
143
|
+
if tar_path_value is None:
|
|
144
|
+
return None
|
|
145
|
+
return str(tar_path_value)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def normalize_job_record(job_payload: object) -> dict[str, object]:
|
|
149
|
+
"""Normalize daemon `Job` payload to one dictionary."""
|
|
150
|
+
|
|
151
|
+
if not isinstance(job_payload, dict):
|
|
152
|
+
return {}
|
|
153
|
+
job_entry: object = job_payload.get("Job")
|
|
154
|
+
if isinstance(job_entry, list):
|
|
155
|
+
if not job_entry:
|
|
156
|
+
return {}
|
|
157
|
+
first_entry: object = job_entry[0]
|
|
158
|
+
if isinstance(first_entry, dict):
|
|
159
|
+
return cast(dict[str, object], first_entry)
|
|
160
|
+
return {}
|
|
161
|
+
if isinstance(job_entry, dict):
|
|
162
|
+
return cast(dict[str, object], job_entry)
|
|
163
|
+
return {}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def coerce_log_message(log_payload: object) -> str:
|
|
167
|
+
"""Convert daemon log payloads to stable strings."""
|
|
168
|
+
|
|
169
|
+
if isinstance(log_payload, str):
|
|
170
|
+
return log_payload
|
|
171
|
+
if isinstance(log_payload, dict):
|
|
172
|
+
stdout_payload: object = log_payload.get("Out", "")
|
|
173
|
+
stderr_payload: object = log_payload.get("Err", "")
|
|
174
|
+
error_payload: object = log_payload.get("Error", "")
|
|
175
|
+
stdout_text: str = str(stdout_payload).strip()
|
|
176
|
+
stderr_text: str = str(stderr_payload).strip()
|
|
177
|
+
error_text: str = str(error_payload).strip()
|
|
178
|
+
log_parts: list[str] = [
|
|
179
|
+
text for text in (stdout_text, stderr_text, error_text) if text
|
|
180
|
+
]
|
|
181
|
+
return "\n".join(log_parts)
|
|
182
|
+
return str(log_payload)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def resolve_result_base_dir(pending_job: PendingDaemonJob) -> Path | None:
|
|
186
|
+
"""Resolve result base directory from a pending job payload."""
|
|
187
|
+
|
|
188
|
+
result_payload: object = pending_job.result
|
|
189
|
+
if isinstance(result_payload, BundledSolveResult):
|
|
190
|
+
project_dirs: list[str] = [
|
|
191
|
+
str(result.project_file.parent)
|
|
192
|
+
for result in result_payload
|
|
193
|
+
if result.project_file is not None
|
|
194
|
+
]
|
|
195
|
+
return Path(os.path.commonpath(project_dirs)) if project_dirs else None
|
|
196
|
+
project_file: object = getattr(result_payload, "project_file", None)
|
|
197
|
+
if isinstance(project_file, Path):
|
|
198
|
+
return project_file.parent
|
|
199
|
+
return None
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def map_execution_source_path(
|
|
203
|
+
source_path: Path,
|
|
204
|
+
source_project_mappings: tuple[tuple[Path, Path], ...],
|
|
205
|
+
) -> Path:
|
|
206
|
+
"""Map a prepared project or support-file path to its original input path."""
|
|
207
|
+
|
|
208
|
+
resolved_source_path: Path = source_path.resolve()
|
|
209
|
+
exact_targets: set[Path] = set()
|
|
210
|
+
candidate_targets: list[tuple[int, Path]] = []
|
|
211
|
+
execution_project: Path
|
|
212
|
+
original_project: Path
|
|
213
|
+
for execution_project, original_project in source_project_mappings:
|
|
214
|
+
resolved_execution_project: Path = execution_project.resolve()
|
|
215
|
+
resolved_original_project: Path = original_project.resolve()
|
|
216
|
+
if resolved_source_path == resolved_execution_project:
|
|
217
|
+
exact_targets.add(resolved_original_project)
|
|
218
|
+
continue
|
|
219
|
+
execution_root: Path = resolved_execution_project.parent
|
|
220
|
+
try:
|
|
221
|
+
relative_source: Path = resolved_source_path.relative_to(execution_root)
|
|
222
|
+
except ValueError:
|
|
223
|
+
continue
|
|
224
|
+
mapped_source: Path = (
|
|
225
|
+
resolved_original_project.parent / relative_source
|
|
226
|
+
).resolve()
|
|
227
|
+
candidate_targets.append((len(execution_root.parts), mapped_source))
|
|
228
|
+
|
|
229
|
+
if exact_targets:
|
|
230
|
+
return (
|
|
231
|
+
next(iter(exact_targets))
|
|
232
|
+
if len(exact_targets) == 1
|
|
233
|
+
else resolved_source_path
|
|
234
|
+
)
|
|
235
|
+
if not candidate_targets:
|
|
236
|
+
return resolved_source_path
|
|
237
|
+
|
|
238
|
+
most_specific_depth: int = max(depth for depth, _ in candidate_targets)
|
|
239
|
+
best_targets: set[Path] = {
|
|
240
|
+
target for depth, target in candidate_targets if depth == most_specific_depth
|
|
241
|
+
}
|
|
242
|
+
return next(iter(best_targets)) if len(best_targets) == 1 else resolved_source_path
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def extract_invalid_job_id(error_message: str) -> int | None:
|
|
246
|
+
"""Parse daemon `Invalid job id (N)` diagnostics when available."""
|
|
247
|
+
|
|
248
|
+
match: re.Match[str] | None = re.search(
|
|
249
|
+
r"Invalid job id\s*\((?P<job_id>\d+)\)",
|
|
250
|
+
error_message,
|
|
251
|
+
)
|
|
252
|
+
if match is None:
|
|
253
|
+
return None
|
|
254
|
+
return int(match.group("job_id"))
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def build_wait_request(
|
|
258
|
+
submission: SolveSubmission | BatchSolveSubmission,
|
|
259
|
+
) -> WaitRequest:
|
|
260
|
+
"""Build the normalized wait request model for submit handles."""
|
|
261
|
+
|
|
262
|
+
if isinstance(submission, SolveSubmission):
|
|
263
|
+
return WaitRequest(
|
|
264
|
+
mode="single_submission",
|
|
265
|
+
job_ids=(submission.job_id,),
|
|
266
|
+
single_submission=submission,
|
|
267
|
+
)
|
|
268
|
+
if isinstance(submission, BatchSolveSubmission):
|
|
269
|
+
return WaitRequest(
|
|
270
|
+
mode="batch_submission",
|
|
271
|
+
job_ids=tuple(submission.job_ids()),
|
|
272
|
+
batch_submission=submission,
|
|
273
|
+
)
|
|
274
|
+
raise InvalidConfigurationError(
|
|
275
|
+
"daemon.wait() expects SolveSubmission or BatchSolveSubmission."
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def convert_wait_outputs(
|
|
280
|
+
*,
|
|
281
|
+
wait_request: WaitRequest,
|
|
282
|
+
results: list[object],
|
|
283
|
+
logs: list[str | None],
|
|
284
|
+
) -> SolveResult | BatchSolveResult:
|
|
285
|
+
"""Convert collected daemon wait payloads to typed result objects."""
|
|
286
|
+
|
|
287
|
+
if wait_request.mode == "single_submission":
|
|
288
|
+
if not results:
|
|
289
|
+
submission: SolveSubmission = cast(
|
|
290
|
+
SolveSubmission, wait_request.single_submission
|
|
291
|
+
)
|
|
292
|
+
return submission.result
|
|
293
|
+
first_result: object = results[0]
|
|
294
|
+
if isinstance(first_result, SolveResult):
|
|
295
|
+
return first_result
|
|
296
|
+
raise InvalidConfigurationError(
|
|
297
|
+
"Daemon wait returned invalid single result payload."
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
submission_batch: BatchSolveSubmission = cast(
|
|
301
|
+
BatchSolveSubmission, wait_request.batch_submission
|
|
302
|
+
)
|
|
303
|
+
entries: list[BatchSolveEntry] = []
|
|
304
|
+
submission_entry: SolveSubmission
|
|
305
|
+
for index, submission_entry in enumerate(submission_batch.entries):
|
|
306
|
+
result_obj: object | None = results[index] if index < len(results) else None
|
|
307
|
+
if isinstance(result_obj, SolveResult):
|
|
308
|
+
entries.append(
|
|
309
|
+
BatchSolveEntry(
|
|
310
|
+
index=(
|
|
311
|
+
submission_entry.index
|
|
312
|
+
if submission_entry.index is not None
|
|
313
|
+
else index
|
|
314
|
+
),
|
|
315
|
+
keys=(
|
|
316
|
+
{}
|
|
317
|
+
if submission_entry.keys is None
|
|
318
|
+
else dict(submission_entry.keys)
|
|
319
|
+
),
|
|
320
|
+
result=result_obj,
|
|
321
|
+
failure=None,
|
|
322
|
+
)
|
|
323
|
+
)
|
|
324
|
+
continue
|
|
325
|
+
failure_message: str
|
|
326
|
+
failure_type: type[Exception]
|
|
327
|
+
log_message: str | None = logs[index] if index < len(logs) else None
|
|
328
|
+
if log_message is not None:
|
|
329
|
+
failure_message = log_message
|
|
330
|
+
failure_type = RuntimeError
|
|
331
|
+
else:
|
|
332
|
+
failure_message = "Daemon wait returned before this job completed."
|
|
333
|
+
failure_type = TimeoutError
|
|
334
|
+
entries.append(
|
|
335
|
+
BatchSolveEntry(
|
|
336
|
+
index=(
|
|
337
|
+
submission_entry.index
|
|
338
|
+
if submission_entry.index is not None
|
|
339
|
+
else index
|
|
340
|
+
),
|
|
341
|
+
keys=(
|
|
342
|
+
{} if submission_entry.keys is None else dict(submission_entry.keys)
|
|
343
|
+
),
|
|
344
|
+
result=None,
|
|
345
|
+
failure=BatchSolveFailure(
|
|
346
|
+
index=(
|
|
347
|
+
submission_entry.index
|
|
348
|
+
if submission_entry.index is not None
|
|
349
|
+
else index
|
|
350
|
+
),
|
|
351
|
+
keys=(
|
|
352
|
+
{}
|
|
353
|
+
if submission_entry.keys is None
|
|
354
|
+
else dict(submission_entry.keys)
|
|
355
|
+
),
|
|
356
|
+
exception_type=failure_type,
|
|
357
|
+
message=failure_message,
|
|
358
|
+
),
|
|
359
|
+
_log=SolveExecutionLog(
|
|
360
|
+
backend="daemon",
|
|
361
|
+
stdout=failure_message,
|
|
362
|
+
stderr="",
|
|
363
|
+
return_code=None,
|
|
364
|
+
streamed_to_console=False,
|
|
365
|
+
),
|
|
366
|
+
)
|
|
367
|
+
)
|
|
368
|
+
return BatchSolveResult(entries=tuple(entries))
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Internal helpers for daemon resource and submission management."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Sequence
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from ...exceptions import BackendUnavailableError
|
|
12
|
+
from ..models import DaemonResource
|
|
13
|
+
from ..protocol import (
|
|
14
|
+
build_close_job_command,
|
|
15
|
+
build_job_info_command,
|
|
16
|
+
build_register_resource_command,
|
|
17
|
+
build_remove_resource_command,
|
|
18
|
+
build_resource_info_command,
|
|
19
|
+
extract_return_value,
|
|
20
|
+
)
|
|
21
|
+
from .handler_helpers import (
|
|
22
|
+
coerce_id_sequence,
|
|
23
|
+
extract_return_value_if_present,
|
|
24
|
+
flatten_remote_ids,
|
|
25
|
+
resource_hostname_chain_string,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from ..handler import DaemonHandler
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def job_info_impl(
|
|
33
|
+
handler: "DaemonHandler",
|
|
34
|
+
*,
|
|
35
|
+
job_ids: Sequence[str | int] | None,
|
|
36
|
+
status_only: bool,
|
|
37
|
+
) -> object:
|
|
38
|
+
"""Return submission information using daemon transport or fallback cache."""
|
|
39
|
+
|
|
40
|
+
if not handler._connected:
|
|
41
|
+
raise BackendUnavailableError(
|
|
42
|
+
"Cannot query daemon jobs: daemon handler is not connected."
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
available_job_ids: list[int] = list(handler._pending_jobs.keys()) + list(
|
|
46
|
+
handler._completed_jobs.keys()
|
|
47
|
+
)
|
|
48
|
+
resolved_job_ids: list[int] = handler._resolve_job_ids(
|
|
49
|
+
raw_job_ids=job_ids,
|
|
50
|
+
available_job_ids=available_job_ids,
|
|
51
|
+
)
|
|
52
|
+
if handler._transport_connection is not None:
|
|
53
|
+
return extract_return_value(
|
|
54
|
+
handler._send_transport_command(
|
|
55
|
+
build_job_info_command(
|
|
56
|
+
job_ids=resolved_job_ids,
|
|
57
|
+
status_only=status_only,
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
fallback_statuses: list[str] = []
|
|
63
|
+
fallback_jobs: list[dict[str, object]] = []
|
|
64
|
+
resolved_job_id: int
|
|
65
|
+
for resolved_job_id in resolved_job_ids:
|
|
66
|
+
if resolved_job_id in handler._completed_jobs:
|
|
67
|
+
pending_job, log_message = handler._completed_jobs[resolved_job_id]
|
|
68
|
+
fallback_statuses.append("Finished")
|
|
69
|
+
fallback_jobs.append(
|
|
70
|
+
{
|
|
71
|
+
"Id": resolved_job_id,
|
|
72
|
+
"ExitCode": 0,
|
|
73
|
+
"Log": {"Out": log_message, "Err": ""},
|
|
74
|
+
"Result": pending_job.result,
|
|
75
|
+
}
|
|
76
|
+
)
|
|
77
|
+
else:
|
|
78
|
+
fallback_statuses.append("Running")
|
|
79
|
+
fallback_jobs.append({"Id": resolved_job_id, "ExitCode": None})
|
|
80
|
+
|
|
81
|
+
if status_only:
|
|
82
|
+
return {"Id": resolved_job_ids, "Status": fallback_statuses}
|
|
83
|
+
return {"Job": fallback_jobs}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def resource_info_impl(
|
|
87
|
+
handler: "DaemonHandler",
|
|
88
|
+
*,
|
|
89
|
+
resource_indices: Sequence[int],
|
|
90
|
+
fallback_resources: tuple[DaemonResource, ...],
|
|
91
|
+
) -> object:
|
|
92
|
+
"""Return resource information payload from daemon or local fallback."""
|
|
93
|
+
|
|
94
|
+
if not handler._connected or handler._transport_connection is None:
|
|
95
|
+
return fallback_resources
|
|
96
|
+
if not resource_indices:
|
|
97
|
+
return fallback_resources
|
|
98
|
+
remote_ids: list[int] = flatten_remote_ids(
|
|
99
|
+
handler._resource_remote_ids[index]
|
|
100
|
+
for index in resource_indices
|
|
101
|
+
if 0 <= index < len(handler._resource_remote_ids)
|
|
102
|
+
)
|
|
103
|
+
if not remote_ids and resource_indices:
|
|
104
|
+
return fallback_resources
|
|
105
|
+
return extract_return_value(
|
|
106
|
+
handler._send_transport_command(
|
|
107
|
+
build_resource_info_command(resource_ids=remote_ids)
|
|
108
|
+
)
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def kill_jobs_impl(
|
|
113
|
+
handler: "DaemonHandler",
|
|
114
|
+
*,
|
|
115
|
+
job_ids: Sequence[str | int] | None,
|
|
116
|
+
) -> tuple[int, ...]:
|
|
117
|
+
"""Close selected jobs and release local tracking handles."""
|
|
118
|
+
|
|
119
|
+
if not handler._connected:
|
|
120
|
+
raise BackendUnavailableError(
|
|
121
|
+
"Cannot kill daemon jobs: daemon handler is not connected."
|
|
122
|
+
)
|
|
123
|
+
available_job_ids: list[int] = list(handler._pending_jobs.keys()) + list(
|
|
124
|
+
handler._completed_jobs.keys()
|
|
125
|
+
)
|
|
126
|
+
resolved_job_ids: list[int] = handler._resolve_job_ids(
|
|
127
|
+
raw_job_ids=job_ids,
|
|
128
|
+
available_job_ids=available_job_ids,
|
|
129
|
+
)
|
|
130
|
+
if handler._transport_connection is not None and resolved_job_ids:
|
|
131
|
+
_ = handler._send_transport_command(
|
|
132
|
+
build_close_job_command(job_ids=resolved_job_ids)
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
resolved_job_id: int
|
|
136
|
+
for resolved_job_id in resolved_job_ids:
|
|
137
|
+
pending_job = handler._pending_jobs.pop(resolved_job_id, None)
|
|
138
|
+
completed_entry = handler._completed_jobs.pop(resolved_job_id, None)
|
|
139
|
+
if pending_job is not None and pending_job.resultbag is not None:
|
|
140
|
+
pending_job.resultbag.management.release_job(resolved_job_id)
|
|
141
|
+
if completed_entry is not None and completed_entry[0].resultbag is not None:
|
|
142
|
+
completed_entry[0].resultbag.management.release_job(resolved_job_id)
|
|
143
|
+
return tuple(resolved_job_ids)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def synchronize_resources(handler: "DaemonHandler") -> None:
|
|
147
|
+
"""Register any local resources that are not yet registered remotely."""
|
|
148
|
+
|
|
149
|
+
if handler._transport_connection is None:
|
|
150
|
+
return
|
|
151
|
+
resource: DaemonResource
|
|
152
|
+
for index, resource in enumerate(handler._resources):
|
|
153
|
+
if index < len(handler._resource_remote_ids):
|
|
154
|
+
if handler._resource_remote_ids[index] is not None:
|
|
155
|
+
continue
|
|
156
|
+
registered_ids: tuple[int, ...] | None = register_resource_with_daemon(
|
|
157
|
+
handler, resource
|
|
158
|
+
)
|
|
159
|
+
if index < len(handler._resource_remote_ids):
|
|
160
|
+
handler._resource_remote_ids[index] = registered_ids
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def register_resource_with_daemon(
|
|
164
|
+
handler: "DaemonHandler",
|
|
165
|
+
resource: DaemonResource,
|
|
166
|
+
) -> tuple[int, ...] | None:
|
|
167
|
+
"""Send one resource declaration to the daemon."""
|
|
168
|
+
|
|
169
|
+
if handler._transport_connection is None:
|
|
170
|
+
return None
|
|
171
|
+
register_payload: object = extract_return_value(
|
|
172
|
+
handler._send_transport_command(build_register_resource_command(resource))
|
|
173
|
+
)
|
|
174
|
+
return coerce_id_sequence(register_payload)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def add_resource_internal(
|
|
178
|
+
handler: "DaemonHandler",
|
|
179
|
+
resource: DaemonResource,
|
|
180
|
+
) -> DaemonResource:
|
|
181
|
+
"""Store one resource declaration and register when connected."""
|
|
182
|
+
|
|
183
|
+
handler._resources.append(resource)
|
|
184
|
+
handler._resource_remote_ids.append(None)
|
|
185
|
+
if handler._connected:
|
|
186
|
+
handler._resource_remote_ids[-1] = register_resource_with_daemon(
|
|
187
|
+
handler, resource
|
|
188
|
+
)
|
|
189
|
+
return resource
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def delete_resources_by_indices(
|
|
193
|
+
handler: "DaemonHandler",
|
|
194
|
+
indices: Sequence[int],
|
|
195
|
+
) -> tuple[DaemonResource, ...]:
|
|
196
|
+
"""Delete resources by absolute index values and return removed entries."""
|
|
197
|
+
|
|
198
|
+
unique_sorted_indices: list[int] = sorted(set(indices))
|
|
199
|
+
if handler._connected and handler._transport_connection is not None:
|
|
200
|
+
remote_ids_to_remove: list[int] = flatten_remote_ids(
|
|
201
|
+
handler._resource_remote_ids[index]
|
|
202
|
+
for index in unique_sorted_indices
|
|
203
|
+
if 0 <= index < len(handler._resource_remote_ids)
|
|
204
|
+
)
|
|
205
|
+
if remote_ids_to_remove:
|
|
206
|
+
remove_response: str = handler._send_transport_command(
|
|
207
|
+
build_remove_resource_command(resource_ids=remote_ids_to_remove)
|
|
208
|
+
)
|
|
209
|
+
_ = extract_return_value_if_present(remove_response)
|
|
210
|
+
else:
|
|
211
|
+
hostname: str
|
|
212
|
+
for hostname in {
|
|
213
|
+
resource_hostname_chain_string(handler._resources[index])
|
|
214
|
+
for index in unique_sorted_indices
|
|
215
|
+
if 0 <= index < len(handler._resources)
|
|
216
|
+
}:
|
|
217
|
+
remove_response = handler._send_transport_command(
|
|
218
|
+
build_remove_resource_command(hostname=hostname)
|
|
219
|
+
)
|
|
220
|
+
_ = extract_return_value_if_present(remove_response)
|
|
221
|
+
removed_resources: list[DaemonResource] = []
|
|
222
|
+
for index in reversed(unique_sorted_indices):
|
|
223
|
+
removed_resources.append(handler._resources.pop(index))
|
|
224
|
+
if index < len(handler._resource_remote_ids):
|
|
225
|
+
_ = handler._resource_remote_ids.pop(index)
|
|
226
|
+
removed_resources.reverse()
|
|
227
|
+
return tuple(removed_resources)
|