File size: 4,191 Bytes
4bc63c7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | """The entry point of this bundle: one request, one verdict.
Contract of section 3.1. The workspace is never touched in place: the request runs
against a temporary copy named lingologos_thinkingpython_*, and the caller decides what
to do with the result. Exit codes: 0 verified, 1 refused, 2 the session itself failed.
"""
import argparse
import json
import os
import shutil
import subprocess
import sys
import tempfile
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
from lingologos_thinkingpython import lexicon # noqa: E402
from lingologos_thinkingpython.orchestrator import MultiInstanceOrchestrator # noqa: E402
PREFIX = "lingologos_thinkingpython_"
SKIP_DIRS = ("__pycache__", ".git", ".venv", "build", ".pytest_cache")
def copy_workspace(src: str, dst: str) -> None:
def ignore(directory, names):
return [n for n in names if n in SKIP_DIRS or n.endswith(".pyc")]
shutil.copytree(src, dst, ignore=ignore)
def write_launchers(dst: str, lang: str, request: str) -> None:
inner = '"%s" "%s" --workspace "%s" --request %s --lang %s --in-process' % (
sys.executable, os.path.abspath(__file__), dst, _quote(request, "cmd"), lang)
with open(os.path.join(dst, "run_session.cmd"), "w", encoding="ascii") as handle:
handle.write("@echo off\r\n" + inner + "\r\n")
with open(os.path.join(dst, "run_session.sh"), "w", encoding="ascii") as handle:
handle.write("#!/bin/sh\n" + '"%s" "%s" --workspace "%s" --request "%s" --lang %s --in-process\n'
% (sys.executable, os.path.abspath(__file__), dst, request.replace('"', '\\"'), lang))
def _quote(text: str, kind: str) -> str:
if kind == "cmd":
return '"' + text.replace('"', '\\"') + '"'
return '"' + text.replace('"', '\\"') + '"'
def derive_verdict(transcript):
"""Re-derive the verdict from the transcript, never trust the stored one."""
from nodes import arbiter as arbiter_mod
nodes = [t for t in transcript if t.get("node") != "arbiter"]
return arbiter_mod.combine(nodes)
def main(argv=None) -> int:
parser = argparse.ArgumentParser(description="Run one maintenance session.")
parser.add_argument("--workspace", required=True)
parser.add_argument("--request", required=True)
parser.add_argument("--lang", default="en")
parser.add_argument("--in-process", action="store_true",
help="run against this exact folder instead of a copy")
parser.add_argument("--out", default="", help="write the session report here")
args = parser.parse_args(argv)
try:
sys.stdout.reconfigure(encoding="utf-8")
except (AttributeError, ValueError):
pass
src = os.path.abspath(args.workspace)
if not os.path.isdir(src):
sys.stderr.write("no such workspace: %s\n" % src)
return 2
lang = lexicon.normalize(args.lang)
if not lexicon.has(lang):
sys.stderr.write("unknown variety: %s\n" % args.lang)
return 2
temp = None
target = src
if not args.in_process:
temp = tempfile.mkdtemp(prefix=PREFIX)
target = os.path.join(temp, "workspace")
copy_workspace(src, target)
write_launchers(target, lang, args.request)
orchestrator = MultiInstanceOrchestrator.from_default()
report = orchestrator.run_session(target, args.request, lang)
verdict, assurance, reasons = derive_verdict(report["transcript"])
session = {"workspace": target, "lang": lang, "verdict": verdict,
"assurance": assurance, "reasons": reasons,
"transcript": report["transcript"], "labels": report["labels"]}
if args.out:
with open(args.out, "w", encoding="utf-8") as handle:
json.dump(session, handle, indent=2, ensure_ascii=False)
if temp:
shutil.rmtree(temp, ignore_errors=True)
print(json.dumps({"verdict": verdict, "assurance": assurance, "reasons": reasons,
"verdict_label": session["labels"]["verdict"]}, ensure_ascii=False))
return 0 if verdict == "VERIFIED" else (2 if verdict == "UNKNOWN" else 1)
if __name__ == "__main__":
raise SystemExit(main())
|