-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsync.py
More file actions
506 lines (412 loc) · 15.8 KB
/
Copy pathsync.py
File metadata and controls
506 lines (412 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
"""Synchronous SDK entry points and management interfaces."""
from __future__ import annotations
from typing import Dict, Mapping, Optional
from pathlib import Path
from typing_extensions import Unpack
import httpx
from ._types import (
LongRequestOptions,
SDKDevboxListParams,
SDKObjectListParams,
SDKDevboxCreateParams,
SDKObjectCreateParams,
SDKBlueprintListParams,
SDKBlueprintCreateParams,
SDKDiskSnapshotListParams,
SDKDevboxExtraCreateParams,
)
from .devbox import Devbox
from .._types import Timeout, NotGiven, not_given
from .._client import DEFAULT_MAX_RETRIES, Runloop
from ._helpers import detect_content_type
from .snapshot import Snapshot
from .blueprint import Blueprint
from .storage_object import StorageObject
from ..types.object_create_params import ContentType
class DevboxOps:
"""High-level manager for creating and managing Devbox instances.
Accessed via ``runloop.devbox`` from :class:`RunloopSDK`, provides methods to
create devboxes from scratch, blueprints, or snapshots, and to list
existing devboxes.
Example:
>>> runloop = RunloopSDK()
>>> devbox = runloop.devbox.create(name="my-devbox")
>>> devboxes = runloop.devbox.list(limit=10)
"""
def __init__(self, client: Runloop) -> None:
"""Initialize the manager.
Args:
client: Generated Runloop client to wrap.
"""
self._client = client
def create(
self,
**params: Unpack[SDKDevboxCreateParams],
) -> Devbox:
"""Provision a new devbox and wait until it reaches ``running`` state.
Args:
**params: Keyword arguments forwarded to the devbox creation API.
Returns:
Devbox: Wrapper bound to the newly created devbox.
"""
devbox_view = self._client.devboxes.create_and_await_running(
**params,
)
return Devbox(self._client, devbox_view.id)
def create_from_blueprint_id(
self,
blueprint_id: str,
**params: Unpack[SDKDevboxExtraCreateParams],
) -> Devbox:
"""Create a devbox from an existing blueprint by identifier.
Args:
blueprint_id: Blueprint ID to create from.
**params: Additional creation parameters (metadata, launch parameters, etc.).
Returns:
Devbox: Wrapper bound to the newly created devbox.
"""
devbox_view = self._client.devboxes.create_and_await_running(
blueprint_id=blueprint_id,
**params,
)
return Devbox(self._client, devbox_view.id)
def create_from_blueprint_name(
self,
blueprint_name: str,
**params: Unpack[SDKDevboxExtraCreateParams],
) -> Devbox:
"""Create a devbox from the latest blueprint with the given name.
Args:
blueprint_name: Blueprint name to create from.
**params: Additional creation parameters (metadata, launch parameters, etc.).
Returns:
Devbox: Wrapper bound to the newly created devbox.
"""
devbox_view = self._client.devboxes.create_and_await_running(
blueprint_name=blueprint_name,
**params,
)
return Devbox(self._client, devbox_view.id)
def create_from_snapshot(
self,
snapshot_id: str,
**params: Unpack[SDKDevboxExtraCreateParams],
) -> Devbox:
"""Create a devbox initialized from a snapshot.
Args:
snapshot_id: Snapshot ID to create from.
**params: Additional creation parameters (metadata, launch parameters, etc.).
Returns:
Devbox: Wrapper bound to the newly created devbox.
"""
devbox_view = self._client.devboxes.create_and_await_running(
snapshot_id=snapshot_id,
**params,
)
return Devbox(self._client, devbox_view.id)
def from_id(self, devbox_id: str) -> Devbox:
"""Attach to an existing devbox by ID.
Blocks until the devbox reaches ``running`` state so callers can begin
issuing commands immediately.
Args:
devbox_id: Existing devbox ID.
Returns:
Devbox: Wrapper bound to the requested devbox.
"""
self._client.devboxes.await_running(devbox_id)
return Devbox(self._client, devbox_id)
def list(
self,
**params: Unpack[SDKDevboxListParams],
) -> list[Devbox]:
"""List devboxes accessible to the caller.
Args:
**params: Filtering and pagination parameters.
Returns:
list[Devbox]: Collection of devbox wrappers.
"""
page = self._client.devboxes.list(
**params,
)
return [Devbox(self._client, item.id) for item in page.devboxes]
class SnapshotOps:
"""High-level manager for working with disk snapshots.
Accessed via ``runloop.snapshot`` from :class:`RunloopSDK`, provides methods
to list snapshots and access snapshot details.
Example:
>>> runloop = RunloopSDK()
>>> snapshots = runloop.snapshot.list(devbox_id="dev-123")
>>> snapshot = runloop.snapshot.from_id("snap-123")
"""
def __init__(self, client: Runloop) -> None:
"""Initialize the manager with the generated Runloop client."""
self._client = client
def list(
self,
**params: Unpack[SDKDiskSnapshotListParams],
) -> list[Snapshot]:
"""List snapshots created from devboxes.
Args:
**params: Filtering and pagination parameters.
Returns:
list[Snapshot]: Snapshot wrappers for each record.
"""
page = self._client.devboxes.disk_snapshots.list(
**params,
)
return [Snapshot(self._client, item.id) for item in page.snapshots]
def from_id(self, snapshot_id: str) -> Snapshot:
"""Return a snapshot wrapper for the given ID.
Args:
snapshot_id: Snapshot ID to wrap.
Returns:
Snapshot: Wrapper for the snapshot resource.
"""
return Snapshot(self._client, snapshot_id)
class BlueprintOps:
"""High-level manager for creating and managing blueprints.
Accessed via ``runloop.blueprint`` from :class:`RunloopSDK`, provides methods
to create blueprints with Dockerfiles and system setup commands, and to
list existing blueprints.
Example:
>>> runloop = RunloopSDK()
>>> blueprint = runloop.blueprint.create(
... name="my-blueprint", dockerfile="FROM ubuntu:22.04\\nRUN apt-get update"
... )
>>> blueprints = runloop.blueprint.list()
"""
def __init__(self, client: Runloop) -> None:
"""Initialize the manager.
Args:
client: Generated Runloop client to wrap.
"""
self._client = client
def create(
self,
**params: Unpack[SDKBlueprintCreateParams],
) -> Blueprint:
"""Create a blueprint and wait for the build to finish.
Args:
**params: Blueprint definition (Dockerfile, metadata, etc.).
Returns:
Blueprint: Wrapper bound to the finished blueprint.
"""
blueprint = self._client.blueprints.create_and_await_build_complete(
**params,
)
return Blueprint(self._client, blueprint.id)
def from_id(self, blueprint_id: str) -> Blueprint:
"""Return a blueprint wrapper for the given ID.
Args:
blueprint_id: Blueprint ID to wrap.
Returns:
Blueprint: Wrapper for the blueprint resource.
"""
return Blueprint(self._client, blueprint_id)
def list(
self,
**params: Unpack[SDKBlueprintListParams],
) -> list[Blueprint]:
"""List available blueprints.
Args:
**params: Filtering and pagination parameters.
Returns:
list[Blueprint]: Blueprint wrappers for each record.
"""
page = self._client.blueprints.list(
**params,
)
return [Blueprint(self._client, item.id) for item in page.blueprints]
class StorageObjectOps:
"""High-level manager for creating and managing storage objects.
Accessed via ``runloop.storage_object`` from :class:`RunloopSDK`, provides
methods to create, upload, download, and list storage objects with convenient
helpers for file and text uploads.
Example:
>>> runloop = RunloopSDK()
>>> obj = runloop.storage_object.upload_from_text("Hello!", "greeting.txt")
>>> content = obj.download_as_text()
>>> objects = runloop.storage_object.list()
"""
def __init__(self, client: Runloop) -> None:
"""Initialize the manager with the generated Runloop client."""
self._client = client
def create(
self,
**params: Unpack[SDKObjectCreateParams],
) -> StorageObject:
"""Create a storage object and obtain an upload URL.
Args:
**params: Object creation parameters (name, content type, metadata).
Returns:
StorageObject: Wrapper with upload URL set for immediate uploads.
"""
obj = self._client.objects.create(**params)
return StorageObject(self._client, obj.id, upload_url=obj.upload_url)
def from_id(self, object_id: str) -> StorageObject:
"""Return a storage object wrapper by identifier.
Args:
object_id: Storage object identifier to wrap.
Returns:
StorageObject: Wrapper for the storage object resource.
"""
return StorageObject(self._client, object_id, upload_url=None)
def list(
self,
**params: Unpack[SDKObjectListParams],
) -> list[StorageObject]:
"""List storage objects owned by the caller.
Args:
**params: Filtering and pagination parameters.
Returns:
list[StorageObject]: Storage object wrappers for each record.
"""
page = self._client.objects.list(
**params,
)
return [StorageObject(self._client, item.id, upload_url=item.upload_url) for item in page.objects]
def upload_from_file(
self,
file_path: str | Path,
name: str | None = None,
*,
content_type: ContentType | None = None,
metadata: Optional[Dict[str, str]] = None,
**options: Unpack[LongRequestOptions],
) -> StorageObject:
"""Create and upload an object from a local file path.
Args:
file_path: Local filesystem path to read.
name: Optional object name; defaults to the file name.
content_type: Optional MIME type to apply to the object.
metadata: Optional key-value metadata.
**options: Additional request configuration.
Returns:
StorageObject: Wrapper for the uploaded object.
Raises:
OSError: If the local file cannot be read.
"""
path = Path(file_path)
try:
content = path.read_bytes()
except OSError as error:
raise OSError(f"Failed to read file {path}: {error}") from error
name = name or path.name
content_type = content_type or detect_content_type(str(file_path))
obj = self.create(name=name, content_type=content_type, metadata=metadata, **options)
obj.upload_content(content)
obj.complete()
return obj
def upload_from_text(
self,
text: str,
name: str,
*,
metadata: Optional[Dict[str, str]] = None,
**options: Unpack[LongRequestOptions],
) -> StorageObject:
"""Create and upload an object from a text payload.
Args:
text: Text content to upload.
name: Object display name.
metadata: Optional key-value metadata.
**options: Additional request configuration.
Returns:
StorageObject: Wrapper for the uploaded object.
"""
obj = self.create(name=name, content_type="text", metadata=metadata, **options)
obj.upload_content(text)
obj.complete()
return obj
def upload_from_bytes(
self,
data: bytes,
name: str,
*,
content_type: ContentType,
metadata: Optional[Dict[str, str]] = None,
**options: Unpack[LongRequestOptions],
) -> StorageObject:
"""Create and upload an object from a bytes payload.
Args:
data: Binary payload to upload.
name: Object display name.
content_type: MIME type describing the payload.
metadata: Optional key-value metadata.
**options: Additional request configuration.
Returns:
StorageObject: Wrapper for the uploaded object.
"""
obj = self.create(name=name, content_type=content_type, metadata=metadata, **options)
obj.upload_content(data)
obj.complete()
return obj
class RunloopSDK:
"""High-level synchronous entry point for the Runloop SDK.
Provides a Pythonic, object-oriented interface for managing devboxes, blueprints,
snapshots, and storage objects. Exposes the generated REST client via the ``api``
attribute for advanced use cases.
Attributes:
api: Direct access to the generated REST API client.
devbox: High-level interface for devbox management.
blueprint: High-level interface for blueprint management.
snapshot: High-level interface for snapshot management.
storage_object: High-level interface for storage object management.
Example:
>>> runloop = RunloopSDK() # Uses RUNLOOP_API_KEY env var
>>> devbox = runloop.devbox.create(name="my-devbox")
>>> result = devbox.cmd.exec(command="echo 'hello'")
>>> print(result.stdout())
>>> devbox.shutdown()
"""
api: Runloop
devbox: DevboxOps
blueprint: BlueprintOps
snapshot: SnapshotOps
storage_object: StorageObjectOps
def __init__(
self,
*,
bearer_token: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = not_given,
max_retries: int = DEFAULT_MAX_RETRIES,
default_headers: Mapping[str, str] | None = None,
default_query: Mapping[str, object] | None = None,
http_client: httpx.Client | None = None,
) -> None:
"""Configure the synchronous SDK wrapper.
Args:
bearer_token: API token; falls back to ``RUNLOOP_API_KEY`` env var.
base_url: Override the API base URL.
timeout: Request timeout (seconds) or ``Timeout`` object.
max_retries: Maximum automatic retry attempts.
default_headers: Headers merged into every request.
default_query: Default query parameters merged into every request.
http_client: Custom ``httpx.Client`` instance to reuse.
"""
self.api = Runloop(
bearer_token=bearer_token,
base_url=base_url,
timeout=timeout,
max_retries=max_retries,
default_headers=default_headers,
default_query=default_query,
http_client=http_client,
)
self.devbox = DevboxOps(self.api)
self.blueprint = BlueprintOps(self.api)
self.snapshot = SnapshotOps(self.api)
self.storage_object = StorageObjectOps(self.api)
def close(self) -> None:
"""Close the underlying HTTP client and release resources."""
self.api.close()
def __enter__(self) -> "RunloopSDK":
"""Allow ``with RunloopSDK() as runloop`` usage.
Returns:
RunloopSDK: The active SDK instance.
"""
return self
def __exit__(self, *_exc_info: object) -> None:
"""Ensure the API client closes when leaving the context manager."""
self.close()