-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathsprites_kaos.py
More file actions
470 lines (399 loc) · 15.4 KB
/
Copy pathsprites_kaos.py
File metadata and controls
470 lines (399 loc) · 15.4 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
from __future__ import annotations
import asyncio
import json
import posixpath
import queue
import stat
import threading
from collections.abc import AsyncGenerator, Iterable
from pathlib import PurePosixPath
from typing import TYPE_CHECKING, Literal
from kaos import AsyncReadable, AsyncWritable, Kaos, KaosProcess, StatResult, StrOrKaosPath
from kaos.path import KaosPath
from sprites import Sprite
from sprites.exceptions import (
DirectoryNotEmptyError,
ExecError,
FileNotFoundError_,
FilesystemError,
IsADirectoryError_,
NotADirectoryError_,
PermissionError_,
TimeoutError,
)
from sprites.filesystem import SpriteFilesystem, SpritePath
from sprites.session import kill_session
if TYPE_CHECKING:
def type_check(sprites_kaos: SpritesKaos) -> None:
_: Kaos = sprites_kaos
class _SpritesCommandInput:
def __init__(self) -> None:
self._queue: queue.Queue[bytes | None] = queue.Queue()
self._buffer = bytearray()
self._closed = False
self._eof_sent = False
self._lock = threading.Lock()
def write(self, data: bytes) -> None:
if not data:
return
with self._lock:
if self._closed:
return
self._queue.put(bytes(data))
def writelines(self, data: Iterable[bytes], /) -> None:
for chunk in data:
self.write(chunk)
def close(self) -> None:
with self._lock:
if self._eof_sent:
self._closed = True
return
self._closed = True
self._eof_sent = True
self._queue.put(None)
def write_eof(self) -> None:
self.close()
def is_closing(self) -> bool:
with self._lock:
return self._closed
def read(self, n: int = -1) -> bytes:
if n == 0:
return b""
while not self._buffer:
item = self._queue.get()
if item is None:
return b""
self._buffer.extend(item)
if n < 0:
payload = bytes(self._buffer)
self._buffer.clear()
return payload
payload = bytes(self._buffer[:n])
del self._buffer[:n]
return payload
class _SpritesStdin:
def __init__(self, source: _SpritesCommandInput) -> None:
self._source = source
def can_write_eof(self) -> bool:
return True
def close(self) -> None:
self._source.close()
async def drain(self) -> None:
return None
def is_closing(self) -> bool:
return self._source.is_closing()
async def wait_closed(self) -> None:
return None
def write(self, data: bytes) -> None:
self._source.write(data)
def writelines(self, data: Iterable[bytes], /) -> None:
self._source.writelines(data)
def write_eof(self) -> None:
self._source.write_eof()
class _StreamWriterProxy:
def __init__(self, loop: asyncio.AbstractEventLoop, reader: asyncio.StreamReader) -> None:
self._loop = loop
self._reader = reader
self._closed = False
self._lock = threading.Lock()
def write(self, data: bytes | bytearray) -> int:
payload = bytes(data)
if not payload:
return 0
with self._lock:
if self._closed:
return 0
self._loop.call_soon_threadsafe(self._reader.feed_data, payload)
return len(payload)
def flush(self) -> None:
return None
def close(self) -> None:
with self._lock:
if self._closed:
return
self._closed = True
self._loop.call_soon_threadsafe(self._reader.feed_eof)
class _SpritesProcess:
def __init__(self, sprite: Sprite, args: tuple[str, ...], cwd: str) -> None:
self._sprite = sprite
self._args = args
self._cwd = cwd
self._stdin_source = _SpritesCommandInput()
self._stdout_reader = asyncio.StreamReader()
self._stderr_reader = asyncio.StreamReader()
loop = asyncio.get_running_loop()
self._stdout_proxy = _StreamWriterProxy(loop, self._stdout_reader)
self._stderr_proxy = _StreamWriterProxy(loop, self._stderr_reader)
self.stdin: AsyncWritable = _SpritesStdin(self._stdin_source)
self.stdout: AsyncReadable = self._stdout_reader
self.stderr: AsyncReadable = self._stderr_reader
self._returncode: int | None = None
self._exit_future: asyncio.Future[int] = loop.create_future()
self._session_id: str | None = None
self._session_id_ready = threading.Event()
self._runner_task = asyncio.create_task(self._run())
@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:
if self._returncode is not None:
return
self._stdin_source.write_eof()
await asyncio.to_thread(self._session_id_ready.wait, 1.0)
if self._session_id is None:
return
await asyncio.to_thread(kill_session, self._sprite, self._session_id)
def _on_text_message(self, message: bytes) -> None:
try:
payload: object = json.loads(message.decode("utf-8", errors="replace"))
except json.JSONDecodeError:
return
if not isinstance(payload, dict):
return
if payload.get("type") != "session_info":
return
session_id_obj: object | None = payload.get("id")
if isinstance(session_id_obj, str):
self._session_id = session_id_obj
self._session_id_ready.set()
return
data_obj: object | None = payload.get("data")
if isinstance(data_obj, dict):
nested_session_id: object | None = data_obj.get("id")
if isinstance(nested_session_id, str):
self._session_id = nested_session_id
self._session_id_ready.set()
def _run_sync(self) -> int:
command = self._sprite.command(*self._args, cwd=self._cwd)
command.stdin = self._stdin_source
command.stdout = self._stdout_proxy
command.stderr = self._stderr_proxy
command._text_message_handler = self._on_text_message
try:
command.run()
except ExecError:
exit_code = command.exit_code
return exit_code if exit_code >= 0 else 1
except TimeoutError as exc:
self._stderr_proxy.write(f"[sprites timeout] {exc}\n".encode("utf-8", "replace"))
return 124
except Exception as exc:
self._stderr_proxy.write(f"[sprites command error] {exc}\n".encode("utf-8", "replace"))
return 1
exit_code = command.exit_code
return exit_code if exit_code >= 0 else 1
async def _run(self) -> None:
exit_code = await asyncio.to_thread(self._run_sync)
self._returncode = exit_code
self._stdout_proxy.close()
self._stderr_proxy.close()
if not self._exit_future.done():
self._exit_future.set_result(exit_code)
class SpritesKaos:
"""
KAOS backend for an existing Sprites sprite.
Sprite lifecycle is managed externally; this class never creates or deletes sprites.
"""
name: str = "sprites"
def __init__(
self,
sprite: Sprite,
*,
home_dir: str = "/home/sprite",
cwd: str | None = None,
) -> None:
self._sprite = sprite
self._home_dir = posixpath.normpath(home_dir)
self._cwd = posixpath.normpath(cwd) if cwd is not None else self._home_dir
self._filesystem: SpriteFilesystem = sprite.filesystem("/")
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)
target = self._fs_path(abs_path)
try:
info = await asyncio.to_thread(target.stat)
except FilesystemError as exc:
self._raise_filesystem_error(exc, abs_path)
if not info.is_dir:
raise NotADirectoryError(f"{abs_path} is not a directory")
self._cwd = abs_path
async def stat(self, path: StrOrKaosPath, *, follow_symlinks: bool = True) -> StatResult:
if not follow_symlinks:
raise NotImplementedError("SpritesKaos.stat does not support follow_symlinks=False")
abs_path = self._abs_path(path)
target = self._fs_path(abs_path)
try:
info = await asyncio.to_thread(target.stat)
except FilesystemError as exc:
self._raise_filesystem_error(exc, abs_path)
mode = self._with_type_bits(info.mode, info.is_dir)
mtime = info.mod_time.timestamp()
return StatResult(
st_mode=mode,
st_ino=0,
st_dev=0,
st_nlink=1,
st_uid=0,
st_gid=0,
st_size=info.size,
st_atime=mtime,
st_mtime=mtime,
st_ctime=mtime,
)
async def iterdir(self, path: StrOrKaosPath) -> AsyncGenerator[KaosPath]:
abs_path = self._abs_path(path)
target = self._fs_path(abs_path)
try:
entries = await asyncio.to_thread(lambda: [str(entry) for entry in target.iterdir()])
except FilesystemError as exc:
self._raise_filesystem_error(exc, abs_path)
for entry in 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")
from fnmatch import fnmatchcase
abs_path = self._abs_path(path)
target = self._fs_path(abs_path)
try:
entries = await asyncio.to_thread(lambda: [str(entry) for entry in target.iterdir()])
except FilesystemError as exc:
self._raise_filesystem_error(exc, abs_path)
for entry in entries:
if fnmatchcase(posixpath.basename(entry), pattern):
yield KaosPath(entry)
async def readbytes(self, path: StrOrKaosPath, n: int | None = None) -> bytes:
abs_path = self._abs_path(path)
target = self._fs_path(abs_path)
try:
payload = await asyncio.to_thread(target.read_bytes)
except FilesystemError as exc:
self._raise_filesystem_error(exc, abs_path)
return payload if n is None else payload[: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)
target = self._fs_path(abs_path)
try:
await asyncio.to_thread(target.write_bytes, data)
except FilesystemError as exc:
self._raise_filesystem_error(exc, abs_path)
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:
abs_path = self._abs_path(path)
target = self._fs_path(abs_path)
payload = data.encode(encoding, errors=errors)
try:
if mode == "a":
existing = b""
try:
existing = await asyncio.to_thread(target.read_bytes)
except FileNotFoundError_:
existing = b""
await asyncio.to_thread(target.write_bytes, existing + payload)
else:
await asyncio.to_thread(target.write_bytes, payload)
except FilesystemError as exc:
self._raise_filesystem_error(exc, abs_path)
return len(data)
async def mkdir(
self, path: StrOrKaosPath, parents: bool = False, exist_ok: bool = False
) -> None:
abs_path = self._abs_path(path)
target = self._fs_path(abs_path)
try:
info = await asyncio.to_thread(target.stat)
if not info.is_dir:
raise FileExistsError(f"{abs_path} already exists and is not a directory")
if not exist_ok:
raise FileExistsError(f"{abs_path} already exists")
return
except FileNotFoundError_:
pass
except FilesystemError as exc:
self._raise_filesystem_error(exc, abs_path)
if not parents:
parent_path = posixpath.dirname(abs_path) or "/"
parent = self._fs_path(parent_path)
try:
parent_info = await asyncio.to_thread(parent.stat)
except FilesystemError as exc:
self._raise_filesystem_error(exc, parent_path)
if not parent_info.is_dir:
raise NotADirectoryError(f"{parent_path} is not a directory")
try:
await asyncio.to_thread(target.mkdir, parents=parents, exist_ok=exist_ok)
except FilesystemError as exc:
self._raise_filesystem_error(exc, abs_path)
async def exec(self, *args: str) -> KaosProcess:
if not args:
raise ValueError("At least one argument (the program to execute) is required.")
process = _SpritesProcess(self._sprite, args, self._cwd)
return process
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))
def _fs_path(self, abs_path: str) -> SpritePath:
return self._filesystem / abs_path
@staticmethod
def _raise_filesystem_error(exc: FilesystemError, path: str) -> None:
if isinstance(exc, FileNotFoundError_):
raise FileNotFoundError(path) from exc
if isinstance(exc, NotADirectoryError_):
raise NotADirectoryError(path) from exc
if isinstance(exc, IsADirectoryError_):
raise IsADirectoryError(path) from exc
if isinstance(exc, PermissionError_):
raise PermissionError(path) from exc
if isinstance(exc, DirectoryNotEmptyError):
raise OSError(f"{path} is not empty") from exc
raise RuntimeError(str(exc)) from exc
@staticmethod
def _with_type_bits(mode: str, is_dir: bool) -> int:
mode_value = int(mode, 8)
if stat.S_IFMT(mode_value) != 0:
return mode_value
type_mode = stat.S_IFDIR if is_dir else stat.S_IFREG
return mode_value | type_mode