cocogni4d / code /tools /fixup_colon.py
VEHwang's picture
Upload CoCogni4D-Bench (questions, media, scripts, render records, code) (part 7)
7ff74f6 verified
Raw History Blame Contribute Delete
1.9 kB
#!/usr/bin/env python3
"""One-off follow-up to normalize.py: files whose colon-joined path lists (LD_LIBRARY_PATH etc.) kept local paths.
Re-applies normalize.norm_text (now splitting at ':') to them and propagates their new sha256 to any file embedding it."""
import hashlib, json, os, re, sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import normalize as N
H = N.H; DISK, SHM = N.DISK, N.SHM
sha = lambda b: hashlib.sha256(b).hexdigest()
LEFT = re.compile(rb'${LOCAL}|${LOCAL}|${LOCAL}/shm/ent400|dsw-\d+|<gateway>')
scan = {x['rel']: x for x in json.load(open(H / 'work/scan.json'))}
targets = []
for rel in scan:
if rel.startswith('code/evaluation-code/') and not rel.endswith(N.ENDPOINT_FILES): continue
p = N.where(rel)
if p is not None and LEFT.search(p.read_bytes()): targets.append(rel)
print('targets', targets)
rep = {}
for rel in targets:
p = N.where(rel); b = p.read_bytes(); nb = N.norm_text(b.decode(), rel).encode()
assert not LEFT.search(nb), rel
rep[sha(b)] = sha(nb)
root = SHM if rel.startswith('ent400/') else DISK
tmp = root / (rel + '.norm-tmp'); tmp.write_bytes(nb); os.replace(tmp, root / rel)
# propagate
emb = []
for rel, x in scan.items():
p = N.where(rel)
if p is None or x['size'] > 60_000_000 or not x['text']: continue
t = p.read_bytes()
if any(h.encode() in t for h in rep):
emb.append(rel)
print('files embedding their digests:', emb)
for rel in emb:
p = N.where(rel); t = p.read_text()
for o, n in rep.items(): t = t.replace(o, n)
root = SHM if rel.startswith('ent400/') else DISK
tmp = root / (rel + '.norm-tmp'); tmp.write_text(t); os.replace(tmp, root / rel)
d = json.load(open(H / 'work/normalize-digests.json')); d['rep'].update(rep)
d['order'] += [r for r in targets if r not in d['order']]
json.dump(d, open(H / 'work/normalize-digests.json', 'w'))