Skip to content

Commit

Permalink
Make test-gathering script accessible through CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinMeimar committed Nov 12, 2024
1 parent 9d90ff9 commit f564e5b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 47 deletions.
40 changes: 23 additions & 17 deletions dragon_runner/scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import argparse
from pathlib import Path

def build(build_path:str, log_path: str, n_threads: str):
"""
Entry point for dragon-runner to run the script.
"""
print("[RUNNING] build script")

def build(build_path: str, log_path: str, n_threads: str="2"):
directories = [d for d in Path(build_path).iterdir() if d.is_dir() and d.name != '.']

for dir_path in directories:
print(f"-- Building project: {dir_path.name}")
print(f"-- Building project: {dir_path.name}", end='')

try:
os.chdir(dir_path)
Expand All @@ -28,26 +24,36 @@ def build(build_path:str, log_path: str, n_threads: str):
os.chdir('build')

try:
subprocess.run(['cmake', '..'], check=True)
subprocess.run(['make', '-j', n_threads], check=True)
with open(log_path, 'a') as log_file:
log_file.write(f"\n=== Building {dir_path.name} ===\n")
subprocess.run(
['cmake', '..'],
stdout=log_file,
stderr=subprocess.STDOUT,
check=True
)
subprocess.run(
['make', '-j', n_threads],
stdout=log_file,
stderr=subprocess.STDOUT,
check=True
)
print(" [SUCCESS]")
except subprocess.CalledProcessError:
print(f" [FAILED]")
with open(log_path, 'a') as f:
f.write(f"{dir_path.name}: build failed\n")
finally:
os.chdir(build_path)

print(f"Build process completed. Check {log_path} for any failed builds.")
print(f"Build process completed. Check {log_path} for build output and errors.")

if __name__ == '__main__':

"""
An entry point to run the script manually
"""
parser = argparse.ArgumentParser()
parser.add_argument("build_path", type=Path, help="Path to build directory")
parser.add_argument("log_file", type=Path, help="Path to error log file")
parser.add_argument("log_file", type=Path, help="Path to log file")

args = parser.parse_args()
args.log_file.unlink(missing_ok=True)

args.error_file.unlink(missing_ok=True)

build(args.build_path, args.log_file)
46 changes: 46 additions & 0 deletions dragon_runner/scripts/gather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import shutil
import argparse
from pathlib import Path

def gather(ccids_file: str,
search_path: str,
project_name: str,
output_dir: str = "submitted-testfiles"):
"""
Gather all the testfiles in student directories. Look for directories in
@search_path that contain @project_name. Inside each project look for
tests/testfiles/TEAM_NAME and copy it out.
"""
search_dir = Path(search_path)

if not search_dir.is_dir():
error = "Could not create test directory."
print(error)
return 1

directories = [d for d in search_dir.iterdir() if d.is_dir() and project_name in d.name]
ccids = Path(ccids_file).read_text().splitlines()

output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)

for dir_path in directories:
for ccid in ccids:
if ccid in str(dir_path):
expected_test_dir = dir_path / "tests" / "testfiles" / ccid
if expected_test_dir.is_dir():
print(f"-- Found properly formatted testfiles for {ccid}")
shutil.copytree(expected_test_dir, output_path / ccid, dirs_exist_ok=True)
else:
print(f"-- Could NOT find testfiles for {ccid}")

if __name__ == '__main__':

parser = argparse.ArgumentParser()
parser.add_argument("ccids_file", type=Path, help="File containing CCIDs")
parser.add_argument("search_path", type=Path, help="Path to search for test files")
parser.add_argument("project_name", type=Path, help="Path to search for test files")
args = parser.parse_args()

gather(args.ccids_file, args.search_path, args.project_name)

36 changes: 26 additions & 10 deletions dragon_runner/scripts/loader.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@

from typing import List
from pathlib import Path
from dragon_runner.scripts.build import build
# from dragon_runner.scripts.gather import gather
from dragon_runner.scripts.gather import gather

class Loader:
"""
Dragon runner allows grading scripts to be run through its CLI.
"""

def __init__(self, script: str, args: List[str]):
self.script = script
self.args = args

self.errors = []

def run(self):

"""
Select the script to run from the mode argument passed through
dragon-runner CLI.
"""

def unknown_script():
print(f"script: {self.script} did not match any registered script.")

print(f"Running: {self.script} with args {self.args}")

{
"build": lambda: build(*self.args),
}.get(self.script, lambda: unknown_script)()

script_dispatch = {
"build": lambda: build(*self.args),
"gather": lambda: gather(*self.args),
"anon-tests": lambda: print("TODO"),
"anon-csv": lambda: print("TODO"),
"preview": lambda: print("TODO")
}

try:
print(f"Running: {self.script} with args {self.args}")
script_dispatch.get(self.script, lambda: unknown_script)()

except Exception as e:
print(f"Failed to run script: {e}")

20 changes: 0 additions & 20 deletions dragon_runner/scripts/script_loader.py

This file was deleted.

0 comments on commit f564e5b

Please sign in to comment.