import sys import os import builtins import time import _thread import gc import socket import struct import warnings # === GLOBAL WARNING SUPPRESSION (very early) === try: from marshmallow.warnings import RemovedInMarshmallow4Warning warnings.filterwarnings("ignore", category=RemovedInMarshmallow4Warning) warnings.filterwarnings("ignore", message=".*default.*deprecated.*") warnings.filterwarnings("ignore", message=".*metadata.*deprecated.*") warnings.simplefilter("ignore", category=RemovedInMarshmallow4Warning) print("[Shim] Global warning suppression activated") except Exception: pass _active_server_instance = None # --- 1. Stream Redirector for Android UI --- class AndroidLogStream: def __init__(self, activity_context, original_stream): self.activity_context = activity_context self.original_stream = original_stream self.buffer = "" def write(self, message): if self.original_stream: try: self.original_stream.write(message) except Exception: pass self.buffer += message while "\n" in self.buffer: line, self.buffer = self.buffer.split("\n", 1) clean_line = line.strip() if clean_line and self.activity_context: try: self.activity_context.onServerLog(clean_line) except Exception: pass def flush(self): if self.original_stream: try: self.original_stream.flush() except Exception: pass if self.buffer.strip() and self.activity_context: try: self.activity_context.onServerLog(self.buffer.strip()) self.buffer = "" except Exception: pass # --- 2. Headless Input Mock --- def _mock_input(prompt=""): if prompt: print(f"[CLI]: {prompt}") time.sleep(2) return "" builtins.input = _mock_input # --- 3. Minimal Marshmallow Info --- try: import marshmallow print(f"[Shim] marshmallow {marshmallow.__version__}") except Exception: print("[Shim] marshmallow not available") # --- 4. Shutdown Logic --- def kill_server_process(): global _active_server_instance print("[Shutdown]: Force-stopping server execution...") if _active_server_instance is not None: try: if hasattr(_active_server_instance, 'shutdown'): _active_server_instance.shutdown() if hasattr(_active_server_instance, 'server_close'): _active_server_instance.server_close() except Exception: pass try: sock = getattr(_active_server_instance, 'socket', None) if sock: sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) sock.close() except Exception: pass _active_server_instance = None try: for obj in gc.get_objects(): if isinstance(obj, socket.socket): try: obj.shutdown(socket.SHUT_RDWR) obj.close() except Exception: pass except Exception: pass try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.close() except Exception: pass time.sleep(1.2) for _ in range(5): try: _thread.interrupt_main() time.sleep(0.2) except Exception: pass # --- 5. Main Execution Entrypoint --- def run_server(ppsr_path, activity_context=None): if activity_context is not None: sys.stdout = AndroidLogStream(activity_context, sys.__stdout__) sys.stderr = AndroidLogStream(activity_context, sys.__stderr__) # Extra safety - re-apply on every start try: warnings.filterwarnings("ignore", category=RemovedInMarshmallow4Warning) except Exception: pass ppsr_dir = os.path.dirname(os.path.abspath(ppsr_path)) misc_dir = os.path.join(ppsr_dir, "misc") if os.path.exists(ppsr_dir): os.chdir(ppsr_dir) print(f"Working directory locked: {os.getcwd()}") for path in [ppsr_dir, misc_dir]: if os.path.exists(path) and path not in sys.path: sys.path.insert(0, path) # --- Werkzeug Patch --- try: import werkzeug.serving _orig_make_server = werkzeug.serving.make_server def _patched_make_server(*args, **kwargs): global _active_server_instance kwargs.pop('options', None) server = _orig_make_server(*args, **kwargs) try: if hasattr(server, 'socket') and server.socket: server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except Exception: pass _active_server_instance = server print("[Server] Server instance patched and ready") return server werkzeug.serving.make_server = _patched_make_server print("[Patch] Werkzeug make_server patched successfully") except Exception as e: print(f"[Patch Warning] Werkzeug patch: {e}") print(f"Loading bytecode target: {ppsr_path}...") with open(ppsr_path, "rb") as f: code_bytes = f.read() try: import marshal code_obj = marshal.loads(code_bytes[16:]) except Exception: code_obj = compile(code_bytes, ppsr_path, "exec") main_globals = { "__name__": "__main__", "__file__": ppsr_path, "__doc__": None, "sys": sys, "os": os, "input": _mock_input } try: exec(code_obj, main_globals) except KeyboardInterrupt: print("[System]: Server loop interrupted cleanly.") except Exception as e: print(f"[System]: Server terminated: {e}")