Mercurial > hg > cc > cirrus_work
changeset 309:b6203074f18c trim
to fix unshrunk 2023-40, not yet working
| author | Henry S. Thompson <ht@inf.ed.ac.uk> |
|---|---|
| date | Mon, 22 Dec 2025 22:18:16 +0000 |
| parents | d52602a64e09 |
| children | 4f7977c697ef |
| files | lib/python/cc/lmh/shrink.py |
| diffstat | 1 files changed, 39 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/python/cc/lmh/shrink.py Mon Dec 22 22:18:16 2025 +0000 @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +'''Shrink cdb_input-style files after the fact + Each record is encoded as +klen,dlen:key->data + + Usage: shrink.py file.cdb_in cdate_prefix | sponge file.cdb_in +''' +import sys, io + +fn = sys.argv.pop(1) +dp = sys.argv.pop(1) +dpl = len(dp) +saved = 0 +buf = io.BytesIO(bytearray(4)) + +with open(fn) as f: + for l in f: + sys.stdout.write(l) + sys.stdout.flush() + (bb,od,oe) = l.rstrip().split(':',2) + (os1,os2)=bb[1:].split(',') + kl=int(os1) + ll=int(os2) + for i in range(dpl): + if od[i] != dp[i]: + break + if i > 0: + nd = od[i-1:] + else: + nd = od + print(od,nd) + ol = oe[-ll:] + nli = int(ol) + nl = int.to_bytes(nli,4) + print("+%s,%s"%(kl-(len(od)-len(nd)),4),nd,oe[:-ll],sep=':',end='') + buf.seek(0) + buf.write(nl) + sys.stdout.buffer.write(buf.getbuffer()) + sys.stdout.buffer.write(b'\n') + exit()
