All pastes #215255 Raw Edit

Casanova

public python v1 · immutable
#215255 ·published 2006-10-22 14:25 UTC
rendered paste body
import sysimport osimport timeimport commandsimport threadingtry:    from settings import *except ImportError:    C_COMPILE_STR = 'gcc -lm \'%i\' -o \'%o\''    CPP_COMPILE_STR = 'g++ -lm \%i\' -o \'%o\''    JAVA_COMPILE_STR = 'javac \'%i\''    WORK_DIR = 'evaluator/work_files'class EvaluatorError (Exception):    def __init__ (self, error, value=''):        self.value = value        self.error = error    def __str__ (self):        return repr (self.value)class Evaluator ():    def __str__ (self):        raise NotImplementedError ('Must be Overridden')    def compile (self, code_file, input_file):        raise NotImplementedError ('Must be Overridden')    def run (self, exec_file):        raise NotImplementedError ('Must be Overridden')    def save_file (self, file_path, contents):        try:            if not os.path.exists (WORK_DIR):                os.mkdir (WORK_DIR)                            path_cont = []            path_split = lambda (x): x != '' and \                         path_split(os.path.split(x)[0]) or \                         path_cont.append (os.path.basename(x))            path_cont = path_cont[1:-1]            path = WORKDIR            for cont in path_cont:                path = os.path.join (path, cont)                if not os.path.exists (path):                    os.mkdir (path)                                open_file = open (os.path.join (WORK_DIR, file_path), 'w')            open_file.write (contents)            open_file.close()        except OSError, IOError:            raise         return file_name    def check (self, attempt, output):        raise NotImplementedError ('Must be Overridden')        def evaluate (self, attempt):        # Save the File        save_loc = os.path.join (attemp.user.username, attempt.user.filename)        code_file = self.save_file (save_loc, attempt.code)        # Compile the File        exec_file = self.compile (code_file, attempt.question.test_input)        # Execute the file for preset input        output = self.run (exec_file)        # Match the output to expected output        return self.check (attempt, output)class C_Evaluator (Evaluator):    def __init__ (self):        self.compile_cmd = C_COMPILE_STR            def __str__ (self):        return 'C Evaluator'    def compile (self, code_file):        output_file = code_file # Change this value to change output file name        # replace the code with the object file        cmd = self.compile_cmd.replace ('%i',code_file).replace('%o',output_file)        (status, output) = commands.getstatusoutput (cmd)        if status != 0:            raise EvaluatorError ('Compiler Error', output)        else:            return output_file            def run (self, exec_file, input_file):        (status, output) = commands.getstatusoutput (exec_file + ' < ' \                                                     + input_file)        if status != 0:            raise EvaluatorError ('Run-Time Error')        else:            return output            class CPP_Evaluator (Evaluator):    def __str__ (self):        return 'C++ Evaluator'    def evaluate (self, attempt):        pass    class Java_Evaluator (Evaluator):    def __str__ (self):        return 'Java Evaluator'    def evaluate (self, attempt):        passclass PythonEvaluator (Evaluator):    def __str__ (self):        return 'Python Evaluator'    def evaluate (self, attempt):        pass    class Client (threading.thread):    """ The Evaluator will evaluate and update the status """    def __init__ (self):        self.evaluators = {'c':C_Evaluator, 'c++':CPP_Evaluator,                           'java':Java_Evaluator, 'python':Python_Evaluator}        pass    def queue_not_empty (self):        """ Checks if the ToBeEvaluated queue is empty or not """        pass    def dequeue_attempts (self):        """ Returns an attempt from the ToBeEvaluated queue based on priority        algorithms or return None if queue is empty """        pass        def get_attempt (self):        """ Keep polling the ToBeEvaluated queue until an attempt to be        evaluated is obtained """        while True:            if (self.queue_not_empty()):                return self.dequeue_attempts()            time.sleep (1)    def evaluate (self, attempt):        """ Evaluate the attempt and return the ruling :-) """        lang = attempt.lang_coding.lower()        # first list special case languages whose names cannot be used for        # function names in python        try:            evaluator = self.evaluators[lang](attempt)        except KeyError:            raise NotImplementedError ('Language '+lang+' not supported')        result = evaluator.evaluate()            def run (self):        print 'Evaluator Started'        while True:            attempt = self.get_attempt ()            if attempt == None:                continue            # evalute the attempt            return_code = self.evaluate (attempt)            report_result (attempt, return_code)