1panel-toolkit 0.3.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.
- 1panel_toolkit-0.3.0.dist-info/METADATA +407 -0
- 1panel_toolkit-0.3.0.dist-info/RECORD +47 -0
- 1panel_toolkit-0.3.0.dist-info/WHEEL +4 -0
- 1panel_toolkit-0.3.0.dist-info/entry_points.txt +2 -0
- 1panel_toolkit-0.3.0.dist-info/licenses/LICENSE +21 -0
- p1toolkit/__init__.py +3 -0
- p1toolkit/checks/__init__.py +21 -0
- p1toolkit/checks/compose.py +188 -0
- p1toolkit/checks/forms.py +129 -0
- p1toolkit/checks/hygiene.py +73 -0
- p1toolkit/checks/i18n.py +140 -0
- p1toolkit/checks/lessons.py +111 -0
- p1toolkit/checks/structure.py +140 -0
- p1toolkit/checks/substance.py +55 -0
- p1toolkit/cli.py +890 -0
- p1toolkit/config.py +114 -0
- p1toolkit/dependencies.py +218 -0
- p1toolkit/deploy.py +302 -0
- p1toolkit/depth.py +414 -0
- p1toolkit/errors.py +18 -0
- p1toolkit/evidence.py +523 -0
- p1toolkit/fix.py +394 -0
- p1toolkit/gen.py +728 -0
- p1toolkit/lifecycle.py +143 -0
- p1toolkit/loader.py +110 -0
- p1toolkit/logo.py +174 -0
- p1toolkit/model.py +71 -0
- p1toolkit/net.py +81 -0
- p1toolkit/panel.py +154 -0
- p1toolkit/patterns/__init__.py +250 -0
- p1toolkit/patterns/catalog.py +271 -0
- p1toolkit/patterns/data/patterns.json +724 -0
- p1toolkit/patterns/extract.py +402 -0
- p1toolkit/remote.py +554 -0
- p1toolkit/report.py +96 -0
- p1toolkit/resolve/__init__.py +554 -0
- p1toolkit/resolve/analyze.py +329 -0
- p1toolkit/resolve/sources.py +306 -0
- p1toolkit/rules/__init__.py +99 -0
- p1toolkit/rules/data/decisions.yaml +132 -0
- p1toolkit/rules/data/dependencies.yaml +113 -0
- p1toolkit/rules/data/envkeys.yaml +19 -0
- p1toolkit/rules/data/form-design.yaml +59 -0
- p1toolkit/rules/data/i18n.yaml +354 -0
- p1toolkit/rules/data/lessons.yaml +84 -0
- p1toolkit/rules/data/store.yaml +171 -0
- p1toolkit/selftest.py +211 -0
p1toolkit/selftest.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"""Acceptance checks that run against the installed package.
|
|
2
|
+
|
|
3
|
+
`p1 selftest` answers one question: does this installation actually work, with
|
|
4
|
+
its bundled rule data and pattern library present, on a machine that has none
|
|
5
|
+
of the author's corpora? It generates a package, checks it, and exercises the
|
|
6
|
+
pure functions that the network-dependent commands rely on.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import tempfile
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from . import __version__, rules
|
|
16
|
+
from .checks import run_all
|
|
17
|
+
from .depth import assess, profile_app
|
|
18
|
+
from .evidence import audit
|
|
19
|
+
from .gen import generate
|
|
20
|
+
from .loader import load_package
|
|
21
|
+
from .net import classify, parse_ports
|
|
22
|
+
from .panel import PanelClient
|
|
23
|
+
from .patterns import load_bundled, patterns
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class Check:
|
|
28
|
+
name: str
|
|
29
|
+
ok: bool
|
|
30
|
+
detail: str = ""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass
|
|
34
|
+
class SelfTestResult:
|
|
35
|
+
checks: list[Check] = field(default_factory=list)
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def ok(self) -> bool:
|
|
39
|
+
return all(check.ok for check in self.checks)
|
|
40
|
+
|
|
41
|
+
def add(self, name: str, ok: bool, detail: str = "") -> None:
|
|
42
|
+
self.checks.append(Check(name, ok, detail))
|
|
43
|
+
|
|
44
|
+
def render(self) -> str:
|
|
45
|
+
lines = [f"1panel-toolkit {__version__} 自检"]
|
|
46
|
+
for check in self.checks:
|
|
47
|
+
mark = "ok " if check.ok else "FAIL"
|
|
48
|
+
lines.append(f" {mark} {check.name}" + (f" — {check.detail}" if check.detail else ""))
|
|
49
|
+
failed = [c for c in self.checks if not c.ok]
|
|
50
|
+
lines.append(
|
|
51
|
+
f"-> {len(self.checks) - len(failed)}/{len(self.checks)} 通过"
|
|
52
|
+
if not failed
|
|
53
|
+
else f"-> {len(failed)} 项失败: {', '.join(c.name for c in failed)}"
|
|
54
|
+
)
|
|
55
|
+
return "\n".join(lines)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
DEMO_SPEC = {
|
|
59
|
+
"key": "selftest-demo",
|
|
60
|
+
"name": "SelfTest Demo",
|
|
61
|
+
"description": "由 p1 selftest 生成的最小应用",
|
|
62
|
+
"shortDescEn": "Minimal app generated by p1 selftest",
|
|
63
|
+
"version": "latest",
|
|
64
|
+
"type": "tool",
|
|
65
|
+
"tags": ["Tool"],
|
|
66
|
+
"port": 10000,
|
|
67
|
+
"services": [
|
|
68
|
+
{
|
|
69
|
+
"name": "selftest-demo",
|
|
70
|
+
"image": "nginx:1.27-alpine",
|
|
71
|
+
"container_port": 80,
|
|
72
|
+
"mounts": [{"host": "./data/html", "target": "/usr/share/nginx/html"}],
|
|
73
|
+
"env": {
|
|
74
|
+
"NGINX_ENTRYPOINT_QUIET_LOGS": "1",
|
|
75
|
+
"DEMO_API_KEY": "",
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
],
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def run() -> SelfTestResult:
|
|
83
|
+
result = SelfTestResult()
|
|
84
|
+
|
|
85
|
+
try:
|
|
86
|
+
loaded = {name: rules.load(name) for name in
|
|
87
|
+
("store", "i18n", "envkeys", "lessons", "decisions",
|
|
88
|
+
"dependencies", "form-design")}
|
|
89
|
+
result.add("规则数据可加载", all(loaded.values()), f"{len(loaded)} 个文件")
|
|
90
|
+
except Exception as exc: # noqa: BLE001 - the point is to report, not raise
|
|
91
|
+
result.add("规则数据可加载", False, f"{type(exc).__name__}: {exc}")
|
|
92
|
+
|
|
93
|
+
try:
|
|
94
|
+
data = load_bundled()
|
|
95
|
+
items = patterns(data)
|
|
96
|
+
with_examples = [p for p in items if p.examples]
|
|
97
|
+
result.add(
|
|
98
|
+
"写法库已随包分发",
|
|
99
|
+
len(items) >= 20 and len(with_examples) >= 15,
|
|
100
|
+
f"{len(items)} 条,其中 {len(with_examples)} 条带官方示例",
|
|
101
|
+
)
|
|
102
|
+
except Exception as exc: # noqa: BLE001
|
|
103
|
+
result.add("写法库已随包分发", False, f"{type(exc).__name__}: {exc}")
|
|
104
|
+
|
|
105
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
106
|
+
root = Path(tmp)
|
|
107
|
+
try:
|
|
108
|
+
gen = generate(dict(DEMO_SPEC), root)
|
|
109
|
+
package = load_package(gen.root)
|
|
110
|
+
report = run_all(package)
|
|
111
|
+
result.add(
|
|
112
|
+
"生成 → 校验无失败项",
|
|
113
|
+
report.ok,
|
|
114
|
+
f"{len(gen.files)} 个文件,表单 {len(gen.exposed)} 字段"
|
|
115
|
+
+ (f";{report.render().splitlines()[-1]}" if report.issues else ""),
|
|
116
|
+
)
|
|
117
|
+
except Exception as exc: # noqa: BLE001
|
|
118
|
+
result.add("生成 → 校验无失败项", False, f"{type(exc).__name__}: {exc}")
|
|
119
|
+
gen = None
|
|
120
|
+
|
|
121
|
+
if gen is not None:
|
|
122
|
+
try:
|
|
123
|
+
verdict = assess(profile_app(gen.root), [])
|
|
124
|
+
result.add(
|
|
125
|
+
"生成物不是模板复制品",
|
|
126
|
+
verdict.status == "adapted",
|
|
127
|
+
verdict.status,
|
|
128
|
+
)
|
|
129
|
+
except Exception as exc: # noqa: BLE001
|
|
130
|
+
result.add("生成物不是模板复制品", False, f"{type(exc).__name__}: {exc}")
|
|
131
|
+
|
|
132
|
+
try:
|
|
133
|
+
# The gate is only real if it ships with the rules it reads.
|
|
134
|
+
gapped = dict(DEMO_SPEC)
|
|
135
|
+
gapped["services"] = [dict(DEMO_SPEC["services"][0], image="")]
|
|
136
|
+
gapped["decisions"] = {"D06": {"status": "missing", "answer": "—"}}
|
|
137
|
+
blocked = audit(gapped)
|
|
138
|
+
result.add(
|
|
139
|
+
"证据闸门拦得住缺证据的 spec",
|
|
140
|
+
not blocked.ok and "D06" in blocked.blocked_decisions,
|
|
141
|
+
f"blocked={blocked.blocked_decisions}",
|
|
142
|
+
)
|
|
143
|
+
except Exception as exc: # noqa: BLE001
|
|
144
|
+
result.add("证据闸门拦得住缺证据的 spec", False, f"{type(exc).__name__}: {exc}")
|
|
145
|
+
|
|
146
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
147
|
+
try:
|
|
148
|
+
wired = dict(DEMO_SPEC)
|
|
149
|
+
wired["key"] = "selftest-wired"
|
|
150
|
+
wired["dependencies"] = [
|
|
151
|
+
{
|
|
152
|
+
"kind": "postgresql",
|
|
153
|
+
"env": {
|
|
154
|
+
"DATABASE_HOST": "${PANEL_DB_HOST}",
|
|
155
|
+
"DATABASE_URL": (
|
|
156
|
+
"postgres://${PANEL_DB_USER}:${PANEL_DB_USER_PASSWORD}"
|
|
157
|
+
"@${PANEL_DB_HOST}:${PANEL_DB_PORT}/${PANEL_DB_NAME}"
|
|
158
|
+
),
|
|
159
|
+
},
|
|
160
|
+
}
|
|
161
|
+
]
|
|
162
|
+
wired["data_owner"] = "1000:1000"
|
|
163
|
+
wired["config_files"] = [{"path": "data/app.conf", "content": "debug=false\n"}]
|
|
164
|
+
gen = generate(dict(wired), Path(tmp))
|
|
165
|
+
report = run_all(load_package(gen.root))
|
|
166
|
+
fields = (gen.root / "latest" / "data.yml").read_text(encoding="utf-8")
|
|
167
|
+
result.add(
|
|
168
|
+
"依赖接线(D10)与脚本/模板(D09)能生成可校验的包",
|
|
169
|
+
report.ok
|
|
170
|
+
and "PANEL_DB_TYPE" in fields
|
|
171
|
+
and (gen.root / "latest" / "scripts" / "init.sh").exists()
|
|
172
|
+
and (gen.root / "latest" / "data" / "app.conf").exists(),
|
|
173
|
+
f"{report.to_dict()['counts']}",
|
|
174
|
+
)
|
|
175
|
+
except Exception as exc: # noqa: BLE001
|
|
176
|
+
result.add(
|
|
177
|
+
"依赖接线(D10)与脚本/模板(D09)能生成可校验的包",
|
|
178
|
+
False,
|
|
179
|
+
f"{type(exc).__name__}: {exc}",
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
try:
|
|
183
|
+
assert parse_ports("10000,15000-15001") == [10000, 15000, 15001]
|
|
184
|
+
refused = classify("example.test", 10000, ConnectionRefusedError())
|
|
185
|
+
silent = classify("example.test", 8080, TimeoutError())
|
|
186
|
+
result.add(
|
|
187
|
+
"端口判定(closed 可用 / filtered 被拦)",
|
|
188
|
+
refused.usable and not silent.usable,
|
|
189
|
+
f"{refused.state} / {silent.state}",
|
|
190
|
+
)
|
|
191
|
+
except Exception as exc: # noqa: BLE001
|
|
192
|
+
result.add("端口判定(closed 可用 / filtered 被拦)", False, str(exc))
|
|
193
|
+
|
|
194
|
+
try:
|
|
195
|
+
# A deliberately fake vector: this file ships in the wheel, so it must
|
|
196
|
+
# never carry a real panel key.
|
|
197
|
+
signature = PanelClient.sign(SIGN_VECTOR_KEY, SIGN_VECTOR_TIME)
|
|
198
|
+
result.add(
|
|
199
|
+
"1Panel v2 签名算法",
|
|
200
|
+
signature == SIGN_VECTOR_EXPECTED,
|
|
201
|
+
signature,
|
|
202
|
+
)
|
|
203
|
+
except Exception as exc: # noqa: BLE001
|
|
204
|
+
result.add("1Panel v2 签名算法", False, str(exc))
|
|
205
|
+
|
|
206
|
+
return result
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
SIGN_VECTOR_KEY = "0123456789abcdef0123456789abcdef"
|
|
210
|
+
SIGN_VECTOR_TIME = 1789862903
|
|
211
|
+
SIGN_VECTOR_EXPECTED = "bd9418ddeb51422f30c89aa2c3bc5468"
|