-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
258 lines (206 loc) · 6.28 KB
/
compile.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
compile all src files into a single script by getting the text from the files in src end
extrating between '###START###' and '###END###' and
writing them into the msh file ate the '###FUNCTIONS###' tag
Copy the content from the .phil-project into msh to store environment variables
"""
import sys
import os
OUT_FILE = "out/msh"
QUIET = False
TO_BINARY = False
MAIN_FILE = "src/main"
UTILS_DIR = "src/utils"
COMMANDS_DIR = "src/commands"
ENV_FILE = ".phil-project"
COMPILE_DEV_MODE = False
COMPILE_TESTING_MODE = False
def main():
"""
main function
"""
set_args()
log("Running the myshell compiler...")
main_content = read_file(MAIN_FILE)
main_content = inject_file(main_content, ENV_FILE, "###ENV###")
main_content = inject_dir(main_content, COMMANDS_DIR, "###COMMANDS###")
main_content = inject_dir(main_content, UTILS_DIR, "###UTILS###")
if not COMPILE_DEV_MODE:
main_content = remove_lines_with_tag(main_content, "###DEV-MODE###")
else:
log("*Compiling in development mode*")
if not COMPILE_TESTING_MODE:
main_content = remove_lines_with_tag(main_content, "###TESTING-MODE###")
else:
log("*Compiling in testing mode*")
# create the out directory if it does not exist
base_dir = os.path.dirname(OUT_FILE)
if not os.path.exists(base_dir):
os.makedirs(base_dir)
write_out_file(main_content, OUT_FILE)
make_file_executable(OUT_FILE)
if TO_BINARY:
make_binary(OUT_FILE)
log("Compiled successfully: " + "\033[92m" + OUT_FILE + "\033[0m")
log()
def inject_dir(
main_content: str,
dir_path: str,
placeholder: str,
with_bounds=True,
start_bound="###START###",
end_bound="###END###",
only_return_content=False,
) -> str:
"""
inject the content of the files in the directory into the main content
"""
files = os.listdir(dir_path)
result = ""
for file in files:
# if file is a directory recursively inject the content
if os.path.isdir(dir_path + "/" + file):
result += inject_dir(
main_content,
dir_path + "/" + file,
placeholder,
with_bounds,
start_bound,
end_bound,
only_return_content=True,
)
continue
if with_bounds:
result += read_file_with_bounds(
dir_path + "/" + file, start_bound, end_bound
)
else:
result += read_file(dir_path + "/" + file)
if only_return_content:
return result
return main_content.replace(placeholder, result)
def remove_lines_with_tag(content: str, tag: str) -> str:
"""
Remove all lines with the tag
"""
new_content = ""
for line in content.split("\n"):
if tag in line:
continue
new_content += line + "\n"
return new_content
def inject_file(
main_content: str,
file_path: str,
placeholder: str,
with_bounds=True,
start_bound="###START###",
end_bound="###END###",
) -> str:
"""
inject the environment variables into the main content
"""
result = ""
if with_bounds:
result = read_file_with_bounds(file_path, start_bound, end_bound)
else:
result = read_file(file_path)
return main_content.replace(placeholder, result)
def make_file_executable(file: str):
"""
make the file executable
"""
os.system("chmod +x " + file)
def write_out_file(content: str, path: str):
"""
write the content of the msh file into the out file
"""
with open(path, "w", encoding="utf-8") as file:
file.write(content)
def read_file_with_bounds(file_path: str, start: str, end: str):
"""
read the file and get the content between the start and end bounds
"""
content = read_file(file_path)
new_content = ""
in_bounds = False
for line in content.split("\n"):
if line.strip() == start:
in_bounds = True
continue
if line.strip() == end:
in_bounds = False
continue
if in_bounds:
new_content += line + "\n"
return new_content
def read_file(file_path: str):
"""
read the main file and get the content
"""
try:
with open(file_path, "r", encoding="utf-8") as file:
return file.read()
except FileNotFoundError:
print("Error: file not found: ", file_path)
sys.exit(1)
def make_binary(file: str):
"""
make the file a binary
"""
# check if shc is installed
if os.system("command -v shc > /dev/null") != 0:
os.system("sudo apt-get install shc")
os.system("shc -f " + file + " -o " + file + ".bin")
os.system("rm " + file + ".x.c")
os.system("rm " + file)
os.system("mv " + file + ".bin " + file)
def set_args():
"""
set the arguments for the command line
"""
args = sys.argv
for index, arg in enumerate(args):
if arg == "-out" or arg == "-o":
if index + 1 >= len(args):
print("Error: no output file specified")
sys.exit(1)
global OUT_FILE
OUT_FILE = args[index + 1]
if arg == "-q" or arg == "-quiet":
global QUIET
QUIET = True
if arg == "-b" or arg == "-binary":
global TO_BINARY
print("*Compiling to binary*")
TO_BINARY = True
if arg == "-h" or arg == "-help":
print_help()
sys.exit(0)
if arg == "-dev-mode":
global COMPILE_DEV_MODE
COMPILE_DEV_MODE = True
if arg == "-testing-mode":
global COMPILE_TESTING_MODE
COMPILE_TESTING_MODE = True
def print_help():
"""
print the help message
"""
print("Usage: compile.py [options]")
print()
print("Options:")
print(" -h, -help print this help message")
print(" -out, -o <file> specify the output file")
print(" -q, -quiet run in quiet mode")
print(" -b, -binary compile to binary")
print(" -dev-mode compile in development mode")
def log(message: str = ""):
"""
log the message
"""
global QUIET
if not QUIET:
print(message)
if __name__ == "__main__":
main()