forked from datacamp/pythonwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal.py
More file actions
291 lines (227 loc) · 9.37 KB
/
local.py
File metadata and controls
291 lines (227 loc) · 9.37 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import io
import os
import random
from pathlib import Path
from contextlib import redirect_stdout
from multiprocessing import Process, Queue
from pythonwhat.reporter import Reporter
try:
from pythonbackend.shell_utils import create
from pythonbackend.tasks import TaskCaptureFullOutput
BACKEND_AVAILABLE = True
except:
BACKEND_AVAILABLE = False
class StubShell:
def __init__(self, init_code=None):
self.user_ns = {}
if init_code:
self.run_code(init_code)
def run_code(self, code):
exec(code, self.user_ns)
class StubProcess:
def __init__(self, init_code=None, pid=None):
self.shell = StubShell(init_code)
self._identity = (pid,) if pid else (random.randint(0, 1e12),)
def executeTask(self, task):
return task(self.shell)
class TaskCaptureOutput:
def __init__(self, code):
self.code = code
def __call__(self, shell):
return run_code(shell.run_code, self.code)
class TaskKillProcess:
def __call__(self, shell):
return None
class CaptureErrors:
def __init__(self, output):
self.output = output
def __enter__(self):
pass
def __exit__(self, exc_type, exception, traceback):
if exc_type is not None:
self.output.append({"type": "backend-error", "payload": str(exception)})
return True
class WorkerProcess(Process):
instances = []
def __init__(self, pid=None):
Process.__init__(self)
self.task_queue = Queue()
self.result_queue = Queue()
self.daemon = (
True
) # when parent process is killed, sub/childprocess get also killed
self.instances.append(self)
# used to detect single process exercise
self._identity = (pid,) if pid else (random.randint(0, 1e12),)
def get_shell(self):
return create({})
def run(self):
shell = self.get_shell()
while True:
output = []
with CaptureErrors(output):
next_task = self.task_queue.get()
answer = next_task(shell)
if len(output) > 0: # means backend error happened
answer = output
output = []
with CaptureErrors(output):
self.result_queue.put_nowait(answer)
if len(output) > 0: # means backend error happened
self.result_queue.put_nowait(output)
if isinstance(next_task, TaskKillProcess):
break # break while loop -> we do not wait upon new task
return
def executeTask(self, task):
self.task_queue.put_nowait(task)
return self.result_queue.get() # wait and fetches next item in queue
def kill(self):
try:
if self.is_alive():
self.executeTask(TaskKillProcess())
self.join(timeout=3.0)
if self.is_alive():
self.terminate()
self.join(timeout=3.0)
if self in self.instances:
self.instances.remove(self)
finally:
pass
# python 3.7:
# self.close()
@classmethod
def kill_all(cls):
for instance in list(cls.instances):
instance.kill()
class SimpleProcess(WorkerProcess):
def get_shell(self):
return StubShell()
class ChDir(object):
"""
Step into a directory temporarily.
"""
def __init__(self, path):
self.old_dir = os.getcwd()
self.new_dir = str(path)
def __enter__(self):
os.chdir(self.new_dir)
def __exit__(self, *args):
os.chdir(self.old_dir)
def run_code(executor, code):
with io.StringIO() as output:
try:
with redirect_stdout(output):
executor(code)
raw_output = output.getvalue()
error = None
except BaseException as e:
raw_output = ""
error = str(e)
return raw_output, error
def run_single_process(pec, code, pid=None, mode="simple"):
if mode == "stub":
# no isolation
process = StubProcess(init_code=pec, pid=pid)
raw_stu_output, error = run_code(process.shell.run_code, code)
elif mode == "simple":
# no advanced functionality
process = SimpleProcess(pid)
process.start()
_ = process.executeTask(TaskCaptureOutput(pec))
raw_stu_output, error = process.executeTask(TaskCaptureOutput(code))
elif mode == "full" and BACKEND_AVAILABLE:
# slow
process = WorkerProcess(pid)
process.start()
_ = process.executeTask(
TaskCaptureFullOutput((pec,), "<PEC>", None, silent=True)
)
output, raw_output = process.executeTask(
TaskCaptureFullOutput((code,), "script.py", None, silent=True)
)
raw_stu_output = raw_output["output_stream"]
error = raw_output["error"]
else:
raise ValueError("Invalid mode")
return process, raw_stu_output, error
def run_exercise(pec, sol_code, stu_code, sol_wd=None, stu_wd=None, **kwargs):
with ChDir(sol_wd or os.getcwd()):
sol_process, _, _ = run_single_process(pec, sol_code, **kwargs)
with ChDir(stu_wd or os.getcwd()):
stu_process, raw_stu_output, error = run_single_process(pec, stu_code, **kwargs)
return sol_process, stu_process, raw_stu_output, error
# todo:
# imports from local modules (solution needs to be materialised somewhere)
# converge with xbackend (pythonbackend + look at scalabackend)
# move towards xwhat controlling all execution and xbackend providing the execution interface?
# running with arbitrary wd + path + flags (now only wd) needed?
# e.g. `python -m project.run
# allow setting env vars? e.g. PYTHONPATH, could help running more complex setup
# allow prepending code? set_env? e.g. (automatically) setting __file__?
def run(state, relative_working_dir=None, solution_dir="../solution"):
"""Run the focused student and solution code in the specified location
This function can be used after ``check_file`` to execute student and solution code.
The arguments allow configuring the correct context for execution.
SCT functions chained after this one that execute pieces of code (custom expressions or the focused part of a file)
execute in the same student and solution locations as the file.
.. note::
This function does not execute the file itself, but code in memory.
This can have an impact when:
- the solution code imports from a different file in the expected solution (code that is not installed)
- using functionality depending on e.g. ``__file__`` and ``inspect``
When the expected code has imports from a different file that is part of the exercise,
it can only work if the solution code provided earlier does not have these imports but instead
has all that functionality inlined.
Args:
relative_working_dir (str): if specified, this relative path is the subdirectory
inside the student and solution context in which the code is executed
solution_dir (str): a relative path, ``solution`` by default,
that sets the root of the solution context, relative to that of the student execution context
state (State): state as passed by the SCT chain. Don't specify this explicitly.
If ``relative_working_dir`` is not set, it will be the directory the file was loaded from by ``check_file``
and fall back to the root of the student execution context (the working directory pythonwhat runs in).
The ``solution_dir`` helps to prevent solution side effects from conflicting with those of the student.
If the set or derived value of ``relative_working_dir`` is an absolute path,
``relative_working_dir`` will not be used to form the solution execution working directory:
the solution code will be executed in the root of the solution execution context.
:Example:
Suppose the student and solution have a file ``script.py`` in ``/home/repl/``::
if True:
a = 1
print("Hi!")
We can check it with this SCT (with ``file_content`` containing the expected file content)::
Ex().check_file(
"script.py",
solution_code=file_content
).run().multi(
check_object("a").has_equal_value(),
has_printout(0)
)
"""
# todo:
# look into executing the file itself
# and keeping the process alive to extract values
if relative_working_dir is None:
if getattr(state, "path", False):
relative_working_dir = state.path.parent
else:
relative_working_dir = ""
if not os.path.isabs(str(relative_working_dir)):
sol_wd = Path(os.getcwd(), solution_dir, relative_working_dir)
else:
sol_wd = Path(os.getcwd(), solution_dir)
os.makedirs(str(sol_wd), exist_ok=True)
stu_wd = Path(os.getcwd(), relative_working_dir)
sol_process, stu_process, raw_stu_output, error = run_exercise(
pec="",
sol_code=state.solution_code or "",
stu_code=state.student_code,
sol_wd=sol_wd,
stu_wd=stu_wd,
)
return state.to_child(
student_process=stu_process,
solution_process=sol_process,
raw_student_output=raw_stu_output,
reporter=Reporter(state.reporter, errors=[error] if error else []),
)