summaryrefslogtreecommitdiff
path: root/backend/decky_loader/plugin/binary_plugin.py
blob: d1b8cea9ee1e2554737aa41750c5e8399f646dd6 (plain)
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
from asyncio import StreamReader, create_task, sleep, create_subprocess_exec
from asyncio.subprocess import Process
from subprocess import PIPE

from .sandboxed_plugin import SandboxedPlugin
from ..localplatform.localsocket import LocalSocket
from ..customtypes import UserType

from typing import Dict, List

class BinaryPlugin(SandboxedPlugin):
    def __init__(self,
                 socket: LocalSocket,
                 name: str,
                 flags: List[str],
                 file: str,
                 plugin_directory: str,
                 plugin_path: str,
                 version: str | None,
                 author: str,
                 env: Dict[str, str]) -> None:
        super().__init__(socket, name, flags, file, plugin_directory, plugin_path, version, author, env)
        self.process: Process

    def start(self):
        create_task(self._start())

    async def stop(self):
        self.process.terminate()
        while not self.process.returncode:
            await sleep(0)

    async def _start(self):
        self.env["DECKY_SOCKET"] = self.socket.socket_addr
        user_type = UserType.ROOT.value if "root" in self.flags else UserType.HOST_USER.value
        self.process = await create_subprocess_exec(self.file,
                             env=self.env,
                             user=user_type,
                             group=user_type,
                             stdout=PIPE,
                             stderr=PIPE)
        assert self.process.stderr and self.process.stdout
        create_task(self._stream_watcher(self.process.stdout, False))
        create_task(self._stream_watcher(self.process.stderr, True))
        
    async def _stream_watcher(self, stream: StreamReader, is_err: bool):
        async for line in stream:
            line = line.decode("utf-8")
            if not line.strip():
                continue
            if is_err:
                self.log.error(line)
            else:
                self.log.info(line)