-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathparse.py
More file actions
executable file
·358 lines (311 loc) · 11.7 KB
/
parse.py
File metadata and controls
executable file
·358 lines (311 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/python
from urllib.request import urlopen, Request
from html.parser import HTMLParser
from sys import argv
from subprocess import call
from functools import partial, wraps
import os
from pathlib import Path
import re
import html
import time
import argparse
import platform
import logging
###########################
# User modifiable constants
###########################
language_params = {
"c++17": {
"TEMPLATE": "main.cc",
"DEBUG_FLAGS": "-DDEBUG",
"COMPILE_CMD": "g++ -g -std=c++17 -Wall $DBG",
"RUN_CMD": "./a.out",
},
"c++14": {
"TEMPLATE": "main.cc",
"DEBUG_FLAGS": "-DDEBUG",
"COMPILE_CMD": "g++ -g -std=c++14 -Wall $DBG",
"RUN_CMD": "./a.out",
},
"go": {
"TEMPLATE": "main.go",
"COMPILE_CMD": "go build $DBG -o a.out",
"DEBUG_FLAGS": '''"-ldflags '-X=main.DEBUG=Y'"''',
"RUN_CMD": "./a.out",
},
"kotlin": {
"TEMPLATE": "main.kt",
"COMPILE_CMD": "kotlinc -include-runtime -d out.jar",
"DEBUG_FLAGS": "-d",
"RUN_CMD": "java -jar out.jar $DBG",
},
"java": {
"TEMPLATE": "Main.java",
"DEBUG_FLAGS": "-DDEBUG=true",
"COMPILE_CMD": "javac $DBG",
"RUN_CMD": "java -DDEBUG=$DBG Main",
},
"rust": {
"TEMPLATE": "main.rs",
"DEBUG_FLAGS": "--cfg debug_assertions",
"COMPILE_CMD": "rustc $DBG -o a.out",
"RUN_CMD": "./a.out",
},
}
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
SAMPLE_INPUT = "input"
SAMPLE_OUTPUT = "output"
MY_OUTPUT = "my_output"
# Do not modify these!
VERSION = (
"CodeForces Parser v1.5.1: https://github.com/johnathan79717/codeforces-parser"
)
RED_F = "\033[31m"
GREEN_F = "\033[32m"
BOLD = "\033[1m"
NORM = "\033[0m"
if platform.system() == "Darwin":
TIME_CMD = r'`which gtime` -o time.out -f "(%es)"'
else:
TIME_CMD = r'`which time` -o time.out -f "(%es)"'
TIME_AP = r"`cat time.out`"
# Problems parser.
class CodeforcesProblemParser(HTMLParser):
def __init__(self, folder):
HTMLParser.__init__(self)
self.folder = folder
self.num_tests = 0
self.testcase = None
self.start_copy = False
self.end_line = False
def handle_starttag(self, tag, attrs):
if tag == "div":
attr_dict = dict(attrs)
if attr_dict.get("class") == "input":
self.num_tests += 1
if self.testcase:
self.testcase.close()
self.testcase = open(
f"{self.folder}/{SAMPLE_INPUT}{self.num_tests}", "wb"
)
elif attr_dict.get("class") == "output":
if self.testcase:
self.testcase.close()
self.testcase = open(
f"{self.folder}/{SAMPLE_OUTPUT}{self.num_tests}", "wb"
)
elif tag == "pre":
if self.testcase is not None:
self.start_copy = True
def handle_endtag(self, tag):
if tag == "br":
if self.start_copy:
self.testcase.write("\n".encode("utf-8"))
self.end_line = True
if tag == "pre":
if self.start_copy:
if not self.end_line:
self.testcase.write("\n".encode("utf-8"))
self.testcase.close()
self.testcase = None
self.start_copy = False
def handle_entityref(self, name):
if self.start_copy:
self.testcase.write(html.unescape(("&%s;" % name)).encode("utf-8"))
def handle_data(self, data):
if self.start_copy:
self.testcase.write(data.strip("\n").encode("utf-8"))
self.end_line = False
def __del__(self):
if self.testcase:
self.testcase.close()
# Contest parser.
class CodeforcesContestParser(HTMLParser):
def __init__(self, contest):
HTMLParser.__init__(self)
self.contest = contest
self.start_contest = False
self.start_problem = False
self.name = ""
self.problem_name = ""
self.problems = []
self.problem_names = []
def handle_starttag(self, tag, attrs):
attr_dict = dict(attrs)
# More robust contest name detection
if (tag == "a" and
self.name == "" and
attr_dict.get("href") == f"/contest/{self.contest}"):
self.start_contest = True
elif tag == "option":
value = attr_dict.get("value", "")
# Look for problem identifiers (A, B, C, etc.)
if re.match(r'^[A-Z][0-9]?$', value):
self.problems.append(value)
self.start_problem = True
def handle_endtag(self, tag):
if tag == "a" and self.start_contest:
self.start_contest = False
elif self.start_problem:
self.problem_names.append(self.problem_name)
self.problem_name = ""
self.start_problem = False
def handle_data(self, data):
if self.start_contest:
self.name = data
elif self.start_problem:
self.problem_name += data
# Parses each problem page.
def parse_problem(folder, contest, problem):
url = f"https://codeforces.com/contest/{contest}/problem/{problem}"
req = Request(url, headers=headers)
try:
with urlopen(req, timeout=10) as response:
html = response.read()
parser = CodeforcesProblemParser(folder)
parser.feed(html.decode("utf-8"))
logger.info(f"Successfully parsed problem {problem}")
return parser.num_tests
except Exception as e:
logger.error(f"Error parsing problem {problem}: {e}")
return 0
# Parses the contest page.
def parse_contest(contest):
url = f"https://codeforces.com/contest/{contest}"
req = Request(url, headers=headers)
try:
html = urlopen(req).read()
parser = CodeforcesContestParser(contest)
parser.feed(html.decode("utf-8"))
return parser
except Exception as e:
print(f"Error parsing contest {contest}: {e}")
return None
# Generates the test script.
def generate_test_script(folder, language, num_tests, problem):
param = language_params[language]
with open(folder + "test.sh", "w") as test:
test.write(
(
"#!/bin/bash\n"
'DBG=""\n'
'while getopts ":d" opt; do\n'
" case $opt in\n"
" d)\n"
' echo "-d was selected; compiling in DEBUG mode!" >&2\n'
" DBG=" + param["DEBUG_FLAGS"] + "\n"
" ;;\n"
r" \?)" + "\n"
' echo "Invalid option: -$OPTARG" >&2\n'
" ;;\n"
" esac\n"
"done\n"
"\n"
"if ! " + param["COMPILE_CMD"] + " {0}.{1}; then\n"
" exit\n"
"fi\n"
"INPUT_NAME=" + SAMPLE_INPUT + "\n"
"OUTPUT_NAME=" + SAMPLE_OUTPUT + "\n"
"MY_NAME=" + MY_OUTPUT + "\n"
"rm -R $MY_NAME* &>/dev/null\n"
).format(problem, param["TEMPLATE"].split(".")[1])
)
# Fixed: Use INPUT_NAME instead of the literal string
script_content = (
"for test_file in $INPUT_NAME*\n"
"do\n"
" i=${{#INPUT_NAME}}\n" # Fixed: Use INPUT_NAME variable
" test_case=${{test_file:$i}}\n"
" if ! {1} {3} < $INPUT_NAME$test_case > $MY_NAME$test_case; then\n"
' echo "{4}{5}Sample test #$test_case: Runtime Error{6} {2}"\n'
" echo ========================================\n"
' echo "Sample Input #$test_case"\n'
" cat $INPUT_NAME$test_case\n"
" else\n"
" if diff --brief --ignore-space-change --ignore-blank-lines $MY_NAME$test_case $OUTPUT_NAME$test_case; then\n"
' echo "{4}{7}Sample test #$test_case: Accepted{6} {2}"\n'
" else\n"
' echo "{4}{5}Sample test #$test_case: Wrong Answer{6} {2}"\n'
" echo ========================================\n"
' echo "Sample Input #$test_case"\n'
" cat $INPUT_NAME$test_case\n"
" echo ========================================\n"
' echo "Sample Output #$test_case"\n'
" cat $OUTPUT_NAME$test_case\n"
" echo ========================================\n"
' echo "My Output #$test_case"\n'
" cat $MY_NAME$test_case\n"
" echo ========================================\n"
" fi\n"
" fi\n"
"done\n"
).format(
SAMPLE_INPUT, # {0} - Not used in this part
TIME_CMD, # {1}
TIME_AP, # {2}
param["RUN_CMD"], # {3}
BOLD, # {4}
RED_F, # {5}
NORM, # {6}
GREEN_F, # {7}
)
test.write(script_content)
call(["chmod", "+x", folder + "test.sh"])
def main():
print(VERSION)
parser = argparse.ArgumentParser(description="Parse Codeforces contest problems")
parser.add_argument(
"--language",
"-l",
default="c++17",
choices=list(language_params.keys()),
help="The programming language you want to use"
)
parser.add_argument("contest", help="Contest number", type=int)
args = parser.parse_args()
contest = args.contest
language = args.language
# Validate contest number
if contest <= 0:
print("Contest number must be positive")
return
# Find contest and problems.
print(f"Parsing contest {contest} for language {language}, please wait...")
content = parse_contest(contest)
if content is None:
print("Failed to parse contest. Check if the contest number is correct.")
return
if not content.problems:
print("No problems found in this contest.")
return
print(f"{BOLD}{GREEN_F}*** Round name: {content.name} ***{NORM}")
print(f"Found {len(content.problems)} problems!")
# Find problems and test cases.
TEMPLATE = language_params[language]["TEMPLATE"]
for index, problem in enumerate(content.problems):
problem_name = content.problem_names[index] if index < len(content.problem_names) else "Unknown"
print(f"Downloading Problem {problem}: {problem_name}...")
folder = Path(f"{contest}-{language}") / problem
folder.mkdir(parents=True, exist_ok=True)
template_src = Path(TEMPLATE)
template_dst = folder / f"{problem}.{TEMPLATE.split('.')[1]}"
if template_src.exists() and not template_dst.exists():
import shutil
shutil.copy2(template_src, template_dst)
num_tests = parse_problem(str(folder) + "/", contest, problem)
print(f"{num_tests} sample test(s) found.")
if num_tests > 0:
generate_test_script(str(folder) + "/", language, num_tests, problem)
print("=" * 40)
time.sleep(1)
print("Use ./test.sh to run sample tests in each directory.")
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
main()