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
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
"""Pull real snippets out of official app packages.
|
|
2
|
+
|
|
3
|
+
Nothing here writes YAML by hand: every snippet is either raw bytes from a
|
|
4
|
+
store package or a re-serialisation of a parsed block, so a pattern always
|
|
5
|
+
shows something 1Panel already accepts.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any, Callable
|
|
13
|
+
|
|
14
|
+
import yaml
|
|
15
|
+
|
|
16
|
+
from ..model import Version
|
|
17
|
+
from .catalog import CATALOG, PatternDef
|
|
18
|
+
|
|
19
|
+
MAX_SNIPPET = 2600
|
|
20
|
+
MAX_LINES = 48
|
|
21
|
+
EXAMPLES_PER_PATTERN = 3
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class AppCtx:
|
|
26
|
+
key: str
|
|
27
|
+
root: Path
|
|
28
|
+
app_data: dict[str, Any]
|
|
29
|
+
app_text: str
|
|
30
|
+
readme: str = ""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass
|
|
34
|
+
class Hit:
|
|
35
|
+
app: str
|
|
36
|
+
version: str
|
|
37
|
+
file: str
|
|
38
|
+
snippet: str
|
|
39
|
+
score: float = 0.0
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass
|
|
43
|
+
class Detector:
|
|
44
|
+
"""A detector either inspects the app once or each of its versions."""
|
|
45
|
+
|
|
46
|
+
scope: str
|
|
47
|
+
func: Callable[[AppCtx, Version | None], list[str]]
|
|
48
|
+
file: str = "data.yml"
|
|
49
|
+
score: Callable[[str], float] | None = None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _dump(value: Any) -> str:
|
|
53
|
+
return yaml.safe_dump(value, allow_unicode=True, sort_keys=False, width=100).strip()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _trim(text: str, lines: int = MAX_LINES) -> str:
|
|
57
|
+
text = text.replace("\r\n", "\n").strip("\n")
|
|
58
|
+
parts = text.split("\n")
|
|
59
|
+
if len(parts) > lines:
|
|
60
|
+
return "\n".join(parts[:lines]) + f"\n# ... (truncated, {len(parts)} lines total)"
|
|
61
|
+
return text
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _read(path: Path) -> str:
|
|
65
|
+
try:
|
|
66
|
+
return path.read_text(encoding="utf-8-sig", errors="replace")
|
|
67
|
+
except OSError:
|
|
68
|
+
return ""
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _services(version: Version) -> dict[str, Any]:
|
|
72
|
+
return {
|
|
73
|
+
name: svc
|
|
74
|
+
for name, svc in version.services.items()
|
|
75
|
+
if isinstance(svc, dict)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _env_text(service: dict[str, Any]) -> str:
|
|
80
|
+
parts: list[str] = []
|
|
81
|
+
env = service.get("environment")
|
|
82
|
+
if isinstance(env, dict):
|
|
83
|
+
env = [f"{k}={v}" for k, v in env.items()]
|
|
84
|
+
for item in env or []:
|
|
85
|
+
parts.append(str(item))
|
|
86
|
+
return "\n".join(parts)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _fields(version: Version) -> list[dict[str, Any]]:
|
|
90
|
+
return version.form_fields
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# --------------------------------------------------------------------------
|
|
94
|
+
# detectors: app scope
|
|
95
|
+
# --------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _root_declaration(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
99
|
+
return [ctx.app_text] if ctx.app_text.strip() else []
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _readme_style(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
103
|
+
return [ctx.readme] if "## 产品介绍" in ctx.readme else []
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# --------------------------------------------------------------------------
|
|
107
|
+
# detectors: version scope
|
|
108
|
+
# --------------------------------------------------------------------------
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _version_form(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
112
|
+
if version is None or not version.form_fields:
|
|
113
|
+
return []
|
|
114
|
+
path = version.files.get("data.yml")
|
|
115
|
+
return [_read(path)] if path else []
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _field_selector(predicate: Callable[[dict[str, Any]], bool]):
|
|
119
|
+
def detector(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
120
|
+
if version is None:
|
|
121
|
+
return []
|
|
122
|
+
return [_dump(f) for f in _fields(version) if predicate(f)]
|
|
123
|
+
|
|
124
|
+
return detector
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _is_service_db(field: dict[str, Any]) -> bool:
|
|
128
|
+
if field.get("type") != "service":
|
|
129
|
+
return False
|
|
130
|
+
return str(field.get("envKey") or "").startswith(("PANEL_DB", "PANEL_REDIS", "REDIS"))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _is_apps_child(field: dict[str, Any]) -> bool:
|
|
134
|
+
child = field.get("child")
|
|
135
|
+
return (
|
|
136
|
+
field.get("type") == "apps"
|
|
137
|
+
and isinstance(child, dict)
|
|
138
|
+
and child.get("type") == "service"
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _compose_text(version: Version, lines: int) -> list[str]:
|
|
143
|
+
if not version.compose_text.strip():
|
|
144
|
+
return []
|
|
145
|
+
return [_trim(version.compose_text, lines)]
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _compose_single(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
149
|
+
if version is None:
|
|
150
|
+
return []
|
|
151
|
+
return _compose_text(version, 40) if len(_services(version)) == 1 else []
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _compose_multi(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
155
|
+
if version is None:
|
|
156
|
+
return []
|
|
157
|
+
return _compose_text(version, 60) if len(_services(version)) > 1 else []
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _ref_count(snippet: str, prefix: str) -> float:
|
|
161
|
+
"""How many distinct variables of a family a snippet actually wires up."""
|
|
162
|
+
return float(len({ref for ref, _ in _env_refs(snippet) if ref.startswith(prefix)}))
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _env_refs(text: str):
|
|
166
|
+
import re
|
|
167
|
+
|
|
168
|
+
for match in re.finditer(r"\$\{([A-Za-z_][A-Za-z0-9_]*)([^}]*)\}", text):
|
|
169
|
+
yield match.group(1), match.group(2)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _label_language_count(snippet: str) -> float:
|
|
173
|
+
try:
|
|
174
|
+
parsed = yaml.safe_load(snippet)
|
|
175
|
+
except yaml.YAMLError:
|
|
176
|
+
return 0.0
|
|
177
|
+
label = (parsed or {}).get("label")
|
|
178
|
+
return float(len(label)) if isinstance(label, dict) else 0.0
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _compose_with(needles: tuple[str, ...], required: tuple[str, ...] = (), lines: int = 30):
|
|
182
|
+
def detector(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
183
|
+
if version is None:
|
|
184
|
+
return []
|
|
185
|
+
out: list[str] = []
|
|
186
|
+
for name, service in _services(version).items():
|
|
187
|
+
env = _env_text(service)
|
|
188
|
+
if env and any(needle in env for needle in needles) and all(
|
|
189
|
+
needle in env for needle in required
|
|
190
|
+
):
|
|
191
|
+
out.append(
|
|
192
|
+
_trim(
|
|
193
|
+
_dump(
|
|
194
|
+
{
|
|
195
|
+
"services": {
|
|
196
|
+
name: {
|
|
197
|
+
"image": service.get("image"),
|
|
198
|
+
"environment": service.get("environment"),
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
),
|
|
203
|
+
lines,
|
|
204
|
+
)
|
|
205
|
+
)
|
|
206
|
+
return out
|
|
207
|
+
|
|
208
|
+
return detector
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _compose_healthcheck(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
212
|
+
if version is None:
|
|
213
|
+
return []
|
|
214
|
+
out = []
|
|
215
|
+
for name, service in _services(version).items():
|
|
216
|
+
if service.get("healthcheck"):
|
|
217
|
+
out.append(
|
|
218
|
+
_dump(
|
|
219
|
+
{
|
|
220
|
+
"services": {
|
|
221
|
+
name: {
|
|
222
|
+
"image": service.get("image"),
|
|
223
|
+
"healthcheck": service.get("healthcheck"),
|
|
224
|
+
"depends_on": service.get("depends_on"),
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
)
|
|
229
|
+
)
|
|
230
|
+
return out
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _is_named_volume(entry: str) -> bool:
|
|
234
|
+
if ":" not in entry:
|
|
235
|
+
return False
|
|
236
|
+
host = entry.split(":", 1)[0].strip()
|
|
237
|
+
return not host.startswith((".", "/", "$"))
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _compose_named_volume(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
241
|
+
if version is None:
|
|
242
|
+
return []
|
|
243
|
+
out = []
|
|
244
|
+
for name, service in _services(version).items():
|
|
245
|
+
volumes = [v for v in (service.get("volumes") or []) if isinstance(v, str)]
|
|
246
|
+
named = [v for v in volumes if _is_named_volume(v)]
|
|
247
|
+
if named:
|
|
248
|
+
out.append(
|
|
249
|
+
_dump(
|
|
250
|
+
{
|
|
251
|
+
"services": {name: {"image": service.get("image"), "volumes": named}},
|
|
252
|
+
"volumes": {
|
|
253
|
+
v.split(":", 1)[0]: None for v in named
|
|
254
|
+
},
|
|
255
|
+
}
|
|
256
|
+
)
|
|
257
|
+
)
|
|
258
|
+
return out
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def _compose_data_dir(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
262
|
+
if version is None:
|
|
263
|
+
return []
|
|
264
|
+
out = []
|
|
265
|
+
for name, service in _services(version).items():
|
|
266
|
+
volumes = [
|
|
267
|
+
v
|
|
268
|
+
for v in (service.get("volumes") or [])
|
|
269
|
+
if isinstance(v, str) and v.strip().startswith((".", "$"))
|
|
270
|
+
]
|
|
271
|
+
if volumes:
|
|
272
|
+
out.append(
|
|
273
|
+
_dump({"services": {name: {"volumes": volumes}}})
|
|
274
|
+
)
|
|
275
|
+
return out
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _compose_external_network(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
279
|
+
if version is None:
|
|
280
|
+
return []
|
|
281
|
+
networks = version.compose.get("networks") or {}
|
|
282
|
+
if not isinstance(networks, dict):
|
|
283
|
+
return []
|
|
284
|
+
external = {
|
|
285
|
+
name: cfg
|
|
286
|
+
for name, cfg in networks.items()
|
|
287
|
+
if isinstance(cfg, dict) and cfg.get("external")
|
|
288
|
+
}
|
|
289
|
+
if not external:
|
|
290
|
+
return []
|
|
291
|
+
attachment = {
|
|
292
|
+
name: service.get("networks")
|
|
293
|
+
for name, service in _services(version).items()
|
|
294
|
+
if service.get("networks")
|
|
295
|
+
}
|
|
296
|
+
return [
|
|
297
|
+
_dump(
|
|
298
|
+
{
|
|
299
|
+
"services": {name: {"networks": nets} for name, nets in attachment.items()},
|
|
300
|
+
"networks": external,
|
|
301
|
+
}
|
|
302
|
+
)
|
|
303
|
+
]
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def _raw_script(name: str, lines: int = 40):
|
|
307
|
+
def detector(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
308
|
+
if version is None:
|
|
309
|
+
return []
|
|
310
|
+
path = version.files.get(f"scripts/{name}")
|
|
311
|
+
return [_trim(_read(path), lines)] if path else []
|
|
312
|
+
|
|
313
|
+
return detector
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _env_sample(ctx: AppCtx, version: Version | None) -> list[str]:
|
|
317
|
+
if version is None or not version.env_sample:
|
|
318
|
+
return []
|
|
319
|
+
return [_trim(version.env_sample, 40)]
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
DETECTORS: dict[str, Detector] = {
|
|
323
|
+
"root_declaration": Detector("app", _root_declaration),
|
|
324
|
+
"readme_style": Detector("app", _readme_style, "README.md"),
|
|
325
|
+
"version_form": Detector("version", _version_form, "{version}/data.yml"),
|
|
326
|
+
"ff_service_db": Detector("version", _field_selector(_is_service_db)),
|
|
327
|
+
"ff_apps_child": Detector("version", _field_selector(_is_apps_child)),
|
|
328
|
+
"ff_port": Detector(
|
|
329
|
+
"version", _field_selector(lambda f: str(f.get("envKey") or "").startswith("PANEL_APP_PORT_"))
|
|
330
|
+
),
|
|
331
|
+
"ff_password": Detector("version", _field_selector(lambda f: f.get("type") == "password")),
|
|
332
|
+
"ff_text_random": Detector(
|
|
333
|
+
"version",
|
|
334
|
+
_field_selector(lambda f: f.get("type") == "text" and bool(f.get("random"))),
|
|
335
|
+
),
|
|
336
|
+
"ff_select": Detector(
|
|
337
|
+
"version",
|
|
338
|
+
_field_selector(lambda f: f.get("type") == "select" and bool(f.get("values"))),
|
|
339
|
+
),
|
|
340
|
+
"ff_i18n_label": Detector(
|
|
341
|
+
"version",
|
|
342
|
+
_field_selector(
|
|
343
|
+
lambda f: isinstance(f.get("label"), dict) and len(f["label"]) >= 5
|
|
344
|
+
),
|
|
345
|
+
score=_label_language_count,
|
|
346
|
+
),
|
|
347
|
+
"compose_single": Detector("version", _compose_single, "{version}/docker-compose.yml"),
|
|
348
|
+
"compose_multi": Detector("version", _compose_multi, "{version}/docker-compose.yml"),
|
|
349
|
+
"compose_db_env": Detector(
|
|
350
|
+
"version",
|
|
351
|
+
_compose_with(("PANEL_DB_",), required=("PANEL_DB_HOST",)),
|
|
352
|
+
"{version}/docker-compose.yml",
|
|
353
|
+
score=lambda text: _ref_count(text, "PANEL_DB_"),
|
|
354
|
+
),
|
|
355
|
+
"compose_redis_env": Detector(
|
|
356
|
+
"version",
|
|
357
|
+
_compose_with(("REDIS",), required=("REDIS_HOST",)),
|
|
358
|
+
"{version}/docker-compose.yml",
|
|
359
|
+
score=lambda text: _ref_count(text, "REDIS"),
|
|
360
|
+
),
|
|
361
|
+
"compose_healthcheck": Detector("version", _compose_healthcheck, "{version}/docker-compose.yml"),
|
|
362
|
+
"compose_named_volume": Detector("version", _compose_named_volume, "{version}/docker-compose.yml"),
|
|
363
|
+
"compose_data_dir": Detector("version", _compose_data_dir, "{version}/docker-compose.yml"),
|
|
364
|
+
"compose_external_network": Detector(
|
|
365
|
+
"version", _compose_external_network, "{version}/docker-compose.yml"
|
|
366
|
+
),
|
|
367
|
+
"scripts_init": Detector("version", _raw_script("init.sh"), "{version}/scripts/init.sh"),
|
|
368
|
+
"scripts_upgrade": Detector("version", _raw_script("upgrade.sh"), "{version}/scripts/upgrade.sh"),
|
|
369
|
+
"scripts_uninstall": Detector(
|
|
370
|
+
"version", _raw_script("uninstall.sh"), "{version}/scripts/uninstall.sh"
|
|
371
|
+
),
|
|
372
|
+
"env_sample": Detector("version", _env_sample, "{version}/.env.sample"),
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def missing_detectors() -> list[str]:
|
|
377
|
+
return [p.detector for p in CATALOG if p.detector not in DETECTORS]
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def collect(hits: dict[str, list[Hit]], definition: PatternDef, ctx: AppCtx, version: Version | None) -> None:
|
|
381
|
+
detector = DETECTORS.get(definition.detector)
|
|
382
|
+
if detector is None:
|
|
383
|
+
return
|
|
384
|
+
for snippet in detector.func(ctx, version):
|
|
385
|
+
snippet = snippet.strip()
|
|
386
|
+
if not snippet:
|
|
387
|
+
continue
|
|
388
|
+
if len(snippet) > MAX_SNIPPET:
|
|
389
|
+
snippet = snippet[:MAX_SNIPPET] + "\n# ... (truncated)"
|
|
390
|
+
score = detector.score(snippet) if detector.score else 0.0
|
|
391
|
+
if score < 0:
|
|
392
|
+
continue
|
|
393
|
+
template = DETECTORS[definition.detector].file
|
|
394
|
+
hits.setdefault(definition.id, []).append(
|
|
395
|
+
Hit(
|
|
396
|
+
app=ctx.key,
|
|
397
|
+
version=version.name if version else "-",
|
|
398
|
+
file=template.format(version=version.name if version else ""),
|
|
399
|
+
snippet=snippet,
|
|
400
|
+
score=score,
|
|
401
|
+
)
|
|
402
|
+
)
|