|
| 1 | +import subprocess |
| 2 | +import argparse |
| 3 | +import os |
| 4 | +import multiprocessing as mp |
| 5 | + |
| 6 | +from sinol_make import util |
| 7 | +from sinol_make.commands.inwer.structs import TestResult, InwerExecution, VerificationResult, TableData |
| 8 | +from sinol_make.helpers import package_util, compile |
| 9 | +from sinol_make.helpers.parsers import add_compilation_arguments |
| 10 | +from sinol_make.interfaces.BaseCommand import BaseCommand |
| 11 | +from sinol_make.commands.inwer import inwer_util |
| 12 | + |
| 13 | + |
| 14 | +class Command(BaseCommand): |
| 15 | + """ |
| 16 | + Class for "inwer" command. |
| 17 | + """ |
| 18 | + |
| 19 | + def get_name(self): |
| 20 | + return "inwer" |
| 21 | + |
| 22 | + def configure_subparser(self, subparser: argparse.ArgumentParser): |
| 23 | + parser = subparser.add_parser( |
| 24 | + self.get_name(), |
| 25 | + help='Verify if input files are correct', |
| 26 | + description='Verify if input files are correct using inwer program ' |
| 27 | + '(for example prog/abcinwer.cpp for abc task). You can also ' |
| 28 | + 'specify your inwer source file which will be used.' |
| 29 | + ) |
| 30 | + |
| 31 | + parser.add_argument('inwer_path', type=str, nargs='?', |
| 32 | + help='path to inwer source file, for example prog/abcinwer.cpp') |
| 33 | + parser.add_argument('--tests', type=str, nargs='+', |
| 34 | + help='test to verify, for example in/abc{0,1}*') |
| 35 | + parser.add_argument('--cpus', type=int, |
| 36 | + help=f'number of cpus to use, by default {mp.cpu_count()} (all available)') |
| 37 | + add_compilation_arguments(parser) |
| 38 | + |
| 39 | + def compile_inwer(self, args: argparse.Namespace): |
| 40 | + self.inwer_executable, compile_log_path = inwer_util.compile_inwer(self.inwer, args) |
| 41 | + if self.inwer_executable is None: |
| 42 | + print(util.error('Compilation failed.')) |
| 43 | + compile.print_compile_log(compile_log_path) |
| 44 | + exit(1) |
| 45 | + else: |
| 46 | + print(util.info('Compilation successful.')) |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def verify_test(execution: InwerExecution) -> VerificationResult: |
| 50 | + """ |
| 51 | + Verifies a test and returns the result of inwer on this test. |
| 52 | + """ |
| 53 | + output_dir = os.path.join(os.getcwd(), 'cache', 'executions', execution.test_name) |
| 54 | + os.makedirs(output_dir, exist_ok=True) |
| 55 | + |
| 56 | + command = f'{execution.inwer_exe_path} < {execution.test_path}' |
| 57 | + process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 58 | + process.wait() |
| 59 | + exit_code = process.returncode |
| 60 | + out, _ = process.communicate() |
| 61 | + |
| 62 | + return VerificationResult( |
| 63 | + execution.test_path, |
| 64 | + exit_code == 0, |
| 65 | + out.decode('utf-8') |
| 66 | + ) |
| 67 | + |
| 68 | + def verify_and_print_table(self) -> dict[str, TestResult]: |
| 69 | + results = {} |
| 70 | + sorted_tests = sorted(self.tests, key=lambda x: x[0]) |
| 71 | + executions: list[InwerExecution] = [] |
| 72 | + for test in sorted_tests: |
| 73 | + results[test] = TestResult(test) |
| 74 | + executions.append(InwerExecution(test, results[test].test_name, self.inwer_executable)) |
| 75 | + |
| 76 | + table_data = TableData(results, 0) |
| 77 | + print('Verifying tests...\n\n') |
| 78 | + with mp.Pool(self.cpus) as pool: |
| 79 | + for i, result in enumerate(pool.imap(self.verify_test, executions)): |
| 80 | + table_data.results[result.test_path].set_results(result.valid, result.output) |
| 81 | + inwer_util.print_view(table_data) |
| 82 | + |
| 83 | + return results |
| 84 | + |
| 85 | + def run(self, args: argparse.Namespace): |
| 86 | + if not util.check_if_project(): |
| 87 | + util.exit_with_error('You are not in a project directory (couldn\'t find config.yml in current directory).') |
| 88 | + |
| 89 | + self.task_id = package_util.get_task_id() |
| 90 | + self.inwer = inwer_util.get_inwer_path(self.task_id, args.inwer_path) |
| 91 | + if self.inwer is None: |
| 92 | + if args.inwer_path is None: |
| 93 | + util.exit_with_error('No inwer found in `prog/` directory.') |
| 94 | + else: |
| 95 | + util.exit_with_error(f'Inwer "{args.inwer_path}" not found.') |
| 96 | + relative_path = os.path.relpath(self.inwer, os.getcwd()) |
| 97 | + print(f'Verifying with inwer {util.bold(relative_path)}') |
| 98 | + |
| 99 | + self.cpus = args.cpus or mp.cpu_count() |
| 100 | + self.tests = package_util.get_tests(args.tests) |
| 101 | + |
| 102 | + if len(self.tests) == 0: |
| 103 | + util.exit_with_error('No tests found.') |
| 104 | + else: |
| 105 | + print('Verifying tests: ' + util.bold(', '.join(self.tests))) |
| 106 | + |
| 107 | + self.compile_inwer(args) |
| 108 | + results: dict[str, TestResult] = self.verify_and_print_table() |
| 109 | + print('') |
| 110 | + |
| 111 | + failed_tests = [] |
| 112 | + for result in results.values(): |
| 113 | + if not result.valid: |
| 114 | + failed_tests.append(result.test_name) |
| 115 | + |
| 116 | + if len(failed_tests) > 0: |
| 117 | + util.exit_with_error(f'Verification failed for tests: {", ".join(failed_tests)}') |
| 118 | + else: |
| 119 | + print(util.info('Verification successful.')) |
| 120 | + exit(0) |
0 commit comments