icp/oer/exercise-formats/program/match-regex-parallel/controller.py

98 lines
2.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# program/match-regex
# Standardized types do not require authors to specify any control script as
# it can be generated on import. However when provided it will overwrite the
# default behaivior.
import sys
import subprocess
import re
import time
def build(ctx):
# Add filenames
allArgs = ['/usr/bin/make']
#, self.sourcefile, "-o", "%s.exe" % self.sourcefile]
# start subprocess in with work_path as cwd
p = subprocess.Popen(allArgs, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=ctx.work_path)
msgs,errs = p.communicate()
retval = p.wait()
errs = errs.decode("utf-8")
msgs = msgs.decode("utf-8")
return retval, errs, msgs
def execute(ctx):
print("parallel execute()", file=sys.stderr)
# if build failed, return early
if ctx.build_result[0] != 0:
return
allArgs = ["/usr/bin/sbatch", ctx.work_path + "/job.slurm"]
p = subprocess.Popen(allArgs, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=ctx.work_path)
msgs,errs = p.communicate()
retval = p.wait()
errs = errs.decode("utf-8")
msgs = msgs.decode("utf-8")
ctx.execute_result = (retval, errs, msgs)
#ctx.celery_task.update_state(state='SUBMITTED TO SLURM', meta=response(ctx))
#print("parallel execute(), before sleep", file=sys.stderr)
#time.sleep(20)
#print("parallel execute(), after sleep", file=sys.stderr)
retval = ctx.get_file_content('job.exit')
errs = ctx.get_file_content('job.err')
msgs = ctx.get_file_content('job.out')
print( (retval, errs, msgs), file=sys.stderr)
return retval, errs, msgs
def grade(ctx):
"""A function that governs the grading process when different from default.
Expected is return value that indicates if the task passed or failed."""
print("parallel grade()", file=sys.stderr)
# if build failed, return early and set grade to FAIL
if ctx.build_result[0] != 0:
return 'FAIL'
ctx.execute_result = execute(ctx)
print(ctx.data['data']['regex'], file=sys.stderr)
print(ctx.execute_result, file=sys.stderr)
if re.match(ctx.data['data']['regex'], ctx.execute_result[2]) != None:
return 'PASS'
else:
return 'FAIL'
def response(ctx):
output = ""
if ctx.build_result[0] != 0:
output = ctx.build_result[1]
else:
output = ctx.execute_result[2]
grade = ctx.grade_result
msg = ""
return {'output': output, 'grade': grade, 'msg': msg, 'data': None}