def grade(solution: Solution): exercise = solution.exercise run_command = exercise.run_command output_file_name = exercise.program_name + OUTPUT_FILE_SUFFIX for test in exercise.get_sorted_tests(): error_file = open(join(solution.get_directory(), ERROR_TEST_FILENAME), 'w+') command = [ RUN_SCRIPT_PATH, solution.get_directory(), test.get_input_path(), test.get_output_path(), run_command, output_file_name ] process = subprocess.Popen(command, stderr=error_file, preexec_fn=limit_memory()) try: process.communicate(timeout=test.timeout) if error_occurred(error_file): handle_test_error(solution, error_file) break else: if process.returncode == SUCCESS_RETURN_CODE: solution.test_passed(test.points) else: solution.output_file = output_file_name break except subprocess.TimeoutExpired: error_file.close() solution.timeout_occurred() break finally: kill_processes(process.pid)
def execute_compilation(solution: Solution, compile_command: str) -> bool: error_file = open(join(solution.get_directory(), COMPILE_ERROR_FILENAME), 'w+') bash_command = [ COMPILE_SCRIPT_PATH, solution.get_directory(), compile_command ] subprocess.Popen(bash_command, stderr=error_file).wait() if error_occurred(error_file): handle_compile_error(solution, error_file) return False return True
def clear_directory(solution: Solution): solution_dir = solution.get_directory() files = [f for f in listdir(solution_dir) if isfile(join(solution_dir, f))] for file in files: if file != solution.filename: if not file.endswith(OUTPUT_FILE_SUFFIX) or \ (file.endswith(OUTPUT_FILE_SUFFIX) and solution.output_file is None): os.remove(join(solution_dir, file))
def add_solution(exercise: Exercise, member: Member, file: FileStorage, ip_address: str, attempt_nr: int, os_info: str): filename = secure_filename(file.filename) solution = Solution(filename=filename, ip_address=ip_address, send_date=get_current_date(), os_info=os_info, attempt=attempt_nr) exercise.solutions.append(solution) member.solutions.append(solution) solution_directory = solution.get_directory() create_directory(solution_directory) file.save(join(solution_directory, solution.filename)) unpack_file(solution.filename, solution_directory) db.session.commit() solution.enqueue_execution()