-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexport_code.py
More file actions
executable file
·98 lines (81 loc) · 2.9 KB
/
export_code.py
File metadata and controls
executable file
·98 lines (81 loc) · 2.9 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
import os, argparse
from pathlib import Path
import shutil
def remove_implementation(filepath: Path):
def is_start(s: str):
start = '"""YOUR IMPLEMENTATION START"""'
s = s.strip()
return s == start
def comment(s: str):
s = s.strip()
return s.startswith("#")
def is_end(s: str):
end = '"""YOUR IMPLEMENTATION END"""'
s = s.strip()
return s == end
with open(filepath, "r+") as f:
lines = f.readlines()
i = 0
modifications = 0
n = len(lines)
new_lines = []
while i < n:
# read lines until start of implementation
while (i < n) and not is_start(lines[i]):
new_lines.append(lines[i])
i += 1
# add start of implementation comment
if i < n:
new_lines.append(lines[i])
pass_str = lines[i][: lines[i].index("#")] + "pass\n"
new_lines.append(pass_str)
modifications += 1
i += 1
# read lines until end of implementation
while i < n and not is_end(lines[i]):
if comment(lines[i]):
new_lines.append(lines[i])
i += 1
# add end of implementation comment
if i < n:
new_lines.append(lines[i])
i += 1
f.seek(0)
f.truncate(0)
f.writelines(new_lines)
return modifications
generated_path = Path("generated")
lib_name = "edunn"
lib_folderpath = generated_path / lib_name
import sys
if __name__ == "__main__":
# parser = argparse.ArgumentParser()
# parser.add_argument(dest="language", help="This is the first argument")
# args=parser.parse_args()
# language = parser.language
print(f"Generating unimplemented library version to {generated_path}")
if generated_path.exists():
print(f"Deleting folder {generated_path.absolute()}...")
shutil.rmtree(generated_path)
generated_path.mkdir()
print(f"Copying new version from {lib_name} to {generated_path.absolute()}...")
shutil.copytree(lib_name, lib_folderpath)
print(f"Removing implementation code...")
total_files = 0
modified_files = 0
for root, dirs, files in os.walk(generated_path):
for file in files:
if file.endswith(".py"):
filepath = Path(os.path.join(root, file))
modifications = remove_implementation(filepath)
total_files += 1
if modifications > 0:
modified_files += 1
print(f"Done, {modified_files} files modified out of {total_files} python files.")
extra_files = ["requirements.txt"]
print("Copying additional files...")
for f in extra_files:
print(f)
shutil.copy(f, generated_path / f)
print(f"Done, {len(extra_files)} files copied")