|
| 1 | +import pprint |
| 2 | +from run_eval import CompileResult, RunResult |
| 3 | +import discord |
| 4 | + |
| 5 | + |
| 6 | +def _limit_length(text: str, maxlen: int): |
| 7 | + if len(text) > maxlen: |
| 8 | + return text[:maxlen-6] + " [...]" |
| 9 | + else: |
| 10 | + return text |
| 11 | + |
| 12 | + |
| 13 | +def _send_split_log(thread: discord.Thread, partial_message: str, header: str, log: str): |
| 14 | + if len(partial_message) + len(log) + len(header) < 1900: |
| 15 | + partial_message += f"\n\n## {header}:\n" |
| 16 | + partial_message += f"```\n{log}```" |
| 17 | + return partial_message |
| 18 | + else: |
| 19 | + # send previous chunk |
| 20 | + thread.send(partial_message) |
| 21 | + lines = log.splitlines() |
| 22 | + chunks = [] |
| 23 | + partial_message = "" |
| 24 | + for line in lines: |
| 25 | + if len(partial_message) + len(line) < 1900: |
| 26 | + partial_message += line + "\n" |
| 27 | + else: |
| 28 | + chunks.append(partial_message) |
| 29 | + partial_message = line |
| 30 | + |
| 31 | + # now, format the chunks |
| 32 | + for i, chunk in enumerate(chunks): |
| 33 | + partial_message += f"\n\n## {header} ({i}/{len(chunks)}):\n" |
| 34 | + partial_message += f"```\n{_limit_length(log, 1900)}```" |
| 35 | + thread.send(partial_message) |
| 36 | + |
| 37 | + return "" |
| 38 | + |
| 39 | + |
| 40 | +def generate_report(thread: discord.Thread, comp: CompileResult, run: RunResult): |
| 41 | + message = "" |
| 42 | + if not comp.success or not run.success: |
| 43 | + message = "# Run was not successful\n" |
| 44 | + else: |
| 45 | + message = "# Run was successful\n" |
| 46 | + |
| 47 | + if not comp.success: |
| 48 | + if not comp.nvcc_found: |
| 49 | + message += "**Compilation failed**\nNVCC could not be found.\n" |
| 50 | + message += "This indicates a bug in the runner configuration, _not in your code_.\n" |
| 51 | + message += "Please notify the server admins of this problem" |
| 52 | + thread.send(message) |
| 53 | + return |
| 54 | + |
| 55 | + # ok, we found nvcc |
| 56 | + message += "**Compilation failed**\n" |
| 57 | + message += "Command " |
| 58 | + message += f"```bash\n>{_limit_length(comp.command, 1000)}```\n" |
| 59 | + message += f"exited with code **{comp.exit_code}**." |
| 60 | + |
| 61 | + message += _send_split_log(thread, message, "Compiler stderr", comp.stderr.strip()) |
| 62 | + |
| 63 | + if len(comp.stdout.strip()) > 0: |
| 64 | + message += _send_split_log(thread, message, "Compiler stdout", comp.stdout.strip()) |
| 65 | + |
| 66 | + if len(message) != 0: |
| 67 | + thread.send(message) |
| 68 | + |
| 69 | + return |
| 70 | + |
| 71 | + if not run.success: |
| 72 | + message += "**Running failed**\n" |
| 73 | + message += "Command " |
| 74 | + message += f"```bash\n>{_limit_length(run.command, 1000)}```\n" |
| 75 | + message += f"exited with error code **{run.exit_code}** after {run.duration} seconds." |
| 76 | + |
| 77 | + if len(run.stderr.strip()) > 0: |
| 78 | + message += _send_split_log(thread, message, "Program stderr", run.stderr.strip()) |
| 79 | + |
| 80 | + if len(run.stdout.strip()) > 0: |
| 81 | + message += _send_split_log(thread, message, "Program stdout", run.stdout.strip()) |
| 82 | + |
| 83 | + if len(message) != 0: |
| 84 | + thread.send(message) |
| 85 | + |
| 86 | + return |
| 87 | + |
| 88 | + if not run.passed: |
| 89 | + message += "**Testing failed**\n" |
| 90 | + message += "Command " |
| 91 | + message += f"```bash\n>{_limit_length(run.command, 1000)}```\n" |
| 92 | + message += f"ran successfully in {run.duration} seconds, but did not pass all tests.\n" |
| 93 | + |
| 94 | + if len(run.stderr.strip()) > 0: |
| 95 | + message += _send_split_log(thread, message, "Program stderr", run.stderr.strip()) |
| 96 | + |
| 97 | + if len(run.stdout.strip()) > 0: |
| 98 | + message += _send_split_log(thread, message, "Program stdout", run.stdout.strip()) |
| 99 | + |
| 100 | + if len(message) != 0: |
| 101 | + thread.send(message) |
| 102 | + |
| 103 | + # TODO dedicated "error" entry in our results dict that gets populated by check_implementation |
| 104 | + return |
| 105 | + |
| 106 | + # OK, we were successful |
| 107 | + message += "**Success!**\n" |
| 108 | + message += _send_split_log(thread, message, "Result", pprint.pformat(run.result)) |
| 109 | + |
| 110 | + if len(run.stderr.strip()) > 0: |
| 111 | + message += _send_split_log(thread, message, "Program stderr", run.stderr).strip() |
| 112 | + |
| 113 | + if len(run.stdout.strip()) > 0: |
| 114 | + message += _send_split_log(thread, message, "Program stdout", run.stdout.strip()) |
| 115 | + |
| 116 | + if len(message) != 0: |
| 117 | + thread.send(message) |
0 commit comments