"""Install Libraries — make the Python XFrame package globally available so generated scripts run without PYTHONPATH. Tools -> Install Libraries (mirrors the C 6568b6f + 5f12f04 and Rust 1eabec0 flow: build as the user, then install as root and report the result in a scrollable log): * the ``lightgui`` package is copied to the system site-packages (``sysconfig`` purelib — on this server /usr/local/lib/python3.14/dist-packages), * a ``lightgui`` launcher is installed to /usr/local/bin (a shell script that runs ``python3 -m lightgui``), * a ``lightgui.desktop`` entry is installed to /usr/local/share/applications so the app menu lists it. There is nothing to build first (pure Python, no shared libraries, so no ldconfig step — the C headers have no equivalent here). The privileged step runs via pkexec when present (graphical root password prompt), else via ``sudo -S`` with the password typed into a dialog (the 5f12f04 fix: the password entry's activate signal submits on Enter; the password is piped to sudo's stdin and wiped immediately after, never written to disk or passed on a command line). Honest limit (same as the C/Rust fix): pkexec is NOT installed on the server, so the real root-password prompt cannot be completed live — only the prompt/piping/flow can be verified. Unit tests cover the dry-run path only: command construction, file list and target paths. No test performs a real privileged install. The logic (paths, script text, command construction) is GTK-free and unit-testable; the dialog at the bottom is the GTK half. """ import os import shutil import subprocess import sysconfig import tempfile import threading LAUNCHER_NAME = "lightgui" DESKTOP_FILENAME = "lightgui.desktop" LAUNCHER_PATH = "/usr/local/bin/lightgui" DESKTOP_PATH = "/usr/local/share/applications/lightgui.desktop" def repo_root(): """Directory holding the ``lightgui`` package.""" return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def site_packages(): """System site-packages (purelib) for this interpreter.""" return sysconfig.get_paths()["purelib"] def package_source(root=None): """The ``lightgui`` package directory to install.""" return os.path.join(repo_root() if root is None else root, "lightgui") def package_target(site=None): """Where the package lands: /lightgui.""" return os.path.join(site_packages() if site is None else site, "lightgui") def launcher_text(): """Content of the /usr/local/bin/lightgui launcher.""" return ("#!/bin/sh\n" "# LightGUI launcher (installed by Tools > Install Libraries).\n" "# The lightgui package lives in the system site-packages,\n" "# so the designer runs from anywhere without PYTHONPATH.\n" "exec python3 -m lightgui \"$@\"\n") def desktop_text(): """Content of the lightgui.desktop entry.""" return ("[Desktop Entry]\n" "Type=Application\n" "Name=LightGUI\n" "Comment=Visual GUI designer (Python/XFrame)\n" "Exec=lightgui\n" "Terminal=false\n" "Categories=Development;GUIDesigner;\n") def install_files(root=None, site=None): """[(source, destination)] installed by this flow — the file list the dialog shows and the tests check. The package entry is a directory (copied recursively); the others are files.""" root = repo_root() if root is None else root return [(package_source(root), package_target(site)), ("", LAUNCHER_PATH), ("", DESKTOP_PATH)] def build_install_script(root=None, site=None): """Shell script (run as root) that performs the install. Pure function of its inputs — this is what the dry-run shows and the tests check. ``set -e`` so a failed copy aborts before the launcher is installed; __pycache__ is dropped so no stale bytecode ships.""" root = repo_root() if root is None else root src = package_source(root) dest = package_target(site) launcher = launcher_text().replace("'", "'\\''") desktop = desktop_text().replace("'", "'\\''") lines = [ "#!/bin/sh", "# LightGUI Install Libraries (generated; run as root).", "set -e", "echo 'Installing lightgui package to %s'" % dest, "mkdir -p '%s'" % os.path.dirname(dest), "rm -rf '%s'" % dest, "cp -r '%s' '%s'" % (src, dest), ("find '%s' -name __pycache__ -type d " "-exec rm -rf {} + 2>/dev/null || true" % dest), "echo 'Installing launcher to %s'" % LAUNCHER_PATH, "printf '%s' > '%s'" % (launcher, LAUNCHER_PATH), "chmod 755 '%s'" % LAUNCHER_PATH, "echo 'Installing desktop entry to %s'" % DESKTOP_PATH, "mkdir -p '%s'" % os.path.dirname(DESKTOP_PATH), "printf '%s' > '%s'" % (desktop, DESKTOP_PATH), "chmod 644 '%s'" % DESKTOP_PATH, ("command -v update-desktop-database >/dev/null 2>&1 && " "update-desktop-database /usr/local/share/applications " "|| true"), "echo 'LightGUI installed.'", "echo 'Run it with: lightgui'", ] return "\n".join(lines) + "\n" def privileged_runner(): """How the install is elevated: 'pkexec' when present, else 'sudo' when present, else None. (On the server pkexec is missing, so this returns 'sudo'.)""" if shutil.which("pkexec"): return "pkexec" if shutil.which("sudo"): return "sudo" return None def install_command(script_path, runner=None): """argv that runs *script_path* as root: ``pkexec sh