diff --git a/scripts/autorun.py b/scripts/autorun.py index 544b5430c..ca4e18656 100644 --- a/scripts/autorun.py +++ b/scripts/autorun.py @@ -33,1064 +33,24 @@ Usage: from __future__ import annotations import argparse -import contextlib -import datetime as dt -import fnmatch import json import os -import re -import shlex -import shutil -import subprocess import sys -import time -from collections.abc import Callable, Sequence -from dataclasses import dataclass, field from pathlib import Path -from core.childenv import is_secret, safe_env from core.hostguard import ( DEFAULT_LOCK, LOCK_ENV, - LOCK_HELD_ENV, - LockBusy, - free_gb, - global_lock, - lock_busy, ) -from core.paths import REPO_ROOT -from core.statefile import now_iso, save_state +from unattended.breaker import gh_json +from unattended.jobs import DEFERRED, OK, load_jobs +from unattended.runner import Context, render_status, run_job, status +from unattended.timers import install_timers, render_units +from unattended.worktree import main_checkout -JOBS_FILE = REPO_ROOT / "automation" / "jobs.yml" -AUTORUN_DIR = Path(".cache") / "autorun" -BRANCH = "autorun" -REMOTE = "origin" -TARGET = "main" -REPO_SLUG = "Abdess/retrobios" - -OK = "ok" -FAILED = "failed" -DEFERRED = "deferred" -REFUSED = "refused" -BLOCKED = "blocked" EXIT_CODES = {OK: 0, DEFERRED: 75} -ALERT_FREE_GB = 100 -LOG_DAYS = 30 -SEGFAULT_CODES = (139, -11) -HOST_MISREAD_MARKERS = ("SystemError", "unknown opcode") -VOLATILE_KEYS = frozenset({"generated_at", "generated", "imported_at"}) - -CI_RED = frozenset({"failure", "timed_out", "startup_failure"}) -CI_GREEN = frozenset({"success", "skipped", "neutral"}) - -JOB_KEYS = frozenset({ - "schedule", "steps", "gate", "twice", "commits", "worktree", "lock", - "network", "min_free_gb", "max_load_wait", "timeout", "scratch", "report", -}) - - -class AutorunError(RuntimeError): - """A condition that ends a job with a named result.""" - - def __init__(self, result: str, detail: str) -> None: - super().__init__(detail) - self.result = result - self.detail = detail - - -@dataclass(frozen=True) -class Step: - argv: tuple[str, ...] - ok_codes: tuple[int, ...] = (0,) - stdout: str = "" - # Variables passed through beside the allowlist (romfetch's settings). - env: tuple[str, ...] = () - # GITHUB_TOKEN for a first-party script that calls the GitHub API. - github_token: bool = False - - -@dataclass(frozen=True) -class Twice: - """An artifact the gate wrote, computed again into {out} and compared.""" - - artifact: str - argv: tuple[str, ...] - glob: str = "" - - -@dataclass(frozen=True) -class CommitRule: - subject: str - paths: tuple[str, ...] - body: str = "" - - -@dataclass(frozen=True) -class Job: - name: str - steps: tuple[Step, ...] = () - gate: tuple[Step, ...] = () - twice: tuple[Twice, ...] = () - commits: tuple[CommitRule, ...] = () - schedule: str = "" - worktree: bool = True - lock: bool = True - network: bool = False - min_free_gb: float = 30.0 - max_load_wait: int = 3600 - timeout: int = 21600 - scratch: tuple[str, ...] = () - report: str = "" - - -@dataclass -class Outcome: - returncode: int - text: str = "" - retried: bool = False - - -@dataclass -class Host: - """What a run reads from the machine, replaced by fakes in the tests.""" - - loadavg: Callable[[], tuple[float, float, float]] = os.getloadavg - sleep: Callable[[float], None] = time.sleep - clock: Callable[[], float] = time.monotonic - cpu_count: int = os.cpu_count() or 1 - poll: float = 60.0 - - @property - def max_load(self) -> float: - return self.cpu_count / 4 - - -@dataclass -class Context: - repo: Path - jobs: dict[str, Job] - lock_path: Path - host: Host = field(default_factory=Host) - gh: Callable[[list[str]], object] | None = None - dry_run: bool = False - push: bool = False - - @property - def home(self) -> Path: - return self.repo / AUTORUN_DIR - - @property - def worktree(self) -> Path: - return self.home / "worktree" - - @property - def state_path(self) -> Path: - return self.home / "state.json" - - -# Job registry - - -def check_subject(subject: str, body: str = "") -> list[str]: - """What the repository's commit rules refuse in this message, checked - when the job list loads instead of two hours into the job.""" - from devtools.commitrules import problems - - return problems(f"{subject}\n\n{body}" if body else subject) - - -# The first-party scripts that call the GitHub API and may receive -# GITHUB_TOKEN: run as `python