summaryrefslogtreecommitdiff
path: root/backend/plugin/binary_plugin.py
diff options
context:
space:
mode:
Diffstat (limited to 'backend/plugin/binary_plugin.py')
-rw-r--r--backend/plugin/binary_plugin.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/backend/plugin/binary_plugin.py b/backend/plugin/binary_plugin.py
new file mode 100644
index 00000000..4bd4f39b
--- /dev/null
+++ b/backend/plugin/binary_plugin.py
@@ -0,0 +1,48 @@
+import os
+from asyncio import get_event_loop, sleep, subprocess
+from posixpath import join
+from tempfile import mkdtemp
+
+from plugin_protocol import PluginProtocolServer
+
+
+class BinaryPlugin:
+ def __init__(self, plugin_directory, file_name, flags, logger) -> None:
+ self.server = PluginProtocolServer(self)
+ self.connection = None
+ self.flags = flags
+ self.logger = logger
+
+ self.plugin_directory = plugin_directory
+ self.file_name = file_name
+
+
+ async def start(self):
+ if self.connection:
+ self.connection.close()
+
+ self.unix_socket_path = BinaryPlugin.generate_socket_path()
+ self.logger.debug(f"starting unix server on {self.unix_socket_path}")
+ self.connection = await get_event_loop().create_unix_server(lambda: self.server, path=self.unix_socket_path)
+
+ env = dict(DECKY_PLUGIN_SOCKET = self.unix_socket_path)
+ self.process = await subprocess.create_subprocess_exec(join(self.plugin_directory, self.file_name), env=env)
+ get_event_loop().create_task(self.process_loop())
+
+ async def process_loop(self):
+ await self.process.wait()
+ self.logger.info("backend process was killed - restarting in 10 seconds")
+ await sleep(10)
+ await self.start()
+
+ def generate_socket_path():
+ tmp_dir = mkdtemp("decky-plugin")
+ os.chown(tmp_dir, 1000, 1000)
+ return join(tmp_dir, "socket")
+
+ # called on the server/loader process
+ async def call_method(self, method_name, method_args):
+ if self.process.returncode == None:
+ return dict(success = False, result = "Process not alive")
+
+ return await self.server.call_method(method_name, method_args)