|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import csv |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from dotenv import load_dotenv |
| 11 | +from playwright.sync_api import sync_playwright |
| 12 | + |
| 13 | +load_dotenv() |
| 14 | + |
| 15 | +playwright = sync_playwright().start() |
| 16 | + |
| 17 | +browser = playwright.chromium.launch() |
| 18 | +page = browser.new_page() |
| 19 | + |
| 20 | +page.goto("https://cses.fi/problemset/") |
| 21 | +page.get_by_text("Login").click() |
| 22 | +page.fill('input[type="text"]', os.environ["CSES_USERNAME"]) |
| 23 | +page.fill('input[type="password"]', os.environ["CSES_PASSWORD"]) |
| 24 | +page.click('input[type="submit"]') |
| 25 | + |
| 26 | +task_submit_base_url = "https://cses.fi/problemset/submit" |
| 27 | + |
| 28 | +tasks_csv = Path(__file__).parent / "tasks.csv" |
| 29 | +current_task = None |
| 30 | + |
| 31 | +with tasks_csv.open() as f: |
| 32 | + reader = csv.DictReader(f) |
| 33 | + for row in reader: |
| 34 | + if row["status"] == "unsolved": |
| 35 | + current_task = row |
| 36 | + break |
| 37 | + |
| 38 | + |
| 39 | +def test(): |
| 40 | + task_dir = Path(__file__).parent / "tasks" / current_task["path"] |
| 41 | + print( |
| 42 | + "\n", |
| 43 | + "\033[33m", |
| 44 | + " ".join( |
| 45 | + word.title() for word in current_task["path"].split("/")[1].split("-")[1:] |
| 46 | + ), |
| 47 | + "\033[0m", |
| 48 | + "\n", |
| 49 | + ) |
| 50 | + for example_dir in (task_dir / "examples").iterdir(): |
| 51 | + with (example_dir / "in").open("r") as f_in, (example_dir / "out").open( |
| 52 | + "r" |
| 53 | + ) as f_out: |
| 54 | + result = None |
| 55 | + try: |
| 56 | + expected_output = f_out.read().strip() |
| 57 | + |
| 58 | + result = subprocess.run( |
| 59 | + [sys.executable, task_dir / "solution.py"], |
| 60 | + input=f_in.read(), |
| 61 | + capture_output=True, |
| 62 | + text=True, |
| 63 | + timeout=2, |
| 64 | + ) |
| 65 | + |
| 66 | + if result.returncode != 0: |
| 67 | + print( |
| 68 | + " " * 2, |
| 69 | + "\033[31m", |
| 70 | + f"[{example_dir.name}]", |
| 71 | + "RUNTIME ERROR", |
| 72 | + "\033[0m", |
| 73 | + ) |
| 74 | + print( |
| 75 | + " " * 6, |
| 76 | + "\n".join( |
| 77 | + " " * 6 + line for line in result.stderr.splitlines() |
| 78 | + ), |
| 79 | + "\n", |
| 80 | + ) |
| 81 | + else: |
| 82 | + output = result.stdout.strip() |
| 83 | + |
| 84 | + if output == expected_output: |
| 85 | + print( |
| 86 | + "\033[32m", |
| 87 | + " " * 2, |
| 88 | + f"[{example_dir.name}]", |
| 89 | + "ACCEPTED", |
| 90 | + "\033[0m", |
| 91 | + ) |
| 92 | + else: |
| 93 | + print( |
| 94 | + " " * 2, |
| 95 | + "\033[31m", |
| 96 | + f"[{example_dir.name}]", |
| 97 | + "WRONG ANSWER", |
| 98 | + "\033[0m", |
| 99 | + ) |
| 100 | + print(" " * 6, repr(output)) |
| 101 | + print(" " * 6, repr(expected_output), "\n") |
| 102 | + |
| 103 | + except subprocess.TimeoutExpired: |
| 104 | + print( |
| 105 | + " " * 2, |
| 106 | + "\033[31m", |
| 107 | + f"[{example_dir.name}]", |
| 108 | + "TIME LIMIT EXCEEDED", |
| 109 | + "\033[0m", |
| 110 | + ) |
| 111 | + |
| 112 | + print() |
| 113 | + |
| 114 | + |
| 115 | +def submit(): |
| 116 | + task_submit_url = f"{task_submit_base_url}/{current_task['cses_id']}/" |
| 117 | + task_dir = Path(__file__).parent / "tasks" / current_task["path"] |
| 118 | + |
| 119 | + page.goto(task_submit_url) |
| 120 | + with page.expect_file_chooser() as fc_info: |
| 121 | + page.click('input[type="file"]') |
| 122 | + |
| 123 | + file_chooser = fc_info.value |
| 124 | + file_chooser.set_files(task_dir / "solution.py") |
| 125 | + page.click('input[type="submit"]') |
| 126 | + |
| 127 | + page.wait_for_selector( |
| 128 | + 'td:has-text("result")', timeout=10000 |
| 129 | + ) # wait for 10 seconds |
| 130 | + |
| 131 | + print( |
| 132 | + "\n", |
| 133 | + "\033[33m", |
| 134 | + " ".join( |
| 135 | + word.title() for word in current_task["path"].split("/")[1].split("-")[1:] |
| 136 | + ), |
| 137 | + "\033[0m", |
| 138 | + end="", |
| 139 | + ) |
| 140 | + verdict = page.locator("td:has-text('result') + td").text_content() |
| 141 | + if verdict == "ACCEPTED": |
| 142 | + print("\033[32m", f"[{verdict}]", "\033[0m", "\n") |
| 143 | + else: |
| 144 | + print("\033[31m", f"[{verdict}]", "\033[0m", "\n") |
| 145 | + |
| 146 | + |
| 147 | +def next_(): |
| 148 | + rows = [] |
| 149 | + with tasks_csv.open("r") as f: |
| 150 | + reader = csv.DictReader(f) |
| 151 | + rows = [ |
| 152 | + {**row, "status": "solved"} if row["path"] == current_task["path"] else row |
| 153 | + for row in reader |
| 154 | + ] |
| 155 | + print(len(rows)) |
| 156 | + with tasks_csv.open("w") as f: |
| 157 | + print(len(rows)) |
| 158 | + writer = csv.DictWriter(f, fieldnames=rows[0].keys()) |
| 159 | + writer.writeheader() |
| 160 | + writer.writerows(rows) |
| 161 | + |
| 162 | + |
| 163 | +def main(): |
| 164 | + parser = argparse.ArgumentParser("cses") |
| 165 | + subparsers = parser.add_subparsers(dest="command") |
| 166 | + |
| 167 | + test_parser = subparsers.add_parser("test") |
| 168 | + test_parser.set_defaults(func=test) |
| 169 | + |
| 170 | + submit_parser = subparsers.add_parser("submit") |
| 171 | + submit_parser.set_defaults(func=submit) |
| 172 | + |
| 173 | + submit_parser = subparsers.add_parser("next") |
| 174 | + submit_parser.set_defaults(func=next_) |
| 175 | + |
| 176 | + args = parser.parse_args() |
| 177 | + if hasattr(args, "func"): |
| 178 | + args.func() |
| 179 | + |
| 180 | + |
| 181 | +if __name__ == "__main__": |
| 182 | + main() |
0 commit comments