-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathboxlite_kaos.py
More file actions
465 lines (409 loc) · 15 KB
/
Copy pathboxlite_kaos.py
File metadata and controls
465 lines (409 loc) · 15 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
from __future__ import annotations
import asyncio
import base64
import json
import posixpath
import shlex
from collections.abc import AsyncGenerator, Iterable
from pathlib import PurePosixPath
from typing import TYPE_CHECKING, Literal
import boxlite
from kaos import AsyncReadable, AsyncWritable, Kaos, KaosProcess, StatResult, StrOrKaosPath
from kaos.path import KaosPath
if TYPE_CHECKING:
def type_check(boxlite_kaos: BoxliteKaos) -> None:
_: Kaos = boxlite_kaos
class _BoxliteStdin:
def __init__(self, stdin: boxlite.ExecStdin) -> None:
self._stdin = stdin
def can_write_eof(self) -> bool:
return True
def close(self) -> None:
self._stdin.close()
async def drain(self) -> None:
return None
def is_closing(self) -> bool:
return self._stdin.is_closed()
async def wait_closed(self) -> None:
return None
def write(self, data: bytes) -> None:
if self._stdin.is_closed():
return
payload = data if isinstance(data, (bytes, bytearray)) else bytes(data)
self._stdin.send_input(payload)
def writelines(self, data: Iterable[bytes], /) -> None:
for chunk in data:
self.write(chunk)
def write_eof(self) -> None:
self.close()
class _BoxliteProcess:
def __init__(self, execution: boxlite.Execution) -> None:
self._execution = execution
self._stdout = asyncio.StreamReader()
self._stderr = asyncio.StreamReader()
self._stdin = _BoxliteStdin(execution.stdin())
self.stdin: AsyncWritable = self._stdin
self.stdout: AsyncReadable = self._stdout
self.stderr: AsyncReadable = self._stderr
self._returncode: int | None = None
self._exit_future: asyncio.Future[int] = asyncio.get_running_loop().create_future()
self._stdout_task = asyncio.create_task(self._pipe_stream(execution.stdout(), self._stdout))
self._stderr_task = asyncio.create_task(self._pipe_stream(execution.stderr(), self._stderr))
self._monitor_task = asyncio.create_task(self._monitor())
@property
def pid(self) -> int:
return -1
@property
def returncode(self) -> int | None:
return self._returncode
async def wait(self) -> int:
return await self._exit_future
async def kill(self) -> None:
await self._execution.kill()
async def _pipe_stream(
self, stream: AsyncGenerator[bytes], reader: asyncio.StreamReader
) -> None:
try:
async for chunk in stream:
if isinstance(chunk, (bytes, bytearray)):
reader.feed_data(bytes(chunk))
else:
reader.feed_data(str(chunk).encode("utf-8", "replace"))
except Exception as exc:
reader.feed_data(f"[boxlite stream error] {exc}\n".encode("utf-8", "replace"))
finally:
reader.feed_eof()
async def _monitor(self) -> None:
exit_code = 1
try:
result = await self._execution.wait()
exit_code = result.exit_code
except Exception as exc:
self._stderr.feed_data(f"[boxlite command error] {exc}\n".encode("utf-8", "replace"))
finally:
self._returncode = exit_code
await asyncio.gather(self._stdout_task, self._stderr_task, return_exceptions=True)
if not self._exit_future.done():
self._exit_future.set_result(exit_code)
class BoxliteKaos:
"""
KAOS backend for an existing BoxLite box.
Box lifecycle is managed externally; this class never creates or stops boxes.
"""
name: str = "boxlite"
def __init__(
self,
box: boxlite.Box,
*,
home_dir: str = "/root",
cwd: str | None = None,
) -> None:
self._box = box
self._home_dir = posixpath.normpath(home_dir)
self._cwd = posixpath.normpath(cwd) if cwd is not None else self._home_dir
def pathclass(self) -> type[PurePosixPath]:
return PurePosixPath
def normpath(self, path: StrOrKaosPath) -> KaosPath:
return KaosPath(posixpath.normpath(str(path)))
def gethome(self) -> KaosPath:
return KaosPath(self._home_dir)
def getcwd(self) -> KaosPath:
return KaosPath(self._cwd)
async def chdir(self, path: StrOrKaosPath) -> None:
abs_path = self._abs_path(path)
payload = await self._python_json(
"""
import json
import os
import sys
path = sys.argv[1]
if not os.path.exists(path):
print(json.dumps({"ok": False, "error": "not_found"}))
elif not os.path.isdir(path):
print(json.dumps({"ok": False, "error": "not_dir"}))
else:
print(json.dumps({"ok": True}))
""",
[abs_path],
)
if not payload.get("ok"):
self._raise_path_error(payload, abs_path)
self._cwd = abs_path
async def stat(self, path: StrOrKaosPath, *, follow_symlinks: bool = True) -> StatResult:
if not follow_symlinks:
raise NotImplementedError("BoxliteKaos.stat does not support follow_symlinks=False")
abs_path = self._abs_path(path)
payload = await self._python_json(
"""
import json
import os
import sys
path = sys.argv[1]
try:
st = os.stat(path)
data = {
"st_mode": st.st_mode,
"st_ino": st.st_ino,
"st_dev": st.st_dev,
"st_nlink": st.st_nlink,
"st_uid": st.st_uid,
"st_gid": st.st_gid,
"st_size": st.st_size,
"st_atime": st.st_atime,
"st_mtime": st.st_mtime,
"st_ctime": st.st_ctime,
}
print(json.dumps({"ok": True, "data": data}))
except FileNotFoundError:
print(json.dumps({"ok": False, "error": "not_found"}))
""",
[abs_path],
)
if not payload.get("ok"):
self._raise_path_error(payload, abs_path)
data = payload["data"]
return StatResult(
st_mode=int(data["st_mode"]),
st_ino=int(data["st_ino"]),
st_dev=int(data["st_dev"]),
st_nlink=int(data["st_nlink"]),
st_uid=int(data["st_uid"]),
st_gid=int(data["st_gid"]),
st_size=int(data["st_size"]),
st_atime=float(data["st_atime"]),
st_mtime=float(data["st_mtime"]),
st_ctime=float(data["st_ctime"]),
)
async def iterdir(self, path: StrOrKaosPath) -> AsyncGenerator[KaosPath]:
abs_path = self._abs_path(path)
payload = await self._python_json(
"""
import json
import os
import sys
path = sys.argv[1]
if not os.path.exists(path):
print(json.dumps({"ok": False, "error": "not_found"}))
elif not os.path.isdir(path):
print(json.dumps({"ok": False, "error": "not_dir"}))
else:
entries = [os.path.join(path, entry) for entry in os.listdir(path)]
print(json.dumps({"ok": True, "entries": entries}))
""",
[abs_path],
)
if not payload.get("ok"):
self._raise_path_error(payload, abs_path)
for entry in payload["entries"]:
yield KaosPath(entry)
async def glob(
self, path: StrOrKaosPath, pattern: str, *, case_sensitive: bool = True
) -> AsyncGenerator[KaosPath]:
if not case_sensitive:
raise ValueError("Case insensitive glob is not supported in current environment")
abs_path = self._abs_path(path)
payload = await self._python_json(
"""
import json
import os
import sys
from fnmatch import fnmatchcase
path = sys.argv[1]
pattern = sys.argv[2]
if not os.path.exists(path):
print(json.dumps({"ok": False, "error": "not_found"}))
elif not os.path.isdir(path):
print(json.dumps({"ok": False, "error": "not_dir"}))
else:
entries = []
for entry in os.listdir(path):
if fnmatchcase(entry, pattern):
entries.append(os.path.join(path, entry))
print(json.dumps({"ok": True, "entries": entries}))
""",
[abs_path, pattern],
)
if not payload.get("ok"):
self._raise_path_error(payload, abs_path)
for entry in payload["entries"]:
yield KaosPath(entry)
async def readbytes(self, path: StrOrKaosPath, n: int | None = None) -> bytes:
abs_path = self._abs_path(path)
payload = await self._python_json(
"""
import base64
import json
import sys
path = sys.argv[1]
try:
with open(path, "rb") as f:
data = f.read()
print(json.dumps({"ok": True, "data": base64.b64encode(data).decode("ascii")}))
except FileNotFoundError:
print(json.dumps({"ok": False, "error": "not_found"}))
""",
[abs_path],
)
if not payload.get("ok"):
self._raise_path_error(payload, abs_path)
raw = base64.b64decode(payload["data"])
return raw if n is None else raw[:n]
async def readtext(
self,
path: StrOrKaosPath,
*,
encoding: str = "utf-8",
errors: Literal["strict", "ignore", "replace"] = "strict",
) -> str:
payload = await self.readbytes(path)
return payload.decode(encoding, errors=errors)
async def readlines(
self,
path: StrOrKaosPath,
*,
encoding: str = "utf-8",
errors: Literal["strict", "ignore", "replace"] = "strict",
) -> AsyncGenerator[str]:
text = await self.readtext(path, encoding=encoding, errors=errors)
for line in text.splitlines(keepends=True):
yield line
async def writebytes(self, path: StrOrKaosPath, data: bytes) -> int:
abs_path = self._abs_path(path)
payload = base64.b64encode(data)
await self._python_write(abs_path, payload, append=False)
return len(data)
async def writetext(
self,
path: StrOrKaosPath,
data: str,
*,
mode: Literal["w", "a"] = "w",
encoding: str = "utf-8",
errors: Literal["strict", "ignore", "replace"] = "strict",
) -> int:
payload = data.encode(encoding, errors=errors)
await self._python_write(
self._abs_path(path),
base64.b64encode(payload),
append=(mode == "a"),
)
return len(data)
async def mkdir(
self, path: StrOrKaosPath, parents: bool = False, exist_ok: bool = False
) -> None:
abs_path = self._abs_path(path)
payload = await self._python_json(
"""
import json
import os
import sys
path = sys.argv[1]
parents = sys.argv[2] == "1"
exist_ok = sys.argv[3] == "1"
if os.path.exists(path):
if not exist_ok:
print(json.dumps({"ok": False, "error": "exists"}))
elif not os.path.isdir(path):
print(json.dumps({"ok": False, "error": "exists_not_dir"}))
else:
print(json.dumps({"ok": True}))
else:
if not parents:
parent = os.path.dirname(path) or "/"
if not os.path.exists(parent):
print(json.dumps({"ok": False, "error": "parent_missing"}))
elif not os.path.isdir(parent):
print(json.dumps({"ok": False, "error": "parent_not_dir"}))
else:
os.mkdir(path)
print(json.dumps({"ok": True}))
else:
os.makedirs(path, exist_ok=exist_ok)
print(json.dumps({"ok": True}))
""",
[abs_path, "1" if parents else "0", "1" if exist_ok else "0"],
)
if not payload.get("ok"):
error = payload.get("error")
if error in {"exists", "exists_not_dir"}:
raise FileExistsError(f"{abs_path} already exists")
if error == "parent_missing":
raise FileNotFoundError(
f"Parent directory {posixpath.dirname(abs_path)} does not exist"
)
if error == "parent_not_dir":
raise NotADirectoryError(
f"Parent path {posixpath.dirname(abs_path)} is not a directory"
)
raise RuntimeError(f"Failed to create directory {abs_path}")
async def exec(self, *args: str) -> KaosProcess:
if not args:
raise ValueError("At least one argument (the program to execute) is required.")
command = " ".join(shlex.quote(arg) for arg in args)
if self._cwd:
command = f"cd {shlex.quote(self._cwd)} && {command}"
execution = await self._box.exec("sh", ["-c", command])
return _BoxliteProcess(execution)
async def _exec_capture(
self, command: str, args: list[str], *, stdin: bytes | None = None
) -> tuple[bytes, bytes, int]:
execution = await self._box.exec(command, args)
if stdin is not None:
writer = execution.stdin()
writer.send_input(stdin)
writer.close()
stdout_task = asyncio.create_task(self._collect_stream(execution.stdout()))
stderr_task = asyncio.create_task(self._collect_stream(execution.stderr()))
result = await execution.wait()
stdout, stderr = await asyncio.gather(stdout_task, stderr_task)
return stdout, stderr, result.exit_code
async def _collect_stream(self, stream: AsyncGenerator[bytes]) -> bytes:
data = bytearray()
async for chunk in stream:
if isinstance(chunk, (bytes, bytearray)):
data.extend(chunk)
else:
data.extend(str(chunk).encode("utf-8", "replace"))
return bytes(data)
async def _python_json(self, code: str, args: list[str]) -> dict[str, object]:
stdout, stderr, exit_code = await self._exec_capture("python", ["-c", code, *args])
if exit_code != 0:
message = stderr.decode("utf-8", "replace").strip()
raise RuntimeError(message or "BoxliteKaos python helper failed")
try:
return json.loads(stdout.decode("utf-8"))
except json.JSONDecodeError as exc:
raise RuntimeError(f"Invalid JSON response: {stdout!r}") from exc
async def _python_write(self, path: str, payload: bytes, *, append: bool) -> None:
code = """
import base64
import sys
path = sys.argv[1]
mode = "ab" if sys.argv[2] == "1" else "wb"
data = base64.b64decode(sys.stdin.buffer.read())
with open(path, mode) as f:
f.write(data)
"""
_, stderr, exit_code = await self._exec_capture(
"python",
["-c", code, path, "1" if append else "0"],
stdin=payload,
)
if exit_code != 0:
message = stderr.decode("utf-8", "replace").strip()
raise RuntimeError(message or f"Failed to write {path}")
def _abs_path(self, path: StrOrKaosPath) -> str:
raw = str(path)
if posixpath.isabs(raw):
return posixpath.normpath(raw)
return posixpath.normpath(posixpath.join(self._cwd, raw))
@staticmethod
def _raise_path_error(payload: dict[str, object], path: str) -> None:
error = payload.get("error")
if error == "not_found":
raise FileNotFoundError(path)
if error == "not_dir":
raise NotADirectoryError(f"{path} is not a directory")
if error == "exists":
raise FileExistsError(f"{path} already exists")
raise RuntimeError(f"BoxliteKaos operation failed for {path}")