mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
dfae68db87
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Construit dist/ : app + niveaux + Pyodide, avec liste de pré-cache du service worker à jour."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import re
|
|
import shutil
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
DIST = ROOT / "dist"
|
|
PRECACHED_SUFFIXES = {".html", ".css", ".js", ".mjs", ".json", ".svg", ".webmanifest", ".py"}
|
|
NOT_PRECACHED = {"service-worker.js"}
|
|
|
|
|
|
def copy_tree(source: Path, target: Path) -> None:
|
|
shutil.copytree(source, target, ignore=shutil.ignore_patterns("__pycache__", "*.pyc", "*.md"))
|
|
|
|
|
|
def shell_files() -> list[str]:
|
|
"""Fichiers de l'app à pré-cacher (chemins relatifs à la racine du site)."""
|
|
files = [
|
|
path.relative_to(DIST).as_posix()
|
|
for path in sorted(DIST.rglob("*"))
|
|
if path.is_file()
|
|
and path.suffix in PRECACHED_SUFFIXES
|
|
and path.name not in NOT_PRECACHED
|
|
and not path.relative_to(DIST).parts[0] in {"vendor", "levels"}
|
|
]
|
|
return ["./", *files]
|
|
|
|
|
|
def content_hash(paths: list[str]) -> str:
|
|
digest = hashlib.sha256()
|
|
for relative in paths:
|
|
if relative != "./":
|
|
digest.update((DIST / relative).read_bytes())
|
|
return digest.hexdigest()[:12]
|
|
|
|
|
|
def main() -> int:
|
|
pyodide = ROOT / "vendor" / "pyodide"
|
|
if not pyodide.exists():
|
|
print("vendor/pyodide absent : lancez `make vendor-pyodide` d'abord.", file=sys.stderr)
|
|
return 1
|
|
|
|
if DIST.exists():
|
|
shutil.rmtree(DIST)
|
|
copy_tree(ROOT / "app", DIST)
|
|
copy_tree(ROOT / "levels", DIST / "levels")
|
|
shutil.copytree(pyodide, DIST / "vendor" / "pyodide")
|
|
|
|
precache = shell_files()
|
|
worker = DIST / "service-worker.js"
|
|
source = worker.read_text(encoding="utf-8")
|
|
source, shell_count = re.subn(r"const SHELL = \[.*?\];", f"const SHELL = {json.dumps(precache)};", source, flags=re.S)
|
|
source, version_count = re.subn(
|
|
r"const CACHE_VERSION = '[^']*';", f"const CACHE_VERSION = 'pyquest-{content_hash(precache)}';", source
|
|
)
|
|
if shell_count != 1 or version_count != 1:
|
|
print("service-worker.js : motifs SHELL/CACHE_VERSION introuvables.", file=sys.stderr)
|
|
return 1
|
|
worker.write_text(source, encoding="utf-8")
|
|
print(f"dist/ prêt : {len(precache)} fichiers pré-cachés.")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|