Skip to content

Commit fd94e89

Browse files
committed
simplify code
1 parent 34b82ce commit fd94e89

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

bin/generate.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,19 @@ def generate_visualization(testcase: Testcase, bar: ProgressBar):
10121012
assert in_path.is_file()
10131013
assert ans_path.is_file()
10141014

1015+
feedbackdir = in_path.with_suffix(".feedbackdir")
1016+
image_files = [f"judgeimage{ext}" for ext in config.KNOWN_VISUALIZER_EXTENSIONS] + [
1017+
f"teamimage{ext}" for ext in config.KNOWN_VISUALIZER_EXTENSIONS
1018+
]
1019+
1020+
def use_feedback_image(feedbackdir: Path, source: str) -> None:
1021+
for name in image_files:
1022+
path = feedbackdir / name
1023+
if path.exists():
1024+
ensure_symlink(in_path.with_suffix(path.suffix), path)
1025+
bar.log(f"Using {name} from {source} as visualization")
1026+
return
1027+
10151028
visualizer: Optional[visualize.AnyVisualizer] = problem.visualizer(
10161029
visualize.InputVisualizer
10171030
)
@@ -1025,7 +1038,9 @@ def generate_visualization(testcase: Testcase, bar: ProgressBar):
10251038
out_path = ans_path
10261039

10271040
if visualizer is None:
1028-
# copy potential teamimage/judgeimage from output validator?
1041+
for ext in config.KNOWN_VISUALIZER_EXTENSIONS:
1042+
in_path.with_suffix(ext).unlink(True)
1043+
use_feedback_image(feedbackdir, "validator")
10291044
return True
10301045

10311046
visualizer_args = testcase.testdata_yaml_args(visualizer, bar)
@@ -1043,16 +1058,13 @@ def generate_visualization(testcase: Testcase, bar: ProgressBar):
10431058
if isinstance(visualizer, visualize.InputVisualizer):
10441059
result = visualizer.run(in_path, ans_path, cwd)
10451060
else:
1046-
feedbackdir = in_path.with_suffix(".feedbackdir")
10471061
feedbackcopy = in_path.with_suffix(".feedbackcopy")
10481062
shutil.rmtree(feedbackcopy)
1049-
shutil.copytree(feedbackdir, feedbackcopy)
1050-
teamimage = feedbackcopy / "teamimage"
1051-
judgeimage = feedbackcopy / "judgeimage"
10521063

1053-
for ext in config.KNOWN_VISUALIZER_EXTENSIONS:
1054-
teamimage.with_suffix(ext).unlink(True)
1055-
judgeimage.with_suffix(ext).unlink(True)
1064+
def skip_images(src: str, content: list[str]) -> list[str]:
1065+
return [] if src != str(feedbackdir) else image_files
1066+
1067+
shutil.copytree(feedbackdir, feedbackcopy, ignore=skip_images)
10561068

10571069
result = visualizer.run(
10581070
in_path,
@@ -1062,19 +1074,7 @@ def generate_visualization(testcase: Testcase, bar: ProgressBar):
10621074
visualizer_args,
10631075
)
10641076
if result.status:
1065-
found = None
1066-
for ext in config.KNOWN_VISUALIZER_EXTENSIONS:
1067-
file = teamimage.with_suffix(ext)
1068-
if file.is_file():
1069-
found = file
1070-
for ext in config.KNOWN_VISUALIZER_EXTENSIONS:
1071-
file = judgeimage.with_suffix(ext)
1072-
if file.is_file():
1073-
found = file
1074-
if found is not None:
1075-
found.rename(in_path.with_suffix(found.suffix))
1076-
bar.log(f"Using {found.name} from output_visualizer as visualization")
1077-
shutil.rmtree(feedbackcopy)
1077+
use_feedback_image(feedbackdir, "output_visualizer")
10781078

10791079
if result.status == ExecStatus.TIMEOUT:
10801080
bar.debug(f"{Style.RESET_ALL}-> {shorten_path(problem, cwd)}")

0 commit comments

Comments
 (0)