"""Command line: argument parsing, `status`, and dispatch to the UI or the runner.""" import argparse import json import os import sys import tempfile import time from .common import JOB_FILE, ME, VERSION, FlowError, fmt_dur, job_state, read_json from .demo import setup_demo from .flow import Flow from .runner import Runner def cmd_status(flow, as_json): job = read_json(flow.path(JOB_FILE)) if as_json: print(json.dumps(dict(state=job_state(job), job=job), indent=1, sort_keys=True)) return 0 if job and job.get("status") in ("passed", "running", "queued") else 1 if not job: print("no sanity run recorded in %s" % flow.ws) return 1 st = job_state(job) label = "RUNNER LOST" if st == "orphaned" else job["status"].upper() start = job.get("started") or job["created"] print("run %s %s (%s, elapsed %s)" % (job["id"], label, job.get("host", "?"), fmt_dur((job.get("finished") or time.time()) - start))) print("mode: %s%s" % ("check in" if job["mode"] == "checkin" else "unlock only", ", tag %s" % job["tag"] if job.get("tag") else "")) print("files: %s" % " ".join(job["files"] + job["new_files"])) if job.get("locked_by_job"): print("locked by run: %s" % " ".join(job["locked_by_job"])) for s in job["steps"]: mark = {"pending": ".", "running": ">", "done": "+", "failed": "x", "cancelled": "-", "skipped": "-"}[s["status"]] el = ((s.get("t1") or time.time()) - s["t0"]) if s.get("t0") else None print(" [%s] %-34s %-9s %s" % (mark, s["label"], fmt_dur(el), s.get("detail", ""))) if job.get("summary"): print(job["summary"]) return 0 if job["status"] in ("passed", "running", "queued") and st != "orphaned" else 1 def main(argv=None): ap = argparse.ArgumentParser( prog="dsflow_tui.py", description="Terminal UI for the dsflow DesignSync check-in flow.", formatter_class=argparse.RawDescriptionHelpFormatter, epilog="examples:\n" " dsflow_tui.py # from the workspace root\n" " dsflow_tui.py status # progress of the current sanity run\n" " dsflow_tui.py --sanity-cmd 'sim/regress.sh --cfg {cfg}'\n" " dsflow_tui.py --demo # sandbox with a fake dssc\n" " DEMO_FAIL=jaguar_1x1 dsflow_tui.py --demo # watch a failing run release its locks") ap.add_argument("command", nargs="?", default="tui", choices=("tui", "status", "_runjob"), help="tui (default) or status") ap.add_argument("--ws", default=os.getcwd(), help="workspace root (default: current directory)") ap.add_argument("--conf", help="dsflow.conf (default: $DSFLOW_CONF or /flow/dsflow.conf)") ap.add_argument("--sanity-cmd", help="external sanity command; {cfg} and {ws} are substituted " "(default: SANITY_SCRIPT from dsflow.conf, else run_sanity)") ap.add_argument("--tags", help="comma-separated tags offered for batch tagging (default: DSFLOW_TAGS)") ap.add_argument("--auto-refresh", type=int, default=0, metavar="SEC", help="re-query DesignSync every SEC seconds (default: only on r / after actions)") ap.add_argument("--json", action="store_true", help="status: print JSON") ap.add_argument("--theme", default="tokyo-night", help="colour theme (default tokyo-night; ctrl+p in the app lists the others)") ap.add_argument("--demo", action="store_true", help="run against a sandbox workspace with a fake dssc") ap.add_argument("--demo-dir", default=os.path.join(tempfile.gettempdir(), "dsflow_tui_demo-" + ME)) ap.add_argument("--demo-reset", action="store_true", help="rebuild the demo sandbox from scratch") ap.add_argument("--version", action="version", version="dsflow_tui " + VERSION) args = ap.parse_args(argv) if args.demo: args.ws, args.conf = setup_demo(args.demo_dir, args.demo_reset) conf = args.conf or os.environ.get("DSFLOW_CONF") or os.path.join(args.ws, "flow", "dsflow.conf") tags = [t for t in (args.tags or "").replace(",", " ").split() if t] or None try: flow = Flow(args.ws, conf, args.sanity_cmd, tags) flow.load_vars() except FlowError as e: print("dsflow_tui: %s" % e, file=sys.stderr) return 2 if args.command == "_runjob": return Runner(flow).main() if args.command == "status": return cmd_status(flow, args.json) if not sys.stdout.isatty(): print("dsflow_tui: the TUI needs a terminal (use `status` for scripts)", file=sys.stderr) return 2 if sys.version_info < (3, 8, 1): print("dsflow_tui: the TUI needs Python >= 3.8.1 (this is %s); `status` still works" % sys.version.split()[0], file=sys.stderr) return 2 from .ui.app import DsflowApp # imported late: status and the runner don't need Textual DsflowApp(flow, auto_refresh=args.auto_refresh, theme=args.theme).run() job = read_json(flow.path(JOB_FILE)) if job_state(job) == "active": print("dsflow_tui: sanity run %s is still going in the background." % job["id"]) print(" follow it with: %s status (or reopen the TUI)" % os.path.basename(sys.argv[0])) return 0