awcap 0.1.0__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.
- awcap/__init__.py +6 -0
- awcap/__main__.py +8 -0
- awcap/_doctor.py +199 -0
- awcap/_selftest.py +88 -0
- awcap/cli.py +204 -0
- awcap/descriptor.py +314 -0
- awcap/index.py +54 -0
- awcap/py.typed +0 -0
- awcap/scaffold.py +342 -0
- awcap-0.1.0.dist-info/METADATA +117 -0
- awcap-0.1.0.dist-info/RECORD +15 -0
- awcap-0.1.0.dist-info/WHEEL +5 -0
- awcap-0.1.0.dist-info/entry_points.txt +2 -0
- awcap-0.1.0.dist-info/licenses/LICENSE +202 -0
- awcap-0.1.0.dist-info/top_level.txt +1 -0
awcap/__init__.py
ADDED
awcap/__main__.py
ADDED
awcap/_doctor.py
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"""Stack-aware `doctor` for awcap.
|
|
2
|
+
|
|
3
|
+
GENERATED BY gen_aw_doctor.py -- DO NOT EDIT.
|
|
4
|
+
Regenerate it with the generator named above; a hand-edit here is reverted by
|
|
5
|
+
the next run and fails the parity gate.
|
|
6
|
+
|
|
7
|
+
Why a doctor exists at all: the aw* bricks are designed to COMPOSE, so the
|
|
8
|
+
interesting failures live BETWEEN them. "awcap is installed" is not the useful
|
|
9
|
+
fact -- "awcap is installed and the thing it pairs with is not" is. This reports
|
|
10
|
+
the whole stack, not just itself.
|
|
11
|
+
|
|
12
|
+
stdlib only, on purpose: a diagnostic that cannot run because a dependency is
|
|
13
|
+
missing is worthless precisely when you need it.
|
|
14
|
+
"""
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import importlib.util
|
|
18
|
+
import os
|
|
19
|
+
import shutil
|
|
20
|
+
import sys
|
|
21
|
+
|
|
22
|
+
#: Frozen from the Aither World registry at generation time. A shipped
|
|
23
|
+
#: package cannot read the registry, and a doctor that guessed at the family
|
|
24
|
+
#: would go stale in silence. Regenerate to update.
|
|
25
|
+
SELF = 'awcap'
|
|
26
|
+
FAMILY = [
|
|
27
|
+
'awask', 'awavatar', 'awbac', 'awbrain', 'awbrowse', 'awcams', 'awclassify', 'awdecide',
|
|
28
|
+
'awdeck', 'awdelphi', 'awdiscord', 'awdit', 'awembed', 'awevolve', 'awfind', 'awflow',
|
|
29
|
+
'awfocus', 'awgit', 'awgraph', 'awgym', 'awiam', 'awkno', 'awm', 'awmail', 'awmine', 'awnboard',
|
|
30
|
+
'awnest', 'awnet', 'awpermit', 'awplay', 'awpool', 'awpredict', 'awprism', 'awprove',
|
|
31
|
+
'awreason', 'awrecover', 'awrecurse', 'awrelay', 'awrena', 'awrepl', 'awreport', 'awresearch',
|
|
32
|
+
'awrise', 'awrouter', 'awrtifact', 'awrun', 'awscope', 'awscreen', 'awseal', 'awsettings',
|
|
33
|
+
'awshare', 'awsprite', 'awstorage', 'awsuite', 'awswarm', 'awtax', 'awtoll', 'awtunnel',
|
|
34
|
+
'awvision', 'awvoice', 'awwall', 'gawbbonet',
|
|
35
|
+
]
|
|
36
|
+
PAIRS_WITH = ['adk', 'awflow', 'awgit', 'awm', 'awrelay']
|
|
37
|
+
|
|
38
|
+
#: This brick's OWN config, read out of its source at generation time.
|
|
39
|
+
#: ENV_REQUIRED is `os.environ["X"]` -- absent, that is a KeyError the moment
|
|
40
|
+
#: the line runs. ENV_OPTIONAL is `os.getenv("X")`, which returns None and lets
|
|
41
|
+
#: the caller cope. Only this brick's namespace is listed: reporting the
|
|
42
|
+
#: platform-wide vars it also touches would be noise, and a doctor that floods
|
|
43
|
+
#: gets ignored.
|
|
44
|
+
ENV_REQUIRED = []
|
|
45
|
+
ENV_OPTIONAL = []
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _installed(mod: str) -> "str | None":
|
|
49
|
+
"""Version if importable, else None. Never raises -- a broken sibling must
|
|
50
|
+
not take the diagnostic down with it."""
|
|
51
|
+
try:
|
|
52
|
+
if importlib.util.find_spec(mod) is None:
|
|
53
|
+
return None
|
|
54
|
+
except (ImportError, ValueError):
|
|
55
|
+
return None
|
|
56
|
+
try:
|
|
57
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
58
|
+
try:
|
|
59
|
+
return version(mod)
|
|
60
|
+
except PackageNotFoundError:
|
|
61
|
+
return "installed"
|
|
62
|
+
except Exception:
|
|
63
|
+
return "installed"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def report(out=None) -> int:
|
|
67
|
+
"""Print the stack picture. 0 = this brick and its pairs are present."""
|
|
68
|
+
out = out or sys.stdout
|
|
69
|
+
print(f"{SELF} doctor", file=out)
|
|
70
|
+
|
|
71
|
+
mine = _installed(SELF)
|
|
72
|
+
print(f" self {SELF} {mine or 'NOT IMPORTABLE'}", file=out)
|
|
73
|
+
shim = shutil.which(SELF)
|
|
74
|
+
print(f" command {shim or 'not on PATH'}", file=out)
|
|
75
|
+
|
|
76
|
+
# The stack. Siblings this brick pairs with are called out separately,
|
|
77
|
+
# because a missing pair is a REASON, while a missing unrelated brick is
|
|
78
|
+
# just a fact about your machine.
|
|
79
|
+
missing_pairs, present = [], []
|
|
80
|
+
for name in FAMILY:
|
|
81
|
+
v = _installed(name)
|
|
82
|
+
if v:
|
|
83
|
+
present.append(name)
|
|
84
|
+
elif name in PAIRS_WITH:
|
|
85
|
+
missing_pairs.append(name)
|
|
86
|
+
print(f" stack {len(present)}/{len(FAMILY)} aw* packages installed",
|
|
87
|
+
file=out)
|
|
88
|
+
if present:
|
|
89
|
+
print(f" {' '.join(sorted(present))}", file=out)
|
|
90
|
+
|
|
91
|
+
missing_req = [v for v in ENV_REQUIRED if not os.environ.get(v)]
|
|
92
|
+
if ENV_REQUIRED or ENV_OPTIONAL:
|
|
93
|
+
have = sum(1 for v in ENV_REQUIRED + ENV_OPTIONAL if os.environ.get(v))
|
|
94
|
+
total = len(ENV_REQUIRED) + len(ENV_OPTIONAL)
|
|
95
|
+
print(f" config {have}/{total} of this brick's own vars set", file=out)
|
|
96
|
+
if missing_req:
|
|
97
|
+
# Not a preference. os.environ[...] raises the moment it runs.
|
|
98
|
+
print(f" MISSING REQUIRED: {' '.join(missing_req)}", file=out)
|
|
99
|
+
|
|
100
|
+
local = _local_checks()
|
|
101
|
+
for line in local:
|
|
102
|
+
print(f" {line}", file=out)
|
|
103
|
+
problems, unjudged = _local_verdict()
|
|
104
|
+
|
|
105
|
+
if mine is None:
|
|
106
|
+
print(f"\nverdict: {SELF} itself is not importable. Reinstall it before "
|
|
107
|
+
f"anything else here means much.", file=out)
|
|
108
|
+
return 1
|
|
109
|
+
if missing_req:
|
|
110
|
+
print(f"\nverdict: {SELF} is missing required config "
|
|
111
|
+
f"({', '.join(missing_req)}). Those are read with os.environ[...], "
|
|
112
|
+
f"so the code path that needs them raises rather than degrades.",
|
|
113
|
+
file=out)
|
|
114
|
+
return 1
|
|
115
|
+
# A measured NO outranks a missing optional pair: awrise printed
|
|
116
|
+
# "nothing wakes run-due" and still exited 0 because the missing-pairs
|
|
117
|
+
# branch returned first (measured 2026-09-20).
|
|
118
|
+
if problems:
|
|
119
|
+
for p in problems:
|
|
120
|
+
print(f"\nverdict: {p}", file=out)
|
|
121
|
+
return 1
|
|
122
|
+
if unjudged:
|
|
123
|
+
for u in unjudged:
|
|
124
|
+
print(f"\nverdict: UNJUDGED -- could not judge: {u}", file=out)
|
|
125
|
+
return 2
|
|
126
|
+
if missing_pairs:
|
|
127
|
+
print(f"\nverdict: {SELF} works, but pairs with "
|
|
128
|
+
f"{', '.join(sorted(missing_pairs))} which "
|
|
129
|
+
f"{'is' if len(missing_pairs) == 1 else 'are'} not installed. "
|
|
130
|
+
f"That is a capability you are missing, not an error.", file=out)
|
|
131
|
+
return 0
|
|
132
|
+
print(f"\nverdict: {SELF} and everything it pairs with are present.", file=out)
|
|
133
|
+
return 0
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _local_verdict() -> tuple:
|
|
137
|
+
"""The brick's OWN verdict, folded into the exit code.
|
|
138
|
+
|
|
139
|
+
Without this the local lines are DECORATION. Measured 2026-09-20: awrise
|
|
140
|
+
printed "hostclock NOT INSTALLED -- nothing wakes run-due" and "last tick
|
|
141
|
+
never", then exited 0 -- teaching an operator that a doctor exit code
|
|
142
|
+
carries no information. A brick opts in with `_doctor_local_verdict()`
|
|
143
|
+
returning (problems, unjudged); a brick without one is unaffected.
|
|
144
|
+
"""
|
|
145
|
+
try:
|
|
146
|
+
mod = importlib.import_module(f"{SELF}.doctor_local")
|
|
147
|
+
problems, unjudged = mod._doctor_local_verdict()
|
|
148
|
+
except Exception: # noqa: BLE001
|
|
149
|
+
return [], []
|
|
150
|
+
return list(problems or []), list(unjudged or [])
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _local_checks() -> "list[str]":
|
|
154
|
+
"""Per-brick checks, if this package defines them.
|
|
155
|
+
|
|
156
|
+
Kept as a HOOK rather than generated guesses: the generator knows the family
|
|
157
|
+
from the registry, but it does not know what awcap needs at runtime, and a
|
|
158
|
+
doctor that invented config requirements would be confidently wrong. A
|
|
159
|
+
package supplies `_doctor_local()` returning display lines; absent, the
|
|
160
|
+
stack picture above still stands on its own.
|
|
161
|
+
"""
|
|
162
|
+
try:
|
|
163
|
+
mod = importlib.import_module(f"{SELF}.doctor_local")
|
|
164
|
+
except Exception:
|
|
165
|
+
return []
|
|
166
|
+
try:
|
|
167
|
+
return list(mod._doctor_local())
|
|
168
|
+
except Exception as exc: # noqa: BLE001
|
|
169
|
+
return [f"local checks raised {type(exc).__name__}: {exc}"]
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def main(argv: "list[str] | None" = None) -> int:
|
|
173
|
+
# --self-test delegates to a SIBLING module when one exists.
|
|
174
|
+
#
|
|
175
|
+
# This file is generated and a fresh run replaces it, so a self-test
|
|
176
|
+
# written HERE is deleted by the next regeneration. awdelphi learned that
|
|
177
|
+
# the expensive way: 125 lines exercising four real failure paths --
|
|
178
|
+
# convergence, roster anonymization, resume, gateway-down -- lived in this
|
|
179
|
+
# file and were destroyed by a routine regeneration, silently, leaving a
|
|
180
|
+
# --self-test flag that reported PASS while asserting nothing.
|
|
181
|
+
#
|
|
182
|
+
# So the seam is a separate module the generator never writes. A package
|
|
183
|
+
# with real machinery to prove puts it in _selftest.py; everything else
|
|
184
|
+
# keeps the honest answer below rather than a self-test that only ever
|
|
185
|
+
# passes.
|
|
186
|
+
argv = list(argv if argv is not None else __import__("sys").argv[1:])
|
|
187
|
+
if "--self-test" in argv:
|
|
188
|
+
try:
|
|
189
|
+
from . import _selftest as _st
|
|
190
|
+
except Exception:
|
|
191
|
+
print("no _selftest module: this doctor reports the stack, and has",
|
|
192
|
+
"no machinery of its own to prove")
|
|
193
|
+
return 0
|
|
194
|
+
return int(_st.run())
|
|
195
|
+
return report()
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
if __name__ == "__main__":
|
|
199
|
+
raise SystemExit(main())
|
awcap/_selftest.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""`awcap --self-test`: prove every exit code still fires, on throwaway files.
|
|
2
|
+
|
|
3
|
+
A checker whose failure path nobody has watched fire is indistinguishable from
|
|
4
|
+
one that always passes. Each case below asserts the exit code AND, for a
|
|
5
|
+
violation, that the problem is named in the output.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import io
|
|
10
|
+
import json
|
|
11
|
+
import sys
|
|
12
|
+
import tempfile
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
VALID = """\
|
|
16
|
+
awcap: 1
|
|
17
|
+
brick: demo
|
|
18
|
+
capabilities:
|
|
19
|
+
- id: demo.echo
|
|
20
|
+
surfaces:
|
|
21
|
+
cli: {argv: [demo, echo, "{text}"]}
|
|
22
|
+
python: {module: demo.core, callable: echo}
|
|
23
|
+
input: {text: str, times: "int?"}
|
|
24
|
+
output: {text: str}
|
|
25
|
+
effects: read
|
|
26
|
+
state: live
|
|
27
|
+
checks: [demo.echo.smoke]
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
LIVE_NO_CHECKS = VALID.replace("checks: [demo.echo.smoke]", "checks: []")
|
|
31
|
+
BAD_EFFECTS = VALID.replace("effects: read", "effects: maybe")
|
|
32
|
+
BAD_YAML = "awcap: 1\ncapabilities: [unclosed\n"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _cases(tmp: Path) -> list[tuple[str, list[str], int, str]]:
|
|
36
|
+
def put(rel: str, text: str) -> str:
|
|
37
|
+
p = tmp / rel
|
|
38
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
39
|
+
p.write_text(text, encoding="utf-8")
|
|
40
|
+
return str(p)
|
|
41
|
+
|
|
42
|
+
valid = put("good/awcap.yaml", VALID)
|
|
43
|
+
live = put("live/awcap.yaml", LIVE_NO_CHECKS)
|
|
44
|
+
eff = put("eff.awcap.yaml", BAD_EFFECTS)
|
|
45
|
+
bad_yaml = put("yaml/awcap.yaml", BAD_YAML)
|
|
46
|
+
(tmp / "empty").mkdir()
|
|
47
|
+
out_ok = str(tmp / "index.json")
|
|
48
|
+
out_bad = str(tmp / "index-bad.json")
|
|
49
|
+
return [
|
|
50
|
+
("valid descriptor", ["check", valid], 0, "VALID"),
|
|
51
|
+
("live without checks", ["check", live], 1, "requires a non-empty checks"),
|
|
52
|
+
("unknown effects", ["check", eff], 1, "effects"),
|
|
53
|
+
("missing file", ["check", str(tmp / "nope.yaml")], 2, "UNREADABLE"),
|
|
54
|
+
("not YAML", ["check", bad_yaml], 2, "UNREADABLE"),
|
|
55
|
+
("--all on an empty dir", ["check", "--all", str(tmp / "empty")], 2, "empty"),
|
|
56
|
+
("--all on a valid tree", ["check", "--all", str(tmp / "good")], 0, "1 valid"),
|
|
57
|
+
("--all with a violation", ["check", "--all", str(tmp)], 1, "invalid"),
|
|
58
|
+
("index of valid", ["index", "--out", out_ok, valid], 0, "wrote"),
|
|
59
|
+
("index refuses invalid", ["index", "--out", out_bad, valid, live], 1, "NOT written"),
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def run(out=None) -> int:
|
|
64
|
+
from awcap.cli import main
|
|
65
|
+
|
|
66
|
+
out = out or sys.stdout
|
|
67
|
+
failures = 0
|
|
68
|
+
with tempfile.TemporaryDirectory(prefix="awcap-selftest-") as d:
|
|
69
|
+
tmp = Path(d)
|
|
70
|
+
for name, argv, want, needle in _cases(tmp):
|
|
71
|
+
buf = io.StringIO()
|
|
72
|
+
got = main(argv, out=buf)
|
|
73
|
+
text = buf.getvalue()
|
|
74
|
+
ok = got == want and needle in text
|
|
75
|
+
failures += 0 if ok else 1
|
|
76
|
+
print(f" {'ok' if ok else 'FAIL'} {name}: exit {got} (want {want})", file=out)
|
|
77
|
+
if not ok:
|
|
78
|
+
print(" " + text.replace("\n", "\n "), file=out)
|
|
79
|
+
idx = tmp / "index.json"
|
|
80
|
+
written = idx.exists() and json.loads(idx.read_text(encoding="utf-8"))
|
|
81
|
+
ok = bool(written) and written.get("capability_count") == 1
|
|
82
|
+
failures += 0 if ok else 1
|
|
83
|
+
print(f" {'ok' if ok else 'FAIL'} index content: one capability", file=out)
|
|
84
|
+
ok = not (tmp / "index-bad.json").exists()
|
|
85
|
+
failures += 0 if ok else 1
|
|
86
|
+
print(f" {'ok' if ok else 'FAIL'} refused index left no file", file=out)
|
|
87
|
+
print("awcap self-test: " + ("PASS" if not failures else f"{failures} FAILED"), file=out)
|
|
88
|
+
return 0 if not failures else 1
|
awcap/cli.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"""awcap command line.
|
|
2
|
+
|
|
3
|
+
awcap check FILE validate one descriptor; print surfaces + schemas
|
|
4
|
+
awcap check --all DIR validate every awcap.yaml under DIR
|
|
5
|
+
awcap index --out FILE F... merge descriptors into one JSON index
|
|
6
|
+
awcap scaffold BRICK draft a descriptor for an installed brick
|
|
7
|
+
awcap doctor what is installed around awcap
|
|
8
|
+
awcap --self-test prove each exit code still fires
|
|
9
|
+
|
|
10
|
+
Exit codes everywhere: 0 clean, 1 violation (each problem named), 2 could not
|
|
11
|
+
judge (unreadable input, nothing found). Silence is never a 0.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import argparse
|
|
16
|
+
import json
|
|
17
|
+
import sys
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
from awcap import __version__
|
|
21
|
+
from awcap.descriptor import Result, check_file, compile_capability, find_descriptors
|
|
22
|
+
|
|
23
|
+
_SURFACE_ORDER = ("cli", "python", "mcp", "relay")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _surface_text(name: str, body: dict | None) -> str:
|
|
27
|
+
if not body:
|
|
28
|
+
return "-"
|
|
29
|
+
if name == "cli":
|
|
30
|
+
return " ".join(body.get("argv", []))
|
|
31
|
+
if name == "python":
|
|
32
|
+
return f"{body.get('module')}:{body.get('callable')}"
|
|
33
|
+
if name == "mcp":
|
|
34
|
+
return str(body.get("tool"))
|
|
35
|
+
return f"{body.get('channel')} (kind {body.get('kind')})"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _schema_line(schema: dict) -> str:
|
|
39
|
+
slim = {k: v for k, v in schema.items() if k != "$schema"}
|
|
40
|
+
return json.dumps(slim, sort_keys=True, separators=(",", ":"))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _print_problems(res: Result, out) -> None:
|
|
44
|
+
if res.unreadable is not None:
|
|
45
|
+
print(f"UNREADABLE: {res.unreadable}", file=out)
|
|
46
|
+
return
|
|
47
|
+
print(f"INVALID: {len(res.problems)} problem(s) in {res.path}", file=out)
|
|
48
|
+
for p in res.problems:
|
|
49
|
+
print(f" x {p}", file=out)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def cmd_check(args, out) -> int:
|
|
53
|
+
if args.all:
|
|
54
|
+
return cmd_check_all(Path(args.target), args, out)
|
|
55
|
+
res = check_file(args.target)
|
|
56
|
+
if args.json:
|
|
57
|
+
print(json.dumps(res.as_dict(), indent=2, sort_keys=True), file=out)
|
|
58
|
+
return res.exit_code
|
|
59
|
+
if res.exit_code:
|
|
60
|
+
_print_problems(res, out)
|
|
61
|
+
return res.exit_code
|
|
62
|
+
doc = res.doc or {}
|
|
63
|
+
caps = doc["capabilities"]
|
|
64
|
+
print(f"awcap check: {res.path}", file=out)
|
|
65
|
+
print(f"brick: {doc['brick']} ({len(caps)} capabilities)", file=out)
|
|
66
|
+
for cap in caps:
|
|
67
|
+
c = compile_capability(cap)
|
|
68
|
+
checks = ", ".join(c["checks"]) or "none"
|
|
69
|
+
print(f"\n {c['id']} [{c['state']}, {c['effects']}] checks: {checks}", file=out)
|
|
70
|
+
for s in _SURFACE_ORDER:
|
|
71
|
+
print(f" {s + ':':8} {_surface_text(s, c['surfaces'].get(s))}", file=out)
|
|
72
|
+
print(f" input: {_schema_line(c['input_schema'])}", file=out)
|
|
73
|
+
print(f" output: {_schema_line(c['output_schema'])}", file=out)
|
|
74
|
+
print("\nVALID", file=out)
|
|
75
|
+
return 0
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def cmd_check_all(root: Path, args, out) -> int:
|
|
79
|
+
if not root.is_dir():
|
|
80
|
+
print(f"COULD NOT JUDGE: {root} is not a directory", file=out)
|
|
81
|
+
return 2
|
|
82
|
+
files = find_descriptors(root)
|
|
83
|
+
if not files:
|
|
84
|
+
print(f"COULD NOT JUDGE: no awcap.yaml under {root} -- empty is not a pass", file=out)
|
|
85
|
+
return 2
|
|
86
|
+
results = [check_file(f) for f in files]
|
|
87
|
+
if args.json:
|
|
88
|
+
print(json.dumps([r.as_dict() for r in results], indent=2, sort_keys=True), file=out)
|
|
89
|
+
else:
|
|
90
|
+
for r in results:
|
|
91
|
+
mark = {0: "ok", 1: "x ", 2: "? "}[r.exit_code]
|
|
92
|
+
print(f"{mark} {r.path}", file=out)
|
|
93
|
+
if r.exit_code:
|
|
94
|
+
for p in r.problems or [r.unreadable or ""]:
|
|
95
|
+
print(f" {p}", file=out)
|
|
96
|
+
bad = sum(1 for r in results if r.exit_code == 1)
|
|
97
|
+
dead = sum(1 for r in results if r.exit_code == 2)
|
|
98
|
+
if not args.json:
|
|
99
|
+
print(f"{len(results)} descriptor(s): {len(results) - bad - dead} valid, "
|
|
100
|
+
f"{bad} invalid, {dead} unreadable", file=out)
|
|
101
|
+
if bad:
|
|
102
|
+
return 1
|
|
103
|
+
return 2 if dead else 0
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def cmd_index(args, out) -> int:
|
|
107
|
+
from awcap.index import build_index, write_index
|
|
108
|
+
|
|
109
|
+
if not args.files:
|
|
110
|
+
print("COULD NOT JUDGE: no descriptor files given", file=out)
|
|
111
|
+
return 2
|
|
112
|
+
index, results, problems = build_index(args.files)
|
|
113
|
+
for r in results:
|
|
114
|
+
if r.exit_code:
|
|
115
|
+
_print_problems(r, out)
|
|
116
|
+
for p in problems:
|
|
117
|
+
print(f" x {p}", file=out)
|
|
118
|
+
if index is None:
|
|
119
|
+
worst = max([r.exit_code for r in results] + [1 if problems else 0])
|
|
120
|
+
print(f"index NOT written to {args.out}", file=out)
|
|
121
|
+
return 1 if (problems or worst == 1) else 2
|
|
122
|
+
write_index(index, args.out)
|
|
123
|
+
print(f"wrote {args.out}: {len(index['bricks'])} brick(s), "
|
|
124
|
+
f"{index['capability_count']} capability(ies)", file=out)
|
|
125
|
+
return 0
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def cmd_scaffold(args, out) -> int:
|
|
129
|
+
from awcap.scaffold import render, scaffold, self_check
|
|
130
|
+
|
|
131
|
+
rep = scaffold(args.brick)
|
|
132
|
+
for n in rep.notes:
|
|
133
|
+
print(f"note: {n}", file=sys.stderr)
|
|
134
|
+
if rep.descriptor is None:
|
|
135
|
+
print(f"COULD NOT SCAFFOLD {args.brick}", file=out)
|
|
136
|
+
return 2
|
|
137
|
+
text = render(rep)
|
|
138
|
+
problems = self_check(text)
|
|
139
|
+
if problems:
|
|
140
|
+
print("scaffold produced an invalid descriptor (a bug in awcap):", file=out)
|
|
141
|
+
for p in problems:
|
|
142
|
+
print(f" x {p}", file=out)
|
|
143
|
+
return 1
|
|
144
|
+
if args.out == "-":
|
|
145
|
+
out.write(text)
|
|
146
|
+
return 0
|
|
147
|
+
dest = Path(args.out)
|
|
148
|
+
if dest.exists() and not args.force:
|
|
149
|
+
print(f"refusing to overwrite {dest} (pass --force)", file=out)
|
|
150
|
+
return 2
|
|
151
|
+
dest.write_text(text, encoding="utf-8")
|
|
152
|
+
n = len(rep.descriptor["capabilities"])
|
|
153
|
+
print(f"wrote {dest}: {n} capability(ies), all planned", file=out)
|
|
154
|
+
return 0
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def cmd_doctor(_args, out) -> int:
|
|
158
|
+
from awcap import _doctor
|
|
159
|
+
|
|
160
|
+
return _doctor.report(out)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
164
|
+
ap = argparse.ArgumentParser(prog="awcap", description=__doc__.split("\n\n")[0])
|
|
165
|
+
ap.add_argument("--version", action="version", version=f"awcap {__version__}")
|
|
166
|
+
ap.add_argument("--self-test", action="store_true", help="prove each exit code fires")
|
|
167
|
+
sub = ap.add_subparsers(dest="cmd")
|
|
168
|
+
|
|
169
|
+
c = sub.add_parser("check", help="validate a descriptor (or --all DIR)")
|
|
170
|
+
c.add_argument("target", help="descriptor file, or a directory with --all")
|
|
171
|
+
c.add_argument("--all", action="store_true", help="walk TARGET for awcap.yaml files")
|
|
172
|
+
c.add_argument("--json", action="store_true", help="machine-readable output")
|
|
173
|
+
|
|
174
|
+
i = sub.add_parser("index", help="merge descriptors into one JSON index")
|
|
175
|
+
i.add_argument("--out", required=True, help="index file to write")
|
|
176
|
+
i.add_argument("files", nargs="*", help="descriptor files")
|
|
177
|
+
|
|
178
|
+
s = sub.add_parser("scaffold", help="draft a descriptor for an installed brick")
|
|
179
|
+
s.add_argument("brick", help="installed package / console script name")
|
|
180
|
+
s.add_argument("--out", default="awcap.yaml", help="file to write, or - for stdout")
|
|
181
|
+
s.add_argument("--force", action="store_true", help="overwrite an existing file")
|
|
182
|
+
|
|
183
|
+
sub.add_parser("doctor", help="what is installed around awcap")
|
|
184
|
+
return ap
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def main(argv: list[str] | None = None, out=None) -> int:
|
|
188
|
+
out = out or sys.stdout
|
|
189
|
+
ap = build_parser()
|
|
190
|
+
args = ap.parse_args(argv)
|
|
191
|
+
if args.self_test:
|
|
192
|
+
from awcap._selftest import run
|
|
193
|
+
|
|
194
|
+
return run(out)
|
|
195
|
+
handlers = {"check": cmd_check, "index": cmd_index, "scaffold": cmd_scaffold,
|
|
196
|
+
"doctor": cmd_doctor}
|
|
197
|
+
if args.cmd not in handlers:
|
|
198
|
+
ap.print_help(out)
|
|
199
|
+
return 2
|
|
200
|
+
return handlers[args.cmd](args, out)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
if __name__ == "__main__":
|
|
204
|
+
raise SystemExit(main())
|