From cf1118f2894150f5b2e3cd64c757658c8a7d896d Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Sat, 12 Oct 2024 09:54:46 -0400 Subject: [PATCH] Improve file-io grader to prevent race conditions --- docs/source/usage/graders/examples/file_io.py | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/docs/source/usage/graders/examples/file_io.py b/docs/source/usage/graders/examples/file_io.py index d1c13475..003525cf 100644 --- a/docs/source/usage/graders/examples/file_io.py +++ b/docs/source/usage/graders/examples/file_io.py @@ -4,11 +4,16 @@ import sys from pathlib import Path -DIR = Path(__file__).parent -INPUT_FILE = DIR / "input.txt" -OUTPUT_FILE = DIR / "output.txt" - submission = sys.argv[1] +student_submission = Path(sys.argv[2]) + +# input file is in the same directory as our grader (this file) +INPUT_FILE = Path(__file__).parent / "input.txt" +# output file is in the same directory as the student submission +# This way we can avoid multiple submissions trying to write to +# the same file. +OUTPUT_FILE = student_submission.parent / "output.txt" + command = [ sys.executable, @@ -25,13 +30,14 @@ OUTPUT_FILE, ] -try: - resp = subprocess.run( - command, - capture_output=True, - check=True, - ) -except Exception as e: - print(f"Error in submission: {e}") +resp = subprocess.run( + command, + stdout=sys.stdout, + stderr=subprocess.STDOUT, + check=False, +) + +if resp.returncode == 0: + print(f"Score: {100 if OUTPUT_FILE.read_text() == INPUT_FILE.read_text() else 0}%") else: - print(f"Score: {100 if OUTPUT_FILE.read_text() == '2' else 0}%") + print("Score: 0%")