def test_question(request, question_id): question = get_object_or_404(Question, id=question_id) if request.method == "POST": code = request.POST['code'] else: code = question.get_suggested_solution() required_language = question.required_language work_dir = get_temp_code_store_dir(user_id=request.user.id) storer = CodeStorer(work_dir=work_dir, is_abs=True) code_path = "" try: code_path = storer.save_code(code=code, language=required_language) except CodeStorerException as e: return HttpResponse(json.dumps({"sucess": False, "message": e.message, "score": 0}), content_type="application/json") compilation_result = do_compilation(code_file_path=code_path, language=question.required_language) relative_path = get_relative_code_path_from_abspath(code_path) temp_exe_file_path = get_relative_code_path_from_abspath(compilation_result["executable_path"]) if \ compilation_result["executable_path"] else "", exe_file_path = "".join(temp_exe_file_path) score = do_all_testcases(compilation_result["is_successful"], question, relative_path, exe_file_path) # remove the file clean_dir(work_dir) return HttpResponse(json.dumps({"score": score, "success": compilation_result["is_successful"], "message": compilation_result["error_message"]}), content_type="application/json")
def test_compilation(request): required_language = request.POST['required_language'] code = request.POST['code'] work_dir = get_temp_code_store_dir(user_id=request.user.id) storer = CodeStorer(work_dir=work_dir, is_abs=True) code_path = "" if required_language == "Java": question = get_object_or_404(Question, pk=request.POST['questionID']) code_files = question.codefile_set.all() codes = code.split(SEPARATOR) files = [] for f in code_files: files += [f.file_name] del codes[-1] try: code_path = storer.save_multi_codes(codes=codes, language=required_language, filesname=files) except CodeStorerException as e: return HttpResponse(json.dumps({"sucess": False, "message": e.message}), content_type="application/json") else: try: code_path = storer.save_code(code=code, language=required_language, filename="") except CodeStorerException as e: return HttpResponse(json.dumps({"sucess": False, "message": e.message}), content_type="application/json") compilation_result = do_compilation(code_file_path=code_path, language=required_language) # remove the file clean_dir(work_dir) return HttpResponse( json.dumps({"success": compilation_result["is_successful"], "message": compilation_result["error_message"]}), content_type="application/json")
def run_user_input(request): required_language = request.POST['required_language'] code = request.POST['code'] questionID = request.POST['questionID'] user_input = request.POST['user_input'] question = get_object_or_404(Question, id=questionID) work_dir = get_temp_code_store_dir(user_id=request.user.id) storer = CodeStorer(work_dir=work_dir, is_abs=True) code_path = "" if required_language == "Java": question = get_object_or_404(Question, pk=request.POST['questionID']) code_files = question.codefile_set.all() codes = code.split(SEPARATOR) files = [] for f in code_files: files += [f.file_name] del codes[-1] try: code_path = storer.save_multi_codes(codes=codes, language=required_language, filesname=files) except CodeStorerException as e: return HttpResponse(json.dumps({"sucess": False, "message": e.message}), content_type="application/json") else: try: code_path = storer.save_code(code=code, language=required_language, filename="") except CodeStorerException as e: return HttpResponse(json.dumps({"sucess": False, "message": e.message}), content_type="application/json") compilation_result = do_compilation(code_file_path=code_path, language=question.required_language) relative_path = get_relative_code_path_from_abspath(code_path) temp_exe_file_path = get_relative_code_path_from_abspath(compilation_result["executable_path"]) if \ compilation_result["executable_path"] else "", exe_file_path = "".join(temp_exe_file_path) program_output = do_run_user_input(compilation_result["is_successful"], question, relative_path, exe_file_path, user_input) # remove the file clean_dir(work_dir) # if cannot complite, print the error message if program_output == 0: return HttpResponse(json.dumps({"score": -1, "success": compilation_result["is_successful"], "message": compilation_result["error_message"]}), content_type="application/json") return HttpResponse(json.dumps( {"program_output": program_output[0], "return_code": program_output[1], "timeout_status": program_output[2], "success": compilation_result["is_successful"], "message": compilation_result["error_message"]}), content_type="application/json")
def pretest(request): required_language = request.POST['required_language'] code = request.POST['code'] questionID = request.POST['questionID'] question = get_object_or_404(Question, id=questionID) work_dir = get_temp_code_store_dir(user_id=request.user.id) storer = CodeStorer(work_dir=work_dir, is_abs=True) code_path = "" if required_language == "Java": question = get_object_or_404(Question, pk=request.POST['questionID']) code_files = question.codefile_set.all() codes = code.split(SEPARATOR) files = [] for f in code_files: files += [f.file_name] del codes[-1] try: code_path = storer.save_multi_codes(codes=codes, language=required_language, filesname=files) except CodeStorerException as e: return HttpResponse(json.dumps({"sucess": False, "message": e.message}), content_type="application/json") else: try: code_path = storer.save_code(code=code, language=required_language, filename="") except CodeStorerException as e: return HttpResponse(json.dumps({"sucess": False, "message": e.message}), content_type="application/json") compilation_result = do_compilation(code_file_path=code_path, language=question.required_language) relative_path = get_relative_code_path_from_abspath(code_path) temp_exe_file_path = get_relative_code_path_from_abspath(compilation_result["executable_path"]) if \ compilation_result["executable_path"] else "", exe_file_path = "".join(temp_exe_file_path) score, failed_testcases_list = do_pretest(compilation_result["is_successful"], question, relative_path, exe_file_path) # remove the file clean_dir(work_dir) return HttpResponse(json.dumps({"score": score, "failed_testcases_list": failed_testcases_list, "success": compilation_result["is_successful"], "message": compilation_result["error_message"]}), content_type="application/json")
def create_question_submissions_from_code(code_list, question_list, task, user): """ :param code_list: list of code :param question_list: list of Question :param task: Assignment or OnlineTest instance :param user: request.user :return: a list of created question_submissions """ q_submission_list = [] for i in range(len(code_list)): code = code_list[i] question = question_list[i] # 1. store the code = > get the source file name relative_work_dir = get_relative_work_dir(str(task), task.id, question.id, user.id) storer = CodeStorer(work_dir=relative_work_dir) code_path = "" try: code_path = storer.save_code(code=code, language=question.required_language) except CodeStorerException as e: raise Exception("Error in storing code : " + e) # 2. compile the file compilation_result = do_compilation(code_file_path=code_path, language=question.required_language) # 3. store the question submission q_submission = QuestionSubmission.objects.create( src_file_path=get_relative_code_path_from_abspath(code_path), exe_file_path=get_relative_code_path_from_abspath(compilation_result["executable_path"]) if compilation_result["executable_path"] else "", submission_time=datetime.now(), question=question, submitted_by=user, compilation_okay=compilation_result["is_successful"] ) q_submission.save() q_submission_list.append(q_submission) return q_submission_list