-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsoinfo.py
62 lines (49 loc) · 1.88 KB
/
csoinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import print_function
import struct
import sys
import os
def pretty_print_filesize(fsize):
if fsize < 1024:
return '{} bytes'.format(fsize)
if fsize < 1024 ** 2:
return '{:.3f} KiB'.format(fsize / 1024.0)
if fsize < 1024 ** 3:
return '{:.3f} MiB'.format(fsize / (1024.0 ** 2))
return '{:.3f} GiB'.format(fsize / (1024.0 ** 3))
def main(argv0, args):
if not args:
print("Usage: {} [-t] file.cso file.zso ...".format(argv0))
return 1
printtotal = args[0] == '-t'
if printtotal:
args.pop(0)
totalsize = 0
totalorig = 0
for fname in args:
try:
with open(fname, 'rb') as f:
data = f.read(24)
if len(data) != 24:
raise RuntimeError("{}: failed to read 24 bytes, read only {}".format(fname, len(data)))
if data[:4] not in (b'CISO', b'ZISO'):
raise RuntimeError("{}: no CISO or ZISO 4 magic bytes".format(fname))
filesize = os.path.getsize(fname)
origsize, blocksize = struct.unpack('<QI', data[8:20])
totalsize += filesize
totalorig += origsize
print("{}: {}, {}/{}, {}/{}, {:.2f}%, {} byte blocks".format(
fname, 'cso' if data[:4] == b'CISO' else 'zso',
filesize, origsize,
pretty_print_filesize(filesize), pretty_print_filesize(origsize),
(100.0 * filesize) / origsize if origsize else 0, blocksize
))
except Exception as e:
print(e)
if printtotal:
print("TOTAL: total, {}/{}, {}/{}, {:.2f}%, {} byte blocks".format(
totalsize, totalorig,
pretty_print_filesize(totalsize), pretty_print_filesize(totalorig),
(100.0 * totalsize) / totalorig, 0
))
if __name__ == '__main__':
exit(main(sys.argv[0], sys.argv[1:]))