microsandbox-core 0.8.2__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.
- microsandbox_core/__init__.py +549 -0
- microsandbox_core/_native.py +250 -0
- microsandbox_core/_native.pyi +1858 -0
- microsandbox_core/_schema.py +3 -0
- microsandbox_core/agent.py +127 -0
- microsandbox_core/contexts.py +132 -0
- microsandbox_core/errors.py +374 -0
- microsandbox_core/events.py +6 -0
- microsandbox_core/inspection.py +77 -0
- microsandbox_core/jobs.py +123 -0
- microsandbox_core/py.typed +0 -0
- microsandbox_core/setup.py +79 -0
- microsandbox_core/storage_diagnosis.py +93 -0
- microsandbox_core/types.py +2145 -0
- microsandbox_core-0.8.2.dist-info/METADATA +5 -0
- microsandbox_core-0.8.2.dist-info/RECORD +17 -0
- microsandbox_core-0.8.2.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"""Shared wrappers retaining the native library that owns each object.
|
|
2
|
+
|
|
3
|
+
Package staging supplies the declaration schema from the existing binding stub.
|
|
4
|
+
No backend implementation lives here: calls, cancellation and finalization stay
|
|
5
|
+
with the selected native object, and results cross back through one wrapper family.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import importlib
|
|
9
|
+
import inspect
|
|
10
|
+
import json
|
|
11
|
+
import threading
|
|
12
|
+
|
|
13
|
+
from ._schema import CLASSES, FUNCTIONS, VERSION
|
|
14
|
+
|
|
15
|
+
_loaders = {}
|
|
16
|
+
_loaded = {}
|
|
17
|
+
_owners = {}
|
|
18
|
+
_wrappers = {}
|
|
19
|
+
_full_loader = None
|
|
20
|
+
_full = None
|
|
21
|
+
_default_local = None
|
|
22
|
+
_lock = threading.RLock()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def register_provider(kind, module_name, version):
|
|
26
|
+
if kind not in ("local", "remote"):
|
|
27
|
+
raise ValueError("provider must be local or remote")
|
|
28
|
+
with _lock:
|
|
29
|
+
if kind in _loaders:
|
|
30
|
+
raise RuntimeError(f"A {kind} microsandbox provider is already registered")
|
|
31
|
+
_loaders[kind] = (module_name, version)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def register_full_provider(module_name, version):
|
|
35
|
+
global _full_loader
|
|
36
|
+
with _lock:
|
|
37
|
+
if _full_loader is not None:
|
|
38
|
+
raise RuntimeError("A full microsandbox provider is already registered")
|
|
39
|
+
_full_loader = (module_name, version)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _load(selection, kind):
|
|
43
|
+
module_name, version = selection
|
|
44
|
+
native = importlib.import_module(module_name)
|
|
45
|
+
info = json.loads(native.provider_info())
|
|
46
|
+
supported = info.get("local") and info.get("remote") if kind == "full" else info.get(kind)
|
|
47
|
+
if (
|
|
48
|
+
info.get("abi") != 1
|
|
49
|
+
or info.get("version") != version
|
|
50
|
+
or version != VERSION
|
|
51
|
+
or not supported
|
|
52
|
+
):
|
|
53
|
+
raise ImportError(
|
|
54
|
+
f"Incompatible microsandbox {kind} provider; "
|
|
55
|
+
"reinstall matching core and provider versions"
|
|
56
|
+
)
|
|
57
|
+
for name, wrapper in _wrappers.items():
|
|
58
|
+
value = getattr(native, name, None)
|
|
59
|
+
if isinstance(value, type):
|
|
60
|
+
_owners[value] = (wrapper, native)
|
|
61
|
+
return native
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def binding(kind):
|
|
65
|
+
global _full
|
|
66
|
+
with _lock:
|
|
67
|
+
if kind in _loaded:
|
|
68
|
+
return _loaded[kind]
|
|
69
|
+
if kind in _loaders:
|
|
70
|
+
native = _load(_loaders[kind], kind)
|
|
71
|
+
_loaded[kind] = native
|
|
72
|
+
return native
|
|
73
|
+
if _full is None and _full_loader is not None:
|
|
74
|
+
_full = _load(_full_loader, "full")
|
|
75
|
+
if _full is not None:
|
|
76
|
+
return _full
|
|
77
|
+
raise ImportError(f"Install and import microsandbox-{kind} before using {kind} operations")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def default_binding():
|
|
81
|
+
global _default_local
|
|
82
|
+
with _lock:
|
|
83
|
+
kind = _mount_binding().configured_provider_kind()
|
|
84
|
+
if kind == "remote":
|
|
85
|
+
return binding(kind)
|
|
86
|
+
if _default_local is None:
|
|
87
|
+
_default_local = binding("local")
|
|
88
|
+
return _default_local
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _mount_binding():
|
|
92
|
+
# Mount factories construct shared MountConfig values without opening a resource.
|
|
93
|
+
with _lock:
|
|
94
|
+
if "remote" in _loaders and "local" not in _loaders and _full_loader is None:
|
|
95
|
+
return binding("remote")
|
|
96
|
+
return binding("local")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _unwrap(value, owner):
|
|
100
|
+
if isinstance(value, NativeObject):
|
|
101
|
+
if value._provider is not owner:
|
|
102
|
+
raise TypeError(
|
|
103
|
+
"Native objects from different microsandbox providers cannot be passed "
|
|
104
|
+
"to each other; use configuration values or resource references"
|
|
105
|
+
)
|
|
106
|
+
return value._inner
|
|
107
|
+
if isinstance(value, list):
|
|
108
|
+
return [_unwrap(item, owner) for item in value]
|
|
109
|
+
if isinstance(value, tuple):
|
|
110
|
+
return tuple(_unwrap(item, owner) for item in value)
|
|
111
|
+
if isinstance(value, dict):
|
|
112
|
+
return {key: _unwrap(item, owner) for key, item in value.items()}
|
|
113
|
+
return value
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _wrap(value, owner, receiver=None):
|
|
117
|
+
if receiver is not None and value is receiver._inner:
|
|
118
|
+
return receiver
|
|
119
|
+
registered = _owners.get(type(value))
|
|
120
|
+
if registered is not None:
|
|
121
|
+
wrapper, native = registered
|
|
122
|
+
result = object.__new__(wrapper)
|
|
123
|
+
object.__setattr__(result, "_inner", value)
|
|
124
|
+
object.__setattr__(result, "_provider", native)
|
|
125
|
+
return result
|
|
126
|
+
if inspect.isawaitable(value):
|
|
127
|
+
|
|
128
|
+
async def resolve():
|
|
129
|
+
# Cancellation propagates to the original future. The wrapper never
|
|
130
|
+
# turns a cancelled observation into a terminal execution outcome.
|
|
131
|
+
return _wrap(await value, owner, receiver)
|
|
132
|
+
|
|
133
|
+
return resolve()
|
|
134
|
+
if isinstance(value, list):
|
|
135
|
+
return [_wrap(item, owner) for item in value]
|
|
136
|
+
if isinstance(value, tuple):
|
|
137
|
+
return tuple(_wrap(item, owner) for item in value)
|
|
138
|
+
if isinstance(value, dict):
|
|
139
|
+
return {key: _wrap(item, owner) for key, item in value.items()}
|
|
140
|
+
return value
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _call(function, owner, args, kwargs, receiver=None):
|
|
144
|
+
return _wrap(function(*_unwrap(args, owner), **_unwrap(kwargs, owner)), owner, receiver)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class NativeType(type):
|
|
148
|
+
def __getattr__(cls, name):
|
|
149
|
+
if name.startswith("_"):
|
|
150
|
+
raise AttributeError(name)
|
|
151
|
+
if name == "ctx" and cls._native_name in ("Sandbox", "Volume", "Snapshot"):
|
|
152
|
+
collection = {"Sandbox": "sandboxes", "Volume": "volumes", "Snapshot": "snapshots"}[
|
|
153
|
+
cls._native_name
|
|
154
|
+
]
|
|
155
|
+
return lambda context: getattr(context, collection)
|
|
156
|
+
|
|
157
|
+
def invoke(*args, **kwargs):
|
|
158
|
+
if (
|
|
159
|
+
name in ("local", "remote")
|
|
160
|
+
and cls._native_name in ("Sandbox", "Volume", "Snapshot")
|
|
161
|
+
):
|
|
162
|
+
owner = binding(name)
|
|
163
|
+
elif cls._native_name == "Volume" and name in (
|
|
164
|
+
"bind", "named", "owned", "tmpfs", "disk",
|
|
165
|
+
):
|
|
166
|
+
owner = _mount_binding()
|
|
167
|
+
else:
|
|
168
|
+
owner = default_binding()
|
|
169
|
+
function = getattr(getattr(owner, cls._native_name), name)
|
|
170
|
+
return _call(function, owner, args, kwargs)
|
|
171
|
+
|
|
172
|
+
invoke.__name__ = name
|
|
173
|
+
return invoke
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class NativeObject(metaclass=NativeType):
|
|
177
|
+
__slots__ = ("_inner", "_provider")
|
|
178
|
+
|
|
179
|
+
def __new__(cls, *args, **kwargs):
|
|
180
|
+
owner = None
|
|
181
|
+
if cls._native_name == "Context":
|
|
182
|
+
base = args[1] if len(args) > 1 else kwargs.get("base")
|
|
183
|
+
if base is not None:
|
|
184
|
+
if not isinstance(base, NativeObject):
|
|
185
|
+
raise TypeError("base must be a retained native context")
|
|
186
|
+
# Derive through its original provider even if registrations changed.
|
|
187
|
+
owner = base._provider
|
|
188
|
+
else:
|
|
189
|
+
owner = binding(json.loads(args[0])["kind"])
|
|
190
|
+
else:
|
|
191
|
+
owner = default_binding()
|
|
192
|
+
result = object.__new__(cls)
|
|
193
|
+
object.__setattr__(result, "_provider", owner)
|
|
194
|
+
object.__setattr__(
|
|
195
|
+
result,
|
|
196
|
+
"_inner",
|
|
197
|
+
getattr(owner, cls._native_name)(*_unwrap(args, owner), **_unwrap(kwargs, owner)),
|
|
198
|
+
)
|
|
199
|
+
return result
|
|
200
|
+
|
|
201
|
+
def __init__(self, *args, **kwargs):
|
|
202
|
+
# Construction is in __new__, matching the existing context subclasses.
|
|
203
|
+
pass
|
|
204
|
+
|
|
205
|
+
def __getattr__(self, name):
|
|
206
|
+
value = getattr(self._inner, name)
|
|
207
|
+
if callable(value):
|
|
208
|
+
|
|
209
|
+
def invoke(*args, **kwargs):
|
|
210
|
+
return _call(value, self._provider, args, kwargs, self)
|
|
211
|
+
|
|
212
|
+
invoke.__name__ = name
|
|
213
|
+
return invoke
|
|
214
|
+
return _wrap(value, self._provider, self)
|
|
215
|
+
|
|
216
|
+
def __setattr__(self, name, value):
|
|
217
|
+
setattr(self._inner, name, _unwrap(value, self._provider))
|
|
218
|
+
|
|
219
|
+
def __repr__(self):
|
|
220
|
+
return repr(self._inner)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _protocol(name):
|
|
224
|
+
def invoke(self, *args, **kwargs):
|
|
225
|
+
return _call(getattr(self._inner, name), self._provider, args, kwargs, self)
|
|
226
|
+
|
|
227
|
+
invoke.__name__ = name
|
|
228
|
+
return invoke
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
for _name, _protocols in CLASSES.items():
|
|
232
|
+
_attributes = {"_native_name": _name, "__module__": __package__, "__slots__": ()}
|
|
233
|
+
_attributes.update({name: _protocol(name) for name in _protocols})
|
|
234
|
+
_wrappers[_name] = NativeType(_name, (NativeObject,), _attributes)
|
|
235
|
+
globals()[_name] = _wrappers[_name]
|
|
236
|
+
BranchOutcome = _wrappers["ForkOutcome"]
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def __getattr__(name):
|
|
240
|
+
if name not in FUNCTIONS:
|
|
241
|
+
raise AttributeError(name)
|
|
242
|
+
if name == "version":
|
|
243
|
+
return lambda: VERSION
|
|
244
|
+
|
|
245
|
+
def invoke(*args, **kwargs):
|
|
246
|
+
owner = default_binding()
|
|
247
|
+
return _call(getattr(owner, name), owner, args, kwargs)
|
|
248
|
+
|
|
249
|
+
invoke.__name__ = name
|
|
250
|
+
return invoke
|