Skip to content

Commit

Permalink
feat(py): support Rust for stomp.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vEnhance committed Aug 20, 2024
1 parent 21cb2f7 commit e8f8e26
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions py-scripts/stomp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import subprocess
import sys
import tempfile
from pathlib import Path

TERM_COLOR: dict[str, str] = {}
Expand Down Expand Up @@ -28,11 +29,11 @@

parser = argparse.ArgumentParser(
"stomp",
description="Stomps on your work. A C++/Py3 grader for comp programming.",
description="Stomps on your work. A Rust/C++/Py3 grader for comp programming.",
)
parser.add_argument(
"program_path",
help="The C++/Py3 program that you're going to use.",
help="The Rust/C++/Py3 program that you're going to use.",
)
parser.add_argument(
"-o",
Expand All @@ -52,9 +53,14 @@
PROGRAM_TYPE = "PYTHON"
elif opts.program_path.endswith(".cpp"):
PROGRAM_TYPE = "C++"
elif opts.program_path.endswith(".rs"):
PROGRAM_TYPE = "RUST"
else:
raise ValueError(f"stomp doesn't support {opts.program_path} yet")

TMPDIR = Path(tempfile.gettempdir())
binary_output_path = TMPDIR / (Path(opts.program_path).stem + ".out")


if __name__ == "__main__":
if PROGRAM_TYPE == "C++":
Expand All @@ -72,6 +78,8 @@
"-std=c++17",
opts.program_path,
"-DDEBUG",
"-o",
binary_output_path,
]
)
if compile_process.returncode != 0:
Expand All @@ -81,6 +89,19 @@
sys.exit(1)
else:
print("🆗 Compilation OK")
elif PROGRAM_TYPE == "RUST":
print("⏳ Compiling Rust code...")
compile_process = subprocess.run(
["rustc", opts.program_path, "-o", binary_output_path]
)
if compile_process.returncode != 0:
print(
f"👿 {TERM_COLOR['BOLD_YELLOW']} COMPILATION FAILED{TERM_COLOR['RESET']}"
)
sys.exit(1)
else:
print("🆗 Compilation OK")

elif PROGRAM_TYPE == "PYTHON":
print("🐍 Python programs don't need compilers haha")
else:
Expand All @@ -100,7 +121,14 @@
):
if PROGRAM_TYPE == "C++":
process = subprocess.run(
["./a.out"],
[binary_output_path],
stdin=input_file,
stdout=stdout_file,
stderr=stderr_file,
)
elif PROGRAM_TYPE == "RUST":
process = subprocess.run(
[binary_output_path],
stdin=input_file,
stdout=stdout_file,
stderr=stderr_file,
Expand Down

0 comments on commit e8f8e26

Please sign in to comment.