Skip to content

Commit a0736af

Browse files
mzuennimpsijm
authored andcommitted
Use Validator.source_dir at all places (#444)
* use source_dir at all places * use source_dir even more * fix bits/stdc++ check
1 parent b721537 commit a0736af

File tree

7 files changed

+47
-37
lines changed

7 files changed

+47
-37
lines changed

bin/export.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from contest import *
1212
from latex import PdfType
1313
from problem import Problem
14+
from validate import InputValidator, AnswerValidator, OutputValidator
1415

1516

1617
def select_languages(problems: list[Problem]) -> list[str]:
@@ -118,8 +119,8 @@ def build_problem_zip(problem: Problem, output: Path) -> bool:
118119
("solution/*", False),
119120
("problem_slide/*", False),
120121
("generators/*", False),
121-
("input_validators/**/*", True),
122-
("answer_validators/**/*", False), # TODO make required when not problem.interactive?
122+
(f"{InputValidator.source_dir}/**/*", True),
123+
(f"{AnswerValidator.source_dir}/**/*", False), # TODO required when not interactive?
123124
("submissions/accepted/**/*", True),
124125
("submissions/*/**/*", False),
125126
("attachments/**/*", problem.interactive or problem.multi_pass),
@@ -133,7 +134,7 @@ def build_problem_zip(problem: Problem, output: Path) -> bool:
133134
files.append((PdfType.SOLUTION.path(language, ".pdf").name, False))
134135

135136
if problem.custom_output:
136-
files.append(("output_validator/**/*", True))
137+
files.append((f"{OutputValidator.source_dir}/**/*", True))
137138

138139
message("preparing zip file content", "Zip", problem.path, color_type=MessageType.LOG)
139140

@@ -205,9 +206,9 @@ def add_testcase(in_file: Path) -> None:
205206
if problem.settings.constants:
206207
constants_supported = [
207208
"data/**/testdata.yaml",
208-
"input_validators/**/*",
209-
"answer_validators/**/*",
210-
"output_validator/**/*",
209+
f"{InputValidator.source_dir}/**/*",
210+
f"{AnswerValidator.source_dir}/**/*",
211+
f"{OutputValidator.source_dir}/**/*",
211212
# "statement/*", "solution/*", "problem_slide/*", use \constant{} commands
212213
# "submissions/*/**/*", removed support?
213214
]
@@ -316,10 +317,10 @@ def add_testcase(in_file: Path) -> None:
316317
util.error(f"{f}: no name set for language {lang}.")
317318

318319
# rename output_validator dir
319-
if (export_dir / "output_validator").exists():
320+
if (export_dir / OutputValidator.source_dir).exists():
320321
(export_dir / "output_validators").mkdir(parents=True)
321-
(export_dir / "output_validator").rename(
322-
export_dir / "output_validators" / "output_validator"
322+
(export_dir / OutputValidator.source_dir).rename(
323+
export_dir / "output_validators" / OutputValidator.source_dir
323324
)
324325

325326
# rename statement dirs

bin/problem.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ def __init__(
241241
self.interactive: bool = "interactive" in mode
242242
self.multi_pass: bool = "multi-pass" in mode
243243
self.custom_output: bool = (
244-
self.interactive or self.multi_pass or (problem.path / "output_validator").is_dir()
244+
self.interactive
245+
or self.multi_pass
246+
or (problem.path / validate.OutputValidator.source_dir).is_dir()
245247
)
246248

247249
self.name: dict[str, str] = parse_setting(yaml_data, "name", {"en": ""})

bin/program.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,11 @@ def _checks(self, bar: ProgressBar):
307307
for f in self.source_files:
308308
try:
309309
if f.read_text().find("bits/stdc++.h") != -1:
310-
if "validators/" in str(f):
311-
bar.error("Must not depend on bits/stdc++.h.", resume=True)
312-
break
313-
else:
310+
if f.is_relative_to(self.problem.path / "submissions"):
314311
bar.log("Should not depend on bits/stdc++.h")
315-
break
312+
else:
313+
bar.error("Must not depend on bits/stdc++.h.", resume=True)
314+
break
316315
except UnicodeDecodeError:
317316
pass
318317

bin/skel.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
# Local imports
66
import config
7+
import contest
78
import latex
89
from problem import Problem
910
from util import *
10-
import contest
11+
from validate import OutputValidator
1112

1213
try:
1314
import questionary
@@ -254,7 +255,7 @@ def new_problem():
254255
variables,
255256
exist_ok=True,
256257
preserve_symlinks=preserve_symlinks,
257-
skip=[skeldir / "output_validator"] if not custom_output else None,
258+
skip=[skeldir / OutputValidator.source_dir] if not custom_output else None,
258259
)
259260

260261
# Warn about missing problem statement skeletons for non-en languages

bin/stats.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import generate
1414
import latex
1515
import program
16+
import validate
1617
from util import error, exec_command, glob, warn
1718

1819
Selector = str | Callable | list[str] | list[Callable]
@@ -51,9 +52,9 @@ def problem_stats(problems):
5152
("yaml", "problem.yaml"),
5253
("tex", str(latex.PdfType.PROBLEM.path("*")), 1),
5354
("sol", str(latex.PdfType.SOLUTION.path("*")), 1),
54-
(" val: I", ["input_validators/*"]),
55-
("A", ["answer_validators/*"]),
56-
("O", ["output_validator/"]),
55+
(" val: I", [f"{validate.InputValidator.source_dir}/*"]),
56+
("A", [f"{validate.AnswerValidator.source_dir}/*"]),
57+
("O", [f"{validate.OutputValidator.source_dir}/*"]),
5758
(
5859
" sample",
5960
[lambda s: {x.stem for x in s if x.parts[2] == "sample"}],

bin/upgrade.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import config
22
import generate
33
from util import *
4+
from validate import InputValidator, AnswerValidator, OutputValidator
45

56
import shutil
67
from typing import Any
@@ -153,8 +154,8 @@ def upgrade_statement(problem_path: Path, bar: ProgressBar) -> None:
153154

154155
def upgrade_format_validators(problem_path: Path, bar: ProgressBar) -> None:
155156
rename = [
156-
("input_format_validators", "input_validators"),
157-
("answer_format_validators", "answer_validators"),
157+
("input_format_validators", InputValidator.source_dir),
158+
("answer_format_validators", AnswerValidator.source_dir),
158159
]
159160
for old_name, new_name in rename:
160161
old_path = problem_path / old_name
@@ -169,14 +170,17 @@ def upgrade_format_validators(problem_path: Path, bar: ProgressBar) -> None:
169170

170171
def upgrade_output_validators(problem_path: Path, bar: ProgressBar) -> None:
171172
if (problem_path / "output_validators").is_dir():
172-
if (problem_path / "output_validator").exists():
173+
if (problem_path / OutputValidator.source_dir).exists():
173174
bar.error(
174-
"can't rename 'output_validators/', 'output_validator/' already exists", resume=True
175+
f"can't rename 'output_validators/', '{OutputValidator.source_dir}/' already exists",
176+
resume=True,
175177
)
176178
return
177179
content = [*(problem_path / "output_validators").iterdir()]
178180
if len(content) == 1 and content[0].is_dir():
179-
bar.log(f"renaming 'output_validators/{content[0].name}' to 'output_validator/'")
181+
bar.log(
182+
f"renaming 'output_validators/{content[0].name}' to '{OutputValidator.source_dir}/'"
183+
)
180184

181185
def move(src: str, dst: str) -> None:
182186
if Path(src).is_symlink():
@@ -198,11 +202,13 @@ def move(src: str, dst: str) -> None:
198202
else:
199203
Path(src).rename(dst)
200204

201-
shutil.copytree(content[0], problem_path / "output_validator", copy_function=move)
205+
shutil.copytree(
206+
content[0], problem_path / OutputValidator.source_dir, copy_function=move
207+
)
202208
shutil.rmtree(problem_path / "output_validators")
203209
else:
204-
bar.log("renaming 'output_validators/' to 'output_validator/'")
205-
(problem_path / "output_validators").rename(problem_path / "output_validator")
210+
bar.log(f"renaming 'output_validators/' to '{OutputValidator.source_dir}/'")
211+
(problem_path / "output_validators").rename(problem_path / OutputValidator.source_dir)
206212

207213

208214
def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:

bin/validate.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,13 @@ class InputValidator(Validator):
224224
Also supports checktestdata and viva files, with different invocation.
225225
"""
226226

227-
def __init__(self, problem, path, **kwargs):
228-
super().__init__(problem, path, "input_validators", **kwargs)
229-
230227
validator_type: Final[str] = "input"
231228

232229
source_dir: Final[str] = "input_validators"
233230

231+
def __init__(self, problem, path, **kwargs):
232+
super().__init__(problem, path, InputValidator.source_dir, **kwargs)
233+
234234
def run(
235235
self,
236236
testcase: testcase.Testcase,
@@ -284,13 +284,13 @@ class AnswerValidator(Validator):
284284
Also supports checktestdata and viva files, with different invocation.
285285
"""
286286

287-
def __init__(self, problem, path, **kwargs):
288-
super().__init__(problem, path, "answer_validators", **kwargs)
289-
290287
validator_type: Final[str] = "answer"
291288

292289
source_dir: Final[str] = "answer_validators"
293290

291+
def __init__(self, problem, path, **kwargs):
292+
super().__init__(problem, path, AnswerValidator.source_dir, **kwargs)
293+
294294
def run(
295295
self,
296296
testcase: testcase.Testcase,
@@ -335,13 +335,13 @@ class OutputValidator(Validator):
335335
./validator input answer feedbackdir [arguments from problem.yaml] < output
336336
"""
337337

338-
def __init__(self, problem, path, **kwargs):
339-
super().__init__(problem, path, "output_validator", **kwargs)
340-
341338
validator_type: Final[str] = "output"
342339

343340
source_dir: Final[str] = "output_validator"
344341

342+
def __init__(self, problem, path, **kwargs):
343+
super().__init__(problem, path, OutputValidator.source_dir, **kwargs)
344+
345345
def run(
346346
self,
347347
testcase: testcase.Testcase,

0 commit comments

Comments
 (0)