-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (49 loc) · 1.9 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from src.compiler import compile_program
from src.tester import run_tests
LANGUAGE_EXTENSIONS = {
"c": "c",
"cpp": "cpp",
"java": "java",
"py": "python",
}
def main():
if len(sys.argv) < 2:
print("Usage: python3 main.py <path_to_program> [<time_limit>] [<memory_limit>]")
sys.exit(1)
# Extract the path to the program
program_path = os.path.abspath(sys.argv[1])
# Extract directory, full filename, and split out name + extension
program_dir = os.path.dirname(program_path)
program_file = os.path.basename(program_path)
program_name, program_ext = os.path.splitext(program_file)
print(f"Testing program: {program_name} with extension: {program_ext}")
print(f"Located at: {program_path}")
print(f"Time limit: {sys.argv[2] if len(sys.argv) >= 3 else 12} seconds")
print(f"Memory limit: {sys.argv[3] if len(sys.argv) >= 4 else 128} MB")
print(f"Running tests...")
# Ensure the language is recognized
ext_no_dot = program_ext.replace('.', '') # eg. ".java" -> "java"
try:
language = LANGUAGE_EXTENSIONS[ext_no_dot.lower()]
except KeyError:
print("Unsupported language or invalid file extension!")
sys.exit(1)
# If time_limit is provided, parse it; otherwise use default=12
if len(sys.argv) >= 3:
time_limit = float(sys.argv[2])
else:
time_limit = 12
# If memory_limit is provided, parse it; otherwise use default=128
if len(sys.argv) >= 4:
memory_limit = int(sys.argv[3])
else:
memory_limit = 128
# Compile the program
compile_program(program_path, language)
# Run tests with the specified or default time/memory limits
run_tests(program_name, program_dir, language, time_limit=time_limit, memory_limit=memory_limit)
if __name__ == "__main__":
main()