-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·40 lines (35 loc) · 1.41 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
#!/usr/bin/env python3
import os
import argparse
import subprocess
func_to_file = {
"find_smallest_number" : "findsmallestpositive.c",
"bitarray": "bitarray.c",
"fixed_point_array": "fixed_point_array.c",
"merge_binary_trees": "merge_binary_trees.c",
"sorted_squared": "sorted_squared.c"
}
def compile_and_run(func_file, args=[]):
directory = os.path.dirname(os.path.realpath(__file__))
p = subprocess.run(["gcc", func_file, "-o", "a.out"], cwd=directory)
if p.returncode == 0:
r = subprocess.run(['./a.out'] + args, capture_output=True, cwd=directory)
if r.returncode == 0:
print(r.stdout.decode('utf-8'), end="")
else:
print(r.stderr.decode('utf-8'), emd="")
else:
print("Was unable to compilee " + func_file)
def parse_args():
parser = argparse.ArgumentParser("Dailycoding exercises")
parser.add_argument(dest="func_name", type=str, choices=list(func_to_file.keys()) + ["all"], help="the function to run")
parser.add_argument(dest="args", nargs="*", type=str, help="arguments to the function")
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
file_for_func = func_to_file.get(args.func_name, "all")
if file_for_func == "all":
for file_for_func in func_to_file.values():
compile_and_run(file_for_func, [])
else:
compile_and_run(file_for_func, args.args)