"""unpack.py: extract the computations behind Theorem B from sources.md and outputs_*.log into OUTDIR, and check every file's SHA-256. Usage: python3 unpack.py [OUTDIR] (default: ./unpacked; standard library only; run it from this directory) The programs and their two input files go to OUTDIR/, the stored outputs to OUTDIR/outputs/. Each extracted file is checked against the "as shipped" SHA-256 recorded with it. For an output whose shipped text is the file as its run wrote it, the "original" SHA-256 is the same value, so the original file is recovered exactly; for the outputs with declared substitutions (stated in their header lines), only the shipped hash can be checked. Exits with status 1 on any mismatch. """ import hashlib, os, re, shutil, sys FENCE = '~' * 12 HERE = os.path.dirname(os.path.abspath(__file__)) def sha(b): return hashlib.sha256(b).hexdigest() def main(): out = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else 'unpacked') os.makedirs(os.path.join(out, 'outputs'), exist_ok=True) bad = 0 n_src = n_out = n_orig = 0 lines = open(os.path.join(HERE, 'sources.md'), encoding='utf-8').read().split('\n') i = 0 while i < len(lines): m = re.match(r'#### file: (\S+)$', lines[i]) if not m: i += 1 continue name = m.group(1) meta = {} i += 1 while not lines[i].startswith(FENCE): mm = re.match(r'- ([a-z ]+): (.*)$', lines[i]) if mm: meta[mm.group(1)] = mm.group(2) i += 1 i += 1 body = [] while lines[i] != FENCE: body.append(lines[i]) i += 1 text = '\n'.join(body) + ('\n' if meta['trailing newline'] == 'yes' else '') want = meta['as shipped'].split()[-1] ok = sha(text.encode('utf-8')) == want bad += not ok n_src += 1 with open(os.path.join(out, name), 'w', encoding='utf-8', newline='') as fh: fh.write(text) print('%s %s' % ('OK ' if ok else 'MISMATCH', name)) for log in sorted(f for f in os.listdir(HERE) if f.startswith('outputs_') and f.endswith('.log')): L = open(os.path.join(HERE, log), encoding='utf-8').read().split('\n') i = 0 while i < len(L): m = re.match(r'==== file: (\S+) \| original SHA-256 ([0-9a-f]{64}) \| shipped SHA-256 ([0-9a-f]{64}) \| trailing newline: (yes|no) \| (.*)$', L[i]) if not m: i += 1 continue name, orig, shipped, nl, note = m.groups() i += 1 body = [] while L[i] != '==== end: %s' % name: body.append(L[i]) i += 1 text = '\n'.join(body) + ('\n' if nl == 'yes' else '') h = sha(text.encode('utf-8')) ok = h == shipped bad += not ok n_out += 1 n_orig += ok and h == orig with open(os.path.join(out, 'outputs', name), 'w', encoding='utf-8', newline='') as fh: fh.write(text) print('%s outputs/%s (%s)' % ('OK ' if ok else 'MISMATCH', name, 'original recovered' if h == orig else note)) print('%d sources and %d outputs extracted to %s; %d outputs are the original files byte for byte; %s' % (n_src, n_out, out, n_orig, 'ALL HASHES MATCH' if bad == 0 else '%d MISMATCH(ES)' % bad)) sys.exit(1 if bad else 0) if __name__ == '__main__': main()