-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiomemdec.py
More file actions
executable file
·45 lines (38 loc) · 1.19 KB
/
iomemdec.py
File metadata and controls
executable file
·45 lines (38 loc) · 1.19 KB
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
#!/usr/bin/env python
import sys
SUFFIXES = ('', 'KiB', 'MiB', 'GiB', 'TiB')
def do_decode(infile, human):
for line in infile:
line = line[:-1]
newline = line.strip()
lpad = line[:len(line)-len(newline)]
num, rest = newline.split(maxsplit=1)
start, end = num.split(sep='-')
start = int(start, base=16)
end = int(end, base=16)
size = end - start + 1
suffix = ''
if human:
for suffix in SUFFIXES:
if size // 1024 > 0:
size //= 1024
else:
break
print("{}{} {}{} {}".format(lpad, hex(start), size, suffix, rest))
def decode(filename, human=False):
if filename == '-':
do_decode(sys.stdin, human)
else:
with open(filename, 'r') as infile:
do_decode(infile, human)
if __name__ == '__main__':
human = False
if len(sys.argv) > 1:
if len(sys.argv) > 2 and sys.argv[1] == '-h':
human = True
filename = sys.argv[2]
else:
filename = sys.argv[1]
decode(filename, human)
else:
print("USAGE: {} [-h] <filename>".format(sys.argv[0]))