134 lines
6.0 KiB
Python
134 lines
6.0 KiB
Python
"""Encode a qualified private group plan with the existing Db2Bake producer.
|
|
|
|
This produces a raw SpellClassOptions artifact only. It neither qualifies new
|
|
membership nor composes a store/provider or authorizes installation.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
|
|
|
|
def sha(path):
|
|
with Path(path).open('rb') as stream:
|
|
return hashlib.file_digest(stream, 'sha256').hexdigest()
|
|
|
|
|
|
def load(path):
|
|
return json.loads(Path(path).read_text())
|
|
|
|
|
|
def save(path, value):
|
|
Path(path).write_text(json.dumps(value, indent=2) + '\n')
|
|
|
|
|
|
def rows(path):
|
|
result = {}
|
|
for line in Path(path).read_text().splitlines():
|
|
columns = line.split('\t')
|
|
key = int(columns[0])
|
|
record = {}
|
|
for cell in columns[1:]:
|
|
name, value = cell.split('=', 1)
|
|
record[name] = [int(x) for x in value.split(',')] if name == 'SpellClassMask' else int(value)
|
|
if record.get('ID') != key or key in result:
|
|
raise ValueError('duplicate or inconsistent physical row ID')
|
|
result[key] = record
|
|
return result
|
|
|
|
|
|
def build(args):
|
|
plan = load(args.plan)
|
|
if plan.get('installable') is not False or plan.get('incomplete'):
|
|
raise ValueError('expected complete qualified metadata rehearsal, not an installable package')
|
|
for name, digest in plan['pins'].items():
|
|
if sha(name) != digest:
|
|
raise ValueError('qualified plan dependency changed: ' + name)
|
|
if plan['pins'].get(str(args.parent_tsv.resolve())) != sha(args.parent_tsv):
|
|
raise ValueError('parent TSV is not the qualified selected table')
|
|
lineage = load(args.parent_receipt)
|
|
parent_sha = sha(args.parent_db2)
|
|
if lineage.get('after', {}).get('979663') != parent_sha:
|
|
raise ValueError('cached DB2 does not match accepted selected physical readback')
|
|
before = rows(args.parent_tsv)
|
|
expected = {}
|
|
for record in plan['native_inventory']:
|
|
key = record['ID']
|
|
if key in expected:
|
|
raise ValueError('duplicate final plan row')
|
|
expected[key] = record
|
|
if not before.keys() <= expected.keys():
|
|
raise ValueError('group plan removes existing rows')
|
|
added = [expected[key] for key in sorted(expected.keys() - before.keys())]
|
|
changed = [expected[key] for key in sorted(before) if expected[key] != before[key]]
|
|
if not added or not changed:
|
|
raise ValueError('expected additive group plan with existing-row changes')
|
|
output = args.output.resolve()
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
for name in ('parent', 'intermediate', 'child'):
|
|
(output / name).mkdir(exist_ok=True)
|
|
parent = output / 'parent' / 'SpellClassOptions.db2'
|
|
child = output / 'child' / 'SpellClassOptions.db2'
|
|
if args.parent_db2.resolve() in (parent, child):
|
|
raise ValueError('input cannot be an output path')
|
|
shutil.copyfile(args.parent_db2, parent)
|
|
save(output / 'changed.json', changed)
|
|
save(output / 'added.json', added)
|
|
calls = []
|
|
for mode, source, changes, destination in (
|
|
('setrows', parent.parent, output / 'changed.json', output / 'intermediate' / 'SpellClassOptions.db2'),
|
|
('addrows', output / 'intermediate', output / 'added.json', child),
|
|
):
|
|
command = ['dotnet', str(args.tool), mode, str(source), str(args.definitions),
|
|
'SpellClassOptions', str(changes), str(destination)]
|
|
with (output / (mode + '.log')).open('w') as stream:
|
|
subprocess.run(command, stdout=stream, stderr=subprocess.STDOUT, check=True)
|
|
message = (output / (mode + '.log')).read_text().strip()
|
|
if not message.endswith('roundtripBad=0'):
|
|
raise ValueError('existing producer did not report a clean full reload')
|
|
calls.append({'command': command, 'result': message})
|
|
with (output / 'child' / 'SpellClassOptions.tsv').open('w') as stream:
|
|
subprocess.run(['dotnet', str(args.tool), 'dump', str(child.parent), str(args.definitions),
|
|
'SpellClassOptions', '1.60.1.70334'], stdout=stream, check=True)
|
|
actual = rows(output / 'child' / 'SpellClassOptions.tsv')
|
|
if actual != expected:
|
|
mismatched = sorted(key for key in actual.keys() | expected.keys()
|
|
if actual.get(key) != expected.get(key))
|
|
save(output / 'mismatches.json', mismatched)
|
|
raise ValueError('physical readback differs from qualified plan')
|
|
if sha(parent) != parent_sha:
|
|
raise ValueError('rollback preimage changed')
|
|
receipt = {
|
|
'status': 'raw physical encoding passed; not installable', 'fdid': 979663,
|
|
'plan_sha256': sha(args.plan), 'accepted_parent_receipt_sha256': sha(args.parent_receipt),
|
|
'accepted_parent_content': lineage.get('child'),
|
|
'parent_sha256': parent_sha, 'child_sha256': sha(child),
|
|
'child_md5': hashlib.md5(child.read_bytes()).hexdigest(),
|
|
'before_rows': len(before), 'after_rows': len(actual),
|
|
'added_rows': len(added), 'changed_existing_rows': len(changed),
|
|
'all_decoded_rows_equal_qualified_plan': True,
|
|
'rollback_preimage': str(parent), 'rollback_preimage_sha256': sha(parent),
|
|
'tool_sha256': sha(args.tool),
|
|
'definition_sha256': sha(args.definitions / 'SpellClassOptions.dbd'),
|
|
'producer_calls': calls,
|
|
'remaining': ['paired native group runtime', 'matching normalized provider rows',
|
|
'signed-parent store composition and inverse', 'native acceptance',
|
|
'all operation-specific qualification holds'],
|
|
}
|
|
save(output / 'receipt.json', receipt)
|
|
print(f"Raw SpellClassOptions PASS: {len(before)} -> {len(actual)} rows; {len(added)} additions, {len(changed)} existing changes")
|
|
return receipt
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
for option in ('plan', 'parent-db2', 'parent-tsv', 'parent-receipt', 'tool', 'definitions', 'output'):
|
|
parser.add_argument('--' + option, type=Path, required=True)
|
|
return build(parser.parse_args())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|