Files
buryan-infra/ops/work/tools/build_catalog.py
T

164 lines
6.3 KiB
Python

#!/usr/bin/env python3
"""Единый каталог устройств Yealink + граф совместимости.
Правила:
- устройства не склеиваем без доказательств (VCC22 ≠ UVC84)
- роль выводится из названия и контекста источника
- граф строится только из официальных источников
"""
import glob
import json
import os
import re
import sys
BASE = "/opt/work-hub/compat"
OUT = "/opt/work-hub/compat"
ROLE_RULES = [
(r"^MeetingEye|^MeetingBar|^MeetingBoard|^MCore|^MVC|^PVB|^PVT|^Roomcast|^A\d{2}$", "codec"),
(r"^UVC\d|VCC\d|^SmartVision", "camera"),
(r"^VCM\d|^CM\d|^CS\d|^CPW\d|^MSpeech|^CPE\d|^CP\d{2,3}|^PA\d", "audio"),
(r"^MSpeaker", "speaker"),
(r"^CTP\d|^MTouch", "touch"),
(r"^WPP\d|^VCH\d|^AVBridge|^AP\d|^AVHub", "sharing_or_hub"),
(r"^VCR\d", "remote"),
(r"Wall|Mount|Bracket|Stand", "mount"),
(r"^WH\d|^YHS\d|^UH\d", "headset"),
]
def role_of(name):
for pat, role in ROLE_RULES:
if re.match(pat, name, re.I):
return role
if re.search(r"mount|bracket|stand", name, re.I):
return "mount"
return "unknown"
def load_matrix():
"""Возвращает список хостов с аксессуарами из матрицы."""
hosts = []
for f in glob.glob(os.path.join(BASE, "matrix", "*.yaml")):
if os.path.basename(f).startswith("_"):
continue
import yaml
d = yaml.safe_load(open(f, encoding="utf-8"))
c = (d or {}).get("compatibility") or {}
if not c.get("device"):
continue
hosts.append({
"host": c["device"], "firmware": c.get("firmware"),
"accessories": [{k: a.get(k) for k in
("model", "supported", "max_qty", "note", "usable")}
for a in c.get("accessories") or []],
})
return hosts
def load_product_family():
"""Каталог моделей по сериям из rooms-product-family."""
import yaml
f = glob.glob(os.path.join(BASE, "raw", "rooms-product-family-*.yaml"))
if not f:
return []
d = yaml.safe_load(open(f[0], encoding="utf-8"))
models = []
for table in d.get("tables") or []:
for row in table:
for cell in row:
if not cell:
continue
for m in re.findall(r"[A-Z][A-Za-z0-9 \-/]{2,30}", cell):
m = m.strip()
if len(m) > 2 and role_of(m) != "unknown":
models.append(m)
return sorted(set(models))
def load_conflicts():
"""Пары (хост, аксессуар) с расхождением источников — исключаются из подбора."""
import yaml
f = os.path.join(BASE, "reconciled.yaml")
if not os.path.exists(f):
return set()
d = yaml.safe_load(open(f, encoding="utf-8")) or {}
out = set()
for h in d.get("hosts") or []:
for a in h.get("accessories") or []:
if a.get("conflict"):
out.add((str(h.get("host", "")).strip().lower(),
str(a.get("model", "")).strip().lower()))
return out
def main():
import yaml
hosts = load_matrix()
family = load_product_family()
conflicts = load_conflicts()
print("пар с расхождением источников: %d" % len(conflicts))
# каталог: все устройства, которые встретились
catalog = {}
for h in hosts:
catalog.setdefault(h["host"].lower(), {"name": h["host"], "role": role_of(h["host"]),
"seen_in": set()})
catalog[h["host"].lower()]["seen_in"].add("matrix")
for a in h["accessories"]:
m = (a.get("model") or "").strip()
if not m:
continue
entry = catalog.setdefault(m.lower(), {"name": m, "role": role_of(m), "seen_in": set()})
entry["seen_in"].add("matrix")
for m in family:
entry = catalog.setdefault(m.lower(), {"name": m, "role": role_of(m), "seen_in": set()})
entry["seen_in"].add("product_family")
for c in catalog.values():
c["seen_in"] = sorted(c["seen_in"])
c["aliases"] = [] # заполняется только с доказательствами
# граф
edges = []
for h in hosts:
for a in h["accessories"]:
edges.append({
"from": h["host"], "to": a.get("model"),
"kind": "supports",
"supported": a.get("supported"),
"max_qty": a.get("max_qty"),
"condition": (a.get("note") or "")[:200] or None,
"usable": (False if (h["host"].strip().lower(),
(a.get("model") or "").strip().lower()) in conflicts
else (a.get("usable") if a.get("usable") is not None
else not a.get("conflict", False))),
"source": "matrix",
})
os.makedirs(OUT, exist_ok=True)
with open(os.path.join(OUT, "catalog.yaml"), "w", encoding="utf-8") as f:
yaml.safe_dump({"vendor": "yealink",
"devices": sorted(catalog.values(), key=lambda x: x["name"].lower())},
f, allow_unicode=True, sort_keys=False, width=160)
with open(os.path.join(OUT, "graph.yaml"), "w", encoding="utf-8") as f:
yaml.safe_dump({"vendor": "yealink", "edges": edges},
f, allow_unicode=True, sort_keys=False, width=200)
roles = {}
for c in catalog.values():
roles[c["role"]] = roles.get(c["role"], 0) + 1
print("устройств в каталоге: %d" % len(catalog))
print(" по ролям:", dict(sorted(roles.items(), key=lambda x: -x[1])))
print("рёбер в графе: %d" % len(edges))
usable = sum(1 for e in edges if e["usable"])
print(" пригодны: %d | исключены (конфликт): %d" % (usable, len(edges) - usable))
print("\nкаталог:", os.path.join(OUT, "catalog.yaml"))
print("граф: ", os.path.join(OUT, "graph.yaml"))
return 0
if __name__ == "__main__":
sys.exit(main())