Mercurial > hg > cc > cirrus_work
annotate lib/python/unpackz.py @ 242:e117424e244a
cleaned up indentation to 2 spaces throughout
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Wed, 02 Oct 2024 11:09:58 +0100 |
parents | 96021cfee209 |
children |
rev | line source |
---|---|
239
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
241
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
2 '''See https://stackoverflow.com/a/37042747/2595465 |
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
3 Usage: unpackz.py [-o response-out-file][-b buffer-size] |
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
4 ''' |
239
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
5 import sys |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
6 import isal.isal_zlib |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
7 offset = 0 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
8 obuf_len = 0 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
9 nbuf = lastbuf = False |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
10 BUFSIZE = 1048576 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
11 outfile = None |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
12 if sys.argv[1] == '-o': |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
13 sys.argv.pop(1) |
241
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
14 if len(sys.argv)>=3: |
239
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
15 outfile = open(sys.argv.pop(1),'wb') |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
16 else: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
17 print('need an outfile', file=sys.stderr) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
18 exit(1) |
241
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
19 if sys.argv[1] == '-b': |
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
20 sys.argv.pop(1) |
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
21 if len(sys.argv)==3: |
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
22 BUFSIZE = int(sys.argv.pop(1)) |
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
23 else: |
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
24 print('need a buffer length', file=sys.stderr) |
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
25 exit(2) |
96021cfee209
take bufsize from cmdline
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
240
diff
changeset
|
26 |
239
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
27 with open(sys.argv[1],'rb') as f: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
28 z = isal.isal_zlib.decompressobj(31) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
29 count = 0 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
30 while True: |
242
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
31 if z.unused_data == b"": |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
32 #print('n', obuf_len, file=sys.stderr) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
33 if lastbuf: # buf == b"": |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
34 if outfile is None: |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
35 print(obuf_len, offset) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
36 else: |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
37 outfile.write(b'\000%d\000%d\000'%(obuf_len, offset)) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
38 if count!=0: |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
39 print("Unused data: count=%s offset=%s ?"%(count, offset), |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
40 file=sys.stderr) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
41 break |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
42 if nbuf: |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
43 obuf_len += BUFSIZE # still no EOS after a full buffer processed |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
44 buf = f.read(BUFSIZE) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
45 nbuf = True |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
46 lastbuf = ((truesize:=len(buf)) < BUFSIZE) # will only succeed if now at EOF |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
47 else: |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
48 buf_len = len(buf) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
49 #print('b', obuf_len, buf_len, len(z.unused_data), len(buf)-len(z.unused_data), |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
50 # nbuf, lastbuf, file=sys.stderr) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
51 count = (obuf_len if (buf_len == truesize) else 0) + \ |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
52 (len(buf)-len(z.unused_data)) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
53 if outfile is None: |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
54 print(count, offset) |
239
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
55 else: |
242
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
56 outfile.write(b'\000%d\000%d\000'%(count, offset)) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
57 #if (offset == 1352249): |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
58 # breakpoint() |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
59 offset += count |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
60 count = 0 |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
61 buf = z.unused_data |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
62 obuf_len = len(buf) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
63 nbuf = False |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
64 z = isal.isal_zlib.decompressobj(31) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
65 got = z.decompress(buf) |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
66 if outfile is not None: |
e117424e244a
cleaned up indentation to 2 spaces throughout
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
241
diff
changeset
|
67 outfile.write(got) |
239
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
68 if outfile is not None: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
69 outfile.close() |