forked from Paril/q2repro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed.py
More file actions
33 lines (25 loc) · 952 Bytes
/
embed.py
File metadata and controls
33 lines (25 loc) · 952 Bytes
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
#!/usr/bin/python3
# loosely based on the following answer on SO: https://stackoverflow.com/a/61128721
import os, sys, shutil
# get absolute input and output paths
input_path = sys.argv[1]
output_path = sys.argv[2]
# make sure destination directory exists
dest_dir = os.path.dirname(output_path)
if len(dest_dir) > 0:
os.makedirs(dest_dir, exist_ok = True)
# read in the file
in_file = open(input_path, "r")
in_contents = in_file.read()
in_file.close()
# convert
size = len(in_contents);
in_contents = in_contents.replace('\\', '\\\\')
in_contents = in_contents.replace('\n', '\\n')
in_contents = in_contents.replace('\r', '\\r')
in_contents = in_contents.replace('"', '\\"')
in_contents = '#include <stddef.h>\nconst char ' + sys.argv[3] + '[] = ' + '\"' + in_contents + '\";\nconst size_t ' + sys.argv[3] + '_size = ' + str(size) + ';'
# write
out_file = open(output_path, "w")
out_contents = out_file.write(in_contents)
out_file.close()