-
Notifications
You must be signed in to change notification settings - Fork 1
/
xxd-to-files.py
executable file
·48 lines (35 loc) · 1.02 KB
/
xxd-to-files.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
#!/usr/bin/env python3
import os
import shutil
import subprocess
import sys
import tempfile
from itertools import takewhile
from pathlib import Path
OUTPUT = 'xxd-output'
def is_not_start(line):
return not line.startswith('00000000:')
lines = open(sys.argv[1]).read().splitlines()
filenames = list(takewhile(is_not_start, lines))
lines = lines[len(filenames):]
data = {}
while lines:
chunk = lines[:1]
lines = lines[1:]
chunk += list(takewhile(is_not_start, lines))
lines = lines[len(chunk) - 1:]
data[filenames[0]] = '\n'.join(chunk)
filenames = filenames[1:]
assert not filenames
shutil.rmtree(OUTPUT)
for filename, content in data.items():
path = Path(OUTPUT, filename)
parent = path.parent
if not parent.exists():
parent.mkdir(parents=True)
with tempfile.NamedTemporaryFile('w', delete=False) as fp:
fp.write(content)
fp.close()
subprocess.check_output(f'xxd -r {fp.name} > {path}', shell=True)
os.unlink(fp.name)
print(f'Saving {path}')