runcloud-sdk 0.8.0__tar.gz → 0.8.1__tar.gz

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 (20) hide show
  1. {runcloud_sdk-0.8.0/runcloud_sdk.egg-info → runcloud_sdk-0.8.1}/PKG-INFO +1 -1
  2. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/pyproject.toml +1 -1
  3. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud/client.py +72 -9
  4. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1/runcloud_sdk.egg-info}/PKG-INFO +1 -1
  5. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/tests/test_sdk.py +90 -2
  6. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/LICENSE +0 -0
  7. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/README.md +0 -0
  8. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud/__init__.py +0 -0
  9. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud/compat/__init__.py +0 -0
  10. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud/compat/blaxel.py +0 -0
  11. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud/compat/daytona.py +0 -0
  12. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud/compat/e2b.py +0 -0
  13. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud/compat/modal.py +0 -0
  14. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud/compat/sprites.py +0 -0
  15. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud/compat/vercel.py +0 -0
  16. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud_sdk.egg-info/SOURCES.txt +0 -0
  17. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud_sdk.egg-info/dependency_links.txt +0 -0
  18. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud_sdk.egg-info/requires.txt +0 -0
  19. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/runcloud_sdk.egg-info/top_level.txt +0 -0
  20. {runcloud_sdk-0.8.0 → runcloud_sdk-0.8.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: runcloud-sdk
3
- Version: 0.8.0
3
+ Version: 0.8.1
4
4
  Summary: Python SDK for run.cloud, including sandbox-provider compatibility adapters
5
5
  Author: Newly
6
6
  License-Expression: Apache-2.0
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "runcloud-sdk"
3
- version = "0.8.0"
3
+ version = "0.8.1"
4
4
  description = "Python SDK for run.cloud, including sandbox-provider compatibility adapters"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.9"
@@ -20,7 +20,8 @@ from websockets.sync.client import connect as websocket_connect
20
20
  Transport = Callable[[str, str, Any], Any]
21
21
  StreamTransport = Callable[[str, dict[str, Any]], Any]
22
22
  DEFAULT_API_URL = "https://api.run.cloud"
23
- USER_AGENT = "runcloud-python/0.8.0"
23
+ USER_AGENT = "runcloud-python/0.8.1"
24
+ FILE_CHUNK_BYTES = 512 * 1024
24
25
 
25
26
 
26
27
  class RunCloudError(Exception):
@@ -447,14 +448,76 @@ class Sandbox:
447
448
  )
448
449
 
449
450
  def read_file(self, path: str) -> bytes:
