testflows.machine 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.

Potentially problematic release.


This version of testflows.machine might be problematic. Click here for more details.

@@ -0,0 +1,109 @@
1
+ # Copyright 2026 Katteli Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """The service a session's runs live in, and how to reach it."""
15
+
16
+ from __future__ import annotations
17
+
18
+ import contextlib
19
+ from pathlib import Path
20
+ from typing import Any, Callable, Generator, Optional
21
+
22
+ from testflows.machine._core import api
23
+ from testflows.machine._core import ipc
24
+ from testflows.machine._core import machines
25
+ from testflows.machine._core import sessions as _sessions
26
+ from testflows.machine._core import tunnel
27
+
28
+ from .errors import NotFound, ServiceUnavailable, _translated
29
+
30
+
31
+ class _Service:
32
+ """One session's service: where its socket is and how disks are named.
33
+
34
+ `socket` is None for the local service's default socket. `disks` lists
35
+ the account's disks, which a cloud session names by; None for the
36
+ local service, which takes a path. `renew` keeps a cloud session's
37
+ access current before each connection; None for the local service.
38
+ """
39
+
40
+ def __init__(
41
+ self,
42
+ socket: Optional[Path],
43
+ disks: Optional[Callable[[], list[dict[str, Any]]]],
44
+ label: str,
45
+ renew: Optional[Callable[[], None]] = None,
46
+ ) -> None:
47
+ self.socket = socket
48
+ self.disks = disks
49
+ self.label = label
50
+ self.renew = renew
51
+
52
+ @contextlib.contextmanager
53
+ def client(self) -> Generator[ipc.Client, None, None]:
54
+ """A connection to the service, closed on exit, with engine errors
55
+ translated to the SDK's."""
56
+ with _translated():
57
+ if self.socket is None and not ipc.Client.is_available():
58
+ raise ServiceUnavailable(f"The {self.label} is not running.")
59
+ if self.renew is not None:
60
+ self.renew()
61
+ client = ipc.Client(socket_path=self.socket)
62
+ try:
63
+ client.connect()
64
+ except OSError as exc:
65
+ raise ServiceUnavailable(
66
+ f"Could not reach the {self.label}: {exc}"
67
+ ) from None
68
+ try:
69
+ yield client
70
+ finally:
71
+ client.close()
72
+
73
+ def disk(self, disk: str) -> "machines.DiskRef":
74
+ """What the service takes for `disk`, and what the catalog says about
75
+ it: an absolute path for the local service, or the content hash of the
76
+ named disk plus its labels for a cloud session."""
77
+ with _translated():
78
+ return machines.resolve_disk(
79
+ disk, local=self.disks is None, list_disks=self.disks
80
+ )
81
+
82
+
83
+ def local() -> _Service:
84
+ """The local service, on its default socket."""
85
+ return _Service(None, None, "local service")
86
+
87
+
88
+ def of_session(
89
+ client: api.client.Client, creds: dict[str, Any], session_id: str, name: str
90
+ ) -> _Service:
91
+ """A cloud session's service, through this machine's tunnel to it.
92
+
93
+ Opens the tunnel when it is not up; reuses it when it is.
94
+ """
95
+ with _translated():
96
+ _sessions.connect(client, creds, session_id, wait=False)
97
+ socket = api.store.session_socket(session_id)
98
+ sdir = api.store.session_dir(session_id)
99
+
100
+ def renew() -> None:
101
+ if not tunnel.renew_stale_cert(sdir, session_id, client, creds):
102
+ raise NotFound(f"Session {name} no longer exists.")
103
+
104
+ return _Service(
105
+ socket,
106
+ lambda: api.auth.authed(creds, client.disk_list),
107
+ f"service of session {name}",
108
+ renew,
109
+ )
@@ -0,0 +1,16 @@
1
+ # Copyright 2026 Katteli Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """The SDK's version."""
15
+
16
+ __version__ = "0.1.0"
@@ -0,0 +1,55 @@
1
+ # Copyright 2026 Katteli Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """The time bounds a listing takes: a datetime, Unix seconds, or text naming a
15
+ date (`2026-06-01`, any ISO form) or an age (`7d`, `24h`, `30m`). A naive
16
+ datetime is read as UTC."""
17
+
18
+ from __future__ import annotations
19
+
20
+ from datetime import datetime, timezone
21
+ from typing import Optional, Union
22
+
23
+ from testflows.machine._core import disks as _disks
24
+
25
+ from .errors import InvalidArgument
26
+
27
+ When = Union[datetime, int, str]
28
+
29
+
30
+ def iso(when: Optional[When]) -> Optional[str]:
31
+ """`when` as an ISO 8601 UTC instant, or None."""
32
+ if when is None:
33
+ return None
34
+ return _instant(when).isoformat()
35
+
36
+
37
+ def unix_seconds(when: Optional[When]) -> Optional[int]:
38
+ """`when` in Unix seconds, or None."""
39
+ if when is None or isinstance(when, int):
40
+ return when
41
+ return int(_instant(when).timestamp())
42
+
43
+
44
+ def _instant(when: When) -> datetime:
45
+ """`when` as an aware UTC datetime."""
46
+ if isinstance(when, int):
47
+ return datetime.fromtimestamp(when, timezone.utc)
48
+ if isinstance(when, str):
49
+ try:
50
+ return datetime.fromisoformat(_disks.parse_since(when, "time"))
51
+ except RuntimeError as exc:
52
+ raise InvalidArgument(str(exc)) from None
53
+ if when.tzinfo is None:
54
+ return when.replace(tzinfo=timezone.utc)
55
+ return when