revyl-verify 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.
- revyl_verify/__init__.py +146 -0
- revyl_verify/_async.py +214 -0
- revyl_verify/_cleanup.py +73 -0
- revyl_verify/_computer.py +913 -0
- revyl_verify/_login.py +340 -0
- revyl_verify/_models.py +216 -0
- revyl_verify/_session.py +835 -0
- revyl_verify/_transport.py +563 -0
- revyl_verify/py.typed +0 -0
- revyl_verify-0.1.0.dist-info/METADATA +251 -0
- revyl_verify-0.1.0.dist-info/RECORD +13 -0
- revyl_verify-0.1.0.dist-info/WHEEL +4 -0
- revyl_verify-0.1.0.dist-info/licenses/LICENSE +201 -0
revyl_verify/__init__.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
|
|
6
|
+
from ._async import AsyncSession, async_attach, async_open
|
|
7
|
+
from ._cleanup import install_signal_cleanup
|
|
8
|
+
from ._computer import ComputerSession
|
|
9
|
+
from ._models import (
|
|
10
|
+
ComputerProcess,
|
|
11
|
+
ComputerRecording,
|
|
12
|
+
ExecResult,
|
|
13
|
+
FileStat,
|
|
14
|
+
Platform,
|
|
15
|
+
SignIn,
|
|
16
|
+
StepResult,
|
|
17
|
+
Target,
|
|
18
|
+
Viewport,
|
|
19
|
+
)
|
|
20
|
+
from ._session import Session
|
|
21
|
+
from ._transport import Cancellation, CleanupStatus, RefusalReason, VerifyError, origin, seconds
|
|
22
|
+
|
|
23
|
+
__version__ = "0.1.0"
|
|
24
|
+
__all__ = [
|
|
25
|
+
"AsyncSession",
|
|
26
|
+
"ComputerProcess",
|
|
27
|
+
"ComputerRecording",
|
|
28
|
+
"ComputerSession",
|
|
29
|
+
"CleanupStatus",
|
|
30
|
+
"ExecResult",
|
|
31
|
+
"FileStat",
|
|
32
|
+
"Platform",
|
|
33
|
+
"RefusalReason",
|
|
34
|
+
"Session",
|
|
35
|
+
"SignIn",
|
|
36
|
+
"StepResult",
|
|
37
|
+
"Target",
|
|
38
|
+
"VerifyError",
|
|
39
|
+
"Viewport",
|
|
40
|
+
"attach",
|
|
41
|
+
"async_attach",
|
|
42
|
+
"async_current_computer",
|
|
43
|
+
"async_open",
|
|
44
|
+
"current_computer",
|
|
45
|
+
"open",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _current_computer_id() -> str:
|
|
50
|
+
computer_id = os.environ.get("REVYL_COMPUTER_ID", "").strip()
|
|
51
|
+
if not computer_id:
|
|
52
|
+
raise VerifyError(
|
|
53
|
+
"COMPUTER_ID_MISSING",
|
|
54
|
+
"No current Revyl computer is available. Run this job on a revyl-macos runner.",
|
|
55
|
+
)
|
|
56
|
+
return computer_id
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def open(
|
|
60
|
+
target: Target,
|
|
61
|
+
*,
|
|
62
|
+
backend_url: str | None = None,
|
|
63
|
+
app_url: str | None = None,
|
|
64
|
+
step_timeout: float = 180,
|
|
65
|
+
log: Callable[[str], None] | None = None,
|
|
66
|
+
_cancellation: Cancellation | None = None,
|
|
67
|
+
) -> Session:
|
|
68
|
+
from ._transport import Client
|
|
69
|
+
|
|
70
|
+
if not isinstance(target, Target):
|
|
71
|
+
raise VerifyError("INVALID_ARGUMENT", "open() requires a Target value.")
|
|
72
|
+
target.validate()
|
|
73
|
+
install_signal_cleanup()
|
|
74
|
+
seconds(step_timeout, 1, 3600, "step_timeout")
|
|
75
|
+
if log is not None and not callable(log):
|
|
76
|
+
raise VerifyError("INVALID_ARGUMENT", "log must be a callable or None.")
|
|
77
|
+
viewer = origin(app_url or "https://app.revyl.ai", app=True)
|
|
78
|
+
client = Client(backend_url, app_url, _cancellation)
|
|
79
|
+
session = (ComputerSession if target.platform in ("mac", "linux") else Session).start(
|
|
80
|
+
client, viewer, target, step_timeout, log, _cancellation
|
|
81
|
+
)
|
|
82
|
+
return session
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def attach(
|
|
86
|
+
computer_id: str,
|
|
87
|
+
*,
|
|
88
|
+
backend_url: str | None = None,
|
|
89
|
+
app_url: str | None = None,
|
|
90
|
+
step_timeout: float = 180,
|
|
91
|
+
log: Callable[[str], None] | None = None,
|
|
92
|
+
stop_on_close: bool = True,
|
|
93
|
+
_cancellation: Cancellation | None = None,
|
|
94
|
+
) -> ComputerSession:
|
|
95
|
+
from ._models import UUID_PATTERN
|
|
96
|
+
from ._transport import Client
|
|
97
|
+
|
|
98
|
+
if not isinstance(computer_id, str) or not UUID_PATTERN.fullmatch(computer_id):
|
|
99
|
+
raise VerifyError("INVALID_ARGUMENT", "attach() requires a computer UUID.")
|
|
100
|
+
if not isinstance(stop_on_close, bool):
|
|
101
|
+
raise VerifyError("INVALID_ARGUMENT", "stop_on_close must be a boolean.")
|
|
102
|
+
seconds(step_timeout, 1, 3600, "step_timeout")
|
|
103
|
+
if log is not None and not callable(log):
|
|
104
|
+
raise VerifyError("INVALID_ARGUMENT", "log must be a callable or None.")
|
|
105
|
+
install_signal_cleanup()
|
|
106
|
+
viewer = origin(app_url or "https://app.revyl.ai", app=True)
|
|
107
|
+
client = Client(backend_url, app_url, _cancellation)
|
|
108
|
+
return ComputerSession.attach(
|
|
109
|
+
client, viewer, computer_id, step_timeout, log, _cancellation, stop_on_close=stop_on_close
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def current_computer(
|
|
114
|
+
*,
|
|
115
|
+
backend_url: str | None = None,
|
|
116
|
+
app_url: str | None = None,
|
|
117
|
+
step_timeout: float = 180,
|
|
118
|
+
log: Callable[[str], None] | None = None,
|
|
119
|
+
stop_on_close: bool = False,
|
|
120
|
+
) -> ComputerSession:
|
|
121
|
+
return attach(
|
|
122
|
+
_current_computer_id(),
|
|
123
|
+
backend_url=backend_url,
|
|
124
|
+
app_url=app_url,
|
|
125
|
+
step_timeout=step_timeout,
|
|
126
|
+
log=log,
|
|
127
|
+
stop_on_close=stop_on_close,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
async def async_current_computer(
|
|
132
|
+
*,
|
|
133
|
+
backend_url: str | None = None,
|
|
134
|
+
app_url: str | None = None,
|
|
135
|
+
step_timeout: float = 180,
|
|
136
|
+
log: Callable[[str], None] | None = None,
|
|
137
|
+
stop_on_close: bool = False,
|
|
138
|
+
) -> AsyncSession:
|
|
139
|
+
return await async_attach(
|
|
140
|
+
_current_computer_id(),
|
|
141
|
+
backend_url=backend_url,
|
|
142
|
+
app_url=app_url,
|
|
143
|
+
step_timeout=step_timeout,
|
|
144
|
+
log=log,
|
|
145
|
+
stop_on_close=stop_on_close,
|
|
146
|
+
)
|
revyl_verify/_async.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import functools
|
|
5
|
+
import threading
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from types import TracebackType
|
|
10
|
+
from typing import TypeVar
|
|
11
|
+
|
|
12
|
+
from ._cleanup import _close, install_signal_cleanup
|
|
13
|
+
from ._models import (
|
|
14
|
+
ComputerProcess,
|
|
15
|
+
ComputerRecording,
|
|
16
|
+
ExecResult,
|
|
17
|
+
FileStat,
|
|
18
|
+
Platform,
|
|
19
|
+
StepResult,
|
|
20
|
+
Target,
|
|
21
|
+
)
|
|
22
|
+
from ._session import Session
|
|
23
|
+
from ._transport import Cancellation, Json, VerifyError
|
|
24
|
+
|
|
25
|
+
T = TypeVar("T")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class AsyncSession:
|
|
29
|
+
def __init__(self, session: Session) -> None:
|
|
30
|
+
self._session = session
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def id(self) -> str:
|
|
34
|
+
return self._session.id
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def platform(self) -> Platform:
|
|
38
|
+
return self._session.platform
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def viewer_url(self) -> str:
|
|
42
|
+
return self._session.viewer_url
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def expires_at(self) -> datetime | None:
|
|
46
|
+
return self._session.expires_at
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def closed(self) -> bool:
|
|
50
|
+
return self._session.closed
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def steps(self) -> list[StepResult]:
|
|
54
|
+
return list(self._session.steps)
|
|
55
|
+
|
|
56
|
+
async def __aenter__(self) -> AsyncSession:
|
|
57
|
+
return self
|
|
58
|
+
|
|
59
|
+
async def __aexit__(
|
|
60
|
+
self,
|
|
61
|
+
exc_type: type[BaseException] | None,
|
|
62
|
+
exc: BaseException | None,
|
|
63
|
+
traceback: TracebackType | None,
|
|
64
|
+
) -> None:
|
|
65
|
+
try:
|
|
66
|
+
await self.close()
|
|
67
|
+
except BaseException as cleanup_error:
|
|
68
|
+
if exc is None:
|
|
69
|
+
raise
|
|
70
|
+
if isinstance(exc, VerifyError):
|
|
71
|
+
self._session._attach_failure(exc)
|
|
72
|
+
raise exc.with_traceback(traceback) from cleanup_error
|
|
73
|
+
|
|
74
|
+
async def _call(self, call: Callable[[], T]) -> T:
|
|
75
|
+
task = asyncio.create_task(asyncio.to_thread(call))
|
|
76
|
+
try:
|
|
77
|
+
return await asyncio.shield(task)
|
|
78
|
+
except asyncio.CancelledError:
|
|
79
|
+
threading.Thread(target=_close, args=(self._session,), daemon=True).start()
|
|
80
|
+
task.add_done_callback(_consume)
|
|
81
|
+
raise
|
|
82
|
+
|
|
83
|
+
async def instruction(self, text: str, *, timeout: float | None = None) -> StepResult:
|
|
84
|
+
return await self._call(functools.partial(self._session.instruction, text, timeout=timeout))
|
|
85
|
+
|
|
86
|
+
async def validation(
|
|
87
|
+
self, text: str, *, timeout: float | None = None, soft: bool = False
|
|
88
|
+
) -> StepResult:
|
|
89
|
+
return await self._call(
|
|
90
|
+
functools.partial(self._session.validation, text, timeout=timeout, soft=soft)
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
async def screenshot(self, *, path: str | Path | None = None) -> bytes:
|
|
94
|
+
return await self._call(functools.partial(self._session.screenshot, path=path))
|
|
95
|
+
|
|
96
|
+
async def open(self, target: Target) -> AsyncSession:
|
|
97
|
+
return await _create_session(
|
|
98
|
+
lambda cancellation: self._session.open(target, _cancellation=cancellation)
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
async def navigate(self, url: str) -> None:
|
|
102
|
+
await self._call(functools.partial(self._session.navigate, url))
|
|
103
|
+
|
|
104
|
+
async def report(self) -> dict[str, Json]:
|
|
105
|
+
return await self._call(self._session.report)
|
|
106
|
+
|
|
107
|
+
async def recording(self) -> ComputerRecording | None:
|
|
108
|
+
return await self._call(self._session.recording)
|
|
109
|
+
|
|
110
|
+
async def exec(
|
|
111
|
+
self, argv: list[str], *, stdin: bytes | str = b"", timeout: float = 30, check: bool = True
|
|
112
|
+
) -> ExecResult:
|
|
113
|
+
return await self._call(
|
|
114
|
+
functools.partial(self._session.exec, argv, stdin=stdin, timeout=timeout, check=check)
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
async def read_file(self, path: str, *, timeout: float = 30) -> bytes:
|
|
118
|
+
return await self._call(functools.partial(self._session.read_file, path, timeout=timeout))
|
|
119
|
+
|
|
120
|
+
async def write_file(self, path: str, data: bytes | str, *, timeout: float = 30) -> None:
|
|
121
|
+
await self._call(functools.partial(self._session.write_file, path, data, timeout=timeout))
|
|
122
|
+
|
|
123
|
+
async def stat(self, path: str, *, timeout: float = 30) -> FileStat:
|
|
124
|
+
return await self._call(functools.partial(self._session.stat, path, timeout=timeout))
|
|
125
|
+
|
|
126
|
+
async def processes(self, *, timeout: float = 30) -> list[ComputerProcess]:
|
|
127
|
+
return await self._call(functools.partial(self._session.processes, timeout=timeout))
|
|
128
|
+
|
|
129
|
+
async def terminate(self, handle_id: str, *, timeout: float = 30) -> None:
|
|
130
|
+
await self._call(functools.partial(self._session.terminate, handle_id, timeout=timeout))
|
|
131
|
+
|
|
132
|
+
async def close(self) -> None:
|
|
133
|
+
await self._call(self._session.close)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _consume(task: asyncio.Task[object]) -> None:
|
|
137
|
+
if not task.cancelled():
|
|
138
|
+
task.exception()
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
async def async_open(
|
|
142
|
+
target: Target,
|
|
143
|
+
*,
|
|
144
|
+
backend_url: str | None = None,
|
|
145
|
+
app_url: str | None = None,
|
|
146
|
+
step_timeout: float = 180,
|
|
147
|
+
log: Callable[[str], None] | None = None,
|
|
148
|
+
) -> AsyncSession:
|
|
149
|
+
from . import open
|
|
150
|
+
|
|
151
|
+
return await _create_session(
|
|
152
|
+
lambda cancellation: open(
|
|
153
|
+
target,
|
|
154
|
+
backend_url=backend_url,
|
|
155
|
+
app_url=app_url,
|
|
156
|
+
step_timeout=step_timeout,
|
|
157
|
+
log=log,
|
|
158
|
+
_cancellation=cancellation,
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
async def async_attach(
|
|
164
|
+
computer_id: str,
|
|
165
|
+
*,
|
|
166
|
+
backend_url: str | None = None,
|
|
167
|
+
app_url: str | None = None,
|
|
168
|
+
step_timeout: float = 180,
|
|
169
|
+
log: Callable[[str], None] | None = None,
|
|
170
|
+
stop_on_close: bool = True,
|
|
171
|
+
) -> AsyncSession:
|
|
172
|
+
from . import attach
|
|
173
|
+
|
|
174
|
+
return await _create_session(
|
|
175
|
+
lambda cancellation: attach(
|
|
176
|
+
computer_id,
|
|
177
|
+
backend_url=backend_url,
|
|
178
|
+
app_url=app_url,
|
|
179
|
+
step_timeout=step_timeout,
|
|
180
|
+
log=log,
|
|
181
|
+
stop_on_close=stop_on_close,
|
|
182
|
+
_cancellation=cancellation,
|
|
183
|
+
)
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
async def _create_session(create: Callable[[Cancellation], Session]) -> AsyncSession:
|
|
188
|
+
install_signal_cleanup()
|
|
189
|
+
abandoned = threading.Event()
|
|
190
|
+
cancellation = Cancellation()
|
|
191
|
+
lock = threading.Lock()
|
|
192
|
+
created: list[Session] = []
|
|
193
|
+
|
|
194
|
+
def start() -> Session:
|
|
195
|
+
session = create(cancellation)
|
|
196
|
+
with lock:
|
|
197
|
+
if not abandoned.is_set():
|
|
198
|
+
created.append(session)
|
|
199
|
+
return session
|
|
200
|
+
_close(session)
|
|
201
|
+
return session
|
|
202
|
+
|
|
203
|
+
task = asyncio.create_task(asyncio.to_thread(start))
|
|
204
|
+
try:
|
|
205
|
+
session = await asyncio.shield(task)
|
|
206
|
+
except asyncio.CancelledError:
|
|
207
|
+
cancellation.cancel()
|
|
208
|
+
with lock:
|
|
209
|
+
abandoned.set()
|
|
210
|
+
if created:
|
|
211
|
+
threading.Thread(target=_close, args=(created[0],), daemon=True).start()
|
|
212
|
+
task.add_done_callback(_consume)
|
|
213
|
+
raise
|
|
214
|
+
return AsyncSession(session)
|
revyl_verify/_cleanup.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import atexit
|
|
4
|
+
import signal
|
|
5
|
+
import threading
|
|
6
|
+
import time
|
|
7
|
+
from types import FrameType
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from ._session import Session
|
|
12
|
+
|
|
13
|
+
_sessions: dict[Session, tuple[threading.Event, threading.Thread]] = {}
|
|
14
|
+
_lock = threading.RLock()
|
|
15
|
+
shutting_down = False
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def install_signal_cleanup() -> None:
|
|
19
|
+
if threading.current_thread() is threading.main_thread():
|
|
20
|
+
if signal.getsignal(signal.SIGTERM) == signal.SIG_DFL:
|
|
21
|
+
signal.signal(signal.SIGTERM, _terminate)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _terminate(signum: int, frame: FrameType | None) -> None:
|
|
25
|
+
raise SystemExit(128 + signum)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def track(session: Session) -> None:
|
|
29
|
+
with _lock:
|
|
30
|
+
if session in _sessions:
|
|
31
|
+
return
|
|
32
|
+
event = threading.Event()
|
|
33
|
+
worker = threading.Thread(target=_wait_and_close, args=(session, event), daemon=True)
|
|
34
|
+
_sessions[session] = (event, worker)
|
|
35
|
+
worker.start()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def untrack(session: Session) -> None:
|
|
39
|
+
with _lock:
|
|
40
|
+
tracked = _sessions.pop(session, None)
|
|
41
|
+
if tracked:
|
|
42
|
+
tracked[0].set()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _wait_and_close(session: Session, event: threading.Event) -> None:
|
|
46
|
+
event.wait()
|
|
47
|
+
with _lock:
|
|
48
|
+
pending = session in _sessions
|
|
49
|
+
if pending:
|
|
50
|
+
_close(session)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _close(session: Session) -> None:
|
|
54
|
+
try:
|
|
55
|
+
session.close()
|
|
56
|
+
except Exception:
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def cleanup() -> None:
|
|
61
|
+
global shutting_down
|
|
62
|
+
shutting_down = True
|
|
63
|
+
with _lock:
|
|
64
|
+
workers = list(_sessions.values())
|
|
65
|
+
deadline = time.monotonic() + 15
|
|
66
|
+
for event, _ in workers:
|
|
67
|
+
event.set()
|
|
68
|
+
for _, worker in workers:
|
|
69
|
+
if worker.ident is not None:
|
|
70
|
+
worker.join(max(0, deadline - time.monotonic()))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
atexit.register(cleanup)
|