From 75aa1e4851445646994ba3a61ff41325403359fb Mon Sep 17 00:00:00 2001 From: Sims <38142618+suchmememanyskill@users.noreply.github.com> Date: Mon, 5 Aug 2024 00:00:49 +0200 Subject: [PATCH] Sims/pyinstaller misuse fix (#657) * Fix misuse of pyinstaller * Fix breaking change * Fix pywright import errors --- backend/decky_loader/helpers.py | 6 +++++- backend/decky_loader/main.py | 9 ++++----- .../decky_loader/plugin/sandboxed_plugin.py | 18 +++++++++--------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/backend/decky_loader/helpers.py b/backend/decky_loader/helpers.py index 05c4fc8e..2e0fe45f 100644 --- a/backend/decky_loader/helpers.py +++ b/backend/decky_loader/helpers.py @@ -93,7 +93,11 @@ def get_system_pythonpaths() -> list[str]: proc = subprocess.run(["python3" if localplatform.ON_LINUX else "python", "-c", "import sys; print('\\n'.join(x for x in sys.path if x))"], # TODO make this less insane capture_output=True, user=localplatform.localplatform._get_user_id() if localplatform.ON_LINUX else None, env={} if localplatform.ON_LINUX else None) # pyright: ignore [reportPrivateUsage] - return [x.strip() for x in proc.stdout.decode().strip().split("\n")] + + proc.check_returncode() + + versions = [x.strip() for x in proc.stdout.decode().strip().split("\n")] + return [x for x in versions if x and not x.isspace()] except Exception as e: logger.warn(f"Failed to execute get_system_pythonpaths(): {str(e)}") return [] diff --git a/backend/decky_loader/main.py b/backend/decky_loader/main.py index f31fd32a..973919f1 100644 --- a/backend/decky_loader/main.py +++ b/backend/decky_loader/main.py @@ -7,14 +7,16 @@ get_privileged_path, restart_webhelper) if hasattr(sys, '_MEIPASS'): chmod(sys._MEIPASS, 755) # type: ignore + # Full imports +import multiprocessing +multiprocessing.freeze_support() from asyncio import AbstractEventLoop, CancelledError, Task, all_tasks, current_task, gather, new_event_loop, set_event_loop, sleep from logging import basicConfig, getLogger from os import path from traceback import format_exc -import multiprocessing - import aiohttp_cors # pyright: ignore [reportMissingTypeStubs] + # Partial imports from aiohttp import client_exceptions from aiohttp.web import Application, Response, Request, get, run_app, static # pyright: ignore [reportUnknownVariableType] @@ -223,9 +225,6 @@ def main(): # Fix windows/flask not recognising that .js means 'application/javascript' import mimetypes mimetypes.add_type('application/javascript', '.js') - - # Required for multiprocessing support in frozen files - multiprocessing.freeze_support() else: if get_effective_user_id() != 0: logger.warning(f"decky is running as an unprivileged user, this is not officially supported and may cause issues") diff --git a/backend/decky_loader/plugin/sandboxed_plugin.py b/backend/decky_loader/plugin/sandboxed_plugin.py index 32330a9a..93691a44 100644 --- a/backend/decky_loader/plugin/sandboxed_plugin.py +++ b/backend/decky_loader/plugin/sandboxed_plugin.py @@ -1,9 +1,9 @@ +import sys from os import path, environ from signal import SIG_IGN, SIGINT, SIGTERM, getsignal, signal from importlib.util import module_from_spec, spec_from_file_location from json import dumps, loads from logging import getLogger -from sys import exit, path as syspath, modules as sysmodules from traceback import format_exc from asyncio import (get_event_loop, new_event_loop, set_event_loop) @@ -13,7 +13,7 @@ from ..localplatform.localsocket import LocalSocket from ..localplatform.localplatform import setgid, setuid, get_username, get_home_path from ..enums import UserType -from .. import helpers +from .. import helpers, settings, injector # pyright: ignore [reportUnusedImport] from typing import List, TypeVar, Any @@ -78,12 +78,12 @@ def initialize(self, socket: LocalSocket): environ["DECKY_PLUGIN_AUTHOR"] = self.author # append the plugin's `py_modules` to the recognized python paths - syspath.append(path.join(environ["DECKY_PLUGIN_DIR"], "py_modules")) + sys.path.append(path.join(environ["DECKY_PLUGIN_DIR"], "py_modules")) #TODO: FIX IN A LESS CURSED WAY - keys = [key for key in sysmodules if key.startswith("decky_loader.")] + keys = [key for key in sys.modules if key.startswith("decky_loader.")] for key in keys: - sysmodules[key.replace("decky_loader.", "")] = sysmodules[key] + sys.modules[key.replace("decky_loader.", "")] = sys.modules[key] from .imports import decky async def emit(event: str, *args: Any) -> None: @@ -95,9 +95,9 @@ async def emit(event: str, *args: Any) -> None: # copy the docstring over so we don't have to duplicate it emit.__doc__ = decky.emit.__doc__ decky.emit = emit - sysmodules["decky"] = decky + sys.modules["decky"] = decky # provided for compatibility - sysmodules["decky_plugin"] = decky + sys.modules["decky_plugin"] = decky spec = spec_from_file_location("_", self.file) assert spec is not None @@ -123,7 +123,7 @@ async def emit(event: str, *args: Any) -> None: get_event_loop().create_task(socket.setup_server()) except: self.log.error("Failed to start " + self.name + "!\n" + format_exc()) - exit(0) + sys.exit(0) try: get_event_loop().run_forever() except SystemExit: @@ -180,7 +180,7 @@ async def on_new_message(self, message : str) -> str|None: loop = get_event_loop() loop.call_soon_threadsafe(loop.stop) - exit(0) + sys.exit(0) d: SocketResponseDict = {"type": SocketMessageType.RESPONSE, "res": None, "success": True, "id": data["id"]} try: