changeset 313:c05293f9d958 trim

mostly working, date-stamp issue?
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Tue, 23 Dec 2025 16:59:32 +0000
parents cc70ae0b156d
children 5c4d68911dd3
files lib/python/cc/lmh/shrink.py
diffstat 1 files changed, 21 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/lib/python/cc/lmh/shrink.py	Mon Dec 22 22:23:37 2025 +0000
+++ b/lib/python/cc/lmh/shrink.py	Tue Dec 23 16:59:32 2025 +0000
@@ -2,22 +2,23 @@
 '''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
+   Usage: shrink.py infile.cdb_in cdate_prefix outfile.cdb_in
 '''
 import sys, io
 
-fn = sys.argv.pop(1)
-dp = sys.argv.pop(1)
+ifn = sys.argv.pop(1)
+dp = sys.argv.pop(1).encode('ascii')
+ofn = sys.argv.pop(1)
 dpl = len(dp)
 saved = 0
-buf = io.BytesIO(bytearray(4))
+#buf = io.BytesIO(bytearray(4))
 
-with open(fn) as f:
+
+with open(ifn,'rb') as f, open(ofn,'wb') as of:
   for l in f:
-    sys.stdout.write(l)
-    sys.stdout.flush()
-    (bb,od,oe) = l.rstrip().split(':',2)
-    (os1,os2)=bb[1:].split(',')
+    of.write(l)
+    (bb,od,oe) = l.rstrip().split(b':',2)
+    (os1,os2)=bb[1:].split(b',')
     kl=int(os1)
     ll=int(os2)
     for i in range(dpl):
@@ -27,13 +28,16 @@
       nd = od[i-1:]
     else:
       nd = od
-    print(od,nd)
+    print(od,nd,file=sys.stderr)
     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()
+    int.to_bytes(nli,4)
+    of.write(b'+%d,%d'%(kl-(len(od)-len(nd)),4))
+    of.write(b':')
+    of.write(nd)
+    of.write(b':')
+    of.write(oe[:-ll])
+    of.write(int.to_bytes(nli,4))
+    of.write(b'\n')
+
+