]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-cxtm.py
f2a848d8c78cd2b3f0c2fc063d33a6b21786824b
[topic-maps.git] / scripts / check-cxtm.py
1 #!/usr/bin/env python3
2 """Independent raw UTF-8 comparison, XML reference integrity and C14N checks."""
3 import json
4 import pathlib
5 import sys
6 import xml.etree.ElementTree as ET
7 root = pathlib.Path(__file__).resolve().parent.parent
8 report = json.loads(pathlib.Path(sys.argv[1]).read_text())
9 assert report['kind'] == 'cxtm-byte-equality' and report['failed'] == 0
10 assert report['passed'] == len(report['checks']) == len(set(report['checks']))
11 assert len(report['cases']) == 24
12 for case in report['cases']:
13 actual = case['actualCXTM'].encode('utf-8')
14 expected = (root / 'vendor/cxtm/ctm/baseline' / (pathlib.Path(case['id']).name + '.cxtm')).read_bytes()
15 assert actual == expected == case['expectedCXTM'].encode('utf-8'), case['id']
16 assert case['byteCount'] == len(actual) and case['status'] == 'pass'
17 assert case['baseLocator'].endswith('/' + case['id'])
18 # CXTM 3.1 adds an LF after the root. XML reparsing drops that Infoset
19 # sibling; this check is separate from the unmodified byte comparison.
20 assert actual.endswith(b'</topicMap>\n') and not actual.startswith(b'\xef\xbb\xbf')
21 assert ET.canonicalize(case['actualCXTM']) + '\n' == case['actualCXTM'], case['id']
22 xml = ET.fromstring(actual)
23 topics = xml.findall('topic'); associations = xml.findall('association')
24 assert [t.attrib['number'] for t in topics] == list(map(str,range(1,len(topics)+1)))
25 role_refs = {}
26 for i, a in enumerate(associations,1):
27 assert a.attrib['number'] == str(i)
28 for j, r in enumerate(a.findall('role'),1):
29 assert r.attrib['number'] == str(j)
30 role_refs[f'association.{i}.role.{j}'] = r.find('player').attrib['topicref']
31 for t in topics:
32 for r in t.findall('rolePlayed'):
33 assert role_refs[r.attrib['ref']] == t.attrib['number']
34 assert sum(len(t.findall('rolePlayed')) for t in topics) == len(role_refs)
35 for ref in xml.iter():
36 if 'topicref' in ref.attrib:
37 assert 1 <= int(ref.attrib['topicref']) <= len(topics)
38 print('CXTM-INDEPENDENT-PASS: 24 unchanged baselines, exact UTF-8 bytes, canonical XML and reference integrity')