Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sims/pyinstaller misuse fix #657

Merged
merged 3 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/decky_loader/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,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 []
Expand Down
9 changes: 4 additions & 5 deletions backend/decky_loader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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")
Expand Down
18 changes: 9 additions & 9 deletions backend/decky_loader/plugin/sandboxed_plugin.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Loading