Mercurial > hg > cc > cirrus_work
annotate lib/python/unpackz.py @ 239:992f59d21832
working, but last count/offset not being written
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Sat, 28 Sep 2024 15:19:05 +0100 |
parents | |
children | 51bd09d4289e |
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 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
2 '''See https://stackoverflow.com/a/37042747/2595465''' |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
3 import sys |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
4 import isal.isal_zlib |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
5 offset = 0 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
6 obuf_len = 0 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
7 nbuf = lastbuf = False |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
8 BUFSIZE = 1048576 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
9 outfile = None |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
10 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
|
11 sys.argv.pop(1) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
12 if len(sys.argv)==3: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
13 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
|
14 else: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
15 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
|
16 exit(1) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
17 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
|
18 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
|
19 count = 0 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
20 while True: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
21 if z.unused_data == b"": |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
22 #print('n', obuf_len, file=sys.stderr) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
23 if nbuf: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
24 obuf_len += BUFSIZE # still no EOS after a full buffer processed |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
25 buf = f.read(BUFSIZE) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
26 nbuf = True |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
27 lastbuf = ((truesize:=len(buf)) < BUFSIZE) # will only succeed if now at EOF |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
28 if buf == b"": |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
29 if outfile is None: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
30 print(count, offset) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
31 else: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
32 outfile.write(b'\000%d\000%d\000'%(count, offset)) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
33 break |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
34 else: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
35 buf_len = len(buf) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
36 #print('b', obuf_len, buf_len, len(z.unused_data), len(buf)-len(z.unused_data), |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
37 # nbuf, lastbuf, file=sys.stderr) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
38 count = (obuf_len if (buf_len == truesize) else 0) + \ |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
39 (len(buf)-len(z.unused_data)) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
40 if outfile is None: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
41 print(count, offset) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
42 else: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
43 outfile.write(b'\000%d\000%d\000'%(count, offset)) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
44 offset += count |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
45 count = 0 |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
46 buf = z.unused_data |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
47 obuf_len = len(buf) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
48 nbuf = False |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
49 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
|
50 got = z.decompress(buf) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
51 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
|
52 outfile.write(got) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
53 if count!=0: |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
54 print("Unused data: count=%s offset=%s ?"%(count, offset), |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
55 file=sys.stderr) |
992f59d21832
working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
56 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
|
57 outfile.close() |