Last active
March 23, 2024 18:18
-
-
Save rajarsheem/6089309fd5bb9b3c71ab to your computer and use it in GitHub Desktop.
A little code checker tool in python for Java,C,Cpp. Handles compile error, runtime error, accepted, wrong, TLE.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, filecmp | |
codes = {200:'success',404:'file not found',400:'error',408:'timeout'} | |
def compile(file,lang): | |
if lang == 'java': | |
class_file = file[:-4]+"class" | |
elif lang == 'c': | |
class_file = file[:-2] | |
elif lang=='cpp': | |
class_file = file[:-4] | |
if (os.path.isfile(class_file)): | |
os.remove(class_file) | |
if (os.path.isfile(file)): | |
if lang == 'java': | |
os.system('javac '+file) | |
elif lang == 'c' or lang == 'cpp': | |
os.system('gcc -o '+class_file+' '+file) | |
if (os.path.isfile(class_file)): | |
return 200 | |
else: | |
return 400 | |
else: | |
return 404 | |
def run(file,input,timeout,lang): | |
if lang == 'java': | |
cmd = 'java '+file | |
elif lang=='c' or lang=='cpp': | |
cmd = './'+file | |
r = os.system('timeout '+timeout+' '+cmd+' < '+input+' > out.txt') | |
if lang == 'java': | |
os.remove(file+'.class') | |
elif lang == 'c' or lang == 'cpp': | |
os.remove(file) | |
if r==0: | |
return 200 | |
elif r==31744: | |
os.remove('out.txt') | |
return 408 | |
else: | |
os.remove('out.txt') | |
return 400 | |
def match(output): | |
if os.path.isfile('out.txt') and os.path.isfile(output): | |
b = filecmp.cmp('out.txt',output) | |
#os.remove('out.txt') | |
return b | |
else: | |
return 404 | |
file = 'add.c' | |
lang = 'c' | |
testin = 'testin.txt' | |
testout = 'testout.txt' | |
timeout = '1' # secs | |
print(codes[compile(file,'c')]) | |
print (codes[run('add',testin,timeout,lang)]) | |
print (match(testout)) # True implies that code is accepted. |
Hi Rajarsheem, I have updated your code to a more modular object oriented design. Also, regular expression have been used to replace the task of separating file extensions by hand. Here's a look
(https://gist.github.com/isopropylcyanide/fe6a02acad68656a08ddbd57d357cd00)
How do you integrate with java backend?
The only problem is that its not safe. The code may contain some dangerous function call that can affect the server.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Java,C,Cpp.
Handles compile error, runtime error, accepted, wrong, TLE.
Assumes all the files are in the same directory.