From 3b56d8259d6aa8311f86e0561e40d0b17bc62534 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Fri, 4 Mar 2016 14:55:09 +0530 Subject: Language registry altered, Name cleanup: - set_registry will only act when registry is None - _setup, _teardown and _check_code are setup,teardown,check_code - Corresponding name changes in evaluator modules --- yaksh/cpp_code_evaluator.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'yaksh/cpp_code_evaluator.py') diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index 7242884..129a1c8 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -17,11 +17,10 @@ class CppCodeEvaluator(CodeEvaluator): super(CppCodeEvaluator, self).__init__(test_case_data, test, language, user_answer, ref_code_path, in_dir) - self.test_case_args = self._setup() + self.test_case_args = self.setup() - # Private Protocol ########## - def _setup(self): - super(CppCodeEvaluator, self)._setup() + def setup(self): + super(CppCodeEvaluator, self).setup() get_ref_path = self.ref_code_path ref_path, test_case_path = self._set_test_code_file_path(get_ref_path) @@ -44,12 +43,12 @@ class CppCodeEvaluator(CodeEvaluator): return (ref_path, self.submit_path, compile_command, compile_main, run_command_args, remove_user_output, remove_ref_output) - def _teardown(self): + def teardown(self): # Delete the created file. - super(CppCodeEvaluator, self)._teardown() + super(CppCodeEvaluator, self).teardown() os.remove(self.submit_path) - def _check_code(self, ref_code_path, submit_code_path, compile_command, + def check_code(self, ref_code_path, submit_code_path, compile_command, compile_main, run_command_args, remove_user_output, remove_ref_output): """ Function validates student code using instructor code as -- cgit From 597c23866be3ecfdf94c40693b060fe9ebbf6446 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Tue, 22 Mar 2016 12:02:47 +0530 Subject: Refactor C/CPP code evaluator and add test cases --- yaksh/cpp_code_evaluator.py | 290 +++++++++++++++++++++++++++++--------------- 1 file changed, 194 insertions(+), 96 deletions(-) (limited to 'yaksh/cpp_code_evaluator.py') diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index 129a1c8..6d18982 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -12,113 +12,211 @@ from code_evaluator import CodeEvaluator class CppCodeEvaluator(CodeEvaluator): """Tests the C code obtained from Code Server""" - def __init__(self, test_case_data, test, language, user_answer, - ref_code_path=None, in_dir=None): - super(CppCodeEvaluator, self).__init__(test_case_data, test, language, - user_answer, ref_code_path, - in_dir) - self.test_case_args = self.setup() - def setup(self): super(CppCodeEvaluator, self).setup() - - get_ref_path = self.ref_code_path - ref_path, test_case_path = self._set_test_code_file_path(get_ref_path) - self.submit_path = self.create_submit_code_file('submit.c') - - # Set file paths - c_user_output_path = os.getcwd() + '/output' - c_ref_output_path = os.getcwd() + '/executable' - - # Set command variables - compile_command = 'g++ {0} -c -o {1}'.format(self.submit_path, - c_user_output_path) - compile_main = 'g++ {0} {1} -o {2}'.format(ref_path, - c_user_output_path, - c_ref_output_path) - run_command_args = [c_ref_output_path] - remove_user_output = c_user_output_path - remove_ref_output = c_ref_output_path - - return (ref_path, self.submit_path, compile_command, compile_main, - run_command_args, remove_user_output, remove_ref_output) + self.submit_code_path = self.create_submit_code_file('submit.c') def teardown(self): - # Delete the created file. super(CppCodeEvaluator, self).teardown() - os.remove(self.submit_path) - - def check_code(self, ref_code_path, submit_code_path, compile_command, - compile_main, run_command_args, remove_user_output, - remove_ref_output): - """ Function validates student code using instructor code as - reference.The first argument ref_code_path, is the path to - instructor code, it is assumed to have executable permission. - The second argument submit_code_path, is the path to the student - code, it is assumed to have executable permission. - - Returns - -------- - - returns (True, "Correct answer") : If the student function returns - expected output when called by reference code. - - returns (False, error_msg): If the student function fails to return - expected output when called by reference code. - - Returns (False, error_msg): If mandatory arguments are not files or - if the required permissions are not given to the file(s). - - """ - if not isfile(ref_code_path): - return False, "No file at %s or Incorrect path" % ref_code_path - if not isfile(submit_code_path): - return False, 'No file at %s or Incorrect path' % submit_code_path - - success = False - ret = self._compile_command(compile_command) - proc, stdnt_stderr = ret - stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) - - # Only if compilation is successful, the program is executed - # And tested with testcases - if stdnt_stderr == '': - ret = self._compile_command(compile_main) - proc, main_err = ret - main_err = self._remove_null_substitute_char(main_err) - - if main_err == '': - ret = self._run_command(run_command_args, stdin=None, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - proc, stdout, stderr = ret - if proc.returncode == 0: - success, err = True, "Correct answer" + # Delete the created file. + os.remove(self.submit_code_path) + + def set_file_paths(self): + user_output_path = os.getcwd() + '/output' + ref_output_path = os.getcwd() + '/executable' + + return user_output_path, ref_output_path + + def get_commands(self, clean_ref_code_path, user_output_path, + ref_output_path): + compile_command = 'g++ {0} -c -o {1}'.format(self.submit_code_path, + user_output_path) + compile_main = 'g++ {0} {1} -o {2}'.format(clean_ref_code_path, + user_output_path, + ref_output_path) + return compile_command, compile_main + + def check_code(self, user_answer, test_case_data): + """ Function validates student code using instructor code as + reference.The first argument ref_code_path, is the path to + instructor code, it is assumed to have executable permission. + The second argument submit_code_path, is the path to the student + code, it is assumed to have executable permission. + + Returns + -------- + + returns (True, "Correct answer") : If the student function returns + expected output when called by reference code. + + returns (False, error_msg): If the student function fails to return + expected output when called by reference code. + + Returns (False, error_msg): If mandatory arguments are not files or + if the required permissions are not given to the file(s). + + """ + ref_code_path = test_case_data[0] + clean_ref_code_path, clean_test_case_path = self._set_test_code_file_path(ref_code_path) + + if not isfile(clean_ref_code_path): + return False, "No file at %s or Incorrect path" % clean_ref_code_path + if not isfile(self.submit_code_path): + return False, 'No file at %s or Incorrect path' % self.submit_code_path + + success = False + self.write_to_submit_code_file(self.submit_code_path, user_answer) + user_output_path, ref_output_path = self.set_file_paths() + compile_command, compile_main = self.get_commands(clean_ref_code_path, user_output_path, ref_output_path) + ret = self._compile_command(compile_command) + proc, stdnt_stderr = ret + stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) + + # Only if compilation is successful, the program is executed + # And tested with testcases + if stdnt_stderr == '': + ret = self._compile_command(compile_main) + proc, main_err = ret + main_err = self._remove_null_substitute_char(main_err) + + if main_err == '': + ret = self._run_command([ref_output_path], stdin=None, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + proc, stdout, stderr = ret + if proc.returncode == 0: + success, err = True, "Correct answer" + else: + err = stdout + "\n" + stderr + os.remove(ref_output_path) else: - err = stdout + "\n" + stderr - os.remove(remove_ref_output) + err = "Error:" + try: + error_lines = main_err.splitlines() + for e in error_lines: + if ':' in e: + err = err + "\n" + e.split(":", 1)[1] + else: + err = err + "\n" + e + except: + err = err + "\n" + main_err + os.remove(user_output_path) else: - err = "Error:" + err = "Compilation Error:" try: - error_lines = main_err.splitlines() + error_lines = stdnt_stderr.splitlines() for e in error_lines: if ':' in e: err = err + "\n" + e.split(":", 1)[1] else: err = err + "\n" + e except: - err = err + "\n" + main_err - os.remove(remove_user_output) - else: - err = "Compilation Error:" - try: - error_lines = stdnt_stderr.splitlines() - for e in error_lines: - if ':' in e: - err = err + "\n" + e.split(":", 1)[1] - else: - err = err + "\n" + e - except: - err = err + "\n" + stdnt_stderr - - return success, err + err = err + "\n" + stdnt_stderr + + return success, err + + # def __init__(self, test_case_data, test, language, user_answer, + # ref_code_path=None, in_dir=None): + # super(CppCodeEvaluator, self).__init__(test_case_data, test, language, + # user_answer, ref_code_path, + # in_dir) + # self.test_case_args = self.setup() + + # def setup(self): + # super(CppCodeEvaluator, self).setup() + + # get_ref_path = self.ref_code_path + # ref_path, test_case_path = self._set_test_code_file_path(get_ref_path) + # self.submit_path = self.create_submit_code_file('submit.c') + + # # Set file paths #@@@ in different func get_output_file_paths + # c_user_output_path = os.getcwd() + '/output' + # c_ref_output_path = os.getcwd() + '/executable' + + # # Set command variables #@@@ This section in different func get_commands + # compile_command = 'g++ {0} -c -o {1}'.format(self.submit_path, + # c_user_output_path) + # compile_main = 'g++ {0} {1} -o {2}'.format(ref_path, + # c_user_output_path, + # c_ref_output_path) + # run_command_args = [c_ref_output_path] + # remove_user_output = c_user_output_path #@@@ not required + # remove_ref_output = c_ref_output_path #@@@ not required + + # return (ref_path, self.submit_path, compile_command, compile_main, + # run_command_args, remove_user_output, remove_ref_output) + + # def check_code(self, ref_code_path, submit_code_path, compile_command, + # compile_main, run_command_args, remove_user_output, + # remove_ref_output): + # """ Function validates student code using instructor code as + # reference.The first argument ref_code_path, is the path to + # instructor code, it is assumed to have executable permission. + # The second argument submit_code_path, is the path to the student + # code, it is assumed to have executable permission. + + # Returns + # -------- + + # returns (True, "Correct answer") : If the student function returns + # expected output when called by reference code. + + # returns (False, error_msg): If the student function fails to return + # expected output when called by reference code. + + # Returns (False, error_msg): If mandatory arguments are not files or + # if the required permissions are not given to the file(s). + + # """ + # if not isfile(ref_code_path): + # return False, "No file at %s or Incorrect path" % ref_code_path + # if not isfile(submit_code_path): + # return False, 'No file at %s or Incorrect path' % submit_code_path + + # success = False + # ret = self._compile_command(compile_command) + # proc, stdnt_stderr = ret + # stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) + + # # Only if compilation is successful, the program is executed + # # And tested with testcases + # if stdnt_stderr == '': + # ret = self._compile_command(compile_main) + # proc, main_err = ret + # main_err = self._remove_null_substitute_char(main_err) + + # if main_err == '': + # ret = self._run_command(run_command_args, stdin=None, + # stdout=subprocess.PIPE, + # stderr=subprocess.PIPE) + # proc, stdout, stderr = ret + # if proc.returncode == 0: + # success, err = True, "Correct answer" + # else: + # err = stdout + "\n" + stderr + # os.remove(remove_ref_output) + # else: + # err = "Error:" + # try: + # error_lines = main_err.splitlines() + # for e in error_lines: + # if ':' in e: + # err = err + "\n" + e.split(":", 1)[1] + # else: + # err = err + "\n" + e + # except: + # err = err + "\n" + main_err + # os.remove(remove_user_output) + # else: + # err = "Compilation Error:" + # try: + # error_lines = stdnt_stderr.splitlines() + # for e in error_lines: + # if ':' in e: + # err = err + "\n" + e.split(":", 1)[1] + # else: + # err = err + "\n" + e + # except: + # err = err + "\n" + stdnt_stderr + + # return success, err -- cgit From d3241512c71d61b355358a691d18e4ff8a8df34c Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 6 Apr 2016 11:26:52 +0530 Subject: Multiple test cases passed as dicts, check_code() is iterated based on no. of test cases --- yaksh/cpp_code_evaluator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'yaksh/cpp_code_evaluator.py') diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index 6d18982..8cdc27c 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -36,7 +36,7 @@ class CppCodeEvaluator(CodeEvaluator): ref_output_path) return compile_command, compile_main - def check_code(self, user_answer, test_case_data): + def check_code(self, user_answer, test_case): """ Function validates student code using instructor code as reference.The first argument ref_code_path, is the path to instructor code, it is assumed to have executable permission. @@ -56,7 +56,7 @@ class CppCodeEvaluator(CodeEvaluator): if the required permissions are not given to the file(s). """ - ref_code_path = test_case_data[0] + ref_code_path = test_case clean_ref_code_path, clean_test_case_path = self._set_test_code_file_path(ref_code_path) if not isfile(clean_ref_code_path): -- cgit From 2c7f278382f4fe8071508b0a880aae34f8edfd5e Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 18 Apr 2016 15:22:25 +0530 Subject: add compile_code function to compile before checking --- yaksh/cpp_code_evaluator.py | 57 +++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'yaksh/cpp_code_evaluator.py') diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index 8cdc27c..250de8e 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -19,7 +19,7 @@ class CppCodeEvaluator(CodeEvaluator): def teardown(self): super(CppCodeEvaluator, self).teardown() # Delete the created file. - os.remove(self.submit_code_path) + os.remove(self.submit_code_path) def set_file_paths(self): user_output_path = os.getcwd() + '/output' @@ -36,6 +36,24 @@ class CppCodeEvaluator(CodeEvaluator): ref_output_path) return compile_command, compile_main + def compile_code(self, user_answer, test_case): + if hasattr(self, 'compiled_output'): + return None + else: + ref_code_path = test_case + clean_ref_code_path, clean_test_case_path = self._set_test_code_file_path(ref_code_path) + + if not isfile(clean_ref_code_path): + return False, "No file at %s or Incorrect path" % clean_ref_code_path + if not isfile(self.submit_code_path): + return False, 'No file at %s or Incorrect path' % self.submit_code_path + + self.write_to_submit_code_file(self.submit_code_path, user_answer) + self.user_output_path, self.ref_output_path = self.set_file_paths() + self.compile_command, self.compile_main = self.get_commands(clean_ref_code_path, self.user_output_path, self.ref_output_path) + self.compiled_output = self._compile_command(self.compile_command) + return self.compiled_output + def check_code(self, user_answer, test_case): """ Function validates student code using instructor code as reference.The first argument ref_code_path, is the path to @@ -56,31 +74,36 @@ class CppCodeEvaluator(CodeEvaluator): if the required permissions are not given to the file(s). """ - ref_code_path = test_case - clean_ref_code_path, clean_test_case_path = self._set_test_code_file_path(ref_code_path) - - if not isfile(clean_ref_code_path): - return False, "No file at %s or Incorrect path" % clean_ref_code_path - if not isfile(self.submit_code_path): - return False, 'No file at %s or Incorrect path' % self.submit_code_path + # ref_code_path = test_case + # clean_ref_code_path, clean_test_case_path = self._set_test_code_file_path(ref_code_path) + + # if not isfile(clean_ref_code_path): + # return False, "No file at %s or Incorrect path" % clean_ref_code_path + # if not isfile(self.submit_code_path): + # return False, 'No file at %s or Incorrect path' % self.submit_code_path + + # success = False + # self.write_to_submit_code_file(self.submit_code_path, user_answer) + # user_output_path, ref_output_path = self.set_file_paths() + # compile_command, compile_main = self.get_commands(clean_ref_code_path, user_output_path, ref_output_path) + # ret = self._compile_command(compile_command) + # proc, stdnt_stderr = ret + # stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) success = False - self.write_to_submit_code_file(self.submit_code_path, user_answer) - user_output_path, ref_output_path = self.set_file_paths() - compile_command, compile_main = self.get_commands(clean_ref_code_path, user_output_path, ref_output_path) - ret = self._compile_command(compile_command) - proc, stdnt_stderr = ret + proc, stdnt_stderr = self.compiled_output stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) + # Only if compilation is successful, the program is executed # And tested with testcases if stdnt_stderr == '': - ret = self._compile_command(compile_main) + ret = self._compile_command(self.compile_main) proc, main_err = ret main_err = self._remove_null_substitute_char(main_err) if main_err == '': - ret = self._run_command([ref_output_path], stdin=None, + ret = self._run_command([self.ref_output_path], stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) proc, stdout, stderr = ret @@ -88,7 +111,7 @@ class CppCodeEvaluator(CodeEvaluator): success, err = True, "Correct answer" else: err = stdout + "\n" + stderr - os.remove(ref_output_path) + os.remove(self.ref_output_path) else: err = "Error:" try: @@ -100,7 +123,7 @@ class CppCodeEvaluator(CodeEvaluator): err = err + "\n" + e except: err = err + "\n" + main_err - os.remove(user_output_path) + os.remove(self.user_output_path) else: err = "Compilation Error:" try: -- cgit From 5684b1b19fcb383f494f0bfc04ad1bb760abce74 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 21 Apr 2016 17:31:09 +0530 Subject: Post review improvements: - compiled_output is set during setup - python exec context renamed - _compile_command deprecated --- yaksh/cpp_code_evaluator.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'yaksh/cpp_code_evaluator.py') diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index 250de8e..312467d 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -15,6 +15,8 @@ class CppCodeEvaluator(CodeEvaluator): def setup(self): super(CppCodeEvaluator, self).setup() self.submit_code_path = self.create_submit_code_file('submit.c') + self.compiled_user_answer = None + self.compiled_test_code = None def teardown(self): super(CppCodeEvaluator, self).teardown() @@ -37,7 +39,7 @@ class CppCodeEvaluator(CodeEvaluator): return compile_command, compile_main def compile_code(self, user_answer, test_case): - if hasattr(self, 'compiled_output'): + if self.compiled_user_answer and self.compiled_test_code: return None else: ref_code_path = test_case @@ -51,8 +53,19 @@ class CppCodeEvaluator(CodeEvaluator): self.write_to_submit_code_file(self.submit_code_path, user_answer) self.user_output_path, self.ref_output_path = self.set_file_paths() self.compile_command, self.compile_main = self.get_commands(clean_ref_code_path, self.user_output_path, self.ref_output_path) - self.compiled_output = self._compile_command(self.compile_command) - return self.compiled_output + # self.compiled_output = self._compile_command(self.compile_command) + self.compiled_user_answer = self._run_command(self.compile_command, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + self.compiled_test_code = self._run_command(self.compile_main, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + + return self.compiled_user_answer, self.compiled_test_code def check_code(self, user_answer, test_case): """ Function validates student code using instructor code as @@ -89,17 +102,19 @@ class CppCodeEvaluator(CodeEvaluator): # ret = self._compile_command(compile_command) # proc, stdnt_stderr = ret # stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) - success = False - proc, stdnt_stderr = self.compiled_output + proc, stdnt_out, stdnt_stderr = self.compiled_user_answer stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) - # Only if compilation is successful, the program is executed # And tested with testcases if stdnt_stderr == '': - ret = self._compile_command(self.compile_main) - proc, main_err = ret + # ret = self._compile_command(self.compile_main) + # ret = self._run_command(self.compile_main, + # shell=True, + # stdout=subprocess.PIPE, + # stderr=subprocess.PIPE) + proc, main_out, main_err = self.compiled_test_code main_err = self._remove_null_substitute_char(main_err) if main_err == '': -- cgit From 23b7abd3c1125e4c875e214e4f673c48c4bf4752 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 2 May 2016 11:52:58 +0530 Subject: Remove commented code and cleanup --- yaksh/cpp_code_evaluator.py | 127 -------------------------------------------- 1 file changed, 127 deletions(-) (limited to 'yaksh/cpp_code_evaluator.py') diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index 312467d..becf371 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -53,7 +53,6 @@ class CppCodeEvaluator(CodeEvaluator): self.write_to_submit_code_file(self.submit_code_path, user_answer) self.user_output_path, self.ref_output_path = self.set_file_paths() self.compile_command, self.compile_main = self.get_commands(clean_ref_code_path, self.user_output_path, self.ref_output_path) - # self.compiled_output = self._compile_command(self.compile_command) self.compiled_user_answer = self._run_command(self.compile_command, shell=True, stdout=subprocess.PIPE, @@ -87,21 +86,6 @@ class CppCodeEvaluator(CodeEvaluator): if the required permissions are not given to the file(s). """ - # ref_code_path = test_case - # clean_ref_code_path, clean_test_case_path = self._set_test_code_file_path(ref_code_path) - - # if not isfile(clean_ref_code_path): - # return False, "No file at %s or Incorrect path" % clean_ref_code_path - # if not isfile(self.submit_code_path): - # return False, 'No file at %s or Incorrect path' % self.submit_code_path - - # success = False - # self.write_to_submit_code_file(self.submit_code_path, user_answer) - # user_output_path, ref_output_path = self.set_file_paths() - # compile_command, compile_main = self.get_commands(clean_ref_code_path, user_output_path, ref_output_path) - # ret = self._compile_command(compile_command) - # proc, stdnt_stderr = ret - # stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) success = False proc, stdnt_out, stdnt_stderr = self.compiled_user_answer stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) @@ -109,11 +93,6 @@ class CppCodeEvaluator(CodeEvaluator): # Only if compilation is successful, the program is executed # And tested with testcases if stdnt_stderr == '': - # ret = self._compile_command(self.compile_main) - # ret = self._run_command(self.compile_main, - # shell=True, - # stdout=subprocess.PIPE, - # stderr=subprocess.PIPE) proc, main_out, main_err = self.compiled_test_code main_err = self._remove_null_substitute_char(main_err) @@ -152,109 +131,3 @@ class CppCodeEvaluator(CodeEvaluator): err = err + "\n" + stdnt_stderr return success, err - - # def __init__(self, test_case_data, test, language, user_answer, - # ref_code_path=None, in_dir=None): - # super(CppCodeEvaluator, self).__init__(test_case_data, test, language, - # user_answer, ref_code_path, - # in_dir) - # self.test_case_args = self.setup() - - # def setup(self): - # super(CppCodeEvaluator, self).setup() - - # get_ref_path = self.ref_code_path - # ref_path, test_case_path = self._set_test_code_file_path(get_ref_path) - # self.submit_path = self.create_submit_code_file('submit.c') - - # # Set file paths #@@@ in different func get_output_file_paths - # c_user_output_path = os.getcwd() + '/output' - # c_ref_output_path = os.getcwd() + '/executable' - - # # Set command variables #@@@ This section in different func get_commands - # compile_command = 'g++ {0} -c -o {1}'.format(self.submit_path, - # c_user_output_path) - # compile_main = 'g++ {0} {1} -o {2}'.format(ref_path, - # c_user_output_path, - # c_ref_output_path) - # run_command_args = [c_ref_output_path] - # remove_user_output = c_user_output_path #@@@ not required - # remove_ref_output = c_ref_output_path #@@@ not required - - # return (ref_path, self.submit_path, compile_command, compile_main, - # run_command_args, remove_user_output, remove_ref_output) - - # def check_code(self, ref_code_path, submit_code_path, compile_command, - # compile_main, run_command_args, remove_user_output, - # remove_ref_output): - # """ Function validates student code using instructor code as - # reference.The first argument ref_code_path, is the path to - # instructor code, it is assumed to have executable permission. - # The second argument submit_code_path, is the path to the student - # code, it is assumed to have executable permission. - - # Returns - # -------- - - # returns (True, "Correct answer") : If the student function returns - # expected output when called by reference code. - - # returns (False, error_msg): If the student function fails to return - # expected output when called by reference code. - - # Returns (False, error_msg): If mandatory arguments are not files or - # if the required permissions are not given to the file(s). - - # """ - # if not isfile(ref_code_path): - # return False, "No file at %s or Incorrect path" % ref_code_path - # if not isfile(submit_code_path): - # return False, 'No file at %s or Incorrect path' % submit_code_path - - # success = False - # ret = self._compile_command(compile_command) - # proc, stdnt_stderr = ret - # stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) - - # # Only if compilation is successful, the program is executed - # # And tested with testcases - # if stdnt_stderr == '': - # ret = self._compile_command(compile_main) - # proc, main_err = ret - # main_err = self._remove_null_substitute_char(main_err) - - # if main_err == '': - # ret = self._run_command(run_command_args, stdin=None, - # stdout=subprocess.PIPE, - # stderr=subprocess.PIPE) - # proc, stdout, stderr = ret - # if proc.returncode == 0: - # success, err = True, "Correct answer" - # else: - # err = stdout + "\n" + stderr - # os.remove(remove_ref_output) - # else: - # err = "Error:" - # try: - # error_lines = main_err.splitlines() - # for e in error_lines: - # if ':' in e: - # err = err + "\n" + e.split(":", 1)[1] - # else: - # err = err + "\n" + e - # except: - # err = err + "\n" + main_err - # os.remove(remove_user_output) - # else: - # err = "Compilation Error:" - # try: - # error_lines = stdnt_stderr.splitlines() - # for e in error_lines: - # if ':' in e: - # err = err + "\n" + e.split(":", 1)[1] - # else: - # err = err + "\n" + e - # except: - # err = err + "\n" + stdnt_stderr - - # return success, err -- cgit From c384c60c6d7fb5d30f3f929c518e0b41e084c4c4 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 9 May 2016 13:00:04 +0530 Subject: - Adhere to 80 columns - add docstrings - Fix further tests --- yaksh/cpp_code_evaluator.py | 148 ++++++++++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 68 deletions(-) (limited to 'yaksh/cpp_code_evaluator.py') diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index becf371..b869442 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -43,91 +43,103 @@ class CppCodeEvaluator(CodeEvaluator): return None else: ref_code_path = test_case - clean_ref_code_path, clean_test_case_path = self._set_test_code_file_path(ref_code_path) + clean_ref_code_path, clean_test_case_path = \ + self._set_test_code_file_path(ref_code_path) if not isfile(clean_ref_code_path): - return False, "No file at %s or Incorrect path" % clean_ref_code_path + msg = "No file at %s or Incorrect path" % clean_ref_code_path + return False, msg if not isfile(self.submit_code_path): - return False, 'No file at %s or Incorrect path' % self.submit_code_path + msg = "No file at %s or Incorrect path" % self.submit_code_path + return False, msg self.write_to_submit_code_file(self.submit_code_path, user_answer) self.user_output_path, self.ref_output_path = self.set_file_paths() - self.compile_command, self.compile_main = self.get_commands(clean_ref_code_path, self.user_output_path, self.ref_output_path) - self.compiled_user_answer = self._run_command(self.compile_command, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - - self.compiled_test_code = self._run_command(self.compile_main, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - + self.compile_command, self.compile_main = self.get_commands( + clean_ref_code_path, + self.user_output_path, + self.ref_output_path + ) + self.compiled_user_answer = self._run_command( + self.compile_command, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + + self.compiled_test_code = self._run_command( + self.compile_main, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) return self.compiled_user_answer, self.compiled_test_code def check_code(self, user_answer, test_case): - """ Function validates student code using instructor code as - reference.The first argument ref_code_path, is the path to - instructor code, it is assumed to have executable permission. - The second argument submit_code_path, is the path to the student - code, it is assumed to have executable permission. - - Returns - -------- - - returns (True, "Correct answer") : If the student function returns - expected output when called by reference code. - - returns (False, error_msg): If the student function fails to return - expected output when called by reference code. - - Returns (False, error_msg): If mandatory arguments are not files or - if the required permissions are not given to the file(s). - - """ - success = False - proc, stdnt_out, stdnt_stderr = self.compiled_user_answer - stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) - - # Only if compilation is successful, the program is executed - # And tested with testcases - if stdnt_stderr == '': - proc, main_out, main_err = self.compiled_test_code - main_err = self._remove_null_substitute_char(main_err) - - if main_err == '': - ret = self._run_command([self.ref_output_path], stdin=None, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - proc, stdout, stderr = ret - if proc.returncode == 0: - success, err = True, "Correct answer" - else: - err = stdout + "\n" + stderr - os.remove(self.ref_output_path) + """ Function validates student code using instructor code as + reference.The first argument ref_code_path, is the path to + instructor code, it is assumed to have executable permission. + The second argument submit_code_path, is the path to the student + code, it is assumed to have executable permission. + + Returns + -------- + + returns (True, "Correct answer") : If the student function returns + expected output when called by reference code. + + returns (False, error_msg): If the student function fails to return + expected output when called by reference code. + + Returns (False, error_msg): If mandatory arguments are not files or + if the required permissions are not given to the file(s). + """ + + success = False + proc, stdnt_out, stdnt_stderr = self.compiled_user_answer + stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) + + # Only if compilation is successful, the program is executed + # And tested with testcases + if stdnt_stderr == '': + proc, main_out, main_err = self.compiled_test_code + main_err = self._remove_null_substitute_char(main_err) + + if main_err == '': + ret = self._run_command([self.ref_output_path], + stdin=None, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + proc, stdout, stderr = ret + if proc.returncode == 0: + success, err = True, "Correct answer" else: - err = "Error:" - try: - error_lines = main_err.splitlines() - for e in error_lines: - if ':' in e: - err = err + "\n" + e.split(":", 1)[1] - else: - err = err + "\n" + e - except: - err = err + "\n" + main_err - os.remove(self.user_output_path) + err = stdout + "\n" + stderr + os.remove(self.ref_output_path) else: - err = "Compilation Error:" + err = "Error:" try: - error_lines = stdnt_stderr.splitlines() + error_lines = main_err.splitlines() for e in error_lines: if ':' in e: err = err + "\n" + e.split(":", 1)[1] else: err = err + "\n" + e except: - err = err + "\n" + stdnt_stderr + err = err + "\n" + main_err + os.remove(self.user_output_path) + else: + err = "Compilation Error:" + try: + error_lines = stdnt_stderr.splitlines() + for e in error_lines: + if ':' in e: + err = err + "\n" + e.split(":", 1)[1] + else: + err = err + "\n" + e + except: + err = err + "\n" + stdnt_stderr - return success, err + return success, err -- cgit