-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
145 lines (120 loc) · 5.02 KB
/
cli.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import argparse
import os
import subprocess
import shutil
__ITEM_LOCATION = lambda item, lang, pref="", suff="": f"{pref}{item}/{lang}{suff}"
__ITEM_PATH = lambda item, lang, pref="", suff="": __ITEM_LOCATION(item, lang, pref, "/" + item + suff)
__WRITE_UP_TEMPLATE = """## Ease of Switching
## Performance
## Features/ Bugs
"""
__SUPPORTED_LANGS = {
"zig": {
"ext": ".zig",
"build": "zig build-exe {0}.zig",
},
"java": {
"ext": ".java",
"build": "javac -d . {0}.java",
},
"c++": {
"ext": ".cpp",
"build": "g++ {0}.cpp",
},
"odin": {
"ext": ".odin",
"build": "odin run {0}.odin -file"
}
}
# create
def _create_dir(dirname):
print("Making directory:", dirname)
if os.path.exists(dirname):
print(f"Path '{dirname}' already exists")
return
os.mkdir(dirname)
def _create_file(filename, template = None):
print("Making file:", filename)
if os.path.exists(filename):
print(f"Path '{filename}' already exists")
return
with open(filename, "w") as f:
if template:
f.write(str(template))
def _create_supported_langs_dirs(item_name):
_create_dir(f"src/{item_name}")
for lang in __SUPPORTED_LANGS:
_create_dir(f"src/{item_name}/{lang}")
_create_file(f"src/{item_name}/{lang}/{item_name}{__SUPPORTED_LANGS[lang]['ext']}")
# build
def _get_items_in_src():
return [f.name for f in os.scandir("./src/") if f.is_dir()]
def _run_build_command(lang, item):
start_dir = os.getcwd()
try:
print("Building %s in language %s" % (item, lang))
os.chdir(f"{start_dir}/build/")
subprocess.run(__SUPPORTED_LANGS[lang]["build"].format(__ITEM_PATH(item, lang, "../src/")).split(), shell=True, check=True)
for i in [f.name for f in os.scandir(".") if f.is_file()]:
if i.endswith(__SUPPORTED_LANGS[lang]["ext"]):
continue
try:
shutil.copy("./" + i, __ITEM_LOCATION(item, lang, "../build/", "/" + i))
except IOError:
os.makedirs(os.path.dirname(__ITEM_LOCATION(item, lang, "../build/", "/" + i)))
shutil.copy("./" + i, __ITEM_LOCATION(item, lang, "../build/", "/" + i))
os.remove("./" + i)
os.chdir(start_dir)
print("Success.")
except Exception as e:
print(e)
os.chdir(start_dir)
print("Failed to build: %s for language %s\nFull command was %s\n" % (item, lang, __SUPPORTED_LANGS[lang]["build"].format(__ITEM_PATH(item, lang, "./src/"))))
def _build_supported_langs(item_name):
for lang in __SUPPORTED_LANGS:
_run_build_command(lang, item_name)
def _build_items_in_src():
if not os.path.exists("./build/"):
os.mkdir("./build/")
for item in _get_items_in_src():
_build_supported_langs(item)
def _update_director_structure():
for item in _get_items_in_src():
for lang in __SUPPORTED_LANGS:
if not os.path.exists(f"./src/{item}/{lang}"):
_create_dir(f"./src/{item}/{lang}/")
_create_file(f"src/{item}/{lang}/{item}{__SUPPORTED_LANGS[lang]['ext']}")
def _create_extension_dir_langs(dirname, file_ext, item_name, template):
_create_dir(f"{dirname}/")
for lang in __SUPPORTED_LANGS:
_create_dir(f"{dirname}/{lang}")
_create_file(f"{dirname}/{lang}/{item_name}.{file_ext}", template = template)
def _create_extension_dir(dirname, file_ext, item_name, template):
_create_dir(f"{dirname}/")
_create_file(f"{dirname}/{item_name}.{file_ext}", template = template)
def _create_writeups_dir():
for item in _get_items_in_src():
_create_extension_dir_langs("writeups", "md", item, __WRITE_UP_TEMPLATE)
def _create_test_cases_dir():
for item in _get_items_in_src():
_create_extension_dir("test-cases", "tc", item, "Tests: ")
# args
argParser = argparse.ArgumentParser()
argParser.add_argument("-c", "--create", type=str, help="Create a directory structure with all supported programming languages.")
argParser.add_argument("-b", "--build", action='store_true', help="Build all the files in the src directory and places them in a separate build directory structure. Builds all supported programming languages.")
argParser.add_argument("-u", "--update", action='store_true', help="Check if all supported languages are present and if one is missing then add it for each item in the src/ directory.")
argParser.add_argument("-w", "--writeups", action='store_true', help="Create and update directory structure for writeups markdown files.")
argParser.add_argument("-tc", "--test-cases", action='store_true', help="Generate Test Cases directory")
# main
if __name__ == "__main__":
args = argParser.parse_args()
if args.create:
_create_supported_langs_dirs(args.create)
if args.build:
_build_items_in_src()
if args.update:
_update_director_structure()
if args.writeups:
_create_writeups_dir()
if args.test_cases:
_create_test_cases_dir()