from pathlib import Path import difflib,json,hashlib,tempfile,subprocess p=Path(__file__).resolve().parent old=(p/'IMPORT-MAP.md').read_text();row=(p/'row.txt').read_text() lines=old.splitlines(True);idx=[i for i,l in enumerate(lines) if l.startswith('| 23 |')];assert len(idx)==1 assert not any(l.startswith('| 26 |') for l in lines) lines.insert(idx[0]+1,row);new=''.join(lines) assert new.replace(row,'',1)==old (p/'IMPORT-MAP.revised.md').write_text(new) patch=''.join(difflib.unified_diff(old.splitlines(True),new.splitlines(True),fromfile='a/research/IMPORT-MAP.md',tofile='b/research/IMPORT-MAP.md')) (p/'import.patch').write_text(patch) with tempfile.TemporaryDirectory() as t: r=Path(t);(r/'research').mkdir();(r/'research/IMPORT-MAP.md').write_text(old) subprocess.run(['patch','-p1','--batch','-i',str(p/'import.patch')],cwd=r,check=True,capture_output=True) assert (r/'research/IMPORT-MAP.md').read_bytes()==(p/'IMPORT-MAP.revised.md').read_bytes() out={'added_rows':1,'all_original_bytes_preserved':True,'patch_applies_exactly':True,'base_sha256':hashlib.sha256(old.encode()).hexdigest(),'revised_sha256':hashlib.sha256(new.encode()).hexdigest()} (p/'revision-check.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out))