-
Notifications
You must be signed in to change notification settings - Fork 30
/
make_files.py
executable file
·44 lines (36 loc) · 1.11 KB
/
make_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
#!/usr/bin/env python3
import binascii
import os
import sys
PREFIX = b"""import binascii
BUFFER_SIZE = 512
FILE_LIST = [
"""
SUFFIX = b"""]
for file in FILE_LIST:
filename = file['filename']
print('filename =', filename)
data = binascii.unhexlify(file['data'])
print('len(data) =', len(data))
with open(filename, 'wb') as f:
bytes_remaining = len(data)
data_idx = 0
while bytes_remaining > 0:
buf_size = min(bytes_remaining, BUFFER_SIZE)
f.write(data[data_idx:data_idx + buf_size])
data_idx += buf_size
bytes_remaining -= buf_size
"""
def main():
filenames = sys.argv[1:]
with open('files.py', 'wb') as f:
f.write(PREFIX)
for filename in filenames:
basename = os.path.basename(filename)
print('filename =', basename)
with open(filename, 'rb') as src:
data = src.read()
hex_data = binascii.hexlify(data)
f.write("{{'filename': '{}', 'data': {}}},\n".format(basename, hex_data).encode('utf-8'))
f.write(SUFFIX)
main()