feat(packaging): pip-installable package with gui script, menu entry and icon
draft paket obsolete
feat(packaging): pip-installable package with gui script, menu entry and icon pyproject.toml lists the pip dependencies (onnxruntime of any flavour plus the WebGPU plugin as default, CPU fallback stays); PyGObject and pycairo are best taken from the system via --system-site-packages. The gui script `schach` replaces schach.sh and sets GSK_RENDERER=gl on Linux before GTK loads. `schach --install-desktop` writes app.schach.Schach.desktop with the full launcher path plus a new app icon (cburnett knight on wooden squares), which replaces the borrowed org.gnome.Chess icon. README explains pipx and venv setup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 1c7bda939dc75713b7941afad7e54a69c4e0253b |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 2023f76409ee |
| Children | |
| Pushlog | by timo via ssh |
modified .hgignore +5 −0
| @@ -1,3 +1,8 @@ | ||
| 1 | 1 | syntax: glob |
| 2 | 2 | __pycache__/ |
| 3 | 3 | *.pyc |
| 4 | +build/ | |
| 5 | +dist/ | |
| 6 | +*.egg-info/ | |
| 7 | +.venv/ | |
| 8 | +.pytest_cache/ | |
modified README.md +26 −12
| @@ -85,32 +85,46 @@ Schlichtes GNOME-Schachprogramm (Python, | ||
| 85 | 85 | plus eigene im Figurenordner (`~/.local/share/schach/figuren/<name>/wk.svg` … `bp.svg`) – etwa |
| 86 | 86 | Sätze, deren Lizenz nur private Nutzung erlaubt –, Lichess-Konto. |
| 87 | 87 | |
| 88 | -## Starten | |
| 88 | +## Installieren und starten | |
| 89 | 89 | |
| 90 | - ./schach.sh | |
| 90 | +Vom System kommen GTK 4, `libadwaita`, `librsvg` und die GTK-Anbindung für Python (Arch: | |
| 91 | +`python-gobject`). Alles andere holt pip in eine eigene Umgebung – Pakete, die das System schon | |
| 92 | +hat, nimmt es mit (`--system-site-packages`). Am einfachsten mit pipx: | |
| 93 | + | |
| 94 | + pipx install --system-site-packages ~/Projekte/schach | |
| 95 | + schach --install-desktop # Eintrag im App-Menü mit Icon (entfernen: --uninstall-desktop) | |
| 91 | 96 | |
| 92 | -Abhängigkeiten: `python-gobject`, `libadwaita`, `librsvg`, `python-chess`, | |
| 93 | -`stockfish`, `python-requests`, `python-keyring`, und für Maia-3 `onnxruntime-cpu` (oder eine | |
| 94 | -andere onnxruntime-Variante), `python-numpy`. | |
| 97 | +Danach startet `schach` das Programm (Start-Skript aus `pyproject.toml`). Zum Entwickeln: | |
| 98 | + | |
| 99 | + python -m venv --system-site-packages .venv | |
| 100 | + .venv/bin/pip install -e . | |
| 101 | + .venv/bin/schach # oder ohne Installation: python -m schach | |
| 95 | 102 | |
| 96 | -Maia-3 läuft über ONNX Runtime statt PyTorch (`schach/maia_onnx.py`). Das Modell (ONNX-Export | |
| 103 | +Was pip holt, steht in `pyproject.toml`: `chess`, `requests`, `keyring`, `numpy`, `onnxruntime` | |
| 104 | +(jede Variante, die sich so meldet – auch Arch' CPU- oder ROCm-Paket) und | |
| 105 | +`onnxruntime-ep-webgpu`. | |
| 106 | + | |
| 107 | +**Maia-3** läuft über ONNX Runtime statt PyTorch (`schach/maia_onnx.py`). Das Modell (ONNX-Export | |
| 97 | 108 | von Maia3-79M mit Zughistorie, `bqrio/maia3-onnx`, 156 MB, per Prüfsumme festgelegt) lädt |
| 98 | 109 | beim ersten Start; die Titelleiste zeigt den Fortschritt, ein abgebrochener Download wird |
| 99 | -fortgesetzt. Ablage: `~/.cache/schach/`. Optional rechnet Maia auf der Grafikkarte, wenn | |
| 100 | -`onnxruntime-ep-webgpu` installiert ist (pip; Vulkan, D3D12 oder Metal) – sonst auf der CPU. | |
| 110 | +fortgesetzt. Ablage: `~/.cache/schach/`. Maia rechnet über WebGPU auf der Grafikkarte (Vulkan, | |
| 111 | +D3D12 oder Metal); klappt das nicht (keine passende Karte, virtuelle Maschine), auf der CPU. | |
| 112 | +Das WebGPU-Plugin (MIT) ist noch jung (0.6) und deshalb auf `<0.7` begrenzt. | |
| 101 | 113 | Ohne onnxruntime nimmt das Programm `maia3-uci` (AUR `maia3-git`, PyTorch), falls installiert; |
| 102 | 114 | `SCHACH_MAIA=torch` bzw. `=onnx` erzwingt eins von beiden, `SCHACH_MAIA_DEVICE=cpu` die CPU. |
| 103 | 115 | Abgleich mit dem Original: `python tools/maia_onnx_vergleich.py` (braucht maia3 und PyTorch). |
| 116 | + | |
| 117 | +**Stockfish**: das vom System, wenn `stockfish` im PATH liegt (oder `SCHACH_STOCKFISH=Pfad`), | |
| 118 | +sonst lädt das Programm beim ersten Start die offizielle Ausgabe (Stockfish 19, „universal“ – | |
| 119 | +passt sich dem Prozessor an, rund 80 MB, per Prüfsumme festgelegt) für Linux, Windows oder | |
| 120 | +macOS nach `~/.cache/schach/stockfish-sf_19/`, samt Lizenztext (GPL-3.0). | |
| 121 | + | |
| 104 | 122 | Optional für „PDF über LaTeX“: `texlive-games` (`xskak`) und `pdflatex`. |
| 105 | 123 | |
| 106 | 124 | Tests: |
| 107 | 125 | |
| 108 | 126 | python -m unittest discover -s tests -t . |
| 109 | 127 | |
| 110 | -Ins App-Menü aufnehmen: | |
| 111 | - | |
| 112 | - cp schach.desktop ~/.local/share/applications/ | |
| 113 | - | |
| 114 | 128 | ## Tasten |
| 115 | 129 | |
| 116 | 130 | | Taste | Aktion | |
added pyproject.toml +39 −0
| @@ -0,0 +1,39 @@ | ||
| 1 | +[build-system] | |
| 2 | +requires = ["setuptools>=77"] | |
| 3 | +build-backend = "setuptools.build_meta" | |
| 4 | + | |
| 5 | +[project] | |
| 6 | +name = "schach" | |
| 7 | +description = "Schlichtes GNOME-Schachprogramm: gegen Maia-3, mit Hinweisen, Rätseln und Lichess" | |
| 8 | +readme = "README.md" | |
| 9 | +license = "AGPL-3.0-or-later" | |
| 10 | +license-files = ["LICENSE", "schach/daten/HERKUNFT.md", "schach/figuren/HERKUNFT.md", "schach/figuren/LIZENZEN/*"] | |
| 11 | +requires-python = ">=3.11" | |
| 12 | +dynamic = ["version"] | |
| 13 | +dependencies = [ | |
| 14 | + # GTK-Anbindung: am besten aus dem System (python-gobject), venv mit --system-site-packages; | |
| 15 | + # pip müsste beide sonst selbst übersetzen. GTK 4, libadwaita und librsvg kommen immer vom System. | |
| 16 | + "PyGObject", | |
| 17 | + "pycairo", | |
| 18 | + "chess>=1.11", | |
| 19 | + "requests", | |
| 20 | + "keyring", | |
| 21 | + "numpy", | |
| 22 | + # Maia-3: ein beliebiges onnxruntime-Paket, das sich als „onnxruntime“ meldet (auch Arch' | |
| 23 | + # CPU- oder ROCm-Paket); dazu WebGPU (Vulkan/Metal/D3D12) – fehlt eine passende Grafikkarte, | |
| 24 | + # rechnet Maia auf der CPU. | |
| 25 | + "onnxruntime>=1.23", | |
| 26 | + "onnxruntime-ep-webgpu>=0.6,<0.7", | |
| 27 | +] | |
| 28 | + | |
| 29 | +[project.gui-scripts] | |
| 30 | +schach = "schach.__main__:main" | |
| 31 | + | |
| 32 | +[tool.setuptools] | |
| 33 | +packages = ["schach"] | |
| 34 | + | |
| 35 | +[tool.setuptools.dynamic] | |
| 36 | +version = { attr = "schach.__version__" } | |
| 37 | + | |
| 38 | +[tool.setuptools.package-data] | |
| 39 | +schach = ["daten/*", "figuren/**/*", "kurse/*.pgn", "icons/**/*.svg"] | |
removed schach.desktop +0 −7
| @@ -1,7 +0,0 @@ | ||
| 1 | -[Desktop Entry] | |
| 2 | -Type=Application | |
| 3 | -Name=Schach | |
| 4 | -Comment=Schach gegen Maia-3 | |
| 5 | -Exec=/home/timo/Projekte/schach/schach.sh | |
| 6 | -Icon=org.gnome.Chess | |
| 7 | -Categories=Game;BoardGame; | |
removed schach.sh +0 −5
| @@ -1,5 +0,0 @@ | ||
| 1 | -#!/bin/sh | |
| 2 | -# GTKs Vulkan-Renderer meldet auf manchen Treibern harmlose VK_SUBOPTIMAL_KHR-Warnungen; | |
| 3 | -# der OpenGL-Renderer (gl) zeichnet gleich gut und bleibt still. Überschreibbar per GSK_RENDERER. | |
| 4 | -export GSK_RENDERER="${GSK_RENDERER:-gl}" | |
| 5 | -cd "$(dirname "$(readlink -f "$0")")" && exec python3 -m schach "$@" | |
modified schach/__init__.py +1 −0
| @@ -0,0 +1,1 @@ | ||
| 1 | +__version__ = "0.1" | |
modified schach/__main__.py +22 −2
| @@ -1,5 +1,25 @@ | ||
| 1 | +"""Start: ``schach`` (Start-Skript aus pyproject.toml) oder ``python -m schach``. | |
| 2 | + | |
| 3 | +``schach --install-desktop`` legt den Eintrag fürs App-Menü an (Linux), | |
| 4 | +``schach --uninstall-desktop`` entfernt ihn wieder. | |
| 5 | +""" | |
| 6 | + | |
| 7 | +import os | |
| 1 | 8 | import sys |
| 2 | 9 | |
| 3 | -from .app import main | |
| 4 | 10 | |
| 5 | -sys.exit(main()) | |
| 11 | +def main(argv=None): | |
| 12 | + argv = sys.argv if argv is None else argv | |
| 13 | + if argv[1:] in (["--install-desktop"], ["--uninstall-desktop"]): | |
| 14 | + from . import desktop | |
| 15 | + return desktop.main(argv[1]) | |
| 16 | + if sys.platform.startswith("linux"): | |
| 17 | + # GTKs Vulkan-Renderer meldet auf manchen Treibern harmlose VK_SUBOPTIMAL_KHR-Warnungen; | |
| 18 | + # der OpenGL-Renderer zeichnet gleich gut und bleibt still. Überschreibbar per GSK_RENDERER. | |
| 19 | + os.environ.setdefault("GSK_RENDERER", "gl") | |
| 20 | + from .app import main as app_main # erst jetzt GTK laden | |
| 21 | + return app_main() | |
| 22 | + | |
| 23 | + | |
| 24 | +if __name__ == "__main__": | |
| 25 | + sys.exit(main()) | |
modified schach/app.py +3 −3
| @@ -15,7 +15,7 @@ gi.require_version("Gtk", "4.0") | ||
| 15 | 15 | gi.require_version("Adw", "1") |
| 16 | 16 | from gi.repository import Adw, Gdk, Gio, GLib, GObject, Gtk # noqa: E402 |
| 17 | 17 | |
| 18 | -from . import annotate, clipboard, lichess, notation, savegames # noqa: E402 | |
| 18 | +from . import __version__, annotate, clipboard, lichess, notation, savegames # noqa: E402 | |
| 19 | 19 | from .board import BoardView, available_themes # noqa: E402 |
| 20 | 20 | from .movelist import MoveList # noqa: E402 |
| 21 | 21 | from .prefs import Preferences # noqa: E402 |
| @@ -1573,9 +1573,9 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1573 | 1573 | def _about_dialog(self, *_): |
| 1574 | 1574 | about = Adw.AboutDialog( |
| 1575 | 1575 | application_name="Schach", |
| 1576 | - application_icon="org.gnome.Chess", | |
| 1576 | + application_icon=APP_ID, | |
| 1577 | 1577 | developer_name="Timo Stolz", |
| 1578 | - version="0.1", | |
| 1578 | + version=__version__, | |
| 1579 | 1579 | comments="Schach gegen Maia-3 – mit Hinweisen, Zugbewertung von Stockfish, " |
| 1580 | 1580 | "Rätseln und Online-Partien auf Lichess.", |
| 1581 | 1581 | license_type=Gtk.License.AGPL_3_0, |
added schach/desktop.py +94 −0
| @@ -0,0 +1,94 @@ | ||
| 1 | +"""Eintrag fürs App-Menü (freedesktop: GNOME, KDE, …): ``schach --install-desktop``. | |
| 2 | + | |
| 3 | +Schreibt ``app.schach.Schach.desktop`` nach ``~/.local/share/applications`` und das Icon nach | |
| 4 | +``~/.local/share/icons/hicolor/scalable/apps``. Der Befehl darin ist das Start-Skript, mit dem | |
| 5 | +man gerade aufgerufen hat (pipx, venv …) – mit vollem Pfad, weil ``~/.local/bin`` nicht in | |
| 6 | +jeder Desktop-Sitzung im PATH steht. Ohne Start-Skript (``python -m schach``) das laufende | |
| 7 | +Python mit dem Projektordner als Arbeitsverzeichnis. | |
| 8 | +""" | |
| 9 | + | |
| 10 | +import os | |
| 11 | +import shutil | |
| 12 | +import sys | |
| 13 | + | |
| 14 | +APP_ID = "app.schach.Schach" | |
| 15 | +ICON = os.path.join(os.path.dirname(__file__), "icons", "hicolor", "scalable", "apps", APP_ID + ".svg") | |
| 16 | + | |
| 17 | + | |
| 18 | +def data_home(): | |
| 19 | + return os.environ.get("XDG_DATA_HOME") or os.path.join(os.path.expanduser("~"), ".local", "share") | |
| 20 | + | |
| 21 | + | |
| 22 | +def paths(base=None): | |
| 23 | + base = base or data_home() | |
| 24 | + return (os.path.join(base, "applications", APP_ID + ".desktop"), | |
| 25 | + os.path.join(base, "icons", "hicolor", "scalable", "apps", APP_ID + ".svg")) | |
| 26 | + | |
| 27 | + | |
| 28 | +def _quote(arg): | |
| 29 | + """Argument für ``Exec=`` (Desktop-Entry-Spezifikation): bei Bedarf in Anführungszeichen.""" | |
| 30 | + if arg and not any(c in arg for c in ' \t"\'\\><~|&;$*?#()`'): | |
| 31 | + return arg | |
| 32 | + return '"' + "".join("\\" + c if c in '"`$\\' else c for c in arg) + '"' | |
| 33 | + | |
| 34 | + | |
| 35 | +def launch_command(argv0=None): | |
| 36 | + """(Exec-Zeile, Arbeitsverzeichnis oder None).""" | |
| 37 | + argv0 = argv0 if argv0 is not None else sys.argv[0] | |
| 38 | + script = shutil.which(argv0) if argv0 else None | |
| 39 | + if script and os.path.basename(script).startswith("schach") and not script.endswith(".py"): | |
| 40 | + return _quote(os.path.abspath(script)), None | |
| 41 | + project = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| 42 | + return f"{_quote(sys.executable)} -m schach", project | |
| 43 | + | |
| 44 | + | |
| 45 | +def desktop_entry(exec_line, workdir=None): | |
| 46 | + lines = [ | |
| 47 | + "[Desktop Entry]", | |
| 48 | + "Type=Application", | |
| 49 | + "Name=Schach", | |
| 50 | + "Comment=Schach gegen Maia-3, mit Hinweisen, Rätseln und Lichess", | |
| 51 | + f"Exec={exec_line}", | |
| 52 | + f"Icon={APP_ID}", | |
| 53 | + "Terminal=false", | |
| 54 | + "Categories=Game;BoardGame;", | |
| 55 | + "Keywords=Chess;Maia;Stockfish;Lichess;Brettspiel;", | |
| 56 | + f"StartupWMClass={APP_ID}", | |
| 57 | + "StartupNotify=true", | |
| 58 | + ] | |
| 59 | + if workdir: | |
| 60 | + lines.append(f"Path={workdir}") | |
| 61 | + return "\n".join(lines) + "\n" | |
| 62 | + | |
| 63 | + | |
| 64 | +def install(base=None, argv0=None): | |
| 65 | + entry, icon = paths(base) | |
| 66 | + os.makedirs(os.path.dirname(entry), exist_ok=True) | |
| 67 | + os.makedirs(os.path.dirname(icon), exist_ok=True) | |
| 68 | + shutil.copyfile(ICON, icon) | |
| 69 | + with open(entry, "w", encoding="utf-8") as fh: | |
| 70 | + fh.write(desktop_entry(*launch_command(argv0))) | |
| 71 | + return entry, icon | |
| 72 | + | |
| 73 | + | |
| 74 | +def uninstall(base=None): | |
| 75 | + removed = [] | |
| 76 | + for path in paths(base): | |
| 77 | + if os.path.exists(path): | |
| 78 | + os.remove(path) | |
| 79 | + removed.append(path) | |
| 80 | + return removed | |
| 81 | + | |
| 82 | + | |
| 83 | +def main(option): | |
| 84 | + if sys.platform in ("win32", "darwin"): | |
| 85 | + print("Der Menü-Eintrag ist nur für Linux-Desktops (freedesktop) gedacht.", file=sys.stderr) | |
| 86 | + return 1 | |
| 87 | + if option == "--uninstall-desktop": | |
| 88 | + for path in uninstall(): | |
| 89 | + print("entfernt:", path) | |
| 90 | + return 0 | |
| 91 | + entry, icon = install() | |
| 92 | + print("Menü-Eintrag:", entry) | |
| 93 | + print("Icon: ", icon) | |
| 94 | + return 0 | |
added schach/icons/hicolor/scalable/apps/app.schach.Schach.svg +20 −0
| @@ -0,0 +1,20 @@ | ||
| 1 | +<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128"> | |
| 2 | + <!-- App-Icon: Springer aus dem Satz „cburnett“ (Colin M. L. Burnett, GPL-2.0-or-later) | |
| 3 | + auf vier Brettfeldern in den Holzfarben des Programms --> | |
| 4 | + <defs> | |
| 5 | + <clipPath id="face"><rect x="12" y="12" width="104" height="100" rx="12"/></clipPath> | |
| 6 | + </defs> | |
| 7 | + <rect x="12" y="18" width="104" height="100" rx="12" fill="#7a5638"/> | |
| 8 | + <g clip-path="url(#face)"> | |
| 9 | + <rect x="12" y="12" width="104" height="100" fill="#f0d9b5"/> | |
| 10 | + <rect x="64" y="12" width="52" height="50" fill="#b58863"/> | |
| 11 | + <rect x="12" y="62" width="52" height="50" fill="#b58863"/> | |
| 12 | + </g> | |
| 13 | + <ellipse cx="66" cy="103" rx="34" ry="5" fill="#000" opacity="0.18"/> | |
| 14 | + <g transform="translate(5 -3.4) scale(2.6)" fill="none" fill-rule="evenodd" stroke="#000" | |
| 15 | + stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"> | |
| 16 | + <path fill="#fff" d="M22 10c10.5 1 16.5 8 16 29H15c0-9 10-6.5 8-21"/> | |
| 17 | + <path fill="#fff" d="M24 18c.38 2.91-5.550 7.37-8 9-3 2-2.820 4.34-5 4-1.042-.94 1.410-3.04 0-3-1 0 .19 1.23-1 2-1 0-4.003 1-4-4 0-2 6-12 6-12s1.890-1.900 2-3.500c-.73-.994-.5-2-.5-3 1-1 3 2.500 3 2.500h2s.78-1.992 2.500-3c1 0 1 3 1 3"/> | |
| 18 | + <path fill="#000" d="M9.5 25.5a.5.5 0 1 1-1 0 .5.5 0 1 1 1 0m5.433-9.750a.5 1.500 30 1 1-.866-.5.5 1.500 30 1 1 .866.5"/> | |
| 19 | + </g> | |
| 20 | +</svg> | |
added tests/test_packaging.py +182 −0
| @@ -0,0 +1,182 @@ | ||
| 1 | +"""Paket, Start-Skript, Menü-Eintrag und Stockfish-Download (ohne Netz).""" | |
| 2 | + | |
| 3 | +import glob | |
| 4 | +import io | |
| 5 | +import os | |
| 6 | +import tarfile | |
| 7 | +import tempfile | |
| 8 | +import tomllib | |
| 9 | +import unittest | |
| 10 | +import zipfile | |
| 11 | +from unittest import mock | |
| 12 | + | |
| 13 | +from schach import desktop, stockfish | |
| 14 | + | |
| 15 | +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| 16 | + | |
| 17 | + | |
| 18 | +class PyprojectTest(unittest.TestCase): | |
| 19 | + def setUp(self): | |
| 20 | + with open(os.path.join(ROOT, "pyproject.toml"), "rb") as fh: | |
| 21 | + self.project = tomllib.load(fh) | |
| 22 | + | |
| 23 | + def test_all_data_files_are_packaged(self): | |
| 24 | + """Jede Nicht-Python-Datei unter schach/ muss ein package-data-Muster treffen.""" | |
| 25 | + package = os.path.join(ROOT, "schach") | |
| 26 | + patterns = self.project["tool"]["setuptools"]["package-data"]["schach"] | |
| 27 | + packaged = set() | |
| 28 | + for pattern in patterns: | |
| 29 | + packaged |= {os.path.normpath(p) for p in glob.glob(os.path.join(package, pattern), recursive=True) | |
| 30 | + if os.path.isfile(p)} | |
| 31 | + missing = [] | |
| 32 | + for folder, dirs, files in os.walk(package): | |
| 33 | + dirs[:] = [d for d in dirs if d != "__pycache__"] | |
| 34 | + missing += [os.path.relpath(os.path.join(folder, f), package) for f in files | |
| 35 | + if not f.endswith(".py") and os.path.normpath(os.path.join(folder, f)) not in packaged] | |
| 36 | + self.assertEqual(missing, []) | |
| 37 | + | |
| 38 | + def test_gui_script_points_to_main(self): | |
| 39 | + target = self.project["project"]["gui-scripts"]["schach"] | |
| 40 | + module, func = target.split(":") | |
| 41 | + self.assertEqual((module, func), ("schach.__main__", "main")) | |
| 42 | + from schach import __main__ | |
| 43 | + self.assertTrue(callable(__main__.main)) | |
| 44 | + | |
| 45 | + def test_webgpu_is_a_dependency(self): | |
| 46 | + deps = " ".join(self.project["project"]["dependencies"]) | |
| 47 | + self.assertIn("onnxruntime>=", deps) | |
| 48 | + self.assertIn("onnxruntime-ep-webgpu", deps) | |
| 49 | + | |
| 50 | + | |
| 51 | +class StockfishTest(unittest.TestCase): | |
| 52 | + def test_build_per_platform(self): | |
| 53 | + self.assertEqual(stockfish.build("linux", "x86_64")[0], "stockfish-linux-x86-64-universal.tar.gz") | |
| 54 | + self.assertEqual(stockfish.build("linux", "arm64")[0], "stockfish-linux-arm64-universal.tar.gz") | |
| 55 | + self.assertEqual(stockfish.build("win32", "AMD64")[0], "stockfish-windows-x86-64-universal.zip") | |
| 56 | + self.assertEqual(stockfish.build("darwin", "arm64")[0], "stockfish-macos-universal.tar.gz") | |
| 57 | + self.assertIsNone(stockfish.build("linux", "mips")) | |
| 58 | + | |
| 59 | + def test_checksums_look_like_sha256(self): | |
| 60 | + for archive, sha in stockfish.BUILDS.values(): | |
| 61 | + self.assertRegex(sha, r"^[0-9a-f]{64}$", archive) | |
| 62 | + | |
| 63 | + def test_binary_path(self): | |
| 64 | + self.assertTrue(stockfish.binary_path("stockfish-windows-x86-64-universal.zip", "/c") | |
| 65 | + .endswith(os.path.join("stockfish-sf_19", "stockfish-windows-x86-64-universal.exe"))) | |
| 66 | + | |
| 67 | + def test_env_wins(self): | |
| 68 | + with mock.patch.dict(os.environ, {"SCHACH_STOCKFISH": "/opt/sf"}): | |
| 69 | + self.assertEqual(stockfish.command(), (["/opt/sf"], None)) | |
| 70 | + | |
| 71 | + def test_without_system_stockfish_it_prepares(self): | |
| 72 | + with mock.patch.dict(os.environ, {}, clear=False), mock.patch("shutil.which", return_value=None): | |
| 73 | + os.environ.pop("SCHACH_STOCKFISH", None) | |
| 74 | + self.assertEqual(stockfish.command(), ([], stockfish.prepare)) | |
| 75 | + | |
| 76 | + def _archive(self, folder, kind, name): | |
| 77 | + body, licence = b"#!/bin/sh\necho stockfish\n", b"GPL" | |
| 78 | + path = os.path.join(folder, "sf." + kind) | |
| 79 | + if kind == "zip": | |
| 80 | + with zipfile.ZipFile(path, "w") as zf: | |
| 81 | + zf.writestr(f"stockfish/{name}", body) | |
| 82 | + zf.writestr("stockfish/Copying.txt", licence) | |
| 83 | + zf.writestr("stockfish/wiki/Home.md", b"x") | |
| 84 | + else: | |
| 85 | + with tarfile.open(path, "w:gz") as tf: | |
| 86 | + for member, data in ((f"stockfish/{name}", body), ("stockfish/Copying.txt", licence), | |
| 87 | + ("../boese", b"x")): | |
| 88 | + info = tarfile.TarInfo(member) | |
| 89 | + info.size = len(data) | |
| 90 | + tf.addfile(info, io.BytesIO(data)) | |
| 91 | + return path | |
| 92 | + | |
| 93 | + def test_extract_only_program_and_licence(self): | |
| 94 | + for kind in ("tar.gz", "zip"): | |
| 95 | + with self.subTest(kind=kind), tempfile.TemporaryDirectory() as tmp: | |
| 96 | + out = os.path.join(tmp, "out") | |
| 97 | + os.makedirs(out) | |
| 98 | + stockfish.extract(self._archive(tmp, kind, "sf-bin"), out, "sf-bin") | |
| 99 | + self.assertEqual(sorted(os.listdir(out)), ["COPYING.txt", "sf-bin"]) | |
| 100 | + self.assertTrue(os.access(os.path.join(out, "sf-bin"), os.X_OK)) | |
| 101 | + self.assertFalse(os.path.exists(os.path.join(tmp, "boese"))) | |
| 102 | + | |
| 103 | + def test_prepare_downloads_once(self): | |
| 104 | + with tempfile.TemporaryDirectory() as tmp: | |
| 105 | + archive, _ = stockfish.build("linux", "x86_64") | |
| 106 | + name = archive.removesuffix(".tar.gz") | |
| 107 | + | |
| 108 | + def fake_download(url, target, sha256, progress=None, what=""): | |
| 109 | + self.assertTrue(url.endswith(archive)) | |
| 110 | + os.makedirs(os.path.dirname(target), exist_ok=True) # wie das echte download() | |
| 111 | + os.replace(self._archive(tmp, "tar.gz", name), target) | |
| 112 | + | |
| 113 | + with mock.patch.object(stockfish, "build", return_value=(archive, "0" * 64)), \ | |
| 114 | + mock.patch.object(stockfish, "download", side_effect=fake_download) as dl: | |
| 115 | + first = stockfish.prepare(base=tmp) | |
| 116 | + second = stockfish.prepare(base=tmp) | |
| 117 | + self.assertEqual(first, second) | |
| 118 | + self.assertEqual(dl.call_count, 1) | |
| 119 | + self.assertTrue(os.path.isfile(first[0])) | |
| 120 | + self.assertFalse(os.path.exists(os.path.join(os.path.dirname(first[0]), archive))) | |
| 121 | + | |
| 122 | + def test_unknown_platform_explains(self): | |
| 123 | + with mock.patch.object(stockfish, "build", return_value=None): | |
| 124 | + with self.assertRaisesRegex(FileNotFoundError, "Stockfish installieren"): | |
| 125 | + stockfish.prepare() | |
| 126 | + | |
| 127 | + | |
| 128 | +class DesktopTest(unittest.TestCase): | |
| 129 | + def test_script_is_used_with_full_path(self): | |
| 130 | + with tempfile.TemporaryDirectory() as tmp: | |
| 131 | + script = os.path.join(tmp, "schach") | |
| 132 | + with open(script, "w") as fh: | |
| 133 | + fh.write("#!/bin/sh\n") | |
| 134 | + os.chmod(script, 0o755) | |
| 135 | + self.assertEqual(desktop.launch_command(script), (script, None)) | |
| 136 | + | |
| 137 | + def test_python_m_uses_project_folder(self): | |
| 138 | + exec_line, workdir = desktop.launch_command("/irgendwo/schach/__main__.py") | |
| 139 | + self.assertTrue(exec_line.endswith(" -m schach")) | |
| 140 | + self.assertEqual(workdir, ROOT) | |
| 141 | + | |
| 142 | + def test_quote(self): | |
| 143 | + self.assertEqual(desktop._quote("/usr/bin/schach"), "/usr/bin/schach") | |
| 144 | + self.assertEqual(desktop._quote("/home/a b/schach"), '"/home/a b/schach"') | |
| 145 | + self.assertEqual(desktop._quote('/x/"$y'), '"/x/\\"\\$y"') | |
| 146 | + | |
| 147 | + def test_install_and_uninstall(self): | |
| 148 | + with tempfile.TemporaryDirectory() as tmp: | |
| 149 | + entry, icon = desktop.install(base=tmp, argv0="/irgendwo/__main__.py") | |
| 150 | + with open(entry, encoding="utf-8") as fh: | |
| 151 | + text = fh.read() | |
| 152 | + self.assertIn("Icon=app.schach.Schach\n", text) | |
| 153 | + self.assertIn("StartupWMClass=app.schach.Schach\n", text) | |
| 154 | + self.assertIn(f"Path={ROOT}\n", text) | |
| 155 | + self.assertTrue(os.path.isfile(icon)) | |
| 156 | + self.assertEqual(sorted(desktop.uninstall(base=tmp)), sorted([entry, icon])) | |
| 157 | + | |
| 158 | + def test_icon_is_svg(self): | |
| 159 | + with open(desktop.ICON, encoding="utf-8") as fh: | |
| 160 | + self.assertIn("<svg", fh.read(200)) | |
| 161 | + | |
| 162 | + | |
| 163 | +class DownloadStatusTest(unittest.TestCase): | |
| 164 | + def test_subtitle_lists_running_downloads(self): | |
| 165 | + from schach.app import SchachApp | |
| 166 | + | |
| 167 | + class Stub: | |
| 168 | + maia_status = "" | |
| 169 | + downloads = {"Maia": (78_000_000, 156_000_000), "Stockfish": (8_000_000, None)} | |
| 170 | + get_windows = staticmethod(list) | |
| 171 | + | |
| 172 | + stub = Stub() | |
| 173 | + SchachApp._show_downloads(stub) | |
| 174 | + self.assertEqual(stub.maia_status, | |
| 175 | + "Einmaliger Download: Maia 50 % · 78 von 156 MB – Stockfish 8 MB") | |
| 176 | + stub.downloads = {} | |
| 177 | + SchachApp._show_downloads(stub) | |
| 178 | + self.assertEqual(stub.maia_status, "Maia wird geladen …") | |
| 179 | + | |
| 180 | + | |
| 181 | +if __name__ == "__main__": | |
| 182 | + unittest.main() | |