diff options
-rw-r--r-- | yaksh/bash_code_evaluator.py | 19 | ||||
-rw-r--r-- | yaksh/cpp_code_evaluator.py | 21 | ||||
-rw-r--r-- | yaksh/java_code_evaluator.py | 22 | ||||
-rw-r--r-- | yaksh/python_assertion_evaluator.py | 17 | ||||
-rw-r--r-- | yaksh/python_stdout_evaluator.py | 19 | ||||
-rw-r--r-- | yaksh/scilab_code_evaluator.py | 11 |
6 files changed, 84 insertions, 25 deletions
diff --git a/yaksh/bash_code_evaluator.py b/yaksh/bash_code_evaluator.py index a0af0e2..c602ceb 100644 --- a/yaksh/bash_code_evaluator.py +++ b/yaksh/bash_code_evaluator.py @@ -9,6 +9,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles class BashCodeEvaluator(CodeEvaluator): @@ -22,8 +23,11 @@ class BashCodeEvaluator(CodeEvaluator): # Delete the created file. super(BashCodeEvaluator, self).teardown() os.remove(self.submit_code_path) + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, test_case): """ Function validates student script using instructor script as reference. Test cases can optionally be provided. The first argument ref_path, is the path to instructor script, it is assumed to @@ -53,6 +57,10 @@ class BashCodeEvaluator(CodeEvaluator): clean_ref_code_path, clean_test_case_path = \ self._set_test_code_file_path(get_ref_path, get_test_case_path) + self.files_list = [] + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if not isfile(clean_ref_code_path): msg = "No file at %s or Incorrect path" % clean_ref_code_path return False, msg @@ -67,6 +75,7 @@ class BashCodeEvaluator(CodeEvaluator): return False, msg success = False + user_answer = user_answer.replace("\r", "") self.write_to_submit_code_file(self.submit_code_path, user_answer) if clean_test_case_path is None or "": @@ -114,6 +123,12 @@ class BashCodeEvaluator(CodeEvaluator): stderr=subprocess.PIPE ) proc, inst_stdout, inst_stderr = ret + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) args = [self.submit_code_path] + \ [x for x in test_case.split()] ret = self._run_command(args, @@ -126,7 +141,7 @@ class BashCodeEvaluator(CodeEvaluator): return True, "Correct answer" else: err = ("Error:expected" - " %s, got %s").format(inst_stdout+inst_stderr, + " {0}, got {1}").format(inst_stdout+inst_stderr, stdnt_stdout+stdnt_stderr ) return False, err diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index b869442..29b24d5 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -8,6 +8,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles class CppCodeEvaluator(CodeEvaluator): @@ -22,6 +23,13 @@ class CppCodeEvaluator(CodeEvaluator): super(CppCodeEvaluator, self).teardown() # Delete the created file. os.remove(self.submit_code_path) + if os.path.exists(self.ref_output_path): + os.remove(self.ref_output_path) + if os.path.exists(self.user_output_path): + os.remove(self.user_output_path) + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) def set_file_paths(self): user_output_path = os.getcwd() + '/output' @@ -38,14 +46,17 @@ class CppCodeEvaluator(CodeEvaluator): ref_output_path) return compile_command, compile_main - def compile_code(self, user_answer, test_case): + def compile_code(self, user_answer, file_paths, test_case): + self.files_list = [] if self.compiled_user_answer and self.compiled_test_code: 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 file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if not isfile(clean_ref_code_path): msg = "No file at %s or Incorrect path" % clean_ref_code_path return False, msg @@ -57,7 +68,7 @@ class CppCodeEvaluator(CodeEvaluator): 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.user_output_path, self.ref_output_path ) self.compiled_user_answer = self._run_command( @@ -76,7 +87,7 @@ class CppCodeEvaluator(CodeEvaluator): return self.compiled_user_answer, self.compiled_test_code - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, 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. @@ -117,7 +128,6 @@ class CppCodeEvaluator(CodeEvaluator): success, err = True, "Correct answer" else: err = stdout + "\n" + stderr - os.remove(self.ref_output_path) else: err = "Error:" try: @@ -129,7 +139,6 @@ class CppCodeEvaluator(CodeEvaluator): err = err + "\n" + e except: err = err + "\n" + main_err - os.remove(self.user_output_path) else: err = "Compilation Error:" try: diff --git a/yaksh/java_code_evaluator.py b/yaksh/java_code_evaluator.py index c64aa3b..dde2ec2 100644 --- a/yaksh/java_code_evaluator.py +++ b/yaksh/java_code_evaluator.py @@ -8,6 +8,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles class JavaCodeEvaluator(CodeEvaluator): @@ -21,7 +22,14 @@ class JavaCodeEvaluator(CodeEvaluator): def teardown(self): super(JavaCodeEvaluator, self).teardown() # Delete the created file. - os.remove(self.submit_code_path) + os.remove(self.submit_code_path) + if os.path.exists(self.user_output_path): + os.remove(self.user_output_path) + if os.path.exists(self.ref_output_path): + os.remove(self.ref_output_path) + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) def get_commands(self, clean_ref_code_path, user_code_directory): compile_command = 'javac {0}'.format(self.submit_code_path), @@ -35,14 +43,17 @@ class JavaCodeEvaluator(CodeEvaluator): output_path = "{0}{1}.class".format(directory, file_name) return output_path - def compile_code(self, user_answer, test_case): + def compile_code(self, user_answer, file_paths, test_case): + self.files_list = [] if self.compiled_user_answer and self.compiled_test_code: 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 file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if not isfile(clean_ref_code_path): msg = "No file at %s or Incorrect path" % clean_ref_code_path return False, msg @@ -83,7 +94,7 @@ class JavaCodeEvaluator(CodeEvaluator): return self.compiled_user_answer, self.compiled_test_code - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, 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. @@ -123,7 +134,6 @@ class JavaCodeEvaluator(CodeEvaluator): success, err = True, "Correct answer" else: err = stdout + "\n" + stderr - os.remove(self.ref_output_path) else: err = "Error:" try: @@ -135,7 +145,6 @@ class JavaCodeEvaluator(CodeEvaluator): err = err + "\n" + e except: err = err + "\n" + main_err - os.remove(self.user_output_path) else: err = "Compilation Error:" try: @@ -147,5 +156,4 @@ class JavaCodeEvaluator(CodeEvaluator): err = err + "\n" + e except: err = err + "\n" + stdnt_stderr - return success, err diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index bf6a4be..3e98f08 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -7,6 +7,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator, TimeoutException +from copy_delete_files import CopyDeleteFiles class PythonAssertionEvaluator(CodeEvaluator): @@ -16,7 +17,18 @@ class PythonAssertionEvaluator(CodeEvaluator): super(PythonAssertionEvaluator, self).setup() self.exec_scope = None - def compile_code(self, user_answer, test_case): + def teardown(self): + super(PythonAssertionEvaluator, self).teardown() + # Delete the created file. + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) + + def compile_code(self, user_answer, file_paths, test_case): + self.files_list = [] + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if self.exec_scope: return None else: @@ -25,7 +37,7 @@ class PythonAssertionEvaluator(CodeEvaluator): exec submitted in self.exec_scope return self.exec_scope - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, test_case): success = False try: tb = None @@ -42,6 +54,5 @@ class PythonAssertionEvaluator(CodeEvaluator): else: success = True err = 'Correct answer' - del tb return success, err diff --git a/yaksh/python_stdout_evaluator.py b/yaksh/python_stdout_evaluator.py index 6606581..fa8d6c6 100644 --- a/yaksh/python_stdout_evaluator.py +++ b/yaksh/python_stdout_evaluator.py @@ -8,6 +8,7 @@ from contextlib import contextmanager # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles @contextmanager @@ -25,7 +26,18 @@ def redirect_stdout(): class PythonStdoutEvaluator(CodeEvaluator): """Tests the Python code obtained from Code Server""" - def compile_code(self, user_answer, expected_output): + def teardown(self): + super(PythonStdoutEvaluator, self).teardown() + # Delete the created file. + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) + + def compile_code(self, user_answer, file_paths, expected_output): + self.files_list = [] + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if hasattr(self, 'output_value'): return None else: @@ -36,9 +48,8 @@ class PythonStdoutEvaluator(CodeEvaluator): self.output_value = output_buffer.getvalue() return self.output_value - def check_code(self, user_answer, expected_output): + def check_code(self, user_answer, file_paths, expected_output): success = False - tb = None if expected_output in user_answer: success = False @@ -52,7 +63,5 @@ class PythonStdoutEvaluator(CodeEvaluator): else: success = False err = "Incorrect Answer" - del tb return success, err - diff --git a/yaksh/scilab_code_evaluator.py b/yaksh/scilab_code_evaluator.py index 91b4cb3..5f38087 100644 --- a/yaksh/scilab_code_evaluator.py +++ b/yaksh/scilab_code_evaluator.py @@ -8,6 +8,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles class ScilabCodeEvaluator(CodeEvaluator): @@ -21,8 +22,15 @@ class ScilabCodeEvaluator(CodeEvaluator): super(ScilabCodeEvaluator, self).teardown() # Delete the created file. os.remove(self.submit_code_path) + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, test_case): + self.files_list = [] + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) ref_code_path = test_case clean_ref_path, clean_test_case_path = \ self._set_test_code_file_path(ref_code_path) @@ -60,7 +68,6 @@ class ScilabCodeEvaluator(CodeEvaluator): err = add_err + stdout else: err = add_err + stderr - return success, err def _remove_scilab_exit(self, string): |