Skip to content

Commit a7b1870

Browse files
committed
Merge remote-tracking branch 'origin/csm' into nrs
2 parents f17c467 + 220dd45 commit a7b1870

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

jmu_pytest_utils/audit.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def error(message):
144144
print(f"{path}:{node.lineno}:{node.col_offset+1} {message}")
145145
# parse each source file
146146
for path in paths:
147+
if not path.endswith(".py"):
148+
continue
147149
with open(path, encoding="utf-8") as file:
148150
source = file.read()
149151
tree = ast.parse(source, path)

jmu_pytest_utils/builder.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222

2323
def init_cfg():
2424
"""Initialize global variables with default values."""
25-
global SUBMISSION_FILES, AUTOGRADER_TESTS, ADDITIONAL_FILES
25+
global SUBMISSION_FILES, AUTOGRADER_TESTS, ADDITIONAL_FILES, REQUIREMENTS_TXT
2626
global SUBMISSION_LIMIT, FUNCTION_TIMEOUT, SCHOOL_TIME_ZONE, INSTALL_PYTHON_V
2727

2828
SUBMISSION_FILES = []
2929
AUTOGRADER_TESTS = []
3030
ADDITIONAL_FILES = []
31+
REQUIREMENTS_TXT = []
3132
SUBMISSION_LIMIT = os.getenv("SUBMISSION_LIMIT", -1)
3233
FUNCTION_TIMEOUT = os.getenv("FUNCTION_TIMEOUT", 5)
3334
SCHOOL_TIME_ZONE = os.getenv("SCHOOL_TIME_ZONE", "US/Eastern")
@@ -107,8 +108,8 @@ def make_cfg():
107108
for filename in test_files:
108109
module_name = filename[:-3].replace(os.path.sep, ".")
109110
module = importlib.import_module(module_name)
110-
for name in ["SUBMISSION_FILES", "AUTOGRADER_TESTS", "ADDITIONAL_FILES",
111-
"SUBMISSION_LIMIT", "FUNCTION_TIMEOUT", "SCHOOL_TIME_ZONE"]:
111+
for name in ["SUBMISSION_FILES", "AUTOGRADER_TESTS", "ADDITIONAL_FILES", "REQUIREMENTS_TXT",
112+
"SUBMISSION_LIMIT", "FUNCTION_TIMEOUT", "SCHOOL_TIME_ZONE", "INSTALL_PYTHON_V"]:
112113
if hasattr(module, name):
113114
globals()[name] = getattr(module, name)
114115

@@ -139,6 +140,7 @@ def make_cfg():
139140
" ".join(SUBMISSION_FILES).replace("\\", "/"),
140141
" ".join(AUTOGRADER_TESTS).replace("\\", "/"),
141142
" ".join(ADDITIONAL_FILES).replace("\\", "/"),
143+
" ".join(REQUIREMENTS_TXT).replace("\\", "/"),
142144
SUBMISSION_LIMIT,
143145
FUNCTION_TIMEOUT,
144146
SCHOOL_TIME_ZONE,
@@ -165,6 +167,7 @@ def build_cmd(setup=False):
165167
print(" SUBMISSION_FILES =", SUBMISSION_FILES)
166168
print(" AUTOGRADER_TESTS =", AUTOGRADER_TESTS)
167169
print(" ADDITIONAL_FILES =", ADDITIONAL_FILES)
170+
print(" REQUIREMENTS_TXT =", REQUIREMENTS_TXT)
168171
print(" SUBMISSION_LIMIT =", SUBMISSION_LIMIT)
169172
print(" FUNCTION_TIMEOUT =", FUNCTION_TIMEOUT)
170173
print(" SCHOOL_TIME_ZONE =", SCHOOL_TIME_ZONE)
@@ -181,6 +184,16 @@ def build_cmd(setup=False):
181184
points += getattr(function, "weight", 0)
182185
print(f"\033[1;34mAutograder Points: {points}\033[0m")
183186

187+
# Generate requirements.txt file if needed
188+
if REQUIREMENTS_TXT:
189+
if not os.path.exists("requirements.txt"):
190+
print("Creating requirements.txt")
191+
with open("requirements.txt", "w") as file:
192+
for line in REQUIREMENTS_TXT:
193+
file.write(line + "\n")
194+
else:
195+
print("\033[1;31mIgnoring REQUIREMENTS_TXT (requirements.txt exists)\033[0m")
196+
184197
# Copy template files if not already exist
185198
if setup:
186199
for filename in CONFIG_FILES:
@@ -251,6 +264,13 @@ def clean_cmd():
251264
for dirname in CACHE_DIRS:
252265
shutil.rmtree(dirname, True)
253266

267+
# Delete requirements.txt if auto generated
268+
if "REQUIREMENTS_TXT" in globals() and REQUIREMENTS_TXT:
269+
with open("requirements.txt") as file:
270+
lines = [line.rstrip() for line in file]
271+
if lines == REQUIREMENTS_TXT:
272+
delete_file("requirements.txt", "auto generated")
273+
254274
# Delete template files (if not modified)
255275
for filename in CONFIG_FILES + SCRIPT_FILES:
256276
if os.path.exists(filename):

jmu_pytest_utils/common.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Common test functions used in many autograders."""
22

33
import inspect
4+
import json
45
import os
56
import pytest
67
import subprocess
@@ -20,6 +21,23 @@ def chdir_test():
2021
break
2122

2223

24+
def get_username(default="username"):
25+
"""Get the student's username from the submission metadata.
26+
27+
Args:
28+
default (str): Value to return if metadata not found.
29+
30+
Returns:
31+
str: The student's email address up to the @ symbol.
32+
"""
33+
try:
34+
with open("/autograder/submission_metadata.json") as file:
35+
metadata = json.load(file)
36+
return metadata["users"][0]["email"].split("@")[0]
37+
except FileNotFoundError:
38+
return default
39+
40+
2341
def _get_cfg(filename):
2442
"""Get the path of an optional configuration file.
2543

jmu_pytest_utils/template/config.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ export AUTOGRADER_TESTS="{1}"
1717
# Default: blank
1818
export ADDITIONAL_FILES="{2}"
1919

20+
# Packages to install with pip
21+
# Example: "beautifulsoup4 requests"
22+
# Default: blank
23+
export REQUIREMENTS_TXT="{3}"
24+
2025
# Max submissions that can run
2126
# Default: -1
22-
export SUBMISSION_LIMIT="{3}"
27+
export SUBMISSION_LIMIT="{4}"
2328

2429
# Max seconds per test function
2530
# Default: 5
26-
export FUNCTION_TIMEOUT="{4}"
31+
export FUNCTION_TIMEOUT="{5}"
2732

2833
# Time zone for submissions
2934
# Default: US/Eastern
30-
export SCHOOL_TIME_ZONE="{5}"
35+
export SCHOOL_TIME_ZONE="{6}"
3136

3237
# Python version to install
3338
# Default: 3.12
34-
export INSTALL_PYTHON_V="{6}"
39+
export INSTALL_PYTHON_V="{7}"

jmu_pytest_utils/template/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ source .venv/bin/activate
1515
pip install --upgrade pip
1616

1717
# Install libraries
18-
pip install git+https://github.com/JMU-CS/jmu_pytest_utils.git
18+
pip install git+https://github.com/JMU-CS/jmu_pytest_utils.git@csm
1919
if [ -f "requirements.txt" ]; then
2020
pip install -r requirements.txt
2121
fi

0 commit comments

Comments
 (0)