-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle.py
77 lines (63 loc) · 2.39 KB
/
bundle.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import re
import argparse
from datetime import datetime
lib_path = os.path.join(os.path.dirname(__file__), 'lib') # change this if you were move lib or this script
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--inp", help="input file", required=True)
parser.add_argument("--out", help="output file")
args = parser.parse_args()
if not args.inp.endswith(".cpp"):
raise Exception("input file should be .cpp")
if args.out is None:
args.out = args.inp.removesuffix('.cpp') + "_bundle.cpp"
current_datetime = datetime.now()
datetime_string = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
result = f"""
// AlgoBase {datetime_string}
"""
result += recurse(os.path.abspath(args.inp))
print(args.out)
with open(args.out, 'w') as f:
f.write(result)
def find_full_path(filename, from_path):
filename_candids = [filename]
if filename.startswith("AlgoBase/"):
filename_candids.append(filename.removeprefix("AlgoBase/"))
for filename in filename_candids:
from_path_iter = from_path
while from_path_iter.startswith(lib_path):
candid_path = os.path.abspath(os.path.join(from_path_iter, filename))
if os.path.exists(candid_path):
return candid_path
from_path_iter = os.path.dirname(from_path_iter)
raise Exception(f"{filename} not found within {from_path}")
# def get_included_files(text, from_path):
# pattern = r'#include\s*"([^"]+)"'
# matches = re.findall(pattern, text)
# return re.sub(pattern, "", text), [find_full_path(match, from_path) for match in matches]
def recurse(filename, visited=set(), from_path=lib_path):
visited.add(filename)
res = ""
with open(filename, 'r') as f:
for line in f.readlines():
pattern = r'#include\s*"([^"]+)"'
matches = re.findall(pattern, line)
assert len(matches) <= 1
if len(matches) == 0:
res += line
else:
path = find_full_path(matches[0], from_path)
pathname = path.split("/")[-1]
if path not in visited:
res += f"""
// start {pathname}
"""
res += recurse(path, visited, os.path.dirname(path))
res += f"""
// end {pathname}
"""
return res
if __name__ == "__main__":
main()