Skip to content

Instantly share code, notes, and snippets.

@isopropylcyanide
Forked from rajarsheem/littlechecker.py
Last active January 7, 2024 16:50
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save isopropylcyanide/fe6a02acad68656a08ddbd57d357cd00 to your computer and use it in GitHub Desktop.
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.
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()
@apaar97
Copy link

apaar97 commented Jan 25, 2019

Hey,
I had found some errors in using the tool for timeout on windows.
I have updated the code to use subprocess module, which is recommended as the replacement to old os.system calls.
I have also added refinements and cleaned the code a bit.
Check it out at : https://gist.github.com/apaar97/de3ef501d5762ac8cb2f8a7343b6a29d

@isopropylcyanide
Copy link
Author

Awesome, thanks @apaar97

@mamatkasym
Copy link

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