-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpoller_isa.py
192 lines (159 loc) · 6.14 KB
/
poller_isa.py
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
import sys
import subprocess
import re
import logging
from poller import Poller, Grader_Panic
from watchdog import Watchdog
# XXX How about typedef and friends?
# (keyword, is_surrounded_by_whitespace)
ILLEGAL_KEYWORDS = [
("axiomatization", True),
("overloading", True),
("code_printing", True),
# XXX Can remove after checking in ML
("translations", True),
("declaration", False),
# XXX Allow?
("syntax_declaration", False),
("oracle", False),
("judgement", False),
("method_setup", False),
("simproc_setup", False),
("SML_export", False),
("SML_import", False),
("SML_file", False),
("SML_file", False),
("ML_file", False),
("SML_file_debug", False),
("ML_file_debug", False),
("SML_file_no_debug", False),
("ML_file_no_debug", False),
("ML", False),
# XXX Allow the following to commands?
("ML_val", False),
("ML_command", False),
("ML_prf", False),
("setup", False),
("local_setup", False),
("attribute_setup", False),
# Do we need these, i.e. can side-effects be bad?
("parse_ast_translation", False),
("parse_translation", False),
("print_translation", False),
("typed_print_translation", False),
("print_ast_translation", False),
("eval", "Tactic"),
("tactic", "Tactic"),
("raw_tactic", "Tactic")
]
ILLEGAL_SORRY = ("sorry", True)
def check_for_keyword(text, keyword_mode):
open = re.escape("\<open>")
old_open = re.escape("{*")
close = re.escape("\<close>")
old_close = re.escape("*}")
quotation = re.escape('"')
keyword, mode = keyword_mode
start_regex = '(%s|\s+|\A)' % '|'.join([re.escape(')'),
close, old_close, quotation])
regex = '%s%s(\s*(%s|%s|%s))' % (start_regex,
'%s', open, old_open, quotation)
if mode == "Tactic":
# XXX May be incomplete
tactic_ops = [re.escape(s) for s in ['(', ',', ';', '|']]
regex = '|'.join(tactic_ops)
regex = '(%s)\s*%s' % (regex, '%s')
elif mode:
regex = '%s%s\s+' % (start_regex, '%s')
regex = regex % keyword
return re.search(regex, text) is not None
def check_for_keywords(prepared, allow_sorry):
IK = ILLEGAL_KEYWORDS.copy()
# add sorry as keyword if it is not allowed:
if not allow_sorry:
IK.append(ILLEGAL_SORRY)
for keyword_mode in IK:
if check_for_keyword(prepared, keyword_mode):
return {"result": False, "message": "Identified illegal keyword: %s" % keyword_mode[0]}
return {"result": True, "message": ""}
raw_bash_command = 'sudo ip netns exec isabelle-server python2.7 grader.py "{0}" "{1}" "{2}" {3}'
grader_path = "/var/lib/isabelle-grader/"
class Poller_Isa(Poller):
def init(self):
self.password = self.config["pwd"]
self.logger.info("pwd {}, token {}".format(self.password, self.token))
self.make_pollurl("ISA")
def grade_submission(self, submission_id, assessment_id, defs, submission, check, image, version, timeout_socket, timeout_all, allow_sorry, check_file):
global raw_bash_command, grader_path
logger = self.logger
# Check for illegal keywords in submission
res = check_for_keywords(submission, allow_sorry)
if not res["result"]:
grader_msg = res["message"]
result = "0"
else:
# write files into shared folder with Isabelle server
logger.debug("write the theory files")
for name, content in (("Defs", defs), ("Submission", submission), ("Check", check)):
if name == "Defs":
name = "Defs0"
p = content.split("imports", 1)
content = "theory Defs0 imports" + p[1]
logger.debug(
"writing file '{}{}.thy'!".format(grader_path, name))
with open("{}{}.thy".format(grader_path, name), 'w') as text_file:
text_file.write(content)
filename = check_file
# check the file
logger.info("-> Check the theories!")
bashCommand = raw_bash_command.format(
self.password, image, grader_path + filename, timeout_socket)
logger.info(bashCommand)
returncode = -1
timedout = True
error = None
process = subprocess.Popen(
bashCommand, stdout=subprocess.PIPE, shell=True)
try:
output, error = process.communicate(timeout=timeout_all)
timedout = False
returncode = process.returncode
except subprocess.TimeoutExpired:
timedout = True
if timedout:
logger.info("the checking process was killed !!")
returncode = 8
grader_msg = "the checking process was killed after % s !!" % timeout_all
else:
# get the return message
grader_msg = "no message"
with open("grader.out", "r") as f:
grader_msg = f.read()
# invalidate grader output in order to spot bugs
with open("grader.out", 'w') as f:
f.write("should be clean")
logger.info("-> Checking is done")
logger.info("return code is:" + str(returncode))
if returncode == 4:
# sucessfully checked
result = "1"
else:
# error occured or wrong
result = "0"
if "Timer already cancelled" in grader_msg:
# signal error
logger.info(
"found error 'Timer already cancelled', signal error to watch-dog, and let it restart the Isabelle server")
raise Grader_Panic()
return result, error, [grader_msg]
def tidy(self):
pass
if __name__ == "__main__":
loglevel = logging.INFO
if len(sys.argv) > 1:
if sys.argv[1] == "DEBUG":
loglevel = logging.DEBUG
def poll():
Poller_Isa(loglevel).run()
poller = Watchdog(poll, loglevel)
poller.watch()