-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_submission_file.py
executable file
·48 lines (37 loc) · 1.17 KB
/
generate_submission_file.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
# author: Flavio Regis de Arruda
import re
from pathlib import Path
from typing import Final
PROJECT_PATH: Final = Path('.')
MAIN_FILENAME: Final = PROJECT_PATH / 'main.cpp'
SUBMISSION_FILENAME: Final = PROJECT_PATH / 'main_submit.cpp'
PATTERN: Final = r"^\#include\s+\"(.+\.hpp)\"\s*$"
PROG: Final = re.compile(PATTERN)
def needs_preprocess(program: [str]) -> bool:
for line in program:
res = PROG.search(line)
if res is not None:
return True
return False
def preprocess(program: [str]) -> [str]:
response = []
for line in program:
result = PROG.search(line)
if result:
filename = PROJECT_PATH / result.group(1)
with filename.open("r") as f:
for file_line in f.readlines():
response.append(file_line)
else:
response.append(line)
return response
def main():
with MAIN_FILENAME.open("r") as f:
program = f.readlines()
while needs_preprocess(program):
program = preprocess(program)
with SUBMISSION_FILENAME.open("w") as f:
f.writelines(program)
if __name__ == "__main__":
main()