dutchmate-cli 0.1.0__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.
- dutchmate_cli/__init__.py +1 -0
- dutchmate_cli/baseline.py +38 -0
- dutchmate_cli/capture.py +94 -0
- dutchmate_cli/client.py +427 -0
- dutchmate_cli/config.py +160 -0
- dutchmate_cli/devices.py +63 -0
- dutchmate_cli/dut.py +32 -0
- dutchmate_cli/gpio.py +33 -0
- dutchmate_cli/lifecycle.py +225 -0
- dutchmate_cli/logs.py +92 -0
- dutchmate_cli/main.py +615 -0
- dutchmate_cli/mcp.py +51 -0
- dutchmate_cli/send.py +32 -0
- dutchmate_cli/sessions.py +137 -0
- dutchmate_cli/status.py +185 -0
- dutchmate_cli/wait.py +41 -0
- dutchmate_cli-0.1.0.dist-info/METADATA +15 -0
- dutchmate_cli-0.1.0.dist-info/RECORD +20 -0
- dutchmate_cli-0.1.0.dist-info/WHEEL +4 -0
- dutchmate_cli-0.1.0.dist-info/licenses/LICENSE +73 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""DUTchMate CLI package."""
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Terminal formatting for project baseline mutations."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from typing import cast
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def format_baseline_mutation(
|
|
8
|
+
payload: Mapping[str, object],
|
|
9
|
+
*,
|
|
10
|
+
action: str,
|
|
11
|
+
) -> str:
|
|
12
|
+
"""Format one mark or clear result without inferring evidence quality."""
|
|
13
|
+
|
|
14
|
+
changed = payload.get("changed") is True
|
|
15
|
+
lines = [
|
|
16
|
+
f"Baseline {action}: {'changed' if changed else 'unchanged'}",
|
|
17
|
+
f"Session: {_display(payload.get('session_id'))}",
|
|
18
|
+
f"Previous session: {_display(payload.get('previous_session_id'))}",
|
|
19
|
+
f"Marked at: {_display(payload.get('marked_at'))}",
|
|
20
|
+
]
|
|
21
|
+
integrity = _as_mapping(payload.get("integrity"))
|
|
22
|
+
if integrity is not None:
|
|
23
|
+
lines.append(f"Integrity: {_display(integrity.get('loss_status'))}")
|
|
24
|
+
if integrity.get("loss_status") == "not_observable":
|
|
25
|
+
lines.append("Warning: UART loss was not observable for this session.")
|
|
26
|
+
return "\n".join(lines)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _as_mapping(value: object) -> Mapping[str, object] | None:
|
|
30
|
+
if isinstance(value, Mapping):
|
|
31
|
+
return cast(Mapping[str, object], value)
|
|
32
|
+
return None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _display(value: object) -> str:
|
|
36
|
+
if value is None or value == "":
|
|
37
|
+
return "none"
|
|
38
|
+
return str(value)
|
dutchmate_cli/capture.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Terminal formatting for UART capture commands."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def format_capture_result(payload: Mapping[str, object]) -> str:
|
|
9
|
+
"""Format a completed capture session summary."""
|
|
10
|
+
|
|
11
|
+
return _format_capture_summary("Capture complete", payload)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def format_boot_test_result(payload: Mapping[str, object]) -> str:
|
|
15
|
+
"""Format a completed boot-test session summary."""
|
|
16
|
+
|
|
17
|
+
return _format_capture_summary("Boot test complete", payload)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _format_capture_summary(label: str, payload: Mapping[str, object]) -> str:
|
|
21
|
+
session_id = _display(payload.get("session_id"))
|
|
22
|
+
segments = _display(payload.get("segments"))
|
|
23
|
+
flags = ", ".join(
|
|
24
|
+
(
|
|
25
|
+
f"backend={_display(payload.get('backend_mode'))}",
|
|
26
|
+
f"state={_display(payload.get('state'))}",
|
|
27
|
+
f"end={_display(payload.get('end_reason'))}",
|
|
28
|
+
f"loss={_loss_status(payload.get('integrity'))}",
|
|
29
|
+
f"timestamp={_timestamp_source(payload.get('timestamp_provenance'))}",
|
|
30
|
+
f"first_error={_first_error(payload.get('first_error'))}",
|
|
31
|
+
f"lines={_line_processing(payload.get('line_processing'))}",
|
|
32
|
+
f"segments={segments}",
|
|
33
|
+
f"overflow={_flag(payload.get('overflow'))}",
|
|
34
|
+
f"interrupted={_flag(payload.get('interrupted'))}",
|
|
35
|
+
f"resumed={_flag(payload.get('resumed'))}",
|
|
36
|
+
f"truncated={_flag(payload.get('truncated'))}",
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
return f"{label}: {session_id} ({flags})"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _display(value: object) -> str:
|
|
43
|
+
if value is None or value == "":
|
|
44
|
+
return "unknown"
|
|
45
|
+
return str(value)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _flag(value: object) -> str:
|
|
49
|
+
if value is True:
|
|
50
|
+
return "yes"
|
|
51
|
+
if value is False:
|
|
52
|
+
return "no"
|
|
53
|
+
return "unknown"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _loss_status(value: object) -> str:
|
|
57
|
+
if not isinstance(value, Mapping):
|
|
58
|
+
return "unknown"
|
|
59
|
+
return _display(value.get("loss_status"))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _timestamp_source(value: object) -> str:
|
|
63
|
+
if not isinstance(value, list) or not value:
|
|
64
|
+
return "unknown"
|
|
65
|
+
context = value[0]
|
|
66
|
+
if not isinstance(context, Mapping):
|
|
67
|
+
return "unknown"
|
|
68
|
+
timestamp = context.get("timestamp")
|
|
69
|
+
if not isinstance(timestamp, Mapping):
|
|
70
|
+
return "unknown"
|
|
71
|
+
return (
|
|
72
|
+
f"{_display(timestamp.get('source'))}/{_display(timestamp.get('clock'))}:"
|
|
73
|
+
f"{_display(timestamp.get('observation_point'))}/"
|
|
74
|
+
f"{_display(timestamp.get('event_granularity'))}"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _first_error(value: object) -> str:
|
|
79
|
+
if value is None:
|
|
80
|
+
return "none"
|
|
81
|
+
if not isinstance(value, Mapping):
|
|
82
|
+
return "unknown"
|
|
83
|
+
pattern = _display(value.get("pattern"))
|
|
84
|
+
segment_id = _display(value.get("segment_id"))
|
|
85
|
+
ingestion_index = _display(value.get("ingestion_index"))
|
|
86
|
+
return f"{pattern}@segment:{segment_id}/event:{ingestion_index}"
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _line_processing(value: object) -> str:
|
|
90
|
+
if not isinstance(value, Mapping):
|
|
91
|
+
return "unknown"
|
|
92
|
+
status = _display(value.get("status"))
|
|
93
|
+
oversized = _display(value.get("oversized_line_count"))
|
|
94
|
+
return f"{status}/oversized:{oversized}"
|
dutchmate_cli/client.py
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
"""HTTP client helpers for the local Device Core Service."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import Final, cast
|
|
7
|
+
from urllib.parse import quote
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
|
|
11
|
+
from dutchmate_core.validation import (
|
|
12
|
+
prepare_uart_send_payload,
|
|
13
|
+
validate_capture_duration,
|
|
14
|
+
validate_gpio_configuration,
|
|
15
|
+
validate_session_id,
|
|
16
|
+
validate_wait_pattern,
|
|
17
|
+
validate_wait_timeout,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
DEFAULT_SERVICE_URL: Final = "http://127.0.0.1:2040"
|
|
21
|
+
DEFAULT_TIMEOUT_SECONDS: Final = 2.0
|
|
22
|
+
CAPTURE_TIMEOUT_GRACE_SECONDS: Final = 2.0
|
|
23
|
+
RESET_TIMEOUT_GRACE_SECONDS: Final = 1.0
|
|
24
|
+
SERVICE_NOT_RUNNING_MESSAGE: Final = (
|
|
25
|
+
"Device Core Service is not running. Run 'dutchmate start' first."
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ServiceClientError(RuntimeError):
|
|
30
|
+
"""Raised when the CLI cannot complete a Device Core Service request."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ServiceUnavailableError(ServiceClientError):
|
|
34
|
+
"""Raised when the local Device Core Service cannot be reached."""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ServiceApiError(ServiceClientError):
|
|
38
|
+
"""Raised when the local Device Core Service returns an error response."""
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def fetch_status(
|
|
42
|
+
*,
|
|
43
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
44
|
+
transport: httpx.BaseTransport | None = None,
|
|
45
|
+
) -> dict[str, object]:
|
|
46
|
+
"""Fetch the current Device Core Service status payload."""
|
|
47
|
+
|
|
48
|
+
response = _request_service(
|
|
49
|
+
method="GET",
|
|
50
|
+
path="/status",
|
|
51
|
+
service_url=service_url,
|
|
52
|
+
transport=transport,
|
|
53
|
+
)
|
|
54
|
+
return _response_payload(response, description="status payload")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def list_debug_sessions(
|
|
58
|
+
*,
|
|
59
|
+
limit: int = 50,
|
|
60
|
+
cursor: str | None = None,
|
|
61
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
62
|
+
transport: httpx.BaseTransport | None = None,
|
|
63
|
+
) -> dict[str, object]:
|
|
64
|
+
"""Fetch one bounded newest-first session page."""
|
|
65
|
+
|
|
66
|
+
if isinstance(limit, bool) or not isinstance(limit, int) or not 1 <= limit <= 100:
|
|
67
|
+
raise ValueError("session limit must be an integer from 1 to 100")
|
|
68
|
+
if cursor is not None and (not isinstance(cursor, str) or not cursor):
|
|
69
|
+
raise ValueError("session cursor must be a non-empty string")
|
|
70
|
+
params: dict[str, str | int] = {"limit": limit}
|
|
71
|
+
if cursor is not None:
|
|
72
|
+
params["cursor"] = cursor
|
|
73
|
+
response = _request_service(
|
|
74
|
+
method="GET",
|
|
75
|
+
path="/sessions",
|
|
76
|
+
service_url=service_url,
|
|
77
|
+
transport=transport,
|
|
78
|
+
params=params,
|
|
79
|
+
)
|
|
80
|
+
return _response_payload(response, description="session list payload")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def get_debug_session(
|
|
84
|
+
session_id: str,
|
|
85
|
+
*,
|
|
86
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
87
|
+
transport: httpx.BaseTransport | None = None,
|
|
88
|
+
) -> dict[str, object]:
|
|
89
|
+
"""Fetch bounded schema-aware detail for one session."""
|
|
90
|
+
|
|
91
|
+
validated_id = validate_session_id(session_id)
|
|
92
|
+
response = _request_service(
|
|
93
|
+
method="GET",
|
|
94
|
+
path=f"/sessions/{quote(validated_id, safe='')}",
|
|
95
|
+
service_url=service_url,
|
|
96
|
+
transport=transport,
|
|
97
|
+
)
|
|
98
|
+
return _response_payload(response, description="session detail payload")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def mark_session_baseline(
|
|
102
|
+
session_id: str,
|
|
103
|
+
*,
|
|
104
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
105
|
+
transport: httpx.BaseTransport | None = None,
|
|
106
|
+
) -> dict[str, object]:
|
|
107
|
+
"""Designate one stored session as the project baseline."""
|
|
108
|
+
|
|
109
|
+
validated_id = validate_session_id(session_id)
|
|
110
|
+
response = _request_service(
|
|
111
|
+
method="POST",
|
|
112
|
+
path=f"/sessions/{quote(validated_id, safe='')}/baseline",
|
|
113
|
+
service_url=service_url,
|
|
114
|
+
transport=transport,
|
|
115
|
+
)
|
|
116
|
+
return _response_payload(response, description="baseline mark response")
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def clear_session_baseline(
|
|
120
|
+
session_id: str,
|
|
121
|
+
*,
|
|
122
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
123
|
+
transport: httpx.BaseTransport | None = None,
|
|
124
|
+
) -> dict[str, object]:
|
|
125
|
+
"""Clear the project baseline only when it names the requested session."""
|
|
126
|
+
|
|
127
|
+
validated_id = validate_session_id(session_id)
|
|
128
|
+
response = _request_service(
|
|
129
|
+
method="DELETE",
|
|
130
|
+
path=f"/sessions/{quote(validated_id, safe='')}/baseline",
|
|
131
|
+
service_url=service_url,
|
|
132
|
+
transport=transport,
|
|
133
|
+
)
|
|
134
|
+
return _response_payload(response, description="baseline clear response")
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def fetch_recent_logs(
|
|
138
|
+
*,
|
|
139
|
+
session_id: str | None = None,
|
|
140
|
+
lines: int = 300,
|
|
141
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
142
|
+
transport: httpx.BaseTransport | None = None,
|
|
143
|
+
) -> dict[str, object]:
|
|
144
|
+
"""Fetch bounded recent UART replay for one selected native session."""
|
|
145
|
+
|
|
146
|
+
if isinstance(lines, bool) or not isinstance(lines, int) or not 1 <= lines <= 1000:
|
|
147
|
+
raise ValueError("log lines must be an integer from 1 to 1000")
|
|
148
|
+
params: dict[str, str | int] = {"lines": lines}
|
|
149
|
+
if session_id is not None:
|
|
150
|
+
params["session_id"] = validate_session_id(session_id)
|
|
151
|
+
response = _request_service(
|
|
152
|
+
method="GET",
|
|
153
|
+
path="/dut/logs",
|
|
154
|
+
service_url=service_url,
|
|
155
|
+
transport=transport,
|
|
156
|
+
params=params,
|
|
157
|
+
)
|
|
158
|
+
return _response_payload(response, description="recent logs payload")
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def wait_for_pattern(
|
|
162
|
+
*,
|
|
163
|
+
pattern: str,
|
|
164
|
+
timeout_s: float,
|
|
165
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
166
|
+
transport: httpx.BaseTransport | None = None,
|
|
167
|
+
) -> dict[str, object]:
|
|
168
|
+
"""Run one finite literal wait against new UART evidence."""
|
|
169
|
+
|
|
170
|
+
validated_pattern = validate_wait_pattern(pattern)
|
|
171
|
+
validated_timeout = validate_wait_timeout(timeout_s)
|
|
172
|
+
response = _request_service(
|
|
173
|
+
method="POST",
|
|
174
|
+
path="/dut/wait-pattern",
|
|
175
|
+
service_url=service_url,
|
|
176
|
+
transport=transport,
|
|
177
|
+
json={"pattern": validated_pattern, "timeout_s": validated_timeout},
|
|
178
|
+
timeout_s=max(
|
|
179
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
180
|
+
validated_timeout + CAPTURE_TIMEOUT_GRACE_SECONDS,
|
|
181
|
+
),
|
|
182
|
+
)
|
|
183
|
+
return _response_payload(response, description="wait-pattern response")
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def send_uart_command(
|
|
187
|
+
cmd: str,
|
|
188
|
+
*,
|
|
189
|
+
append_newline: bool = True,
|
|
190
|
+
force: bool = False,
|
|
191
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
192
|
+
transport: httpx.BaseTransport | None = None,
|
|
193
|
+
) -> dict[str, object]:
|
|
194
|
+
"""Send one validated text command through the Device Core Service."""
|
|
195
|
+
|
|
196
|
+
prepare_uart_send_payload(cmd, append_newline=append_newline)
|
|
197
|
+
if not isinstance(force, bool):
|
|
198
|
+
raise ValueError("UART send force must be a boolean")
|
|
199
|
+
response = _request_service(
|
|
200
|
+
method="POST",
|
|
201
|
+
path="/dut/uart/send",
|
|
202
|
+
service_url=service_url,
|
|
203
|
+
transport=transport,
|
|
204
|
+
json={
|
|
205
|
+
"cmd": cmd,
|
|
206
|
+
"append_newline": append_newline,
|
|
207
|
+
"force": force,
|
|
208
|
+
},
|
|
209
|
+
)
|
|
210
|
+
return _response_payload(response, description="UART-send response")
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def configure_gpio_mode(
|
|
214
|
+
*,
|
|
215
|
+
channel: str,
|
|
216
|
+
role: str,
|
|
217
|
+
dut_signal: str,
|
|
218
|
+
mode: str,
|
|
219
|
+
active_level: str,
|
|
220
|
+
idle_level: str | None = None,
|
|
221
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
222
|
+
transport: httpx.BaseTransport | None = None,
|
|
223
|
+
) -> dict[str, object]:
|
|
224
|
+
"""Configure one Device Core Service GPIO control channel."""
|
|
225
|
+
|
|
226
|
+
request = validate_gpio_configuration(
|
|
227
|
+
channel=channel,
|
|
228
|
+
role=role,
|
|
229
|
+
dut_signal=dut_signal,
|
|
230
|
+
mode=mode,
|
|
231
|
+
active_level=active_level,
|
|
232
|
+
idle_level=idle_level,
|
|
233
|
+
)
|
|
234
|
+
request_payload: dict[str, object] = {
|
|
235
|
+
"channel": request.channel,
|
|
236
|
+
"role": request.role,
|
|
237
|
+
"dut_signal": request.dut_signal,
|
|
238
|
+
"mode": request.mode,
|
|
239
|
+
"active_level": request.active_level,
|
|
240
|
+
}
|
|
241
|
+
if request.idle_level is not None:
|
|
242
|
+
request_payload["idle_level"] = request.idle_level
|
|
243
|
+
|
|
244
|
+
response = _request_service(
|
|
245
|
+
method="POST",
|
|
246
|
+
path="/gpio/mode",
|
|
247
|
+
service_url=service_url,
|
|
248
|
+
transport=transport,
|
|
249
|
+
json=request_payload,
|
|
250
|
+
)
|
|
251
|
+
return _response_payload(response, description="GPIO mode response")
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def reset_dut(
|
|
255
|
+
*,
|
|
256
|
+
pulse_ms: int = 100,
|
|
257
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
258
|
+
transport: httpx.BaseTransport | None = None,
|
|
259
|
+
) -> dict[str, object]:
|
|
260
|
+
"""Pulse the configured DUT reset role through the Device Core Service."""
|
|
261
|
+
|
|
262
|
+
response = _request_service(
|
|
263
|
+
method="POST",
|
|
264
|
+
path="/dut/reset",
|
|
265
|
+
service_url=service_url,
|
|
266
|
+
transport=transport,
|
|
267
|
+
json={"pulse_ms": pulse_ms},
|
|
268
|
+
timeout_s=max(
|
|
269
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
270
|
+
pulse_ms / 1000.0 + RESET_TIMEOUT_GRACE_SECONDS,
|
|
271
|
+
),
|
|
272
|
+
)
|
|
273
|
+
return _response_payload(response, description="reset response")
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def set_boot_mode(
|
|
277
|
+
*,
|
|
278
|
+
mode: str,
|
|
279
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
280
|
+
transport: httpx.BaseTransport | None = None,
|
|
281
|
+
) -> dict[str, object]:
|
|
282
|
+
"""Set the configured DUT boot/control role through the Device Core Service."""
|
|
283
|
+
|
|
284
|
+
response = _request_service(
|
|
285
|
+
method="POST",
|
|
286
|
+
path="/dut/boot-mode",
|
|
287
|
+
service_url=service_url,
|
|
288
|
+
transport=transport,
|
|
289
|
+
json={"mode": mode},
|
|
290
|
+
)
|
|
291
|
+
return _response_payload(response, description="boot-mode response")
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def capture_uart(
|
|
295
|
+
*,
|
|
296
|
+
duration_s: float,
|
|
297
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
298
|
+
transport: httpx.BaseTransport | None = None,
|
|
299
|
+
) -> dict[str, object]:
|
|
300
|
+
"""Capture DUT UART evidence through the Device Core Service."""
|
|
301
|
+
|
|
302
|
+
return _run_timed_capture_request(
|
|
303
|
+
path="/dut/capture",
|
|
304
|
+
description="capture",
|
|
305
|
+
duration_s=duration_s,
|
|
306
|
+
service_url=service_url,
|
|
307
|
+
transport=transport,
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def run_boot_test(
|
|
312
|
+
*,
|
|
313
|
+
duration_s: float,
|
|
314
|
+
service_url: str = DEFAULT_SERVICE_URL,
|
|
315
|
+
transport: httpx.BaseTransport | None = None,
|
|
316
|
+
) -> dict[str, object]:
|
|
317
|
+
"""Reset the DUT and capture boot evidence through the Device Core Service."""
|
|
318
|
+
|
|
319
|
+
return _run_timed_capture_request(
|
|
320
|
+
path="/dut/boot-test",
|
|
321
|
+
description="boot-test",
|
|
322
|
+
duration_s=duration_s,
|
|
323
|
+
service_url=service_url,
|
|
324
|
+
transport=transport,
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _run_timed_capture_request(
|
|
329
|
+
*,
|
|
330
|
+
path: str,
|
|
331
|
+
description: str,
|
|
332
|
+
duration_s: float,
|
|
333
|
+
service_url: str,
|
|
334
|
+
transport: httpx.BaseTransport | None,
|
|
335
|
+
) -> dict[str, object]:
|
|
336
|
+
try:
|
|
337
|
+
duration_s = validate_capture_duration(duration_s)
|
|
338
|
+
except ValueError as exc:
|
|
339
|
+
raise ValueError(f"{description} {exc}") from exc
|
|
340
|
+
|
|
341
|
+
response = _request_service(
|
|
342
|
+
method="POST",
|
|
343
|
+
path=path,
|
|
344
|
+
service_url=service_url,
|
|
345
|
+
transport=transport,
|
|
346
|
+
json={"duration_s": duration_s},
|
|
347
|
+
timeout_s=max(
|
|
348
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
349
|
+
duration_s + CAPTURE_TIMEOUT_GRACE_SECONDS,
|
|
350
|
+
),
|
|
351
|
+
)
|
|
352
|
+
return _response_payload(response, description=f"{description} response")
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def _request_service(
|
|
356
|
+
*,
|
|
357
|
+
method: str,
|
|
358
|
+
path: str,
|
|
359
|
+
service_url: str,
|
|
360
|
+
transport: httpx.BaseTransport | None = None,
|
|
361
|
+
json: Mapping[str, object] | None = None,
|
|
362
|
+
params: Mapping[str, str | int] | None = None,
|
|
363
|
+
timeout_s: float = DEFAULT_TIMEOUT_SECONDS,
|
|
364
|
+
) -> httpx.Response:
|
|
365
|
+
try:
|
|
366
|
+
with httpx.Client(
|
|
367
|
+
base_url=service_url,
|
|
368
|
+
timeout=timeout_s,
|
|
369
|
+
transport=transport,
|
|
370
|
+
) as client:
|
|
371
|
+
response = client.request(method, path, json=json, params=params)
|
|
372
|
+
except (httpx.ConnectError, httpx.ConnectTimeout) as exc:
|
|
373
|
+
raise ServiceUnavailableError(SERVICE_NOT_RUNNING_MESSAGE) from exc
|
|
374
|
+
except httpx.ReadTimeout as exc:
|
|
375
|
+
raise ServiceApiError("Device Core Service request timed out.") from exc
|
|
376
|
+
except httpx.HTTPError as exc:
|
|
377
|
+
raise ServiceApiError(f"Unable to contact Device Core Service: {exc}") from exc
|
|
378
|
+
|
|
379
|
+
if response.is_error:
|
|
380
|
+
raise ServiceApiError(_response_error_message(response))
|
|
381
|
+
|
|
382
|
+
return response
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def _response_payload(response: httpx.Response, *, description: str) -> dict[str, object]:
|
|
386
|
+
try:
|
|
387
|
+
payload = response.json()
|
|
388
|
+
except ValueError as exc:
|
|
389
|
+
raise ServiceApiError("Device Core Service returned invalid JSON.") from exc
|
|
390
|
+
|
|
391
|
+
if not isinstance(payload, Mapping):
|
|
392
|
+
raise ServiceApiError(f"Device Core Service returned an invalid {description}.")
|
|
393
|
+
|
|
394
|
+
return cast(dict[str, object], payload)
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def _response_error_message(response: httpx.Response) -> str:
|
|
398
|
+
prefix = f"Device Core Service returned HTTP {response.status_code}"
|
|
399
|
+
try:
|
|
400
|
+
payload = response.json()
|
|
401
|
+
except ValueError:
|
|
402
|
+
return f"{prefix}."
|
|
403
|
+
|
|
404
|
+
if isinstance(payload, Mapping):
|
|
405
|
+
detail = payload.get("detail")
|
|
406
|
+
error = payload.get("error")
|
|
407
|
+
if isinstance(detail, str) and detail:
|
|
408
|
+
suffix = _uart_send_error_suffix(payload.get("context"))
|
|
409
|
+
if isinstance(error, str) and error:
|
|
410
|
+
return f"{prefix} [{error}]: {detail}{suffix}"
|
|
411
|
+
return f"{prefix}: {detail}{suffix}"
|
|
412
|
+
|
|
413
|
+
return f"{prefix}."
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def _uart_send_error_suffix(context: object) -> str:
|
|
417
|
+
if not isinstance(context, Mapping):
|
|
418
|
+
return ""
|
|
419
|
+
attempt_id = context.get("attempt_id")
|
|
420
|
+
if not isinstance(attempt_id, str) or not attempt_id:
|
|
421
|
+
return ""
|
|
422
|
+
accepted = context.get("bytes_accepted")
|
|
423
|
+
accepted_text = str(accepted) if isinstance(accepted, int) else "unknown"
|
|
424
|
+
return (
|
|
425
|
+
f" Attempt: {attempt_id}; accepted bytes: {accepted_text}."
|
|
426
|
+
" Warning: bytes may have reached the DUT."
|
|
427
|
+
)
|