1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
#!/usr/bin/python
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2009 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# Author: Nicholas Chadwick
"""Script to upload an exercise file into the database"""
import os, sys, traceback
import xml.dom.minidom as minidom
from ivle.database import Exercise, TestSuite, TestCase, TestSuiteVar, TestCasePart, get_store
class XMLMalformedError(Exception):
"""Error thrown when encountering malformed data."""
def __init__(self, text):
self.msg = text
def getTextData(element):
""" Get the text and cdata inside an element
Leading and trailing whitespace are stripped
"""
data = ''
for child in element.childNodes:
if child.nodeType == child.CDATA_SECTION_NODE:
data += child.data
elif child.nodeType == child.TEXT_NODE:
data += child.data
else:
data += child.toxml()
return unicode(data.strip())
def add_var(store, var_type, var_name=u"", var_value=u"", arg_no=0):
"""Given an var node, parse it into a storm object."""
new_var = TestSuiteVar()
new_var.var_name = unicode(var_name)
new_var.var_value = unicode(var_value)
new_var.var_type = unicode(var_type)
new_var.arg_no = arg_no
store.add(new_var)
return new_var
def add_test_suite(suite_node, suite_num, store):
"""Given a test suite element, get all the cases it contains."""
cases = []
case_num = 0
for case_node in suite_node.getElementsByTagName('function'):
case_num += 1
cases.append(add_test_case(case_node, case_num, store))
## ALLOWED TAGS ##
# stdin - Stdin for the suite - Unique - Text inside element
# file - File to add to the filespace - Name - List
# var - Variable for the suite - Name/Value - List
# arg - Argument to functions - Name/Value - ORDERED List
# exception - Allowed exception name - Name - List
# function - An actual test case
suite_vars = []
# Add file nodes
file_nodes = suite_node.getElementsByTagName('file')
for file_node in file_nodes:
suite_vars.append(add_var(store, 'file', file_node.getAttribute('name')))
# Add vars
var_nodes = suite_node.getElementsByTagName('var')
for var_node in var_nodes:
var_name = var_node.getAttribute('name')
var_value = var_node.getAttribute('value')
suite_vars.append(add_var(store, 'var', var_name, var_value))
# Args need to be numbered as they are found, as this order matters
arg_num = 0
for arg_node in suite_node.getElementsByTagName('arg'):
suite_vars.append(add_var(store, 'arg', arg_node.getAttribute('name'),
arg_node.getAttribute('value'), arg_num))
arg_num += 1
# Add allowed exceptions
exception_nodes = suite_node.getElementsByTagName('exception')
for exception_node in exception_nodes:
name = exception_node.getAttribute('name')
suite_vars.append(add_var(store, 'exception', name))
# Can only have 0-1 stdin elements
stdin = suite_node.getElementsByTagName('stdin')
if len(stdin) > 1:
raise XMLMalformedError('Too many stdin tags found.')
if stdin:
stdin = getTextData(stdin[0])
else:
stdin = ""
new_suite = TestSuite()
new_suite.description = unicode(suite_node.getAttribute('name'))
new_suite.seq_no = suite_num
new_suite.function = unicode(suite_node.getAttribute('function'))
new_suite.stdin = unicode(stdin)
for testcase in cases:
new_suite.test_cases.add(testcase)
for var in suite_vars:
new_suite.variables.add(var)
store.add(new_suite)
return new_suite
def add_part(store, part_type, test_type, data, filename=u""):
new_part = TestCasePart()
new_part.part_type = unicode(part_type)
new_part.test_type = unicode(test_type)
new_part.data = unicode(data)
new_part.filename = unicode(filename)
store.add(new_part)
return new_part
def add_test_case(case_node, case_num, store):
"""Given a test case node, parse it int a storm object."""
## ALLOWED TAGS ##
# A function is allowed to contain the following elements
# stdout
# stderr
# result
# exception
# file
# code
allowed_parts = ['stdout', 'stderr', 'result', 'exception', 'file', 'code']
parts = []
for child_node in case_node.childNodes:
if child_node.nodeType != child_node.ELEMENT_NODE:
continue
if child_node.tagName == 'file':
part_type = 'file'
test_type = child_node.getAttribute('type')
data = getTextData(child_node)
filename = child_node.getAttribute('name')
if filename == "":
raise XMLMalformedException('file tag must have names')
parts.append(add_part(store, part_type, test_type, data,
filename))
elif child_node.tagName in allowed_parts:
part_type = child_node.tagName
test_type = child_node.getAttribute('type')
data = getTextData(child_node)
parts.append(add_part(store, part_type, test_type, data))
#Now create the object to hold the data
new_test_case = TestCase()
new_test_case.passmsg = unicode(case_node.getAttribute(u'pass'))
new_test_case.failmsg = unicode(case_node.getAttribute(u'fail'))
new_test_case.test_default = unicode(case_node.getAttribute(u'default'))
new_test_case.seq_no = case_num
store.add(new_test_case)
for part in parts:
new_test_case.parts.add(part)
return new_test_case
xmlfile = sys.argv[1]
def add_exercise(xmlfile):
store = get_store()
# Skip existing ones.
if store.find(Exercise, id=unicode(xmlfile)).count():
return
print "Adding exercise", xmlfile
try:
filedom = minidom.parse(xmlfile)
except IOError, e:
raise Exception('ivle-addexercise: error opening file ' + xmlfile + ': ' + e[1])
for child in filedom.childNodes:
if child.nodeType == child.ELEMENT_NODE and child.tagName == 'exercise':
exercise = child
else:
raise XMLMalformedError('ivle-addexercise: error parsing XML: root node must be "exercise"')
exercisename = exercise.getAttribute('name')
rows = exercise.getAttribute('rows')
if rows == '':
rows = 4
solution = None
partial_solution = None
include_code = None
description = None
test_suite_nodes = []
for child in exercise.childNodes:
if child.nodeType != child.ELEMENT_NODE:
continue
if child.tagName == 'solution':
if solution is not None:
raise XMLMalformedError('ivle-addexercise: error parsing XML: multiple "solution" nodes')
solution = getTextData(child)
elif child.tagName == 'include':
if include_code is not None:
raise XMLMalformedError('ivle-addexercise: error parsing XML: multiple "include" nodes')
include_code = getTextData(child)
elif child.tagName == 'partial':
if partial_solution is not None:
raise XMLMalformedError('ivle-addexercise: error parsing XML: multiple "include" nodes')
partial_solution = getTextData(child)
elif child.tagName == 'case':
test_suite_nodes.append(child)
elif child.tagName == 'desc':
description = getTextData(child)
new_exercise = Exercise()
new_exercise.id = unicode(xmlfile)
new_exercise.name = exercisename
new_exercise.num_rows = int(rows)
new_exercise.partial = partial_solution
new_exercise.solution = solution
new_exercise.include = include_code
new_exercise.description = description
new_exercise.partial = partial_solution
store.add(new_exercise)
suite_num = 0
for suite in test_suite_nodes:
new_exercise.test_suites.add(add_test_suite(suite, suite_num, store))
suite_num += 1
store.add(new_exercise)
store.commit()
xmlfiles = sys.argv[1:]
for xmlfile in xmlfiles:
try:
add_exercise(xmlfile)
except Exception, e:
print "ERROR: Could not add file", xmlfile
|