-
-
Save isopropylcyanide/fe6a02acad68656a08ddbd57d357cd00 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 | |
import filecmp | |
import re | |
codes = {200: 'success', 404: 'file_name not found', | |
400: 'error', 408: 'timeout'} | |
class program: | |
"""Class that handles all nitty gritties of a user program""" | |
def __init__(self, string, inp, timeout, exp_out=""): | |
"""Receives a name of a file from the user | |
It must be a valid c, c++, java file | |
""" | |
self.file_name = string # Full name of the program | |
self.lang = None # Language | |
self.name = None # Name without extension | |
self.inp_file = inp # Input file | |
self.expectedout = exp_out # Correct output file | |
self.actualout = "out.txt" # Actual output file | |
self.timeout = timeout # Timeout set for execution | |
def isvalidFile(self): | |
"""Check if the filename is valid""" | |
p = re.compile("^(\S+)\.(java|cpp|c)$") | |
matchObj = p.match(self.file_name) | |
if matchObj: | |
self.name, self.lang = matchObj.groups() | |
return True | |
return False | |
def compile(self): | |
"""Compiles the given program""" | |
# Remove previous executables | |
if (os.path.isfile(self.name)): | |
os.remove(self.name) | |
if (os.path.isfile(self.file_name)): | |
if self.lang == 'java': | |
os.system('javac ' + self.file_name) | |
elif self.lang == 'c': | |
os.system('gcc -o ' + self.name + ' ' + self.file_name) | |
elif self.lang == 'cpp': | |
os.system('g++ -o ' + self.name + ' ' + self.file_name) | |
# compilation is successful if the exe is created | |
if (os.path.isfile(self.name)): | |
return 200 | |
else: | |
return 400 | |
else: | |
return 404 | |
def run(self): | |
if self.lang == 'java': | |
cmd = 'java ' + self.name | |
elif self.lang in ['c', 'cpp']: | |
cmd = './' + self.name | |
r = os.system('timeout ' + timeout + ' ' + | |
cmd + ' < ' + self.inp_file + ' > ' + self.actualout) | |
# Perform cleanup | |
if self.lang == 'java': | |
os.remove(self.name + '.class') | |
elif self.lang in ['c', 'cpp']: | |
os.remove(self.name) | |
if r == 0: | |
return 200 | |
elif r == 31744: | |
os.remove(self.actualout) | |
return 408 | |
else: | |
os.remove(self.actualout) | |
return 400 | |
def match(self): | |
print self.actualout, self.expectedout | |
if os.path.isfile(self.actualout) and os.path.isfile(self.expectedout): | |
b = filecmp.cmp(self.actualout, self.expectedout) | |
return b | |
else: | |
return 404 | |
file_name = 'infinite_loop.c' | |
inp_file = "inp.txt" | |
expectedOut = "out.txt" | |
timeout = '2' # secs | |
if __name__ == '__main__': | |
new_program = program(file_name, inp_file, timeout, expectedOut) | |
assert(new_program.isvalidFile()) | |
print 'Compilation: ', (codes[new_program.compile()]) | |
print 'Running: ', (codes[new_program.run()]) | |
# True implies that code is accepted. | |
print 'Result: ', new_program.match() |
Hi all,
How to get compilation error? I want to get where the error occurred in compiled file.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome, thanks @apaar97