From 902529a4eae5cae711e50a909b264822f2376d61 Mon Sep 17 00:00:00 2001 From: Orionis Date: Mon, 16 Nov 2026 22:43:11 +0100 Subject: [PATCH] :sparkles: bypass + correctif --- Dockerfile | 7 ++++++- README.md | 2 +- docs/deploy-coolify.md | 2 +- entrypoint.sh | 6 ++++++ src/scalper/cli.py | 2 ++ src/scalper/pipeline.py | 5 ++++- src/scalper/server.py | 8 +++++--- tests/test_pipeline_exporter.py | 23 +++++++++++++++++++++++ tests/test_server.py | 11 +++++++++++ 9 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 3b30fd9..ea7e631 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,11 @@ COPY src ./src RUN pip install --no-cache-dir ".[ocr]" RUN useradd -m scalper && mkdir -p /app/data /app/out && chown -R scalper:scalper /app -USER scalper + +# Le conteneur démarre en root : l'entrypoint chown les répertoires montés +# (bind mounts root côté hôte, ex. Coolify) puis droppe les privilèges (setpriv). +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh ENV PYTHONUNBUFFERED=1 EXPOSE 8000 @@ -20,4 +24,5 @@ EXPOSE 8000 HEALTHCHECK --interval=30s --timeout=5s --retries=3 \ CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/healthz')" || exit 1 +ENTRYPOINT ["/entrypoint.sh"] CMD ["scalper", "serve", "--host", "0.0.0.0", "--port", "8000"] diff --git a/README.md b/README.md index 5cb2a77..017b7b5 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ scalper run --source demo --series demo-1 --lang fr --mock-llm --out out | `scalper translate --lang fr [--mock-llm]` | traduction LLM + glossaire | | `scalper qa --lang fr` | score 0-100, pass si ≥ 80 | | `scalper export --format md\|json\|html` | export Markdown / JSON / HTML | -| `scalper run --source ... --series ... --lang fr` | pipeline complet end-to-end | +| `scalper run --source ... --series ... --lang fr [--force]` | pipeline complet end-to-end (`--force` = exporte même si la QA échoue) | ## OCR & remplacement du texte dans les images (V2) diff --git a/docs/deploy-coolify.md b/docs/deploy-coolify.md index be7b9af..85b188b 100644 --- a/docs/deploy-coolify.md +++ b/docs/deploy-coolify.md @@ -35,7 +35,7 @@ Les résultats sont écrits dans le volume `./out` (monté dans le compose). |---|---|---| | GET | `/` | **interface web** (formulaire run) | | GET | `/healthz` | sonde de santé | -| POST | `/run` | pipeline complet (scrape → clean → translate → QA → export) | +| POST | `/run` | pipeline complet (scrape → clean → translate → QA → export). Champ optionnel `"force": true` : exporte même si la QA échoue (les chapitres concernés sont marqués `"forced": true`) | ## Sécurité - Rate limiting ≥ 1 s entre requêtes vers les sources. diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..5299c28 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/sh +set -e +# Les répertoires montés (bind mounts compose/Coolify) appartiennent souvent à root +# côté hôte : on les rend à l'utilisateur applicatif, puis on droppe les privilèges. +chown -R scalper:scalper /app/data /app/out 2>/dev/null || true +exec setpriv --reuid scalper --regid scalper --init-groups "$@" diff --git a/src/scalper/cli.py b/src/scalper/cli.py index 78a7a9f..a82999a 100644 --- a/src/scalper/cli.py +++ b/src/scalper/cli.py @@ -96,6 +96,7 @@ def main(argv: list[str] | None = None) -> int: c.add_argument("--max-chapters", type=int, default=None) c.add_argument("--mock-llm", action="store_true", help="démo hors ligne (LLM factice)") c.add_argument("--with-ocr", action="store_true", help="OCR + remplacement du texte dans les images") + c.add_argument("--force", action="store_true", help="exporte même si la QA échoue (bypass vérification)") args = p.parse_args(argv) @@ -203,6 +204,7 @@ def main(argv: list[str] | None = None) -> int: args.source, args.series, args.lang, out_root=Path(args.out), mock_llm=args.mock_llm, max_chapters=args.max_chapters, group=args.group, with_ocr=args.with_ocr, + force=args.force, ) _print(summary) return 0 if summary.get("status") == "done" else 3 diff --git a/src/scalper/pipeline.py b/src/scalper/pipeline.py index b088475..011f8dd 100644 --- a/src/scalper/pipeline.py +++ b/src/scalper/pipeline.py @@ -26,6 +26,7 @@ def run_pipeline( max_chapters: int | None = None, group: str | None = None, with_ocr: bool = False, + force: bool = False, ) -> dict[str, Any]: out_root = Path(out_root) fetcher = fetcher or RespectfulFetcher(min_delay=1.0) @@ -66,13 +67,15 @@ def run_pipeline( report = qa_chapter(chapter, translated, glossary, lang) (dest / f"qa.{lang}.json").write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8") entry["qa"] = {"score": report["score"], "pass": report["pass"], "issues": len(report["issues"])} - if report["pass"]: + if report["pass"] or force: for fmt in ("md", "json", "html"): export_chapter(translated, fmt, dest / "export") if with_ocr: entry["ocr"] = _run_ocr_typeset(dest, meta, lang) state.mark_done(ref.id) entry["status"] = "ok" + if not report["pass"]: + entry["forced"] = True else: entry["status"] = "qa_failed" results.append(entry) diff --git a/src/scalper/server.py b/src/scalper/server.py index f2e4297..9b63857 100644 --- a/src/scalper/server.py +++ b/src/scalper/server.py @@ -2,7 +2,7 @@ Endpoints : - GET /healthz -- POST /run {"source","series","lang","group"?,"max_chapters"?,"mock_llm"?,"with_ocr"?,"out"?} +- POST /run {"source","series","lang","group"?,"max_chapters"?,"mock_llm"?,"with_ocr"?,"force"?,"out"?} """ from __future__ import annotations @@ -26,12 +26,13 @@ INDEX_HTML = """ -
+ +
résultats ici…
""" @@ -81,6 +82,7 @@ class Handler(BaseHTTPRequestHandler): max_chapters=body.get("max_chapters"), group=body.get("group"), with_ocr=bool(body.get("with_ocr", False)), + force=bool(body.get("force", False)), ) except KeyError as e: return self._json(400, {"error": f"champ requis manquant: {e}"}) diff --git a/tests/test_pipeline_exporter.py b/tests/test_pipeline_exporter.py index 002266b..0d02388 100644 --- a/tests/test_pipeline_exporter.py +++ b/tests/test_pipeline_exporter.py @@ -2,6 +2,7 @@ import json import tempfile import unittest from pathlib import Path +from unittest import mock from scalper import pipeline from scalper.exporter import export_chapter, slugify @@ -30,6 +31,28 @@ class TestPipeline(unittest.TestCase): res = pipeline.run_pipeline("demo", "demo-1", "fr", out_root=tmp, mock_llm=True) self.assertTrue(any(c.get("skipped") for c in res["chapters"])) + def test_qa_failure_blocks_export_without_force(self): + fake = {"score": 10, "pass": False, "issues": [{"type": "x", "critical": True}]} + with tempfile.TemporaryDirectory() as tmp: + with mock.patch("scalper.pipeline.qa_chapter", return_value=fake): + res = pipeline.run_pipeline("demo", "demo-1", "fr", out_root=tmp, mock_llm=True, max_chapters=1) + self.assertEqual(res["chapters"][0]["status"], "qa_failed") + raw1 = Path(tmp) / "raw" / "demo" / "demo-1" / "demo-1-ch1" + self.assertFalse((raw1 / "export").exists()) + + def test_force_exports_despite_qa_failure(self): + fake = {"score": 10, "pass": False, "issues": [{"type": "x", "critical": True}]} + with tempfile.TemporaryDirectory() as tmp: + with mock.patch("scalper.pipeline.qa_chapter", return_value=fake): + res = pipeline.run_pipeline("demo", "demo-1", "fr", out_root=tmp, mock_llm=True, max_chapters=1, force=True) + self.assertEqual(res["status"], "done") + ch = res["chapters"][0] + self.assertEqual(ch["status"], "ok") + self.assertTrue(ch.get("forced")) + self.assertFalse(ch["qa"]["pass"]) + raw1 = Path(tmp) / "raw" / "demo" / "demo-1" / "demo-1-ch1" + self.assertTrue(list((raw1 / "export").glob("*.md"))) + class TestExporter(unittest.TestCase): def test_exports(self): diff --git a/tests/test_server.py b/tests/test_server.py index a87baf4..18a8116 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -5,6 +5,7 @@ import unittest import urllib.error import urllib.request from http.server import ThreadingHTTPServer +from unittest import mock from scalper.server import Handler @@ -63,6 +64,16 @@ class TestServer(unittest.TestCase): status, body = self._post("/run", {"source": "demo"}) self.assertEqual(status, 400) + def test_run_force_bypasses_qa_gate(self): + fake = {"score": 10, "pass": False, "issues": [{"type": "x", "critical": True}]} + with tempfile.TemporaryDirectory() as tmp: + with mock.patch("scalper.pipeline.qa_chapter", return_value=fake): + status, body = self._post("/run", {"source": "demo", "series": "demo-1", "lang": "fr", "mock_llm": True, "out": tmp, "max_chapters": 1, "force": True}) + self.assertEqual(status, 200) + self.assertEqual(body["status"], "done") + self.assertEqual(body["chapters"][0]["status"], "ok") + self.assertTrue(body["chapters"][0].get("forced")) + if __name__ == "__main__": unittest.main()