import json, sys, glob, os
# tkvdiff.py DIR_A DIR_B: per fingerprint file, the split-K table cells that differ between two boots' persisted tune tables.
a, b = sys.argv[1:3]
for f in sorted(glob.glob(a + '/splits-*.json')):
    n = os.path.basename(f); ja = json.load(open(f)); g = os.path.join(b, n)
    print(n, 'fingerprint', ja['fingerprint'], 'generation', ja.get('generation'))
    if not os.path.exists(g): print('  missing in', b); continue
    jb = json.load(open(g))
    for c in sorted(set(ja['table']) | set(jb['table']), key=lambda s: tuple(int(x) for x in s.split(':'))):
        ta, tb = ja['table'].get(c), jb['table'].get(c)
        ra, rb = ja['resolution'].get(c, {}), jb['resolution'].get(c, {})
        mark = 'DIFF' if ta != tb else '    '
        print('  %s %-10s %-28s %-28s resolved %s/%s margin %s/%s' % (mark, c, ta, tb, ra.get('resolved'), rb.get('resolved'),
              None if ra.get('margin_pct') is None else round(ra['margin_pct'], 2), None if rb.get('margin_pct') is None else round(rb['margin_pct'], 2)))
