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

220 lines
8.8 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 = [ # ROLE_RULES2
(r"^MeetingEye|^MeetingBar|^MeetingBoard|^MCore|^MVC|^PVB|^PVT|^Roomcast|^A\d{2}$", "codec"),
(r"^UVC\d|VCC\d|^SmartVision|^MB-CAM|^MB-12X", "camera"),
(r"^VCM\d|^CM\d|^CS\d|^CPW\d|^MSpeech|^CPE\d|^CP\d{2,3}|^PA\d|Wireless Microphone|Dante or Analog Microphone", "audio"),
(r"^MSpeaker|Dante or Analog Speaker", "speaker"),
(r"^CTP\d|^MTouch", "touch"),
(r"^WPP\d|^VCH\d|^AVBridge|^AP\d|^AVHub|^BYOD-BOX|Content Sharing Dongle", "sharing_or_hub"),
(r"^VCR\d|^MB-Remote|Remote Control", "remote"),
(r"^ETV\d|MeetingDisplay", "display"),
(r"^RoomSensor|Sensor", "sensor"),
(r"^YPS\d|^Power", "power"),
(r"Room Designer|RoomConnect", "software"),
(r"^MTower|Wall|Mount|Bracket|Stand|Lift", "mount"),
(r"^WH\d|^YHS\d|^UH\d", "headset"),
(r"^USB Connector|^Host Wi-Fi|^MSketch", "accessory"),
]
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_accessories_by_host():
"""Дополнительный источник: rooms-accessories-compatibility-lists (A40/A50 и др.)."""
import yaml
f = os.path.join(BASE, "accessories_by_host.yaml")
if not os.path.exists(f):
return {}
d = yaml.safe_load(open(f, encoding="utf-8")) or {}
out = {}
for e in d.get("edges") or []:
host = (e.get("host") or "").strip()
item = (e.get("item") or "").strip()
if not host or not item:
continue
out.setdefault(host, []).append({
"model": item, "supported": bool(e.get("supported")),
"max_qty": e.get("max_qty"), "note": e.get("condition"),
"origin": "accessories_list",
})
return out
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()
extra = load_accessories_by_host()
print('хостов из accessories-list:', len(extra))
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",
})
# добавляем рёбра из accessories-list
have_pairs = {(e["from"].strip().lower(), (e["to"] or "").strip().lower())
for e in edges}
added = 0
for host, items in extra.items():
for it in items:
pair = (host.strip().lower(), it["model"].strip().lower())
if pair in have_pairs:
continue
edges.append({
"from": host, "to": it["model"], "kind": "supports",
"supported": it["supported"], "max_qty": it["max_qty"],
"condition": (it["note"] or "")[:200] or None,
"usable": True, "source": "accessories_list",
})
have_pairs.add(pair)
added += 1
entry = catalog.setdefault(it["model"].lower(),
{"name": it["model"], "role": role_of(it["model"]),
"seen_in": []})
seen = entry.get("seen_in")
if isinstance(seen, list):
if "accessories_list" not in seen:
seen.append("accessories_list")
else:
entry["seen_in"] = ["accessories_list"]
print("рёбер добавлено из accessories-list:", added)
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())