| 133 | stats.player_stats[player].score = score |
| 134 | |
| 135 | def validate_code( |
| 136 | self, |
| 137 | agent: Player, |
| 138 | map_file_type_to_compile: dict = MAP_FILE_TYPE_TO_COMPILE, |
| 139 | map_file_type_to_run: dict = MAP_FILE_TYPE_TO_RUN, |
| 140 | ) -> tuple[bool, str | None]: |
| 141 | # Check that the `submission/` folder exists |
| 142 | exists_output = agent.environment.execute("test -d submission && echo 'exists'")["output"] |
| 143 | if "exists" != exists_output.strip(): |
| 144 | return False, f"Submission folder `{self.submission}/` does not exist" |
| 145 | |
| 146 | # Check that there is a *single* file called "main.<ext>" in the submission folder |
| 147 | # and that <ext> is one of the supported file types |
| 148 | found_main = False |
| 149 | sub_path = Path(agent.environment.config.cwd) / self.submission |
| 150 | ls_output = agent.environment.execute("ls", cwd=sub_path)["output"].splitlines() |
| 151 | main_files = [ |
| 152 | fname for fname in ls_output if fname.startswith("main.") and Path(fname).suffix in map_file_type_to_run |
| 153 | ] |
| 154 | |
| 155 | if len(main_files) != 1: |
| 156 | # Check if src/main.rs exists for Rust projects |
| 157 | if "src" in ls_output: |
| 158 | src_ls_output = agent.environment.execute("ls src", cwd=sub_path)["output"].splitlines() |
| 159 | if "main.rs" in src_ls_output: |
| 160 | main_files = ["src/main.rs"] |
| 161 | found_main = True |
| 162 | else: |
| 163 | found_main = True |
| 164 | |
| 165 | if not found_main: |
| 166 | supported_exts = "|".join(map_file_type_to_run.keys()) |
| 167 | return ( |
| 168 | False, |
| 169 | f"Exactly one main.[{supported_exts}] file must be present in submission, found {len(main_files)}", |
| 170 | ) |
| 171 | main_ext = Path(main_files[0]).suffix |
| 172 | |
| 173 | # Check that the submission compiles if necessary |
| 174 | if main_ext in map_file_type_to_compile: |
| 175 | compile_cmd = map_file_type_to_compile[main_ext].format(name="main") |
| 176 | try: |
| 177 | compile_response = agent.environment.execute(compile_cmd, timeout=15, cwd=sub_path) |
| 178 | except subprocess.TimeoutExpired: |
| 179 | return False, f"Compilation failed (ran {compile_cmd} inside {self.submission}): timed out" |
| 180 | if compile_response["returncode"] != 0: |
| 181 | return ( |
| 182 | False, |
| 183 | f"Compilation failed (ran {compile_cmd} inside {self.submission}): {compile_response['output']}", |
| 184 | ) |
| 185 | |
| 186 | # Check that submission runs in competition |
| 187 | executable = map_file_type_to_run[main_ext].format(path=self.submission, name="main") |
| 188 | run_cmd = f"{self.executable} {shlex.join([executable, executable])}" |
| 189 | try: |
| 190 | run_response = agent.environment.execute(run_cmd, timeout=15) |
| 191 | except subprocess.TimeoutExpired: |
| 192 | return False, f"Submission failed to run (ran {run_cmd}): timed out" |