# -*- coding: utf-8 -*-
# $Id: JavaRE.py 1634 2013-04-12 15:36:36Z amelung $
#
# Copyright (c) 2007-2011 Otto-von-Guericke-Universität Magdeburg
#
# This file is part of ECSpooler.
import httplib
import tempfile
import logging
# local imports
# The name of the wrapper class that performs the syntactic check
# The name of the wrapper class that performs the semantic check
# load Java function to do a simple test
try:
except IOError:
CLASS_SEMANTIC_CHECK + '.java'),
'r').read()
CLASS_SYNTACTIC_CHECK + '.java'),
'r').read()
# input schema
'modelSolution',
),
'helpFunctions',
),
'testData',
#accessor = # must return a list; default is one element per line
'A function call consists of the ' +
'function name (given in the exercise directives) ' +
'and test data as parameters of this funtion. '+
'Each function call must be written in a single line.',
),
))
# testSchema
'findFirstGroup0',
),
))
#STRING_RE = re.compile(r'"(?:[^"\n\r\\]+|(?:\\.)+)*"')
def non_null_str(s):
"""
"""
def string_p(s):
"""
"""
#log.debug('(2): %s' % s)
#log.debug('(2a): %s' % repr(ret))
return ret
"""
A backend class for checking regulare expressions using Java.
Solutions have to be entered as they would appear in the Java program,
i. e. you'd enter things like
"!^.*foo.*$"
or
"Match \\[ a bracket"
in the "Answer" field. Invalid examples would be
!^.*foo.*$
(missing quote marks) or
"Match \[ a bracket"
('\[' is not valid in a Java string. To escape a reg-exp meta char,
you need to prefix it with '\\', not just '\').
Issues:
* Syntax check has to be improved. In particular, we'd have to run
the syntax check wrapper, too, to find out whether Java's
"Pattern.compile()" raises an error.
* Need better test environments (find all, compare sub groups, etc.)
"""
"""
This constructor is needed to reset the logging environment.
"""
"""
"""
if prot == 'http':
try:
try:
except:
raise Exception("Cannot download file: %s "
"Connecting to host failed."
% url)
r = conn.getresponse()
assert r.status == 200, \
"Cannot download file: %s. Server response: %d, %s" \
return r.read()
finally:
else:
raise Exception("Cannot download file: %s "
"Unsupported protocol: %s"
# -- check syntax ---------------------------------------------------------
"""
Replace module name in students' submission.
@see: ProgrammingBackend._preProcessCheckSyntax
@return: modified source code and new module name
"""
#log.debug('xxx: here we are in _preProcessCheckSyntax')
#log.debug('(1): %s ' % src)
# FIXME: It'd be nice to be able to throw a 'SyntaxError' or
# someething here. We'd have to modify
# ProgrammingBackend._preProcessCheckSyntax for that.
#log.debug('(3): %s ' % src)
# FIXME: Compiling the program is one thing, but to be able to
# tell whether the pattern can be compiled we'd have to run
# the program, too.
"""
@see: AbtractSimpleBackend._postProcessCheckSyntax
"""
# search for path, filename and line numbers
#
# result is something like
# [('Tutor1.java:7', '7'), ('Tutor1.java:7', '7')]
# replace each filename and line number
1)
return message
# -- check semantics ------------------------------------------------------
"""
Runs sematic test on a Java program.
Remember: Before we can run the wrapper code we have to compile it!!
@return: a BackendResult object with result code and value
"""
def unescape(s):
# test for available test specs
msg = 'No test specification selected.'
# test for defined repeat fields in the schema definition
assert repeatFields, 'No RepeatField found.'
# test for available test data
msg = 'No test data defined.'
# 1. get model solution and student's submission
assert non_null_str(modelSolution), \
"Semantic check requires valid 'model solution' (%s)" % \
assert non_null_str(studentSolution), \
"Semantic check requires valid 'student solution' (%s)" % \
assert string_p(modelSolution), \
"'model solution' (%s) is not a string" % \
assert string_p(studentSolution), \
"'student solution' (%s) is not a string" % \
# Create unescaped versions of the solutions that can be used
# as command line arguments. Remove the quote marks as well
# (hence the [1:-1]).
# define return values
feedback = ''
# run selected test specifications
if not solved: break
# 4.1. set wrapper source
# 4.2. get values for all other input fields from schema which are
# available in the job data
# 4.3 set test function
# remove all remaining placeholders
# compile the wrapper
try:
# write module for wrapper
# compile the wrapper
'Error in wrapper code for semantic check: %s' % result
except Exception, e:
msg = 'Internal error during semantic check: %s: %s' % \
# 4.4. run with all test data
for t in testdata:
# and now add repeatable data values
#rfn = repeatField.getName()
# download the test data
contents = None
try:
#print >> sys.stdout, contents
except Exception, e:
msg = 'Internal error during semantic check: %s: %s' % \
# write the test data to a file
fn = None
try:
# "man 2 write" says:
#
# When using non-blocking I/O on objects such as
# sockets that are subject to flow control,
# write() and writev() may write fewer bytes than
# requested; the return value must be noted, and
# the remainder of the oper- ation should be
# retried when possible.
#
# Since we're not using non-blocking I/O, I guess
# this means that all of [contents] should be
# written in one go.
except Exception, e:
msg = 'Internal error during semantic check: %s: %s' % \
# execute wrapper
try:
# run the wrapper
except Exception, e:
msg = 'Internal error during semantic check: %s: %s' % \
# an error occured
result = "\nYour submission failed. Test " \
"case was: '%s' (%s)" \
"\n\n Received result: %s"\
# has the student's solution passed this tests?
else:
# TODO: i18n
feedback = "\nYour submission failed. Test " \
"case was: '%s' (%s)" \
feedback += '\n\n Expected result: %s\n ' \
'Received result: %s' \
break # means: end testing right now
# end inner for loop
#end out for loop
if solved:
# TODO: i18n
feedback = '\nYour submission passed all tests.'
#return ([0, 1][solved], feedback)