scalebox-sdk 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.
Files changed (157) hide show
  1. scalebox/__init__.py +80 -0
  2. scalebox/api/__init__.py +128 -0
  3. scalebox/api/client/__init__.py +8 -0
  4. scalebox/api/client/api/__init__.py +1 -0
  5. scalebox/api/client/api/sandboxes/__init__.py +0 -0
  6. scalebox/api/client/api/sandboxes/delete_sandboxes_sandbox_id.py +161 -0
  7. scalebox/api/client/api/sandboxes/get_sandboxes.py +176 -0
  8. scalebox/api/client/api/sandboxes/get_sandboxes_metrics.py +173 -0
  9. scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id.py +163 -0
  10. scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id_logs.py +199 -0
  11. scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id_metrics.py +214 -0
  12. scalebox/api/client/api/sandboxes/get_v2_sandboxes.py +229 -0
  13. scalebox/api/client/api/sandboxes/post_sandboxes.py +174 -0
  14. scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_pause.py +165 -0
  15. scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_refreshes.py +182 -0
  16. scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_resume.py +190 -0
  17. scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_timeout.py +194 -0
  18. scalebox/api/client/client.py +288 -0
  19. scalebox/api/client/errors.py +16 -0
  20. scalebox/api/client/models/__init__.py +81 -0
  21. scalebox/api/client/models/build_log_entry.py +79 -0
  22. scalebox/api/client/models/created_access_token.py +100 -0
  23. scalebox/api/client/models/created_team_api_key.py +166 -0
  24. scalebox/api/client/models/error.py +67 -0
  25. scalebox/api/client/models/identifier_masking_details.py +83 -0
  26. scalebox/api/client/models/listed_sandbox.py +138 -0
  27. scalebox/api/client/models/log_level.py +11 -0
  28. scalebox/api/client/models/new_access_token.py +59 -0
  29. scalebox/api/client/models/new_sandbox.py +125 -0
  30. scalebox/api/client/models/new_team_api_key.py +59 -0
  31. scalebox/api/client/models/node.py +154 -0
  32. scalebox/api/client/models/node_detail.py +152 -0
  33. scalebox/api/client/models/node_status.py +11 -0
  34. scalebox/api/client/models/node_status_change.py +61 -0
  35. scalebox/api/client/models/post_sandboxes_sandbox_id_refreshes_body.py +59 -0
  36. scalebox/api/client/models/post_sandboxes_sandbox_id_timeout_body.py +59 -0
  37. scalebox/api/client/models/resumed_sandbox.py +68 -0
  38. scalebox/api/client/models/sandbox.py +125 -0
  39. scalebox/api/client/models/sandbox_detail.py +178 -0
  40. scalebox/api/client/models/sandbox_log.py +70 -0
  41. scalebox/api/client/models/sandbox_logs.py +73 -0
  42. scalebox/api/client/models/sandbox_metric.py +110 -0
  43. scalebox/api/client/models/sandbox_state.py +9 -0
  44. scalebox/api/client/models/sandboxes_with_metrics.py +59 -0
  45. scalebox/api/client/models/team.py +83 -0
  46. scalebox/api/client/models/team_api_key.py +158 -0
  47. scalebox/api/client/models/team_user.py +68 -0
  48. scalebox/api/client/models/template.py +179 -0
  49. scalebox/api/client/models/template_build.py +117 -0
  50. scalebox/api/client/models/template_build_file_upload.py +70 -0
  51. scalebox/api/client/models/template_build_request.py +115 -0
  52. scalebox/api/client/models/template_build_request_v2.py +88 -0
  53. scalebox/api/client/models/template_build_start_v2.py +114 -0
  54. scalebox/api/client/models/template_build_status.py +11 -0
  55. scalebox/api/client/models/template_step.py +91 -0
  56. scalebox/api/client/models/template_update_request.py +59 -0
  57. scalebox/api/client/models/update_team_api_key.py +59 -0
  58. scalebox/api/client/py.typed +1 -0
  59. scalebox/api/client/types.py +46 -0
  60. scalebox/api/metadata.py +19 -0
  61. scalebox/cli.py +125 -0
  62. scalebox/client/__init__.py +0 -0
  63. scalebox/client/aclient.py +57 -0
  64. scalebox/client/api.proto +460 -0
  65. scalebox/client/buf.gen.yaml +8 -0
  66. scalebox/client/client.py +102 -0
  67. scalebox/client/requirements.txt +5 -0
  68. scalebox/code_interpreter/__init__.py +12 -0
  69. scalebox/code_interpreter/charts.py +230 -0
  70. scalebox/code_interpreter/code_interpreter_async.py +369 -0
  71. scalebox/code_interpreter/code_interpreter_sync.py +317 -0
  72. scalebox/code_interpreter/constants.py +3 -0
  73. scalebox/code_interpreter/exceptions.py +13 -0
  74. scalebox/code_interpreter/models.py +485 -0
  75. scalebox/connection_config.py +92 -0
  76. scalebox/csx_connect/__init__.py +1 -0
  77. scalebox/csx_connect/client.py +485 -0
  78. scalebox/csx_desktop/__init__.py +0 -0
  79. scalebox/csx_desktop/main.py +651 -0
  80. scalebox/exceptions.py +83 -0
  81. scalebox/generated/__init__.py +0 -0
  82. scalebox/generated/api.py +61 -0
  83. scalebox/generated/api_pb2.py +203 -0
  84. scalebox/generated/api_pb2.pyi +956 -0
  85. scalebox/generated/api_pb2_connect.py +1456 -0
  86. scalebox/generated/rpc.py +50 -0
  87. scalebox/generated/versions.py +3 -0
  88. scalebox/requirements.txt +36 -0
  89. scalebox/sandbox/__init__.py +0 -0
  90. scalebox/sandbox/commands/__init__.py +0 -0
  91. scalebox/sandbox/commands/command_handle.py +69 -0
  92. scalebox/sandbox/commands/main.py +39 -0
  93. scalebox/sandbox/filesystem/__init__.py +0 -0
  94. scalebox/sandbox/filesystem/filesystem.py +95 -0
  95. scalebox/sandbox/filesystem/watch_handle.py +60 -0
  96. scalebox/sandbox/main.py +139 -0
  97. scalebox/sandbox/sandbox_api.py +91 -0
  98. scalebox/sandbox/signature.py +40 -0
  99. scalebox/sandbox/utils.py +34 -0
  100. scalebox/sandbox_async/__init__.py +1 -0
  101. scalebox/sandbox_async/commands/command.py +307 -0
  102. scalebox/sandbox_async/commands/command_handle.py +187 -0
  103. scalebox/sandbox_async/commands/pty.py +187 -0
  104. scalebox/sandbox_async/filesystem/filesystem.py +557 -0
  105. scalebox/sandbox_async/filesystem/watch_handle.py +61 -0
  106. scalebox/sandbox_async/main.py +646 -0
  107. scalebox/sandbox_async/sandbox_api.py +365 -0
  108. scalebox/sandbox_async/utils.py +7 -0
  109. scalebox/sandbox_sync/__init__.py +2 -0
  110. scalebox/sandbox_sync/commands/__init__.py +0 -0
  111. scalebox/sandbox_sync/commands/command.py +300 -0
  112. scalebox/sandbox_sync/commands/command_handle.py +150 -0
  113. scalebox/sandbox_sync/commands/pty.py +181 -0
  114. scalebox/sandbox_sync/filesystem/__init__.py +0 -0
  115. scalebox/sandbox_sync/filesystem/filesystem.py +543 -0
  116. scalebox/sandbox_sync/filesystem/watch_handle.py +66 -0
  117. scalebox/sandbox_sync/main.py +790 -0
  118. scalebox/sandbox_sync/sandbox_api.py +356 -0
  119. scalebox/test/CODE_INTERPRETER_TESTS_READY.md +323 -0
  120. scalebox/test/README.md +329 -0
  121. scalebox/test/__init__.py +0 -0
  122. scalebox/test/aclient.py +72 -0
  123. scalebox/test/code_interpreter_centext.py +21 -0
  124. scalebox/test/code_interpreter_centext_sync.py +21 -0
  125. scalebox/test/code_interpreter_test.py +34 -0
  126. scalebox/test/code_interpreter_test_sync.py +34 -0
  127. scalebox/test/run_all_validation_tests.py +334 -0
  128. scalebox/test/run_code_interpreter_tests.sh +67 -0
  129. scalebox/test/run_tests.sh +230 -0
  130. scalebox/test/test_basic.py +78 -0
  131. scalebox/test/test_code_interpreter_async_comprehensive.py +2653 -0
  132. scalebox/test/test_code_interpreter_e2basync_comprehensive.py +2655 -0
  133. scalebox/test/test_code_interpreter_e2bsync_comprehensive.py +3416 -0
  134. scalebox/test/test_code_interpreter_sync_comprehensive.py +3412 -0
  135. scalebox/test/test_e2b_first.py +11 -0
  136. scalebox/test/test_sandbox_async_comprehensive.py +738 -0
  137. scalebox/test/test_sandbox_stress_and_edge_cases.py +778 -0
  138. scalebox/test/test_sandbox_sync_comprehensive.py +770 -0
  139. scalebox/test/test_sandbox_usage_examples.py +987 -0
  140. scalebox/test/testacreate.py +24 -0
  141. scalebox/test/testagetinfo.py +18 -0
  142. scalebox/test/testcodeinterpreter_async.py +508 -0
  143. scalebox/test/testcodeinterpreter_sync.py +239 -0
  144. scalebox/test/testcomputeuse.py +243 -0
  145. scalebox/test/testnovnc.py +12 -0
  146. scalebox/test/testsandbox_async.py +118 -0
  147. scalebox/test/testsandbox_sync.py +38 -0
  148. scalebox/utils/__init__.py +0 -0
  149. scalebox/utils/httpcoreclient.py +297 -0
  150. scalebox/utils/httpxclient.py +403 -0
  151. scalebox/version.py +16 -0
  152. scalebox_sdk-0.1.0.dist-info/METADATA +292 -0
  153. scalebox_sdk-0.1.0.dist-info/RECORD +157 -0
  154. scalebox_sdk-0.1.0.dist-info/WHEEL +5 -0
  155. scalebox_sdk-0.1.0.dist-info/entry_points.txt +2 -0
  156. scalebox_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
  157. scalebox_sdk-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,651 @@
