|
| 1 | +import os |
| 2 | +from shutil import rmtree |
| 3 | +from threading import Thread |
| 4 | + |
| 5 | +from sio.executors.checker import output_to_fraction |
| 6 | +from sio.executors.common import _extract_input_if_zipfile, _populate_environ |
| 7 | +from sio.workers import ft |
| 8 | +from sio.workers.executors import DetailedUnprotectedExecutor |
| 9 | +from sio.workers.util import TemporaryCwd, decode_fields, replace_invalid_UTF, tempcwd |
| 10 | +from sio.workers.file_runners import get_file_runner |
| 11 | + |
| 12 | +import signal |
| 13 | +import six |
| 14 | + |
| 15 | +DEFAULT_INTERACTOR_MEM_LIMIT = 256 * 2 ** 10 # in KiB |
| 16 | +RESULT_STRING_LENGTH_LIMIT = 1024 # in bytes |
| 17 | + |
| 18 | + |
| 19 | +class InteractorError(Exception): |
| 20 | + def __init__(self, message, interactor_out, env, renv, irenv): |
| 21 | + super().__init__( |
| 22 | + f'{message}\n' |
| 23 | + f'Interactor out: {interactor_out}\n' |
| 24 | + f'Interactor environ dump: {irenv}\n' |
| 25 | + f'Solution environ dump: {renv}\n' |
| 26 | + f'Environ dump: {env}' |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def _limit_length(s): |
| 31 | + if len(s) > RESULT_STRING_LENGTH_LIMIT: |
| 32 | + suffix = b'[...]' |
| 33 | + return s[: max(0, RESULT_STRING_LENGTH_LIMIT - len(suffix))] + suffix |
| 34 | + return s |
| 35 | + |
| 36 | + |
| 37 | +@decode_fields(['result_string']) |
| 38 | +def run(environ, executor, use_sandboxes=True): |
| 39 | + """ |
| 40 | + Common code for executors. |
| 41 | +
|
| 42 | + :param: environ Recipe to pass to `filetracker` and `sio.workers.executors` |
| 43 | + For all supported options, see the global documentation for |
| 44 | + `sio.workers.executors` and prefix them with ``exec_``. |
| 45 | + :param: executor Executor instance used for executing commands. |
| 46 | + :param: use_sandboxes Enables safe checking output correctness. |
| 47 | + See `sio.executors.checkers`. True by default. |
| 48 | + """ |
| 49 | + |
| 50 | + renv = _run(environ, executor, use_sandboxes) |
| 51 | + |
| 52 | + _populate_environ(renv, environ) |
| 53 | + |
| 54 | + for key in ('result_code', 'result_string'): |
| 55 | + environ[key] = replace_invalid_UTF(environ[key]) |
| 56 | + |
| 57 | + if 'out_file' in environ: |
| 58 | + ft.upload( |
| 59 | + environ, |
| 60 | + 'out_file', |
| 61 | + tempcwd('out'), |
| 62 | + to_remote_store=environ.get('upload_out', False), |
| 63 | + ) |
| 64 | + |
| 65 | + return environ |
| 66 | + |
| 67 | + |
| 68 | +def _fill_result(env, renv, irenv, interactor_out): |
| 69 | + sol_sig = renv.get('exit_signal', None) |
| 70 | + inter_sig = irenv.get('exit_signal', None) |
| 71 | + sigpipe = signal.SIGPIPE.value |
| 72 | + |
| 73 | + if irenv['result_code'] != 'OK' and inter_sig != sigpipe: |
| 74 | + renv['result_code'] = 'SE' |
| 75 | + raise InteractorError(f'Interactor got {irenv["result_code"]}.', interactor_out, env, renv, irenv) |
| 76 | + elif renv['result_code'] != 'OK' and sol_sig != sigpipe: |
| 77 | + return |
| 78 | + elif len(interactor_out) == 0: |
| 79 | + renv['result_code'] = 'SE' |
| 80 | + raise InteractorError(f'Empty interactor out.', interactor_out, env, renv, irenv) |
| 81 | + elif inter_sig == sigpipe: |
| 82 | + renv['result_code'] = 'WA' |
| 83 | + renv['result_string'] = 'solution exited prematurely' |
| 84 | + else: |
| 85 | + renv['result_string'] = '' |
| 86 | + if six.ensure_binary(interactor_out[0]) == b'OK': |
| 87 | + renv['result_code'] = 'OK' |
| 88 | + if interactor_out[1]: |
| 89 | + renv['result_string'] = _limit_length(interactor_out[1]) |
| 90 | + renv['result_percentage'] = output_to_fraction(interactor_out[2]) |
| 91 | + else: |
| 92 | + renv['result_code'] = 'WA' |
| 93 | + if interactor_out[1]: |
| 94 | + renv['result_string'] = _limit_length(interactor_out[1]) |
| 95 | + renv['result_percentage'] = (0, 1) |
| 96 | + |
| 97 | + |
| 98 | +def _run(environ, executor, use_sandboxes): |
| 99 | + input_name = tempcwd('in') |
| 100 | + |
| 101 | + file_executor = get_file_runner(executor, environ) |
| 102 | + interactor_executor = DetailedUnprotectedExecutor() |
| 103 | + exe_filename = file_executor.preferred_filename() |
| 104 | + interactor_filename = 'soc' |
| 105 | + |
| 106 | + ft.download(environ, 'exe_file', exe_filename, add_to_cache=True) |
| 107 | + os.chmod(tempcwd(exe_filename), 0o700) |
| 108 | + ft.download(environ, 'interactor_file', interactor_filename, add_to_cache=True) |
| 109 | + os.chmod(tempcwd(interactor_filename), 0o700) |
| 110 | + ft.download(environ, 'in_file', input_name, add_to_cache=True) |
| 111 | + |
| 112 | + zipdir = tempcwd('in_dir') |
| 113 | + os.mkdir(zipdir) |
| 114 | + try: |
| 115 | + input_name = _extract_input_if_zipfile(input_name, zipdir) |
| 116 | + |
| 117 | + r1, w1 = os.pipe() |
| 118 | + r2, w2 = os.pipe() |
| 119 | + for fd in (r1, w1, r2, w2): |
| 120 | + os.set_inheritable(fd, True) |
| 121 | + |
| 122 | + interactor_args = [os.path.basename(input_name), 'out'] |
| 123 | + |
| 124 | + interactor_time_limit = 2 * environ['exec_time_limit'] |
| 125 | + |
| 126 | + class ExecutionWrapper(Thread): |
| 127 | + def __init__(self, executor, *args, **kwargs): |
| 128 | + super(ExecutionWrapper, self).__init__() |
| 129 | + self.executor = executor |
| 130 | + self.args = args |
| 131 | + self.kwargs = kwargs |
| 132 | + self.value = None |
| 133 | + self.exception = None |
| 134 | + |
| 135 | + def run(self): |
| 136 | + with TemporaryCwd(): |
| 137 | + try: |
| 138 | + self.value = self.executor(*self.args, **self.kwargs) |
| 139 | + except Exception as e: |
| 140 | + self.exception = e |
| 141 | + |
| 142 | + with interactor_executor as ie: |
| 143 | + interactor = ExecutionWrapper( |
| 144 | + ie, |
| 145 | + [tempcwd(interactor_filename)] + interactor_args, |
| 146 | + stdin=r2, |
| 147 | + stdout=w1, |
| 148 | + ignore_errors=True, |
| 149 | + environ=environ, |
| 150 | + environ_prefix='interactor_', |
| 151 | + mem_limit=DEFAULT_INTERACTOR_MEM_LIMIT, |
| 152 | + time_limit=interactor_time_limit, |
| 153 | + pass_fds=(r2, w1), |
| 154 | + close_passed_fd=True, |
| 155 | + cwd=tempcwd(), |
| 156 | + in_file=environ['in_file'], |
| 157 | + ) |
| 158 | + |
| 159 | + with file_executor as fe: |
| 160 | + exe = ExecutionWrapper( |
| 161 | + fe, |
| 162 | + tempcwd(exe_filename), |
| 163 | + [], |
| 164 | + stdin=r1, |
| 165 | + stdout=w2, |
| 166 | + ignore_errors=True, |
| 167 | + environ=environ, |
| 168 | + environ_prefix='exec_', |
| 169 | + pass_fds=(r1, w2), |
| 170 | + close_passed_fd=True, |
| 171 | + cwd=tempcwd(), |
| 172 | + in_file=environ['in_file'], |
| 173 | + ) |
| 174 | + |
| 175 | + exe.start() |
| 176 | + interactor.start() |
| 177 | + |
| 178 | + exe.join() |
| 179 | + interactor.join() |
| 180 | + |
| 181 | + for ew in (exe, interactor): |
| 182 | + if ew.exception is not None: |
| 183 | + raise ew.exception |
| 184 | + |
| 185 | + renv = exe.value |
| 186 | + irenv = interactor.value |
| 187 | + |
| 188 | + try: |
| 189 | + with open(tempcwd('out'), 'rb') as result_file: |
| 190 | + interactor_out = [line.rstrip() for line in result_file.readlines()] |
| 191 | + while len(interactor_out) < 3: |
| 192 | + interactor_out.append(b'') |
| 193 | + except FileNotFoundError: |
| 194 | + interactor_out = [] |
| 195 | + |
| 196 | + _fill_result(environ, renv, irenv, interactor_out) |
| 197 | + finally: |
| 198 | + rmtree(zipdir) |
| 199 | + |
| 200 | + return renv |
0 commit comments