Newer
Older
SPIS / run_sets_launcher.py
"""Запуск сборки комплекта дисциплин из AutoCAD.

Вызывается командой SPISRUNSETS через WScript.Shell.Run.
Запускает рабочий скрипт через pythonw.exe (без консольного окна)
с перенаправлением вывода в лог-файл.
"""
import os
import subprocess
import sys
from pathlib import Path

TOOLS_DIR = Path(__file__).resolve().parent
WORKER = TOOLS_DIR / "build_discipline_sets.py"
VENV_PY = TOOLS_DIR / ".venv" / "Scripts" / "python.exe"
LOG = Path(os.environ.get("TEMP", ".")) / "spis_sets_log.txt"


def main() -> int:
    if len(sys.argv) < 3:
        print("Использование: python run_sets_launcher.py <корень> <корпус>")
        return 2

    root = sys.argv[1].strip('"')
    corpus = sys.argv[2].strip('"')

    if not WORKER.exists():
        with open(LOG, "w", encoding="utf-8") as f:
            f.write(f"ОШИБКА: не найден рабочий скрипт:\n{WORKER}\n")
        return 1

    # pythonw.exe — без консольного окна. Если нет — берём python.exe.
    pythonw = VENV_PY.with_name("pythonw.exe")
    python = pythonw if pythonw.exists() else VENV_PY

    if not python.exists():
        python = "python"

    try:
        log = open(LOG, "w", encoding="utf-8")
        subprocess.Popen(
            [str(python), str(WORKER), root, corpus],
            creationflags=subprocess.CREATE_NO_WINDOW,
            close_fds=True,
            cwd=str(TOOLS_DIR),
            stdout=log,
            stderr=subprocess.STDOUT,
        )
        log.close()
    except Exception as exc:
        try:
            log.close()
        except Exception:
            pass
        with open(LOG, "a", encoding="utf-8") as f:
            f.write(f"\nОШИБКА запуска: {exc}\n")

    return 0


if __name__ == "__main__":
    sys.exit(main())