|
| 1 | +import glob |
| 2 | +import os |
| 3 | +import stat |
| 4 | +import argparse |
| 5 | +import signal |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | + |
| 9 | +from sinol_make import util |
| 10 | +from sinol_make.helpers import compiler, package_util, compile |
| 11 | + |
| 12 | + |
| 13 | +def get_ingen(task_id=None, ingen_path=None): |
| 14 | + """ |
| 15 | + Find ingen source file in `prog/` directory. |
| 16 | + If `ingen_path` is specified, then it will be used (if exists). |
| 17 | + :param task_id: task id, for example abc. If None, then |
| 18 | + will return any ingen matching "*ingen.*" |
| 19 | + :param ingen_path: path to ingen source file |
| 20 | + :return: path to ingen source file or None if not found |
| 21 | + """ |
| 22 | + |
| 23 | + if ingen_path is not None: |
| 24 | + if os.path.exists(ingen_path): |
| 25 | + return ingen_path |
| 26 | + else: |
| 27 | + util.exit_with_error(f'Ingen source file {ingen_path} does not exist.') |
| 28 | + |
| 29 | + if task_id is None: |
| 30 | + task_id = '*' |
| 31 | + ingen = glob.glob(os.path.join(os.getcwd(), 'prog', task_id + 'ingen.*')) |
| 32 | + if len(ingen) == 0: |
| 33 | + util.exit_with_error(f'Ingen source file for task {task_id} does not exist.') |
| 34 | + |
| 35 | + # Sio2 first chooses shell scripts, then non-shell source codes. |
| 36 | + if os.path.splitext(ingen[0])[1] == '.sh' and len(ingen) > 1: |
| 37 | + return ingen[1] |
| 38 | + else: |
| 39 | + return ingen[0] |
| 40 | + |
| 41 | + |
| 42 | +def compile_ingen(ingen_path: str, args: argparse.Namespace, weak_compilation_flags=False): |
| 43 | + """ |
| 44 | + Compiles ingen and returns path to compiled executable. |
| 45 | + """ |
| 46 | + compilers = compiler.verify_compilers(args, [ingen_path]) |
| 47 | + ingen_exe, compile_log_path = compile.compile_file(ingen_path, package_util.get_executable(ingen_path), compilers, weak_compilation_flags) |
| 48 | + |
| 49 | + if ingen_exe is None: |
| 50 | + util.exit_with_error('Failed ingen compilation.', lambda: compile.print_compile_log(compile_log_path)) |
| 51 | + else: |
| 52 | + print(util.info('Successfully compiled ingen.')) |
| 53 | + return ingen_exe |
| 54 | + |
| 55 | + |
| 56 | +def get_correct_solution(task_id): |
| 57 | + """ |
| 58 | + Returns path to correct solution for given task. |
| 59 | + :param task_id: task id, for example abc |
| 60 | + :return: path to correct solution or None if not found |
| 61 | + """ |
| 62 | + correct_solution = glob.glob(os.path.join(os.getcwd(), 'prog', task_id + '.*')) |
| 63 | + if len(correct_solution) == 0: |
| 64 | + util.exit_with_error(f'Correct solution for task {task_id} does not exist.') |
| 65 | + return correct_solution[0] |
| 66 | + |
| 67 | + |
| 68 | +def compile_correct_solution(solution_path: str, args: argparse.Namespace, weak_compilation_flags=False): |
| 69 | + """ |
| 70 | + Compiles correct solution and returns path to compiled executable. |
| 71 | + """ |
| 72 | + compilers = compiler.verify_compilers(args, [solution_path]) |
| 73 | + correct_solution_exe, compile_log_path = compile.compile_file(solution_path, package_util.get_executable(solution_path), compilers, |
| 74 | + weak_compilation_flags) |
| 75 | + if correct_solution_exe is None: |
| 76 | + util.exit_with_error('Failed compilation of correct solution.', |
| 77 | + lambda: compile.print_compile_log(compile_log_path)) |
| 78 | + else: |
| 79 | + print(util.info('Successfully compiled correct solution.')) |
| 80 | + |
| 81 | + return correct_solution_exe |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +def run_ingen(ingen_exe): |
| 86 | + """ |
| 87 | + Runs ingen and generates all input files. |
| 88 | + :param ingen_exe: path to ingen executable |
| 89 | + :return: True if ingen was successful, False otherwise |
| 90 | + """ |
| 91 | + is_shell = os.path.splitext(ingen_exe)[1] == '.sh' |
| 92 | + if is_shell: |
| 93 | + util.fix_file_endings(ingen_exe) |
| 94 | + st = os.stat(ingen_exe) |
| 95 | + os.chmod(ingen_exe, st.st_mode | stat.S_IEXEC) |
| 96 | + |
| 97 | + process = subprocess.Popen([ingen_exe], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 98 | + cwd=os.path.join(os.getcwd(), 'in'), shell=is_shell) |
| 99 | + while process.poll() is None: |
| 100 | + print(process.stdout.readline().decode('utf-8'), end='') |
| 101 | + |
| 102 | + print(process.stdout.read().decode('utf-8'), end='') |
| 103 | + exit_code = process.returncode |
| 104 | + |
| 105 | + return exit_code == 0 |
| 106 | + |
| 107 | + |
| 108 | +def generate_output(arguments): |
| 109 | + """ |
| 110 | + Generates output file for given input file. |
| 111 | + :param arguments: arguments for output generation (type OutputGenerationArguments) |
| 112 | + :return: True if the output was successfully generated, False otherwise |
| 113 | + """ |
| 114 | + input_test = arguments.input_test |
| 115 | + output_test = arguments.output_test |
| 116 | + correct_solution_exe = arguments.correct_solution_exe |
| 117 | + |
| 118 | + input_file = open(input_test, 'r') |
| 119 | + output_file = open(output_test, 'w') |
| 120 | + process = subprocess.Popen([correct_solution_exe], stdin=input_file, stdout=output_file, preexec_fn=os.setsid) |
| 121 | + previous_sigint_handler = signal.getsignal(signal.SIGINT) |
| 122 | + |
| 123 | + def sigint_handler(signum, frame): |
| 124 | + try: |
| 125 | + os.killpg(os.getpgid(process.pid), signal.SIGTERM) |
| 126 | + except ProcessLookupError: |
| 127 | + pass |
| 128 | + sys.exit(1) |
| 129 | + signal.signal(signal.SIGINT, sigint_handler) |
| 130 | + |
| 131 | + process.wait() |
| 132 | + signal.signal(signal.SIGINT, previous_sigint_handler) |
| 133 | + exit_code = process.returncode |
| 134 | + input_file.close() |
| 135 | + output_file.close() |
| 136 | + |
| 137 | + return exit_code == 0 |
0 commit comments