Skip to content
Snippets Groups Projects
Commit 54569673 authored by tuhe's avatar tuhe
Browse files

updates

parent f01174d7
No related branches found
No related tags found
No related merge requests found
Showing
with 1030 additions and 0 deletions
require "AssessmentBase.rb"
module 02105week2
include AssessmentBase
def assessmentInitialize(course)
super("02105week2",course)
@problems = []
end
end
\ No newline at end of file
---
general:
name: 02105week2
description: 'This is the stones-problem'
display_name: '02105 week 2: Stone collection'
handin_filename: stones.py
handin_directory: handin
max_grace_days: 0
handout: 02105week2-handout.zip
writeup: writeup/writeup.html
max_submissions: -1
disable_handins: false
max_size: 2
has_svn: false
category_name: Lab
problems:
- name: Unitgrade score
description: 'Score obtained by automatic grading'
max_score: 10
optional: false
- name: Written feedback
description: 'Written (TA) feedback'
max_score: 0
optional: true
autograder:
autograde_timeout: 180
autograde_image: tango_python_tue2
release_score: true
# problems:
# - name: Correctness
# description: ''
# max_score: 100.0
# optional: false
\ No newline at end of file
#
# Makefile to manage the example Hello Lab
#
# Get the name of the lab directory
# LAB = $(notdir $(PWD)) # Fail on windows for some reason...
all: handout handout-tarfile
handout:
# Rebuild the handout directory that students download
(rm -rf 02105week2-handout; mkdir 02105week2-handout)
cp -p src/Makefile-handout 02105week2-handout/Makefile
cp -p src/README-handout 02105week2-handout/README
cp -p src/driver_python.py 02105week2-handout
cp -p src/student_sources.zip 02105week2-handout
cp -p src/stones.py 02105week2-handout
cp -p src/docker_helpers.py 02105week2-handout
cp -p src/stones_tests_grade.py 02105week2-handout
cp -p src/StoneReport_handin.token 02105week2-handout
handout-tarfile: handout
# Build *-handout.tar and autograde.tar
# tar cvf 02105week2-handout.tar 02105week2-handout
# cp -p 02105week2-handout.tar autograde.tar
tar cvf 02105week2-handout.tar autograde.tar
# cp -p 02105week2-handout.tar autograde.tar
clean:
# Clean the entire lab directory tree. Note that you can run
# "make clean; make" at any time while the lab is live with no
# adverse effects.
rm -f *~ *.tar
(cd src; make clean)
(cd test-autograder; make clean)
rm -rf 02105week2-handout
rm -f autograde.tar
#
# CAREFULL!!! This will delete all student records in the logfile and
# in the handin directory. Don't run this once the lab has started.
# Use it to clean the directory when you are starting a new version
# of the lab from scratch, or when you are debugging the lab prior
# to releasing it to the students.
#
cleanallfiles:
# Reset the lab from scratch.
make clean
rm -f log.txt
rm -rf handin/*
all:
tar xf autograde.tar
cp stones.py 02105week2-autograde
(cd 02105week2-autograde; python3 driver_python.py)
clean:
rm -rf *~ 02105week2-autograde
\ No newline at end of file
File added
# Makefile for the Hello Lab
all:
echo "Makefile called... it is empty so far. "
#gcc hello3.c -o hello3
clean:
rm -rf *~ hello3
# Makefile for the Hello Lab
all:
echo "Makefile called... it is empty so far. "
#gcc hello3.c -o hello3
clean:
rm -rf *~ hello3
This directory contains all of the code files for the Hello Lab,
including the files that are handed out to students.
Files:
# Autograder and solution files
Makefile Makefile and ...
README ... README for this directory
driver.sh* Autograder
hello.c Solution hello.c file
# Files that are handed out to students
Makefile-handout Makefile and ...
README-handout ... README handed out to students
hello.c-handout Blank hello.c file handed out to students
This directory contains all of the code files for the Hello Lab,
including the files that are handed out to students.
Files:
# Autograder and solution files
Makefile Makefile and ...
README ... README for this directory
driver.sh* Autograder
hello.c Solution hello.c file
# Files that are handed out to students
Makefile-handout Makefile and ...
README-handout ... README handed out to students
hello.c-handout Blank hello.c file handed out to students
This diff is collapsed.
import os
import glob
import shutil
import time
import zipfile
import io
import subprocess
import urllib.request
def download_docker_images(destination=None):
if destination is None:
destination = os.getcwd()
if not os.path.exists(destination):
os.makedirs(destination)
print('Beginning file download with urllib2...')
url = 'https://gitlab.compute.dtu.dk/tuhe/unitgrade_private/-/archive/master/unitgrade_private-master.zip?path=docker_images'
result, headers = urllib.request.urlretrieve(url)
ex = result +"_extract"
zf = zipfile.ZipFile(result)
zf.extractall(path=ex)
dockers = [f for f in zf.namelist() if f[-1] == "/" and len( [s for s in f if s == "/"] ) == 3]
for f in dockers: # zf.namelist():
tmp_dir = ex + "/" + f
if os.path.isdir(tmp_dir):
dest = destination +"/"+os.path.basename(tmp_dir[:-1])
if os.path.isdir(dest):
print("> Destination for docker image", dest, "exists. Skipping download.")
else:
print("> Extracting docker image", os.path.basename(f[:-1]), "to", dest)
shutil.copytree(tmp_dir, dest)
def compile_docker_image(Dockerfile, tag=None, no_cache=False):
assert os.path.isfile(Dockerfile)
base = os.path.dirname(Dockerfile)
if tag == None:
tag = os.path.basename(base)
cmd = f"cd {base} && docker build {'--no-cache' if no_cache else ''} --tag {tag} ."
os.system(cmd)
return tag
def student_token_file_runner(host_tmp_dir, student_token_file, instructor_grade_script, grade_file_relative_destination):
"""
This code is used to run student unitgrade tests (i.e., a .token file).
Use by autolab code.
It accepts a student .token file which is extracted, the 'correct' instructor grade script is copied
into it, and it is then run.
:param Dockerfile_location:
:param host_tmp_dir:
:param student_token_file:
:param ReportClass:
:param instructor_grade_script:
:return:
"""
assert os.path.exists(student_token_file)
assert os.path.exists(instructor_grade_script)
from unitgrade_private import load_token
start = time.time()
results, _ = load_token(student_token_file)
sources = results['sources'][0]
with io.BytesIO(sources['zipfile']) as zb:
with zipfile.ZipFile(zb) as zip:
zip.extractall(host_tmp_dir)
# Done extracting the zip file! Now time to move the (good) report test class into the location.
gscript = instructor_grade_script
print(f"{sources['report_relative_location']=}")
print(f"{sources['name']=}")
print("Now in docker_helpers.py")
print(f'{gscript=}')
print(f'{instructor_grade_script=}')
gscript_destination = host_tmp_dir + "/" + grade_file_relative_destination
print(f'{gscript_destination=}')
shutil.copy(gscript, gscript_destination)
# Now everything appears very close to being set up and ready to roll!.
d = os.path.normpath(grade_file_relative_destination).split(os.sep)
d = d[:-1] + [os.path.basename(instructor_grade_script)[:-3]]
pycom = ".".join(d)
"""
docker run -v c:/Users/tuhe/Documents/2021/python-docker/tmp:/app python-docker python3 -m cs202courseware.ug2report1_grade
"""
pycom = "python3 -m " + pycom
print(f"{pycom=}")
token_location = host_tmp_dir + "/" + os.path.dirname( grade_file_relative_destination ) + "/*.token"
elapsed = time.time() - start
# print("Elapsed time is", elapsed)
return pycom, host_tmp_dir, token_location
def docker_run_token_file(Dockerfile_location, host_tmp_dir, student_token_file, tag=None, instructor_grade_script=None,
fix_user=None,
# grade_script_relative_destination_dir=None, # The relative location relative to the top-dir containing the package. Example: irlc/project1
xvfb=True):
"""
xvfb: Control whether to use X-windows. Works on linux. This seems like a good idea when using e.g. gym.
This thingy works:
To build the image, run:
docker build --tag python-docker .
To run the app run:
docker run -v c:/Users/tuhe/Documents/2021/python-docker/tmp:/app python-docker > output.log
"""
Dockerfile_location = Dockerfile_location.replace("\\", "/")
host_tmp_dir = host_tmp_dir.replace("\\", "/")
student_token_file = student_token_file.replace("\\", "/")
# A bunch of tests. This is going to be great!
Dockerfile_location = os.path.abspath(Dockerfile_location)
assert os.path.exists(Dockerfile_location)
start = time.time()
if fix_user is None:
fix_user = os.name != 'nt' # On Linux, this should probably be true to avoid problem with edit-rights of docker-created files.
# with open(student_token_file, 'rb') as f:
# results = pickle.load(f)
from unitgrade_private import load_token
results, _ = load_token(student_token_file)
sources = results['sources'][0]
if os.path.exists(host_tmp_dir):
shutil.rmtree(host_tmp_dir)
with io.BytesIO(sources['zipfile']) as zb:
with zipfile.ZipFile(zb) as zip:
zip.extractall(host_tmp_dir)
# Done extracting the zip file! Now time to move the (good) report test class into the location.
gscript = instructor_grade_script
# if grade_script_relative_destination_dir is None:
# student_grade_script = host_tmp_dir + "/" + sources['report_relative_location']
# else:
# student_grade_script = host_tmp_dir + "/" + grade_script_relative_destination_dir
# Get relative location from first line of the grade script.
with open(instructor_grade_script, 'r') as f:
student_grade_script_dir = os.path.dirname( host_tmp_dir + "/" + f.read().splitlines()[0][1:].strip() )
print("student_grade_script", student_grade_script_dir)
student_grade_script_dir = student_grade_script_dir.replace("\\", "/")
instructor_grade_script = student_grade_script_dir + "/"+os.path.basename(gscript)
shutil.copy(gscript, instructor_grade_script)
"""
docker run -v c:/Users/tuhe/Documents/2021/python-docker/tmp:/home python-docker python3 -m cs202courseware.ug2report1_grade
"""
if tag is None:
dockname = os.path.basename( os.path.dirname(Dockerfile_location) )
else:
dockname = tag
tmp_grade_file = sources['name'] + "/" + sources['report_relative_location']
tmp_grade_file = tmp_grade_file.replace("\\", "/")
# pycom = ".".join( sources['report_module_specification'][:-1] + [os.path.basename(gscript)[:-3],] )
pycom = ".".join(os.path.relpath(instructor_grade_script, host_tmp_dir)[:-3].split("/"))
pycom = "python3 -m " + pycom
if fix_user:
user_cmd = ' --user "$(id -u):$(id -g)" '
else:
user_cmd = ''
if xvfb:
user_cmd = " -e DISPLAY=:0 -v /tmp/.X11-unix:/tmp/.X11-unix " + user_cmd
tmp_path = os.path.abspath(host_tmp_dir).replace("\\", "/")
dcom = f"docker run {user_cmd} -v {tmp_path}:/home {dockname} {pycom}"
cdcom = f"cd {os.path.dirname(Dockerfile_location)}"
fcom = f"{cdcom} && {dcom}"
print("> Running docker command")
print(fcom)
init = time.time() - start
# thtools.execute_command(fcom.split())
out = subprocess.check_output(fcom, shell=True).decode("utf-8")
host_tmp_dir +"/" + os.path.dirname(tmp_grade_file) + "/"
tokens = glob.glob( os.path.dirname(instructor_grade_script) + "/*.token" )
for t in tokens:
print("Source image produced token", t)
elapsed = time.time() - start
print("Elapsed time is", elapsed, f"({init=} seconds)")
if len(tokens) != 1:
print("Wrong number of tokens produced:", len(tokens))
print(out)
return tokens[0]
#!/bin/bash
# driver.sh - The simplest autograder we could think of. It checks
# that students can write a C program that compiles, and then
# executes with an exit status of zero.
# Usage: ./driver.sh
# Compile the code
# echo "Compiling hello3.c"
# python3 -c "print('Hello world from python 2')"
# python3 --version
python3 driver_python.py
#(make clean; make)
#status=$?
#if [ ${status} -ne 0 ]; then
# echo "Failure: Unable to compile hello3.c (return status = ${status})"
# echo "{\"scores\": {\"Correctness\": 0}}"
# exit
#fi
#
# Run the code
#echo "Running ./hello3"
#./hello3
#status=$?
#if [ ${status} -eq 0 ]; then
# echo "Success: ./hello3 runs with an exit status of 0"
# echo "{\"scores\": {\"Correctness\": 100}}"
#else
# echo "Failure: ./hello fails or returns nonzero exit status of ${status}"
# echo "{\"scores\": {\"Correctness\": 0}}"
#fi
exit
#!/bin/bash
# driver.sh - The simplest autograder we could think of. It checks
# that students can write a C program that compiles, and then
# executes with an exit status of zero.
# Usage: ./driver.sh
# Compile the code
# echo "Compiling hello3.c"
# python3 -c "print('Hello world from python 2')"
# python3 --version
python3 driver_python.py
#(make clean; make)
#status=$?
#if [ ${status} -ne 0 ]; then
# echo "Failure: Unable to compile hello3.c (return status = ${status})"
# echo "{\"scores\": {\"Correctness\": 0}}"
# exit
#fi
#
# Run the code
#echo "Running ./hello3"
#./hello3
#status=$?
#if [ ${status} -eq 0 ]; then
# echo "Success: ./hello3 runs with an exit status of 0"
# echo "{\"scores\": {\"Correctness\": 100}}"
#else
# echo "Failure: ./hello fails or returns nonzero exit status of ${status}"
# echo "{\"scores\": {\"Correctness\": 0}}"
#fi
exit
import os
import glob
import shutil
import sys
import subprocess
from unitgrade_private.autolab.autolab import format_autolab_json
from unitgrade_private.docker_helpers import student_token_file_runner
from unitgrade_private import load_token
import time
import unitgrade_private
verbose = False
tag = "[driver_python.py]"
if not verbose:
print("="*10)
print(tag, "Starting unitgrade evaluation...")
import unitgrade
print(tag, "Unitgrade version", unitgrade.version.__version__)
print(tag, "Unitgrade-devel version", unitgrade_private.version.__version__)
sys.stderr = sys.stdout
wdir = os.getcwd()
def pfiles():
print("> Files in dir:")
for f in glob.glob(wdir + "/*"):
print(f)
print("---")
handin_filename = "stones.py"
student_token_file = 'StoneReport_handin.token'
instructor_grade_script = 'stones_tests_grade.py'
grade_file_relative_destination = "stones_tests_grade.py"
host_tmp_dir = wdir + "/tmp"
homework_file = "stones.py"
# homework_file = "stones.py"
student_should_upload_token = False # Add these from template.
if not verbose:
pfiles()
print(f"{host_tmp_dir=}")
print(f"{student_token_file=}")
print(f"{instructor_grade_script=}")
print("Current directory", os.getcwd())
print("student_token_file", student_token_file)
for f in glob.glob(os.getcwd() + "/*"):
print(f)
try:
# This is how we get the student file structure.
command, host_tmp_dir, token = student_token_file_runner(host_tmp_dir, student_token_file, instructor_grade_script,
grade_file_relative_destination)
# run the stuff.
if not student_should_upload_token:
""" Add the student homework to the right location. """
print("Moving uploaded file from", os.path.basename(handin_filename), "to", handin_filename)
# print("file exists?", os.path.isfile(os.path.basename(handin_filename)))
shutil.move(os.path.basename(handin_filename), host_tmp_dir + "/" + handin_filename)
command = f"cd tmp && {command} --noprogress --autolab"
def rcom(cm):
rs = subprocess.run(cm, capture_output=True, text=True, shell=True)
print(rs.stdout)
if len(rs.stderr) > 0:
print(tag, "There were errors in executing the file:")
print(rs.stderr)
start = time.time()
rcom(command)
ls = glob.glob(token)
f = ls[0]
results, _ = load_token(ls[0])
except Exception as e:
if not student_should_upload_token:
print(tag, "A major error occured while starting unitgrade.")
print(tag, "This can mean the grader itself is badly configured, or (more likely) that you submitted a completely wrong file.")
print(tag, "The following is the content of the file you uploaded; is it what you expect?")
with open(host_tmp_dir + "/" + handin_filename, 'r') as f:
print( f.read() )
print(" ")
print(tag, "If you cannot resolve the problem, please contact the teacher and include details such as this log, as well as the file you submitted.")
raise e
if verbose:
print(tag, f"{token=}")
print(tag, results['total'])
format_autolab_json(results)
\ No newline at end of file
import os
import glob
import shutil
import sys
import subprocess
from unitgrade_private.autolab.autolab import format_autolab_json
from unitgrade_private.docker_helpers import student_token_file_runner
from unitgrade_private import load_token
import time
import unitgrade_private
verbose = False
tag = "[driver_python.py]"
if not verbose:
print("="*10)
print(tag, "Starting unitgrade evaluation...")
import unitgrade
print(tag, "Unitgrade version", unitgrade.version.__version__)
print(tag, "Unitgrade-devel version", unitgrade_private.version.__version__)
sys.stderr = sys.stdout
wdir = os.getcwd()
def pfiles():
print("> Files in dir:")
for f in glob.glob(wdir + "/*"):
print(f)
print("---")
handin_filename = "stones.py"
student_token_file = 'StoneReport_handin.token'
instructor_grade_script = 'stones_tests_grade.py'
grade_file_relative_destination = "stones_tests_grade.py"
host_tmp_dir = wdir + "/tmp"
homework_file = "stones.py"
# homework_file = "stones.py"
student_should_upload_token = False # Add these from template.
if not verbose:
pfiles()
print(f"{host_tmp_dir=}")
print(f"{student_token_file=}")
print(f"{instructor_grade_script=}")
print("Current directory", os.getcwd())
print("student_token_file", student_token_file)
for f in glob.glob(os.getcwd() + "/*"):
print(f)
try:
# This is how we get the student file structure.
command, host_tmp_dir, token = student_token_file_runner(host_tmp_dir, student_token_file, instructor_grade_script,
grade_file_relative_destination)
# run the stuff.
if not student_should_upload_token:
""" Add the student homework to the right location. """
print("Moving uploaded file from", os.path.basename(handin_filename), "to", handin_filename)
# print("file exists?", os.path.isfile(os.path.basename(handin_filename)))
shutil.move(os.path.basename(handin_filename), host_tmp_dir + "/" + handin_filename)
command = f"cd tmp && {command} --noprogress --autolab"
def rcom(cm):
rs = subprocess.run(cm, capture_output=True, text=True, shell=True)
print(rs.stdout)
if len(rs.stderr) > 0:
print(tag, "There were errors in executing the file:")
print(rs.stderr)
start = time.time()
rcom(command)
ls = glob.glob(token)
f = ls[0]
results, _ = load_token(ls[0])
except Exception as e:
if not student_should_upload_token:
print(tag, "A major error occured while starting unitgrade.")
print(tag, "This can mean the grader itself is badly configured, or (more likely) that you submitted a completely wrong file.")
print(tag, "The following is the content of the file you uploaded; is it what you expect?")
with open(host_tmp_dir + "/" + handin_filename, 'r') as f:
print( f.read() )
print(" ")
print(tag, "If you cannot resolve the problem, please contact the teacher and include details such as this log, as well as the file you submitted.")
raise e
if verbose:
print(tag, f"{token=}")
print(tag, results['total'])
format_autolab_json(results)
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
<html><body>
To hand in this assignment, upload the file <b>stones.py</b>
</body></html>
\ No newline at end of file
#
# Makefile to manage the example Hello Lab
#
# Get the name of the lab directory
# LAB = $(notdir $(PWD)) # Fail on windows for some reason...
all: handout handout-tarfile
handout:
# Rebuild the handout directory that students download
(rm -rf c02105week2-handout; mkdir c02105week2-handout)
cp -p src/Makefile-handout c02105week2-handout/Makefile
cp -p src/README-handout c02105week2-handout/README
cp -p src/driver_python.py c02105week2-handout
cp -p src/stones.py c02105week2-handout
cp -p src/docker_helpers.py c02105week2-handout
cp -p src/stones_tests_grade.py c02105week2-handout
cp -p src/StoneReport_handin.token c02105week2-handout
handout-tarfile: handout
# Build *-handout.tar and autograde.tar
# tar cvf c02105week2-handout.tar c02105week2-handout
# cp -p c02105week2-handout.tar autograde.tar
tar cvf autograde.tar c02105week2-handout
# cp -p c02105week2-handout.tar autograde.tar
clean:
# Clean the entire lab directory tree. Note that you can run
# "make clean; make" at any time while the lab is live with no
# adverse effects.
rm -f *~ *.tar
(cd src; make clean)
(cd test-autograder; make clean)
rm -rf c02105week2-handout
rm -f autograde.tar
#
# CAREFULL!!! This will delete all student records in the logfile and
# in the handin directory. Don't run this once the lab has started.
# Use it to clean the directory when you are starting a new version
# of the lab from scratch, or when you are debugging the lab prior
# to releasing it to the students.
#
cleanallfiles:
# Reset the lab from scratch.
make clean
rm -f log.txt
rm -rf handin/*
all:
tar xf autograde.tar
cp stones.py c02105week2-autograde
(cd c02105week2-autograde; python3 driver_python.py)
clean:
rm -rf *~ c02105week2-autograde
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment