-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (47 loc) · 1.67 KB
/
main.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
import pathlib
import sys
import re
def parse_file(path: pathlib.Path, content: list):
"""Parses a given rust file into the content list.
Args:
path (pathlib.Path): A path to the file
content (list): A list of all lines of content
"""
with open(path, "r") as file:
while line := file.readline():
# Parse mod calls
if not (match := re.match("(pub )?\s*mod\s+(\S+)\s*;", line)) is None:
name = visibility = None
if len(match.groups()) == 2:
visibility = match.groups()[0]
name = match.groups()[1]
else:
name = match.groups()[0]
# ./<name>.rs exists
if (path.parent / f"{name}.rs").exists():
content.append(f"{visibility + ' ' if visibility else ''}mod {name} {{\n")
parse_file(path.parent / f"{name}.rs", content)
content.append(f"}}\n")
# ./<name>/mod.rs exists
elif (path.parent / f"{name}/mod.rs").exists():
content.append(f"{visibility + ' ' if visibility else ''}mod {name} {{\n")
parse_file(path.parent / f"{name}/mod.rs", content)
content.append(f"}}\n")
else:
content.append(line)
# Add newline as delimiter
content.append("\n")
if __name__ == "__main__":
root_dir = pathlib.Path(sys.argv[1])
main_file = sys.argv[2]
out_file = root_dir / "output.rs"
content = list()
# Remove output file if it already exists
if out_file.exists():
out_file.unlink()
# Recursively parse files starting from main file
parse_file(root_dir / main_file, content)
print(f"Writing file {out_file} ...");
# Write output file
with open(out_file, "w") as file:
file.writelines(content)