1+ import requests
2+ import time
3+ import sys
4+
5+ # language IDs on judge0
6+ languages = {
7+ "C++" : 10 ,
8+ "Java" : 27 ,
9+ "Python" : 34 ,
10+ "C" : 4
11+ }
12+
13+ api_params = {
14+ "number_of_runs" : "1" ,
15+ "cpu_time_limit" : "2" ,
16+ "cpu_extra_time" : "0.5" ,
17+ "wall_time_limit" : "5" ,
18+ "memory_limit" : "128000" ,
19+ "stack_limit" : "64000" ,
20+ "max_processes_and_or_threads" : "30" ,
21+ "enable_per_process_and_thread_time_limit" : 'false' ,
22+ "enable_per_process_and_thread_memory_limit" : 'true' ,
23+ "max_file_size" : "1024"
24+ }
25+
26+
27+
28+ def readCode (program ):
29+ with open (program , 'r' ) as myfile :
30+ data = myfile .read ()
31+ return data
32+
33+
34+ def readExpectedOutput (output_file ):
35+ with open (output_file , 'r' ) as out :
36+ data = out .read ()
37+ return data
38+
39+
40+ def read_stdin (output_file ):
41+ with open (output_file , 'r' ) as out :
42+ data = out .read ()
43+ return data
44+
45+
46+ def readStatus (token ):
47+ while True :
48+ res = requests .get ("https://api.judge0.com/submissions/" + token ['token' ])
49+ response = res .json ()
50+ status = response ['status' ]['description' ]
51+ #time.sleep(0.5) # wait for judge0 to compile
52+ if status != "Processing" and status != "In Queue" :
53+ break
54+
55+ print ("Processing 🔵 , \n " + program )
56+ print ("Expected Output : \n " + stdout )
57+
58+ if response ['status' ]['description' ] == "Accepted" :
59+ print ("Output : \n " + str (response ['stdout' ]))
60+ print ("Compile Success ✅" )
61+ else :
62+ print ("Compile Failed ❌" )
63+ print ("Output : " + str (response ['stdout' ]))
64+ print ("Error : " + str (response ['stderr' ]))
65+ print ("Message : " + str (response ['message' ]) + ", " + response ['status' ]['description' ])
66+
67+
68+ def compile (program , language_id , * argv ):
69+ if len (argv ) == 2 :
70+ stdout = argv [0 ]
71+ stdin = argv [1 ]
72+ api_params ['stdin' ] = stdin
73+ elif len (argv ) == 1 :
74+ stdout = argv [0 ]
75+
76+ api_params ['expected_output' ] = stdout
77+ api_params ['language_id' ] = language_id
78+ api_params ['source_code' ] = program
79+
80+ res = requests .post ("https://api.judge0.com/submissions" , data = api_params )
81+ token = res .json ()
82+ readStatus (token )
83+
84+
85+ if __name__ == '__main__' :
86+ if len (sys .argv ) == 4 :
87+ program = sys .argv [1 ]
88+ language = sys .argv [2 ]
89+ output_file = sys .argv [3 ]
90+ stdout = readExpectedOutput (output_file )
91+ inputCode = readCode (program )
92+ compile (inputCode , languages [language ],stdout )
93+
94+ elif len (sys .argv ) == 5 :
95+ program = sys .argv [1 ]
96+ language = sys .argv [2 ]
97+ output_file = sys .argv [3 ]
98+ in_file = sys .argv [4 ]
99+ stdin = read_stdin (in_file )
100+ stdout = readExpectedOutput (output_file )
101+ inputCode = readCode (program )
102+ compile (inputCode , languages [language ], stdout , stdin )
0 commit comments