450
- payload = self._client.request(
451
- "POST",
452
- f"/run-cloud/sandboxes/{urllib.parse.quote(self.id, safe='')}/fs",
453
- {"op": "read", "path": path},
454
- )
455
- if payload.get("error"):
456
- raise RunCloudError(0, str(payload["error"]))
457
- return base64.b64decode(payload.get("content") or "")
451
+ if not path:
452
+ raise ValueError("path must not be empty")
453
+ sandbox_id = urllib.parse.quote(self.id, safe="")
454
+ chunks: list[bytes] = []
455
+ offset = 0
456
+ while True:
457
+ payload = self._client.request(
458
+ "POST",
459
+ f"/run-cloud/sandboxes/{sandbox_id}/fs",
460
+ {
461
+ "op": "read",
462
+ "path": path,
463
+ "offset": offset,
464
+ "length": FILE_CHUNK_BYTES,
465
+ },
466
+ )
467
+ if payload.get("error"):
468
+ raise RunCloudError(0, str(payload["error"]))
469
+ chunk = base64.b64decode(payload.get("content") or "")
470
+ chunks.append(chunk)
471
+ if payload.get("eof") is True:
472
+ return b"".join(chunks)
473
+ raw_next_offset = payload.get("next_offset")
474
+ raw_size = payload.get("size")
475
+ if (
476
+ payload.get("eof") is None
477
+ and raw_next_offset is None
478
+ and raw_size is None
479
+ ):
480
+ return b"".join(chunks)
481
+ next_offset = int(
482
+ raw_next_offset if raw_next_offset is not None else offset + len(chunk)
483
+ )
484
+ if raw_size is not None and next_offset >= int(raw_size):
485
+ return b"".join(chunks)
486
+ if next_offset <= offset:
487
+ raise RunCloudError(0, "file read did not advance")
488
+ offset = next_offset
489
+
490
+ def write_file(
491
+ self,
492
+ path: str,
493
+ content: bytes,
494
+ mode: Optional[int] = None,
495
+ ) -> None:
496
+ if not path:
497
+ raise ValueError("path must not be empty")
498
+ if not isinstance(content, bytes):
499
+ raise TypeError("content must be bytes")
500
+ sandbox_id = urllib.parse.quote(self.id, safe="")
501
+ chunk_count = max(1, (len(content) + FILE_CHUNK_BYTES - 1) // FILE_CHUNK_BYTES)
502
+ for index in range(chunk_count):
503
+ offset = index * FILE_CHUNK_BYTES
504
+ chunk = content[offset : offset + FILE_CHUNK_BYTES]
505
+ body: dict[str, Any] = {
506
+ "op": "write",
507
+ "path": path,
508
+ "offset": offset,
509
+ "content": base64.b64encode(chunk).decode(),
510
+ "truncate": index == 0,
511
+ }
512
+ if mode is not None:
513
+ body["mode"] = mode
514
+ payload = self._client.request(
515
+ "POST",
516
+ f"/run-cloud/sandboxes/{sandbox_id}/fs",
517
+ body,
518
+ )
519
+ if payload.get("error"):
520
+ raise RunCloudError(0, str(payload["error"]))
458
521
 
459
522
  def pause(self) -> "Sandbox":
460
523
  self._data = self._client.request(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: runcloud-sdk
3
- Version: 0.8.0
3
+ Version: 0.8.1
4
4
  Summary: Python SDK for run.cloud, including sandbox-provider compatibility adapters
5
5
  Author: Newly
6
6
  License-Expression: Apache-2.0
@@ -111,6 +111,94 @@ class SDKTests(unittest.TestCase):
111
111
  self.recorder.calls,
112
112
  )
113
113
 
114
+ def test_exec_contract_defaults_legacy_true_and_preserves_false(self):
115
+ for frame, expected in [
116
+ ({"stream": "exit"}, 0),
117
+ ({"stream": "exit", "code": 1}, 1),
118
+ ]:
119
+ with self.subTest(frame=frame):
120
+ def stream_transport(_path, _body, exit_frame=frame):
121
+ return iter([json.dumps(exit_frame)])
122
+
123
+ client = Client(
124
+ api_key="rc_live_test",
125
+ transport=self.recorder,
126
+ stream_transport=stream_transport,
127
+ )
128
+ process = client.get("sb_python").exec_stream(
129
+ "true" if expected == 0 else "false"
130
+ )
131
+ self.assertEqual(process.wait(), expected)
132
+
133
+ def test_read_file_downloads_all_chunks(self):
134
+ def transport(method, path, body):
135
+ if method == "GET":
136
+ return SANDBOX
137
+ self.assertEqual(method, "POST")
138
+ self.assertEqual(path, "/run-cloud/sandboxes/sb_python/fs")
139
+ if body["offset"] == 0:
140
+ return {
141
+ "content": base64.b64encode(b"first").decode(),
142
+ "next_offset": 5,
143
+ "size": 11,
144
+ }
145
+ return {
146
+ "content": base64.b64encode(b"second").decode(),
147
+ "eof": True,
148
+ "next_offset": 11,
149
+ "size": 11,
150
+ }
151
+
152
+ box = Client(api_key="rc_live_test", transport=transport).get("sb_python")
153
+ self.assertEqual(box.read_file("/tmp/chunked"), b"firstsecond")
154
+
155
+ def test_write_file_uploads_binary_content_in_chunks(self):
156
+ box = self.client.create()
157
+ self.recorder.calls.clear()
158
+ content = b"a" * (512 * 1024) + b"tail"
159
+
160
+ box.write_file("/tmp/input.bin", content, mode=0o600)
161
+
162
+ writes = [body for method, path, body in self.recorder.calls if path.endswith("/fs")]
163
+ self.assertEqual(len(writes), 2)
164
+ self.assertEqual(
165
+ writes[0],
166
+ {
167
+ "op": "write",
168
+ "path": "/tmp/input.bin",
169
+ "offset": 0,
170
+ "content": base64.b64encode(content[: 512 * 1024]).decode(),
171
+ "truncate": True,
172
+ "mode": 0o600,
173
+ },
174
+ )
175
+ self.assertEqual(writes[1]["offset"], 512 * 1024)
176
+ self.assertEqual(base64.b64decode(writes[1]["content"]), b"tail")
177
+ self.assertFalse(writes[1]["truncate"])
178
+
179
+ def test_write_file_truncates_when_writing_an_empty_file(self):
180
+ box = self.client.create()
181
+ self.recorder.calls.clear()
182
+
183
+ box.write_file("/tmp/empty", b"")
184
+
185
+ self.assertEqual(
186
+ self.recorder.calls,
187
+ [
188
+ (
189
+ "POST",
190
+ "/run-cloud/sandboxes/sb_python/fs",
191
+ {
192
+ "op": "write",
193
+ "path": "/tmp/empty",
194
+ "offset": 0,
195
+ "content": "",
196
+ "truncate": True,
197
+ },
198
+ ),
199
+ ],
200
+ )
201
+
114
202
  def test_compatibility_adapters_share_the_public_client(self):
115
203
  app = modal.App.lookup("compat", client=self.client)
116
204
  modal_sandbox = modal.Sandbox.create(app=app)
@@ -227,7 +315,7 @@ class SDKTests(unittest.TestCase):
227
315
  Client(api_key="rc_live_test").request("GET", "/run-cloud/account")
228
316
 
229
317
  request = urlopen.call_args.args[0]
230
- self.assertEqual(request.get_header("User-agent"), "runcloud-python/0.8.0")
318
+ self.assertEqual(request.get_header("User-agent"), "runcloud-python/0.8.1")
231
319
 
232
320
  def test_exec_stream_opens_authenticated_websocket_and_sends_command(self):
233
321
  session = WebSocketSession()
@@ -257,7 +345,7 @@ class SDKTests(unittest.TestCase):
257
345
  connect.assert_called_once_with(
258
346
  "wss://api.example.test/run-cloud/sandboxes/sb_1/exec/stream?mode=command",
259
347
  additional_headers={"Authorization": "Bearer rc_live_test"},
260
- user_agent_header="runcloud-python/0.8.0",
348
+ user_agent_header="runcloud-python/0.8.1",
261
349
  open_timeout=60.0,
262
350
  close_timeout=5,
263
351
  max_size=8 << 20,
File without changes
File without changes
File without changes