"""Pārbauda sasaistes/*.xml: - atbilst XSD sasaistes-0.1.xsd (saites unikālas); - datnes nosaukums ir iestādes VPK ID (atribūts institution); - saite sākas ar šī repozitorija objektu (FROM_TYPE); - attiecība der abiem galiem (implements -> fdk; supported_by -> information_system; uses -> information_resource; maintains: information_system -> information_resource; same_as -> arch); - fdk ieraksts pieder tai pašai iestādei. Vai objekti (VIRSIS ID, B22 ieraksti) tiešām eksistē, pārbauda VDE ielādes laikā (pārbaude confirmed_link_resolves). Lietošana: python3 tools/validate_links.py """ import sys from pathlib import Path from lxml import etree ROOT = Path(__file__).resolve().parent.parent FROM_TYPE = "service" NS = {"s": "urn:pppa:vpk:sasaistes:0.1"} ALLOWED = {"implements": ({"service"}, {"fdk"}), "supported_by": ({"service"}, {"information_system"}), "uses": ({"service"}, {"information_resource"}), "maintains": ({"information_system"}, {"information_resource"}), "same_as": ({"service", "information_system", "information_resource"}, {"arch"})} def check(folder): errors, n = [], 0 schema = etree.XMLSchema(etree.parse(str(ROOT / "sasaistes-0.1.xsd"))) for f in sorted(folder.glob("*.xml")): doc = etree.parse(str(f)) if not schema.validate(doc): errors += [f"{f.name}: XSD {e.message} (rinda {e.line})" for e in schema.error_log] continue inst = doc.getroot().get("institution") if f.stem != inst: errors.append(f"{f.name}: datnes nosaukumam jābūt {inst}.xml") for el in doc.getroot().findall("s:Link", NS): n += 1 ft, fid = el.get("from").split(":", 1) tt, tid = el.get("to").split(":", 1) rel = el.get("relation") if ft != FROM_TYPE: errors.append(f"{f.name}: {el.get('from')} — šajā repozitorijā saites sākas ar {FROM_TYPE}") if ft not in ALLOWED[rel][0] or tt not in ALLOWED[rel][1]: errors.append(f"{f.name}: {rel} neder {ft} -> {tt}") if tt == "fdk" and tid[2:9] != inst: errors.append(f"{f.name}: {tid} nav iestādes {inst} ieraksts") return errors, n if __name__ == "__main__": errs, n = check(Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT / "sasaistes") for e in errs: print("E ", e) print(f"I {n} saites\n\n{len(errs)} kļūdas") sys.exit(1 if errs else 0)