1
+ import time
2
+ from re import search as re_search
3
+ from shlex import quote as quote_string
4
+ from typing import Callable, Dict, Iterator, Literal, Optional, Tuple, Union, overload
5
+ from uuid import uuid4
6
+
7
+ from httpx._types import ProxyTypes
8
+
9
+ from ..exceptions import TimeoutException
10
+ from ..sandbox_sync import CommandExitException, CommandHandle, CommandResult
11
+ from ..sandbox_sync import Sandbox as SandboxBase
12
+
13
+ MOUSE_BUTTONS = {"left": 1, "right": 3, "middle": 2}
14
+
15
+ KEYS = {
16
+ "alt": "Alt_L",
17
+ "alt_left": "Alt_L",
18
+ "alt_right": "Alt_R",
19
+ "backspace": "BackSpace",
20
+ "break": "Pause",
21
+ "caps_lock": "Caps_Lock",
22
+ "cmd": "Super_L",
23
+ "command": "Super_L",
24
+ "control": "Control_L",
25
+ "control_left": "Control_L",
26
+ "control_right": "Control_R",
27
+ "ctrl": "Control_L",
28
+ "del": "Delete",
29
+ "delete": "Delete",
30
+ "down": "Down",
31
+ "end": "End",
32
+ "enter": "Return",
33
+ "esc": "Escape",
34
+ "escape": "Escape",
35
+ "f1": "F1",
36
+ "f2": "F2",
37
+ "f3": "F3",
38
+ "f4": "F4",
39
+ "f5": "F5",
40
+ "f6": "F6",
41
+ "f7": "F7",
42
+ "f8": "F8",
43
+ "f9": "F9",
44
+ "f10": "F10",
45
+ "f11": "F11",
46
+ "f12": "F12",
47
+ "home": "Home",
48
+ "insert": "Insert",
49
+ "left": "Left",
50
+ "menu": "Menu",
51
+ "meta": "Meta_L",
52
+ "num_lock": "Num_Lock",
53
+ "page_down": "Page_Down",
54
+ "page_up": "Page_Up",
55
+ "pause": "Pause",
56
+ "print": "Print",
57
+ "right": "Right",
58
+ "scroll_lock": "Scroll_Lock",
59
+ "shift": "Shift_L",
60
+ "shift_left": "Shift_L",
61
+ "shift_right": "Shift_R",
62
+ "space": "space",
63
+ "super": "Super_L",
64
+ "super_left": "Super_L",
65
+ "super_right": "Super_R",
66
+ "tab": "Tab",
67
+ "up": "Up",
68
+ "win": "Super_L",
69
+ "windows": "Super_L",
70
+ }
71
+
72
+
73
+ def map_key(key: str) -> str:
74
+ lower_key = key.lower()
75
+ if lower_key in KEYS:
76
+ return KEYS[lower_key]
77
+ return lower_key
78
+
79
+
80
+ class _VNCServer:
81
+ def __init__(self, desktop: "Sandbox") -> None:
82
+ self.__novnc_handle: Optional[CommandHandle] = None
83
+
84
+ self._vnc_port = 5900
85
+ self._port = 6080
86
+ self._novnc_auth_enabled = False
87
+ self._novnc_password = None
88
+
89
+ self._url = f"https://{self._port}-{desktop.sandbox_domain}/vnc.html"
90
+ # self._url=""
91
+
92
+ self.__desktop = desktop
93
+
94
+ def _wait_for_port(self, port: int) -> bool:
95
+ return self.__desktop._wait_and_verify(
96
+ f'netstat -tuln | grep ":{port} "', lambda r: r.stdout.strip() != ""
97
+ )
98
+
99
+ def _check_vnc_running(self) -> bool:
100
+ try:
101
+ self.__desktop.commands.run("pgrep -x x11vnc")
102
+ return True
103
+ except CommandExitException:
104
+ return False
105
+
106
+ @staticmethod
107
+ def _generate_password(length: int = 16) -> str:
108
+ import secrets
109
+ import string
110
+
111
+ characters = string.ascii_letters + string.digits
112
+ return "".join(secrets.choice(characters) for _ in range(length))
113
+
114
+ def get_url(
115
+ self,
116
+ auto_connect: bool = True,
117
+ view_only: bool = False,
118
+ resize: str = "scale",
119
+ auth_key: Optional[str] = None,
120
+ ) -> str:
121
+ params = []
122
+ if auto_connect:
123
+ params.append("autoconnect=true")
124
+ if view_only:
125
+ params.append(f"view_only=true")
126
+ if resize:
127
+ params.append(f"resize={resize}")
128
+ if auth_key:
129
+ params.append(f"password={auth_key}")
130
+ if params:
131
+ return f"{self._url}?{'&'.join(params)}"
132
+ return self._url
133
+
134
+ def get_auth_key(self) -> str:
135
+ if not self._novnc_password:
136
+ raise RuntimeError(
137
+ "Unable to retrieve stream auth key, check if require_auth is enabled"
138
+ )
139
+ return self._novnc_password
140
+
141
+ def start(
142
+ self,
143
+ vnc_port: Optional[int] = None,
144
+ port: Optional[int] = None,
145
+ require_auth: bool = False,
146
+ window_id: Optional[str] = None,
147
+ ) -> None:
148
+ # If stream is already running, throw an error
149
+ if self._check_vnc_running():
150
+ raise RuntimeError("Stream is already running")
151
+
152
+ # Update parameters if provided
153
+ self._vnc_port = vnc_port or self._vnc_port
154
+ self._port = port or self._port
155
+ self._novnc_auth_enabled = require_auth or self._novnc_auth_enabled
156
+ self._novnc_password = self._generate_password() if require_auth else None
157
+
158
+ # Update URL with new port
159
+ # self._url = ""
160
+ # self._url = f"https://{self.__desktop.get_host(self._port)}/vnc.html"
161
+
162
+ # Set up VNC command
163
+ pwd_flag = "-nopw"
164
+ if self._novnc_auth_enabled:
165
+ self.__desktop.commands.run("mkdir -p ~/.vnc")
166
+ self.__desktop.commands.run(
167
+ f"x11vnc -storepasswd {self._novnc_password} ~/.vnc/passwd"
168
+ )
169
+ pwd_flag = "-usepw"
170
+ window_id_flag = ""
171
+ if window_id:
172
+ window_id_flag = f"-id {window_id}"
173
+
174
+ vnc_command = (
175
+ f"DISPLAY={self.__desktop._display} x11vnc -bg -display {self.__desktop._display} -forever -wait 50 -shared "
176
+ f"-rfbport {self._vnc_port} {pwd_flag} 2>/tmp/x11vnc_stderr.log {window_id_flag}"
177
+ )
178
+
179
+ novnc_command = (
180
+ f"cd /opt/noVNC/utils && ./novnc_proxy --vnc localhost:{self._vnc_port} "
181
+ f"--listen {self._port} --web /opt/noVNC > /tmp/novnc.log 2>&1 &"
182
+ )
183
+
184
+ self.__desktop.commands.run(vnc_command)
185
+ self.__novnc_handle = self.__desktop.commands.run(
186
+ novnc_command, background=True, timeout=0
187
+ )
188
+ if not self._wait_for_port(self._port):
189
+ raise TimeoutException("Could not start noVNC server")
190
+
191
+ def stop(self) -> None:
192
+ if self._check_vnc_running():
193
+ self.__desktop.commands.run("pkill x11vnc")
194
+
195
+ if self.__novnc_handle:
196
+ self.__novnc_handle.kill()
197
+ self.__novnc_handle = None
198
+
199
+
200
+ class Sandbox(SandboxBase):
201
+ default_template = "desktop"
202
+
203
+ # def __init__(
204
+ # self,
205
+ # resolution: Optional[Tuple[int, int]] = None,
206
+ # dpi: Optional[int] = None,
207
+ # display: Optional[str] = None,
208
+ # template: Optional[str] = None,
209
+ # timeout: Optional[int] = None,
210
+ # metadata: Optional[Dict[str, str]] = None,
211
+ # envs: Optional[Dict[str, str]] = None,
212
+ # api_key: Optional[str] = None,
213
+ # domain: Optional[str] = None,
214
+ # debug: Optional[bool] = None,
215
+ # sandbox_id: Optional[str] = None,
216
+ # request_timeout: Optional[float] = None,
217
+ # proxy: Optional[ProxyTypes] = None,
218
+ # ):
219
+ # """
220
+ # Create a new desktop sandbox.
221
+ #
222
+ # By default, the sandbox is created from the `desktop` template.
223
+ #
224
+ # :param resolution: Startup the desktop with custom screen resolution. Defaults to (1024, 768)
225
+ # :param dpi: Startup the desktop with custom DPI. Defaults to 96
226
+ # :param display: Startup the desktop with custom display. Defaults to ":0"
227
+ # :param template: Sandbox template name or ID
228
+ # :param timeout: Timeout for the sandbox in **seconds**, default to 300 seconds. Maximum time a sandbox can be kept alive is 24 hours (86_400 seconds) for Pro users and 1 hour (3_600 seconds) for Hobby users
229
+ # :param metadata: Custom metadata for the sandbox
230
+ # :param envs: Custom environment variables for the sandbox
231
+ # :param api_key: E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable
232
+ # :param domain: E2B domain to use for authentication, defaults to `E2B_DOMAIN` environment variable
233
+ # :param debug: If True, the sandbox will be created in debug mode, defaults to `E2B_DEBUG` environment variable
234
+ # :param sandbox_id: Sandbox ID to connect to, defaults to `E2B_SANDBOX_ID` environment variable
235
+ # :param request_timeout: Timeout for the request in **seconds**
236
+ # :param proxy: Proxy to use for the request and for the requests made to the returned sandbox
237
+ #
238
+ # :return: sandbox instance for the new sandbox
239
+ # """
240
+ # self._display = display or ":0"
241
+ # self._last_xfce4_pid = None
242
+ #
243
+ # # Initialize environment variables with DISPLAY
244
+ # if envs is None:
245
+ # envs = {}
246
+ # envs["DISPLAY"] = self._display
247
+ #
248
+ # _base = SandboxBase.create(
249
+ # template=template,
250
+ # timeout=timeout,
251
+ # metadata=metadata,
252
+ # envs=envs,
253
+ # api_key=api_key,
254
+ # domain=domain,
255
+ # debug=debug,
256
+ # sandbox_id=sandbox_id,
257
+ # request_timeout=request_timeout,
258
+ # proxy=proxy,
259
+ # )
260
+ # self.__dict__.update(_base.__dict__)
261
+ #
262
+ # # Only initialize desktop environment if we're not just connecting to an existing sandbox
263
+ # print("--------------"+sandbox_id)
264
+ # if not sandbox_id:
265
+ # width, height = resolution or (1024, 768)
266
+ #
267
+ # # self.commands.run(
268
+ # # f"Xvfb {self._display} -ac -screen 0 {width}x{height}x24"
269
+ # # f" -retro -dpi {dpi or 96} -nolisten tcp -nolisten unix",
270
+ # # background=True,
271
+ # # timeout=0,
272
+ # # )
273
+ # self.commands.run(
274
+ # f"Xvfb {self._display} -ac -screen 0 {width}x{height}x24"
275
+ # f" -retro -dpi {dpi or 96} -nolisten tcp -nolisten unix &",
276
+ # background=True,
277
+ # timeout=0,
278
+ # )
279
+ # print("11111111111")
280
+ # if not self._wait_and_verify(
281
+ # f"xdpyinfo -display {self._display}", lambda r: r.exit_code == 0
282
+ # ):
283
+ # raise TimeoutException("Could not start Xvfb")
284
+ # print("2222222222")
285
+ # self.__vnc_server = _VNCServer(self)
286
+ # self._start_xfce4()
287
+ # print("333333333333")
288
+ # else:
289
+ # # When connecting to existing sandbox, just initialize VNC server
290
+ # self.__vnc_server = _VNCServer(self)
291
+
292
+ @classmethod
293
+ def create(
294
+ cls,
295
+ resolution: Optional[Tuple[int, int]] = None,
296
+ dpi: Optional[int] = None,
297
+ display: Optional[str] = None,
298
+ template: Optional[str] = None,
299
+ timeout: Optional[int] = None,
300
+ metadata: Optional[Dict[str, str]] = None,
301
+ envs: Optional[Dict[str, str]] = None,
302
+ api_key: Optional[str] = None,
303
+ domain: Optional[str] = None,
304
+ debug: Optional[bool] = None,
305
+ sandbox_id: Optional[str] = None,
306
+ request_timeout: Optional[float] = None,
307
+ proxy: Optional[ProxyTypes] = None,
308
+ ) -> "Sandbox":
309
+ """
310
+ 同步创建或连接到一个桌面沙箱。
311
+
312
+ 参数含义与基类相同。首次创建时会自动启动 Xvfb + xfce4。
313
+ """
314
+ display = display or ":0"
315
+ if envs is None:
316
+ envs = {}
317
+ envs["DISPLAY"] = display
318
+
319
+ # 1. 让父类工厂去实际创建 / 复用
320
+ base = SandboxBase.create(
321
+ template=template or cls.default_template,
322
+ timeout=timeout,
323
+ metadata=metadata,
324
+ envs=envs,
325
+ api_key=api_key,
326
+ domain=domain,
327
+ debug=debug,
328
+ sandbox_id=sandbox_id,
329
+ request_timeout=request_timeout,
330
+ proxy=proxy,
331
+ )
332
+
333
+ # 2. 把类型动态升级成 Sandbox
334
+ base.__class__ = cls
335
+ base._display = display
336
+ base._last_xfce4_pid = None
337
+
338
+ # 3. 首次创建才启动桌面
339
+ if not sandbox_id:
340
+ base._start_desktop(resolution, dpi)
341
+ else:
342
+ # 连接已有沙箱,仅初始化 VNC
343
+ base.__vnc_server = _VNCServer(base)
344
+
345
+ return base
346
+
347
+ # ====================== 子类私有方法 ======================
348
+ def _start_desktop(
349
+ self,
350
+ resolution: Optional[Tuple[int, int]] = None,
351
+ dpi: Optional[int] = None,
352
+ ) -> None:
353
+ """启动 Xvfb 并等待成功,然后起 xfce4 + VNC。"""
354
+ width, height = resolution or (1024, 768)
355
+ self.commands.run(
356
+ f"Xvfb {self._display} -ac -screen 0 {width}x{height}x24 "
357
+ f"-retro -dpi {dpi or 96} -nolisten tcp -nolisten unix",
358
+ background=True,
359
+ timeout=0,
360
+ )
361
+ if not self._wait_and_verify(
362
+ f"xdpyinfo -display {self._display}",
363
+ lambda r: r.exit_code == 0,
364
+ ):
365
+ raise TimeoutException("Could not start Xvfb")
366
+ self.__vnc_server = _VNCServer(self)
367
+ self._start_xfce4()
368
+
369
+ def _wait_and_verify(
370
+ self,
371
+ cmd: str,
372
+ on_result: Callable[[CommandResult], bool],
373
+ timeout: int = 10,
374
+ interval: float = 0.5,
375
+ ) -> bool:
376
+
377
+ elapsed = 0
378
+ while elapsed < timeout:
379
+ try:
380
+ if on_result(self.commands.run(cmd)):
381
+ return True
382
+ except CommandExitException:
383
+ continue
384
+
385
+ time.sleep(interval)
386
+ elapsed += interval
387
+
388
+ return False
389
+
390
+ def _start_xfce4(self):
391
+ """
392
+ Start xfce4 session if logged out or not running.
393
+ """
394
+ if self._last_xfce4_pid is None or "[xfce4-session] <defunct>" in (
395
+ self.commands.run(
396
+ f"ps aux | grep {self._last_xfce4_pid} | grep -v grep | head -n 1"
397
+ ).stdout.strip()
398
+ ):
399
+ self._last_xfce4_pid = self.commands.run(
400
+ f"mkdir -p $HOME/.config $HOME/.cache $HOME/.dbus && DISPLAY={self._display} startxfce4",
401
+ background=True,
402
+ timeout=0,
403
+ envs={"HOME": "/workspace"},
404
+ ).pid
405
+
406
+ @property
407
+ def stream(self) -> _VNCServer:
408
+ return self.__vnc_server
409
+
410
+ @overload
411
+ def screenshot(self, format: Literal["stream"]) -> Iterator[bytes]:
412
+ """
413
+ Take a screenshot and return it as a stream of bytes.
414
+ """
415
+
416
+ @overload
417
+ def screenshot(
418
+ self,
419
+ format: Literal["bytes"],
420
+ ) -> bytearray:
421
+ """
422
+ Take a screenshot and return it as a bytearray.
423
+ """
424
+
425
+ def screenshot(
426
+ self,
427
+ format: Literal["bytes", "stream"] = "bytes",
428
+ ):
429
+ """
430
+ Take a screenshot and return it in the specified format.
431
+
432
+ :param format: The format of the screenshot. Can be 'bytes', 'blob', or 'stream'.
433
+ :returns: The screenshot in the specified format.
434
+ """
435
+ screenshot_path = f"/tmp/screenshot-{uuid4()}.png"
436
+
437
+ self.commands.run(f"DISPLAY={self._display} scrot --pointer {screenshot_path}")
438
+
439
+ file = self.files.read(screenshot_path, format=format)
440
+ self.files.remove(screenshot_path)
441
+ return file
442
+
443
+ def left_click(self, x: Optional[int] = None, y: Optional[int] = None):
444
+ """
445
+ Left click on the mouse position.
446
+ """
447
+ if x and y:
448
+ self.move_mouse(x, y)
449
+ self.commands.run(f"DISPLAY={self._display} xdotool click 1")
450
+
451
+ def double_click(self, x: Optional[int] = None, y: Optional[int] = None):
452
+ """
453
+ Double left click on the mouse position.
454
+ """
455
+ if x and y:
456
+ self.move_mouse(x, y)
457
+ self.commands.run(f"DISPLAY={self._display} xdotool click --repeat 2 1")
458
+
459
+ def right_click(self, x: Optional[int] = None, y: Optional[int] = None):
460
+ if (x is None) != (y is None):
461
+ raise ValueError("Both x and y must be provided together")
462
+ """
463
+ Right click on the mouse position.
464
+ """
465
+ if x and y:
466
+ self.move_mouse(x, y)
467
+ self.commands.run(f"DISPLAY={self._display} xdotool click 3")
468
+
469
+ def middle_click(self, x: Optional[int] = None, y: Optional[int] = None):
470
+ """
471
+ Middle click on the mouse position.
472
+ """
473
+ if x and y:
474
+ self.move_mouse(x, y)
475
+ self.commands.run(f"DISPLAY={self._display} xdotool click 2")
476
+
477
+ def scroll(self, direction: Literal["up", "down"] = "down", amount: int = 1):
478
+ """
479
+ Scroll the mouse wheel by the given amount.
480
+
481
+ :param direction: The direction to scroll. Can be "up" or "down".
482
+ :param amount: The amount to scroll.
483
+ """
484
+ self.commands.run(
485
+ f"DISPLAY={self._display} xdotool click --repeat {amount} {'4' if direction == 'up' else '5'}"
486
+ )
487
+
488
+ def move_mouse(self, x: int, y: int):
489
+ """
490
+ Move the mouse to the given coordinates.
491
+
492
+ :param x: The x coordinate.
493
+ :param y: The y coordinate.
494
+ """
495
+ self.commands.run(f"DISPLAY={self._display} xdotool mousemove --sync {x} {y}")
496
+
497
+ def mouse_press(self, button: Literal["left", "right", "middle"] = "left"):
498
+ """
499
+ Press the mouse button.
500
+ """
501
+ self.commands.run(
502
+ f"DISPLAY={self._display} xdotool mousedown {MOUSE_BUTTONS[button]}"
503
+ )
504
+
505
+ def mouse_release(self, button: Literal["left", "right", "middle"] = "left"):
506
+ """
507
+ Release the mouse button.
508
+ """
509
+ self.commands.run(
510
+ f"DISPLAY={self._display} xdotool mouseup {MOUSE_BUTTONS[button]}"
511
+ )
512
+
513
+ def get_cursor_position(self) -> tuple[int, int]:
514
+ """
515
+ Get the current cursor position.
516
+
517
+ :return: A tuple with the x and y coordinates
518
+ :raises RuntimeError: If the cursor position cannot be determined
519
+ """
520
+ result = self.commands.run(f"DISPLAY={self._display} xdotool getmouselocation")
521
+
522
+ groups = re_search(r"x:(\d+)\s+y:(\d+)", result.stdout)
523
+ if not groups:
524
+ raise RuntimeError(
525
+ f"Failed to parse cursor position from output: {result.stdout}"
526
+ )
527
+
528
+ x, y = groups.group(1), groups.group(2)
529
+ if not x or not y:
530
+ raise RuntimeError(f"Invalid cursor position values: x={x}, y={y}")
531
+
532
+ return int(x), int(y)
533
+
534
+ def get_screen_size(self) -> tuple[int, int]:
535
+ """
536
+ Get the current screen size.
537
+
538
+ :return: A tuple with the width and height
539
+ :raises RuntimeError: If the screen size cannot be determined
540
+ """
541
+ result = self.commands.run(f"DISPLAY={self._display} xrandr")
542
+
543
+ _match = re_search(r"(\d+x\d+)", result.stdout)
544
+ if not _match:
545
+ raise RuntimeError(
546
+ f"Failed to parse screen size from output: {result.stdout}"
547
+ )
548
+
549
+ try:
550
+ return tuple(map(int, _match.group(1).split("x"))) # type: ignore
551
+ except (ValueError, IndexError) as e:
552
+ raise RuntimeError(f"Invalid screen size format: {_match.group(1)}") from e
553
+
554
+ def write(self, text: str, *, chunk_size: int = 25, delay_in_ms: int = 75) -> None:
555
+ """
556
+ Write the given text at the current cursor position.
557
+
558
+ :param text: The text to write.
559
+ :param chunk_size: The size of each chunk of text to write.
560
+ :param delay_in_ms: The delay between each chunk of text.
561
+ """
562
+
563
+ def break_into_chunks(text: str, n: int):
564
+ for i in range(0, len(text), n):
565
+ yield text[i : i + n]
566
+
567
+ for chunk in break_into_chunks(text, chunk_size):
568
+ self.commands.run(
569
+ f"DISPLAY={self._display} xdotool type --delay {delay_in_ms} {quote_string(chunk)}"
570
+ )
571
+
572
+ def press(self, key: Union[str, list[str]]):
573
+ """
574
+ Press a key.
575
+
576
+ :param key: The key to press (e.g. "enter", "space", "backspace", etc.).
577
+ """
578
+ if isinstance(key, list):
579
+ key = "+".join(map_key(k) for k in key)
580
+ else:
581
+ key = map_key(key)
582
+
583
+ self.commands.run(f"DISPLAY={self._display} xdotool key {key}")
584
+
585
+ def drag(self, fr: tuple[int, int], to: tuple[int, int]):
586
+ """
587
+ Drag the mouse from the given position to the given position.
588
+
589
+ :param from: The starting position.
590
+ :param to: The ending position.
591
+ """
592
+ self.move_mouse(fr[0], fr[1])
593
+ self.mouse_press()
594
+ self.move_mouse(to[0], to[1])
595
+ self.mouse_release()
596
+
597
+ def wait(self, ms: int):
598
+ """
599
+ Wait for the given amount of time.
600
+
601
+ :param ms: The amount of time to wait in milliseconds.
602
+ """
603
+ self.commands.run(f"sleep {ms / 1000}")
604
+
605
+ def open(self, file_or_url: str):
606
+ """
607
+ Open a file or a URL in the default application.
608
+
609
+ :param file_or_url: The file or URL to open.
610
+ """
611
+ self.commands.run(
612
+ f"DISPLAY={self._display} xdg-open {file_or_url}", background=True
613
+ )
614
+
615
+ def get_current_window_id(self) -> str:
616
+ """
617
+ Get the current window ID.
618
+ """
619
+ return self.commands.run(
620
+ f"DISPLAY={self._display} xdotool getwindowfocus"
621
+ ).stdout.strip()
622
+
623
+ def get_application_windows(self, application: str) -> list[str]:
624
+ """
625
+ Get the window IDs of all windows for the given application.
626
+ """
627
+ return (
628
+ self.commands.run(
629
+ f"DISPLAY={self._display} xdotool search --onlyvisible --class {application}"
630
+ )
631
+ .stdout.strip()
632
+ .split("\n")
633
+ )
634
+
635
+ def get_window_title(self, window_id: str) -> str:
636
+ """
637
+ Get the title of the window with the given ID.
638
+ """
639
+ return self.commands.run(
640
+ f"DISPLAY={self._display} xdotool getwindowname {window_id}"
641
+ ).stdout.strip()
642
+
643
+ def launch(self, application: str, uri: Optional[str] = None):
644
+ """
645
+ Launch an application.
646
+ """
647
+ self.commands.run(
648
+ f"DISPLAY={self._display} gtk-launch {application} {uri or ''}",
649
+ background=True,
650
+ timeout=0,
651
+ )