packleader 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- packleader/__init__.py +3 -0
- packleader/__main__.py +8 -0
- packleader/_jobrun.py +49 -0
- packleader/banner.py +127 -0
- packleader/cli.py +172 -0
- packleader/config.py +291 -0
- packleader/install_check.py +81 -0
- packleader/jobs.py +445 -0
- packleader/locks.py +230 -0
- packleader/product.py +134 -0
- packleader/runners/__init__.py +0 -0
- packleader/runners/base.py +27 -0
- packleader/runners/local.py +134 -0
- packleader-0.0.1.dist-info/METADATA +16 -0
- packleader-0.0.1.dist-info/RECORD +19 -0
- packleader-0.0.1.dist-info/WHEEL +5 -0
- packleader-0.0.1.dist-info/entry_points.txt +3 -0
- packleader-0.0.1.dist-info/licenses/LICENSE +21 -0
- packleader-0.0.1.dist-info/top_level.txt +1 -0
packleader/__init__.py
ADDED
packleader/__main__.py
ADDED
packleader/_jobrun.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Runner entry point for a detached job (B2)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def main(argv=None) -> int:
|
|
13
|
+
if argv is None:
|
|
14
|
+
argv = sys.argv[1:]
|
|
15
|
+
jobs_dir = Path(argv[0])
|
|
16
|
+
job_id = argv[1]
|
|
17
|
+
with open(jobs_dir / (job_id + ".json"), encoding="utf-8") as handle:
|
|
18
|
+
data = json.load(handle)
|
|
19
|
+
command = data["argv"]
|
|
20
|
+
cwd = data.get("cwd")
|
|
21
|
+
out_path = jobs_dir / (job_id + ".out")
|
|
22
|
+
rc = 0
|
|
23
|
+
with open(out_path, "ab") as out:
|
|
24
|
+
run_kwargs: dict = {
|
|
25
|
+
"cwd": cwd,
|
|
26
|
+
"stdin": subprocess.DEVNULL,
|
|
27
|
+
"stdout": out,
|
|
28
|
+
"stderr": subprocess.STDOUT,
|
|
29
|
+
}
|
|
30
|
+
if os.name == "nt":
|
|
31
|
+
run_kwargs["creationflags"] = getattr(
|
|
32
|
+
subprocess, "CREATE_NO_WINDOW", 0
|
|
33
|
+
)
|
|
34
|
+
try:
|
|
35
|
+
proc = subprocess.run(command, **run_kwargs)
|
|
36
|
+
except OSError as exc:
|
|
37
|
+
out.write(("cannot run: %s\n" % type(exc).__name__).encode("utf-8"))
|
|
38
|
+
rc = 127
|
|
39
|
+
else:
|
|
40
|
+
rc = proc.returncode
|
|
41
|
+
with open(jobs_dir / (job_id + ".rc"), "w", encoding="utf-8") as handle:
|
|
42
|
+
handle.write("%d\n" % rc)
|
|
43
|
+
with open(jobs_dir / (job_id + ".done"), "w", encoding="utf-8") as handle:
|
|
44
|
+
handle.write("done\n")
|
|
45
|
+
return 0
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
if __name__ == "__main__":
|
|
49
|
+
sys.exit(main())
|
packleader/banner.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""The welcome banner and terminal colour helpers for ``pl``.
|
|
2
|
+
|
|
3
|
+
Colour is used only on an interactive terminal that is not asked to stay
|
|
4
|
+
plain (``NO_COLOR``, ``TERM=dumb``). Block glyphs fall back to ``#`` when
|
|
5
|
+
the output encoding cannot carry them (a pipe on an old Windows code page).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
from . import __version__
|
|
14
|
+
|
|
15
|
+
# 5 x 5 letters; "#" is a filled cell.
|
|
16
|
+
_LETTERS = {
|
|
17
|
+
"P": ["#### ", "# #", "#### ", "# ", "# "],
|
|
18
|
+
"A": [" ### ", "# #", "#####", "# #", "# #"],
|
|
19
|
+
"C": [" ####", "# ", "# ", "# ", " ####"],
|
|
20
|
+
"K": ["# #", "# # ", "### ", "# # ", "# #"],
|
|
21
|
+
"L": ["# ", "# ", "# ", "# ", "#####"],
|
|
22
|
+
"E": ["#####", "# ", "#### ", "# ", "#####"],
|
|
23
|
+
"D": ["#### ", "# #", "# #", "# #", "#### "],
|
|
24
|
+
"R": ["#### ", "# #", "#### ", "# # ", "# #"],
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
WORD = "PACKLEADER"
|
|
28
|
+
|
|
29
|
+
# One 256-colour code per letter: amber to purple.
|
|
30
|
+
_GRADIENT = [214, 208, 202, 196, 197, 198, 199, 163, 127, 91]
|
|
31
|
+
|
|
32
|
+
TAGLINE = "plan. judge. build. prove."
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _enable_windows_vt(stream):
|
|
36
|
+
"""Turn on ANSI escape handling for a Windows console. True when on."""
|
|
37
|
+
if os.name != "nt":
|
|
38
|
+
return True
|
|
39
|
+
try:
|
|
40
|
+
import ctypes
|
|
41
|
+
import msvcrt
|
|
42
|
+
|
|
43
|
+
kernel32 = ctypes.windll.kernel32
|
|
44
|
+
handle = msvcrt.get_osfhandle(stream.fileno())
|
|
45
|
+
mode = ctypes.c_uint32()
|
|
46
|
+
if not kernel32.GetConsoleMode(handle, ctypes.byref(mode)):
|
|
47
|
+
return False
|
|
48
|
+
enable_vt = 0x0004
|
|
49
|
+
if mode.value & enable_vt:
|
|
50
|
+
return True
|
|
51
|
+
return bool(kernel32.SetConsoleMode(handle, mode.value | enable_vt))
|
|
52
|
+
except Exception:
|
|
53
|
+
return False
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def use_colour(stream=None):
|
|
57
|
+
"""True when ``stream`` is a terminal that should get colour."""
|
|
58
|
+
stream = stream or sys.stdout
|
|
59
|
+
if os.environ.get("NO_COLOR"):
|
|
60
|
+
return False
|
|
61
|
+
if os.environ.get("TERM") == "dumb":
|
|
62
|
+
return False
|
|
63
|
+
try:
|
|
64
|
+
if not stream.isatty():
|
|
65
|
+
return False
|
|
66
|
+
except Exception:
|
|
67
|
+
return False
|
|
68
|
+
return _enable_windows_vt(stream)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def can_encode(text, stream=None):
|
|
72
|
+
stream = stream or sys.stdout
|
|
73
|
+
encoding = getattr(stream, "encoding", None) or "ascii"
|
|
74
|
+
try:
|
|
75
|
+
text.encode(encoding)
|
|
76
|
+
except (UnicodeEncodeError, LookupError):
|
|
77
|
+
return False
|
|
78
|
+
return True
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def paint(text, code, colour):
|
|
82
|
+
"""Wrap ``text`` in a 256-colour escape when ``colour`` is on."""
|
|
83
|
+
if not colour:
|
|
84
|
+
return text
|
|
85
|
+
return "\033[1;38;5;%dm%s\033[0m" % (code, text)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def dim(text, colour):
|
|
89
|
+
return "\033[2m%s\033[0m" % text if colour else text
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
#: Columns the double-width banner needs (10 letters of 10, gaps, margin).
|
|
93
|
+
WIDE_COLUMNS = 2 + len(WORD) * 10 + (len(WORD) - 1) + 2
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def render(colour=False, block=True, wide=False):
|
|
97
|
+
"""Return the banner text; ``wide`` draws every cell two columns wide."""
|
|
98
|
+
fill = "█" if block else "#"
|
|
99
|
+
scale = 2 if wide else 1
|
|
100
|
+
rows = []
|
|
101
|
+
for row in range(5):
|
|
102
|
+
cells = []
|
|
103
|
+
for index, letter in enumerate(WORD):
|
|
104
|
+
pattern = "".join(
|
|
105
|
+
(fill if cell == "#" else " ") * scale
|
|
106
|
+
for cell in _LETTERS[letter][row])
|
|
107
|
+
cells.append(paint(pattern, _GRADIENT[index], colour))
|
|
108
|
+
rows.append(" " + " ".join(cells).rstrip())
|
|
109
|
+
rows.append("")
|
|
110
|
+
rows.append(" " + dim("v%s %s" % (__version__, TAGLINE), colour))
|
|
111
|
+
return "\n".join(rows) + "\n"
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def show(stream=None):
|
|
115
|
+
"""Print the banner to ``stream`` (default stdout)."""
|
|
116
|
+
stream = stream or sys.stdout
|
|
117
|
+
colour = use_colour(stream)
|
|
118
|
+
block = can_encode("█", stream)
|
|
119
|
+
wide = colour and _columns() >= WIDE_COLUMNS
|
|
120
|
+
stream.write("\n" + render(colour=colour, block=block, wide=wide) + "\n")
|
|
121
|
+
stream.flush()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _columns():
|
|
125
|
+
import shutil
|
|
126
|
+
|
|
127
|
+
return shutil.get_terminal_size((80, 24)).columns
|
packleader/cli.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""Command line interface for Packleader."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
from . import __version__, banner, config, install_check
|
|
9
|
+
from . import jobs
|
|
10
|
+
from .product import Product
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
14
|
+
parser = argparse.ArgumentParser(
|
|
15
|
+
prog="pl",
|
|
16
|
+
description="Packleader: a delivery pipeline for Claude Code",
|
|
17
|
+
)
|
|
18
|
+
parser.add_argument(
|
|
19
|
+
"--version",
|
|
20
|
+
action="version",
|
|
21
|
+
version="packleader " + __version__,
|
|
22
|
+
)
|
|
23
|
+
sub = parser.add_subparsers(dest="command")
|
|
24
|
+
config_parser = sub.add_parser("config", help="inspect the pack configuration")
|
|
25
|
+
config_sub = config_parser.add_subparsers(dest="config_command")
|
|
26
|
+
config_sub.required = True
|
|
27
|
+
check_parser = config_sub.add_parser("check", help="validate product.toml")
|
|
28
|
+
check_parser.add_argument(
|
|
29
|
+
"--root",
|
|
30
|
+
default=".",
|
|
31
|
+
help="project root holding .packleader/product.toml",
|
|
32
|
+
)
|
|
33
|
+
job_parser = sub.add_parser("job", help="manage background jobs")
|
|
34
|
+
job_sub = job_parser.add_subparsers(dest="job_command")
|
|
35
|
+
job_sub.required = True
|
|
36
|
+
job_start = job_sub.add_parser("start", help="start a background job")
|
|
37
|
+
job_start.add_argument("--root", default=".", help="project root")
|
|
38
|
+
job_start.add_argument("--cwd", default=None, help="working directory")
|
|
39
|
+
job_start.add_argument("--tag", default="job", help="job tag")
|
|
40
|
+
job_start.add_argument(
|
|
41
|
+
"cmd", nargs=argparse.REMAINDER, default=[], help="command to run"
|
|
42
|
+
)
|
|
43
|
+
job_state = job_sub.add_parser("state", help="show job state")
|
|
44
|
+
job_state.add_argument("job_id", help="job id")
|
|
45
|
+
job_state.add_argument("--root", default=".", help="project root")
|
|
46
|
+
job_stop = job_sub.add_parser("stop", help="stop a running job")
|
|
47
|
+
job_stop.add_argument("job_id", help="job id")
|
|
48
|
+
job_stop.add_argument("--root", default=".", help="project root")
|
|
49
|
+
watch_parser = sub.add_parser("watch", help="watch a job until it ends")
|
|
50
|
+
watch_parser.add_argument("job_id", help="job id")
|
|
51
|
+
watch_parser.add_argument("--root", default=".", help="project root")
|
|
52
|
+
watch_parser.add_argument("--report", default=None, help="report file")
|
|
53
|
+
watch_parser.add_argument(
|
|
54
|
+
"--poll", type=float, default=60.0, help="poll interval in seconds"
|
|
55
|
+
)
|
|
56
|
+
watch_parser.add_argument(
|
|
57
|
+
"--silent-min",
|
|
58
|
+
type=float,
|
|
59
|
+
default=12.0,
|
|
60
|
+
help="minutes without change before SILENT",
|
|
61
|
+
)
|
|
62
|
+
watch_parser.add_argument(
|
|
63
|
+
"--activity",
|
|
64
|
+
action="append",
|
|
65
|
+
default=[],
|
|
66
|
+
dest="activity",
|
|
67
|
+
help="activity glob relative to root",
|
|
68
|
+
)
|
|
69
|
+
wait_parser = sub.add_parser("wait", help="wait for a job to finish")
|
|
70
|
+
wait_parser.add_argument("job_id", help="job id")
|
|
71
|
+
wait_parser.add_argument("--root", default=".", help="project root")
|
|
72
|
+
wait_parser.add_argument(
|
|
73
|
+
"--poll", type=float, default=60.0, help="poll interval in seconds"
|
|
74
|
+
)
|
|
75
|
+
return parser
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def main(argv=None) -> int:
|
|
79
|
+
"""Run the CLI; return the process exit code."""
|
|
80
|
+
parser = _build_parser()
|
|
81
|
+
args = parser.parse_args(argv)
|
|
82
|
+
if args.command is None:
|
|
83
|
+
banner.show(sys.stdout)
|
|
84
|
+
parser.print_help(sys.stdout)
|
|
85
|
+
return 0
|
|
86
|
+
if args.command == "config" and args.config_command == "check":
|
|
87
|
+
loaded = config.load(args.root)
|
|
88
|
+
version = loaded.data.get("version")
|
|
89
|
+
if isinstance(version, str):
|
|
90
|
+
for warning in install_check.warnings(version):
|
|
91
|
+
print("packleader: warning: %s" % warning, file=sys.stderr)
|
|
92
|
+
for warning in loaded.warnings:
|
|
93
|
+
print("packleader: warning: %s" % warning, file=sys.stderr)
|
|
94
|
+
if loaded.errors:
|
|
95
|
+
for error in loaded.errors:
|
|
96
|
+
print("packleader: error: %s" % error, file=sys.stderr)
|
|
97
|
+
return 1
|
|
98
|
+
product = Product.from_config(loaded.data)
|
|
99
|
+
print(
|
|
100
|
+
"ok: product %s, %d member(s), %d host(s)"
|
|
101
|
+
% (product.name, len(product.members), len(product.hosts))
|
|
102
|
+
)
|
|
103
|
+
return 0
|
|
104
|
+
if args.command == "job":
|
|
105
|
+
try:
|
|
106
|
+
if args.job_command == "start":
|
|
107
|
+
cmd = list(args.cmd)
|
|
108
|
+
if cmd[:1] == ["--"]:
|
|
109
|
+
cmd = cmd[1:]
|
|
110
|
+
if not cmd:
|
|
111
|
+
parser.error("CMD is required")
|
|
112
|
+
job_id = jobs.start(
|
|
113
|
+
args.root, cmd, cwd=args.cwd, tag=args.tag
|
|
114
|
+
)
|
|
115
|
+
print("job=%s" % job_id)
|
|
116
|
+
return 0
|
|
117
|
+
if args.job_command == "state":
|
|
118
|
+
current = jobs.state(args.root, args.job_id)
|
|
119
|
+
line = "%s rc=%s" % (
|
|
120
|
+
current.name,
|
|
121
|
+
current.rc if current.rc is not None else "-",
|
|
122
|
+
)
|
|
123
|
+
if current.detail:
|
|
124
|
+
line += " %s" % current.detail
|
|
125
|
+
print(line)
|
|
126
|
+
return 0
|
|
127
|
+
if args.job_command == "stop":
|
|
128
|
+
print(
|
|
129
|
+
"stopped"
|
|
130
|
+
if jobs.stop(args.root, args.job_id)
|
|
131
|
+
else "not running"
|
|
132
|
+
)
|
|
133
|
+
return 0
|
|
134
|
+
except jobs.JobError as exc:
|
|
135
|
+
print("packleader: error: %s" % exc, file=sys.stderr)
|
|
136
|
+
return 2
|
|
137
|
+
if args.command == "watch":
|
|
138
|
+
try:
|
|
139
|
+
last = None
|
|
140
|
+
for event in jobs.watch(
|
|
141
|
+
args.root,
|
|
142
|
+
args.job_id,
|
|
143
|
+
report=args.report,
|
|
144
|
+
poll_s=args.poll,
|
|
145
|
+
silent_min=args.silent_min,
|
|
146
|
+
activity=tuple(args.activity or ()),
|
|
147
|
+
):
|
|
148
|
+
print(event.line, flush=True)
|
|
149
|
+
last = event
|
|
150
|
+
if last is None:
|
|
151
|
+
return 0
|
|
152
|
+
return jobs.EXIT_CODES[last.kind]
|
|
153
|
+
except jobs.JobError as exc:
|
|
154
|
+
print("packleader: error: %s" % exc, file=sys.stderr)
|
|
155
|
+
return 2
|
|
156
|
+
if args.command == "wait":
|
|
157
|
+
try:
|
|
158
|
+
last = None
|
|
159
|
+
for event in jobs.watch(
|
|
160
|
+
args.root, args.job_id, poll_s=args.poll,
|
|
161
|
+
silent_min=float("inf"),
|
|
162
|
+
):
|
|
163
|
+
last = event
|
|
164
|
+
if last is None:
|
|
165
|
+
return 0
|
|
166
|
+
print(last.line, flush=True)
|
|
167
|
+
return jobs.EXIT_CODES[last.kind]
|
|
168
|
+
except jobs.JobError as exc:
|
|
169
|
+
print("packleader: error: %s" % exc, file=sys.stderr)
|
|
170
|
+
return 2
|
|
171
|
+
parser.print_help(sys.stdout)
|
|
172
|
+
return 0
|
packleader/config.py
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"""Load and validate ``.packleader/product.toml`` (schema 1)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
import tomllib
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
PACK_DIR = ".packleader"
|
|
11
|
+
PACK_FILE = "product.toml"
|
|
12
|
+
SCHEMA = 1
|
|
13
|
+
|
|
14
|
+
_PRODUCT_RE = re.compile(r"^[a-z0-9][a-z0-9-]*$")
|
|
15
|
+
_NAME_RE = re.compile(r"^[a-z0-9][a-z0-9_-]*$")
|
|
16
|
+
|
|
17
|
+
_OS_VALUES = ("windows", "linux", "macos")
|
|
18
|
+
_SHELL_VALUES = ("powershell", "posix")
|
|
19
|
+
_FORGE_VALUES = ("gitlab", "github", "none")
|
|
20
|
+
_REVIEWER_VALUES = ("radarhound", "none")
|
|
21
|
+
|
|
22
|
+
_ROLE_NAMES = ("judge", "inventory", "implementer")
|
|
23
|
+
|
|
24
|
+
_TOP_FIELDS = ("version", "schema", "product", "roles", "hosts", "members")
|
|
25
|
+
_ROLE_FIELDS = ("agent", "model", "effort", "fallback")
|
|
26
|
+
_HOST_FIELDS = ("ssh", "os", "work_root", "shell", "live_paths")
|
|
27
|
+
_MEMBER_FIELDS = ("role", "base", "path", "remote", "forge", "host")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class Loaded:
|
|
32
|
+
path: Path
|
|
33
|
+
data: dict
|
|
34
|
+
errors: list[str]
|
|
35
|
+
warnings: list[str]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _type_name(value):
|
|
39
|
+
"""Short type name used in messages (``table`` for a dict)."""
|
|
40
|
+
if isinstance(value, bool):
|
|
41
|
+
return "bool"
|
|
42
|
+
if isinstance(value, str):
|
|
43
|
+
return "str"
|
|
44
|
+
if isinstance(value, int):
|
|
45
|
+
return "int"
|
|
46
|
+
if isinstance(value, list):
|
|
47
|
+
return "list"
|
|
48
|
+
if isinstance(value, dict):
|
|
49
|
+
return "table"
|
|
50
|
+
return type(value).__name__
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _check_opt_str(table, key, dotted, errors):
|
|
54
|
+
if key in table and not isinstance(table[key], str):
|
|
55
|
+
errors.append(
|
|
56
|
+
"%s.%s: expected str, got %s" % (dotted, key, _type_name(table[key]))
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _check_one_of(value, dotted, allowed, errors):
|
|
61
|
+
if not isinstance(value, str):
|
|
62
|
+
errors.append("%s: expected str, got %s" % (dotted, _type_name(value)))
|
|
63
|
+
elif value not in allowed:
|
|
64
|
+
errors.append("%s: must be one of %s" % (dotted, ", ".join(allowed)))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _check_str_list(value, dotted, errors):
|
|
68
|
+
if not isinstance(value, list):
|
|
69
|
+
errors.append("%s: expected list, got %s" % (dotted, _type_name(value)))
|
|
70
|
+
return
|
|
71
|
+
for index, item in enumerate(value):
|
|
72
|
+
if not isinstance(item, str):
|
|
73
|
+
errors.append(
|
|
74
|
+
"%s[%d]: expected str, got %s" % (dotted, index, _type_name(item))
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _check_role(table, dotted, errors, warnings):
|
|
79
|
+
if not isinstance(table, dict):
|
|
80
|
+
errors.append("%s: expected table, got %s" % (dotted, _type_name(table)))
|
|
81
|
+
return
|
|
82
|
+
for key in table:
|
|
83
|
+
if key not in _ROLE_FIELDS:
|
|
84
|
+
warnings.append("unknown key '%s.%s' ignored" % (dotted, key))
|
|
85
|
+
if "agent" not in table:
|
|
86
|
+
errors.append("%s.agent: required" % dotted)
|
|
87
|
+
elif not isinstance(table["agent"], str):
|
|
88
|
+
errors.append(
|
|
89
|
+
"%s.agent: expected str, got %s" % (dotted, _type_name(table["agent"]))
|
|
90
|
+
)
|
|
91
|
+
elif not table["agent"].strip():
|
|
92
|
+
errors.append("%s.agent: must not be empty" % dotted)
|
|
93
|
+
_check_opt_str(table, "model", dotted, errors)
|
|
94
|
+
_check_opt_str(table, "effort", dotted, errors)
|
|
95
|
+
if "fallback" in table:
|
|
96
|
+
_check_str_list(table["fallback"], dotted + ".fallback", errors)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def load(root) -> Loaded:
|
|
100
|
+
"""Read ``<root>/.packleader/product.toml`` and validate it."""
|
|
101
|
+
path = Path(root) / PACK_DIR / PACK_FILE
|
|
102
|
+
try:
|
|
103
|
+
with open(path, "rb") as handle:
|
|
104
|
+
data = tomllib.load(handle)
|
|
105
|
+
except FileNotFoundError:
|
|
106
|
+
return Loaded(
|
|
107
|
+
path=path,
|
|
108
|
+
data={},
|
|
109
|
+
errors=["%s: not found (run pl setup)" % path],
|
|
110
|
+
warnings=[],
|
|
111
|
+
)
|
|
112
|
+
except (OSError, UnicodeDecodeError) as exc:
|
|
113
|
+
return Loaded(
|
|
114
|
+
path=path,
|
|
115
|
+
data={},
|
|
116
|
+
errors=["%s: cannot read: %s" % (path, exc)],
|
|
117
|
+
warnings=[],
|
|
118
|
+
)
|
|
119
|
+
except tomllib.TOMLDecodeError as exc:
|
|
120
|
+
return Loaded(
|
|
121
|
+
path=path,
|
|
122
|
+
data={},
|
|
123
|
+
errors=["%s: invalid TOML: %s" % (path, exc)],
|
|
124
|
+
warnings=[],
|
|
125
|
+
)
|
|
126
|
+
errors, warnings = validate(data)
|
|
127
|
+
return Loaded(path=path, data=data, errors=errors, warnings=warnings)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def validate(data) -> tuple[list[str], list[str]]:
|
|
131
|
+
"""Check ``data`` against schema 1; return ``(errors, warnings)``.
|
|
132
|
+
|
|
133
|
+
Pure: never raises on a dict. Both lists are sorted by message text.
|
|
134
|
+
"""
|
|
135
|
+
errors: list[str] = []
|
|
136
|
+
warnings: list[str] = []
|
|
137
|
+
if not isinstance(data, dict):
|
|
138
|
+
return (["expected table, got %s" % _type_name(data)], [])
|
|
139
|
+
for key in data:
|
|
140
|
+
if key not in _TOP_FIELDS:
|
|
141
|
+
warnings.append("unknown key '%s' ignored" % key)
|
|
142
|
+
if "version" not in data:
|
|
143
|
+
errors.append("version: required")
|
|
144
|
+
elif not isinstance(data["version"], str):
|
|
145
|
+
errors.append(
|
|
146
|
+
"version: expected str, got %s" % _type_name(data["version"])
|
|
147
|
+
)
|
|
148
|
+
elif not data["version"].strip():
|
|
149
|
+
errors.append("version: must not be empty")
|
|
150
|
+
if "schema" not in data:
|
|
151
|
+
errors.append("schema: required")
|
|
152
|
+
elif isinstance(data["schema"], bool) or not isinstance(data["schema"], int):
|
|
153
|
+
errors.append(
|
|
154
|
+
"schema: expected int, got %s" % _type_name(data["schema"])
|
|
155
|
+
)
|
|
156
|
+
elif data["schema"] != SCHEMA:
|
|
157
|
+
errors.append(
|
|
158
|
+
"schema: unsupported schema %s (this Packleader reads schema 1)"
|
|
159
|
+
% data["schema"]
|
|
160
|
+
)
|
|
161
|
+
if "product" not in data:
|
|
162
|
+
errors.append("product: required")
|
|
163
|
+
elif not isinstance(data["product"], str):
|
|
164
|
+
errors.append(
|
|
165
|
+
"product: expected str, got %s" % _type_name(data["product"])
|
|
166
|
+
)
|
|
167
|
+
elif not _PRODUCT_RE.match(data["product"]):
|
|
168
|
+
errors.append("product: must match ^[a-z0-9][a-z0-9-]*$")
|
|
169
|
+
if "roles" in data:
|
|
170
|
+
roles = data["roles"]
|
|
171
|
+
if not isinstance(roles, dict):
|
|
172
|
+
errors.append("roles: expected table, got %s" % _type_name(roles))
|
|
173
|
+
else:
|
|
174
|
+
for key in roles:
|
|
175
|
+
if key not in _ROLE_NAMES and key != "reviewer":
|
|
176
|
+
warnings.append("unknown key 'roles.%s' ignored" % key)
|
|
177
|
+
for name in _ROLE_NAMES:
|
|
178
|
+
if name in roles:
|
|
179
|
+
_check_role(roles[name], "roles." + name, errors, warnings)
|
|
180
|
+
if "reviewer" in roles:
|
|
181
|
+
_check_one_of(
|
|
182
|
+
roles["reviewer"], "roles.reviewer", _REVIEWER_VALUES, errors
|
|
183
|
+
)
|
|
184
|
+
host_names: set[str] = set()
|
|
185
|
+
if "hosts" in data:
|
|
186
|
+
hosts = data["hosts"]
|
|
187
|
+
if not isinstance(hosts, dict):
|
|
188
|
+
errors.append("hosts: expected table, got %s" % _type_name(hosts))
|
|
189
|
+
else:
|
|
190
|
+
for name, table in hosts.items():
|
|
191
|
+
host_names.add(name)
|
|
192
|
+
dotted = "hosts.%s" % name
|
|
193
|
+
if not _NAME_RE.match(name):
|
|
194
|
+
errors.append(
|
|
195
|
+
"%s: must match ^[a-z0-9][a-z0-9_-]*$" % dotted
|
|
196
|
+
)
|
|
197
|
+
if not isinstance(table, dict):
|
|
198
|
+
errors.append(
|
|
199
|
+
"%s: expected table, got %s" % (dotted, _type_name(table))
|
|
200
|
+
)
|
|
201
|
+
continue
|
|
202
|
+
for key in table:
|
|
203
|
+
if key not in _HOST_FIELDS:
|
|
204
|
+
warnings.append("unknown key '%s.%s' ignored" % (dotted, key))
|
|
205
|
+
is_local = name == "local"
|
|
206
|
+
if is_local and "ssh" in table:
|
|
207
|
+
errors.append(
|
|
208
|
+
"%s.ssh: not allowed (the local host never uses ssh)"
|
|
209
|
+
% dotted
|
|
210
|
+
)
|
|
211
|
+
for key in ("ssh", "work_root"):
|
|
212
|
+
if key in table:
|
|
213
|
+
if is_local and key == "ssh":
|
|
214
|
+
continue
|
|
215
|
+
if not isinstance(table[key], str):
|
|
216
|
+
errors.append(
|
|
217
|
+
"%s.%s: expected str, got %s"
|
|
218
|
+
% (dotted, key, _type_name(table[key]))
|
|
219
|
+
)
|
|
220
|
+
elif not table[key].strip():
|
|
221
|
+
errors.append(
|
|
222
|
+
"%s.%s: must not be empty" % (dotted, key)
|
|
223
|
+
)
|
|
224
|
+
elif not is_local:
|
|
225
|
+
errors.append("%s.%s: required" % (dotted, key))
|
|
226
|
+
if "os" in table:
|
|
227
|
+
_check_one_of(table["os"], dotted + ".os", _OS_VALUES, errors)
|
|
228
|
+
elif not is_local:
|
|
229
|
+
errors.append("%s.os: required" % dotted)
|
|
230
|
+
if "shell" in table:
|
|
231
|
+
_check_one_of(
|
|
232
|
+
table["shell"], dotted + ".shell", _SHELL_VALUES, errors
|
|
233
|
+
)
|
|
234
|
+
if "live_paths" in table:
|
|
235
|
+
_check_str_list(
|
|
236
|
+
table["live_paths"], dotted + ".live_paths", errors
|
|
237
|
+
)
|
|
238
|
+
if "members" not in data:
|
|
239
|
+
errors.append("members: at least one member is required")
|
|
240
|
+
elif not isinstance(data["members"], dict):
|
|
241
|
+
errors.append(
|
|
242
|
+
"members: expected table, got %s" % _type_name(data["members"])
|
|
243
|
+
)
|
|
244
|
+
elif not data["members"]:
|
|
245
|
+
errors.append("members: at least one member is required")
|
|
246
|
+
else:
|
|
247
|
+
for member_id, table in data["members"].items():
|
|
248
|
+
dotted = "members.%s" % member_id
|
|
249
|
+
if not _NAME_RE.match(member_id):
|
|
250
|
+
errors.append(
|
|
251
|
+
"%s: must match ^[a-z0-9][a-z0-9_-]*$" % dotted
|
|
252
|
+
)
|
|
253
|
+
if not isinstance(table, dict):
|
|
254
|
+
errors.append(
|
|
255
|
+
"%s: expected table, got %s" % (dotted, _type_name(table))
|
|
256
|
+
)
|
|
257
|
+
continue
|
|
258
|
+
for key in table:
|
|
259
|
+
if key not in _MEMBER_FIELDS:
|
|
260
|
+
warnings.append("unknown key '%s.%s' ignored" % (dotted, key))
|
|
261
|
+
for key in ("role", "base"):
|
|
262
|
+
if key not in table:
|
|
263
|
+
errors.append("%s.%s: required" % (dotted, key))
|
|
264
|
+
elif not isinstance(table[key], str):
|
|
265
|
+
errors.append(
|
|
266
|
+
"%s.%s: expected str, got %s"
|
|
267
|
+
% (dotted, key, _type_name(table[key]))
|
|
268
|
+
)
|
|
269
|
+
elif not table[key].strip():
|
|
270
|
+
errors.append(
|
|
271
|
+
"%s.%s: must not be empty" % (dotted, key)
|
|
272
|
+
)
|
|
273
|
+
_check_opt_str(table, "path", dotted, errors)
|
|
274
|
+
_check_opt_str(table, "remote", dotted, errors)
|
|
275
|
+
if "forge" in table:
|
|
276
|
+
_check_one_of(
|
|
277
|
+
table["forge"], dotted + ".forge", _FORGE_VALUES, errors
|
|
278
|
+
)
|
|
279
|
+
if "host" in table:
|
|
280
|
+
if not isinstance(table["host"], str):
|
|
281
|
+
errors.append(
|
|
282
|
+
"%s.host: expected str, got %s"
|
|
283
|
+
% (dotted, _type_name(table["host"]))
|
|
284
|
+
)
|
|
285
|
+
elif table["host"] != "local" and table["host"] not in host_names:
|
|
286
|
+
errors.append(
|
|
287
|
+
"%s.host: unknown host '%s'" % (dotted, table["host"])
|
|
288
|
+
)
|
|
289
|
+
errors.sort()
|
|
290
|
+
warnings.sort()
|
|
291
|
+
return (errors, warnings)
|