diff options
Diffstat (limited to 'yaksh/java_code_evaluator.py')
-rw-r--r-- | yaksh/java_code_evaluator.py | 88 |
1 files changed, 63 insertions, 25 deletions
diff --git a/yaksh/java_code_evaluator.py b/yaksh/java_code_evaluator.py index b325208..167981b 100644 --- a/yaksh/java_code_evaluator.py +++ b/yaksh/java_code_evaluator.py @@ -29,6 +29,39 @@ class JavaCodeEvaluator(CodeEvaluator): user_code_directory) return compile_command, compile_main + def set_file_paths(self, directory, file_name): + output_path = "{0}{1}.class".format(directory, file_name) + return output_path + + 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 + + user_code_directory = os.getcwd() + '/' + self.write_to_submit_code_file(self.submit_code_path, user_answer) + ref_file_name = (clean_ref_code_path.split('/')[-1]).split('.')[0] + # user_output_path = "{0}{1}.class".format(user_code_directory, + # 'Test') + # ref_output_path = "{0}{1}.class".format(user_code_directory, + # ref_file_name) + # user_output_path, ref_output_path = self.set_file_paths(user_code_directory, clean_ref_code_path) + self.user_output_path = self.set_file_paths(user_code_directory, 'Test') + self.ref_output_path = self.set_file_paths(user_code_directory, ref_file_name) + + compile_command, self.compile_main = self.get_commands(clean_ref_code_path, user_code_directory) + self.run_command_args = "java -cp {0} {1}".format(user_code_directory, + ref_file_name) + self.compiled_output = self._compile_command(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 @@ -49,39 +82,44 @@ class JavaCodeEvaluator(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 + # user_code_directory = os.getcwd() + '/' + # self.write_to_submit_code_file(self.submit_code_path, user_answer) + # ref_file_name = (clean_ref_code_path.split('/')[-1]).split('.')[0] + # # user_output_path = "{0}{1}.class".format(user_code_directory, + # # 'Test') + # # ref_output_path = "{0}{1}.class".format(user_code_directory, + # # ref_file_name) + # # user_output_path, ref_output_path = self.set_file_paths(user_code_directory, clean_ref_code_path) + # user_output_path = self.set_file_paths(user_code_directory, 'Test') + # ref_output_path = self.set_file_paths(user_code_directory, ref_file_name) + + # compile_command, compile_main = self.get_commands(clean_ref_code_path, user_code_directory) + # run_command_args = "java -cp {0} {1}".format(user_code_directory, + # ref_file_name) + # ret = self._compile_command(compile_command) + # proc, stdnt_stderr = ret success = False - user_code_directory = os.getcwd() + '/' - self.write_to_submit_code_file(self.submit_code_path, user_answer) - ref_file_name = (clean_ref_code_path.split('/')[-1]).split('.')[0] - user_output_path = "{0}{1}.class".format(user_code_directory, - 'Test') - ref_output_path = "{0}{1}.class".format(user_code_directory, - ref_file_name) - # user_output_path, ref_output_path = self.set_file_paths(user_code_directory, clean_ref_code_path) - compile_command, compile_main = self.get_commands(clean_ref_code_path, user_code_directory) - run_command_args = "java -cp {0} {1}".format(user_code_directory, - ref_file_name) - 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(run_command_args, shell=True, + ret = self._run_command(self.run_command_args, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -90,7 +128,7 @@ class JavaCodeEvaluator(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: @@ -102,7 +140,7 @@ class JavaCodeEvaluator(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: |