-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunner.php
More file actions
247 lines (193 loc) · 7.06 KB
/
Copy pathRunner.php
File metadata and controls
247 lines (193 loc) · 7.06 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
<?php
namespace TaskChecker\Module;
use TaskChecker\Codebot\ClientInterface;
use TaskChecker\Codebot\RunScriptTask;
use TaskChecker\Errors\ExecuteTaskFailedError;
use TaskChecker\Errors\StderrNotEmptyError;
use TaskChecker\Errors\VariableInjectError;
use TaskChecker\Promise\UnfailingPromise;
use TaskChecker\Reporter\Report;
use TaskChecker\RunnerQueuedTask;
use TaskChecker\Step\RunScriptStep;
use TaskChecker\Step\Step;
use TaskChecker\TextScanner\TokenArray;
use TaskChecker\TextScanner\VariableInjector;
/**
* Contains methods allowing to run a program with provided data
* via code execution service.
*
* Runner doesn't run the tasks immediately. Instead, it collects
* the tasks and runs them when runAllTasks() method is called.
*/
class Runner extends BaseModule
{
private $code;
/**
* @var VariableInjector
*/
private $injector;
// *
// * @var callable[] Functions to call before test
// private $onExecuteHandlers = [];
/**
* @var Codebot
*/
private $codebotClient;
/**
* @var Report
*/
private $reporter;
/**
* @var RunnerQueuedTask[]
*/
private $queuedTasks = [];
private $allowQueueing = true;
public function __construct(
ClientInterface $codebotClient,
VariableInjector $injector,
Report $reporter,
$code)
{
$this->code = $code;
$this->injector = $injector;
$this->reporter = $reporter;
$this->codebotClient = $codebotClient;
}
public function disableQueuingNewTasks()
{
$this->allowQueueing = false;
}
// public function onExecute(callable $handler)
// {
// $this->onExecuteHandlers[] = $handler;
// }
/** @return UnfailingPromise */
public function queueRunning(array $variables, callable $verifier)
{
if (!$this->allowQueueing) {
throw new \LogicException("Adding new tasks is alredy disabled");
}
$queuedTask = new RunnerQueuedTask($variables, $verifier);
$this->queuedTasks[] = $queuedTask;
return $queuedTask->getPromise();
}
/**
* @param array $data Contains input values and expected output values. Input
* values keys are labeled with @, for example:
* ['@a' => 1, '@b' => 2, 'c' => 3]
*
* @return UnfailingPromise
*/
public function queueRunningForArray(array $data, callable $verifier)
{
if (!$data) {
throw new \Exception("No input data given");
}
$promises = [];
foreach ($data as $id => $row) {
$promises[] = $this->queueRunning($row, $verifier);
}
return $promises;
}
public function runQueuedTasks($code)
{
$queuedTasks = $this->queuedTasks;
$this->disableQueuingNewTasks();
// $this->queuedTasks = [];
// Этот шаг может провалиться при ошибке в синтаксисе программы или
// ошибки подстановки переменных в код
$this->reporter->check("Подготовка программы к запуску",
function ($step) use ($queuedTasks, $code) {
$tasks = $this->prepareQueuedTasksInsideStep($code, $queuedTasks);
$this->codebotClient->execute($tasks);
});
// Проверяем результат выполнения программы
foreach ($queuedTasks as $queuedTask) {
$codebotTask = $queuedTask->getCodebotTask();
assert($codebotTask->isExecuted());
$inputVariables = $this->getInputVariables($queuedTask->getVariables());
$step = new RunScriptStep($codebotTask, $inputVariables);
$this->reporter->executeStep($step, function () use ($queuedTask, $codebotTask) {
$this->checkSuccessulRun($codebotTask);
$verifier = $queuedTask->getVerifier();
$verifier($codebotTask->stdout, $queuedTask->getVariables());
// Сообщаем об успешном выполнении кода
$queuedTask->resolvePromise();
});
}
}
/**
* @return RunScriptTask[]
*/
private function prepareQueuedTasksInsideStep($programCode, array $queuedTasks)
{
$codeTokens = TokenArray::fromCode($programCode);
$runTasks = [];
foreach ($queuedTasks as $queuedTask) {
$inputVariables = $this->getInputVariables($queuedTask->getVariables());
$errors = [];
$codeWithVars = $this->injector->inject($codeTokens, $inputVariables, $errors);
if ($errors) {
throw new VariableInjectError($programCode, reset($errors));
}
$task = new RunScriptTask($codeWithVars);
$queuedTask->setCodebotTask($task);
$runTasks[] = $task;
}
return $runTasks;
}
// $this->reporter->check("Проверка программы", function ($step) use($data, &$tasks) {
// $tasks = $this->prepareCodebotTasks($data);
// $this->codebotClient->execute($tasks);
// });
// foreach ($data as $id => $row) {
// $task = $tasks[$id];
// $inputVariables = $this->getInputVariables($row);
// $step = new RunScriptStep($task, $inputVariables);
// $this->reporter->executeStep($step, function () use ($task, $verify, $row) {
// $this->checkSuccessulRun($task);
// foreach ($this->onExecuteHandlers as $handler) {
// $handler($task, $row);
// }
// $verify($task->stdout, $row);
// });
// }
// }
// private function prepareCodebotTasks(array $data)
// {
// $codeTokens = TokenArray::fromCode($this->code);
// foreach ($data as $id => $row) {
// $inputVariables = $this->getInputVariables($row);
// $errors = [];
// $codeWithVars = $this->injector->inject($codeTokens, $inputVariables, $errors);
// if ($errors) {
// throw new VariableInjectError($this->code, reset($errors));
// }
// $task = new RunScriptTask($codeWithVars);
// $tasks[$id] = $task;
// }
// return $tasks;
// }
private function getInputVariables(array $data)
{
$inputs = [];
foreach ($data as $name => $value) {
if (preg_match("/^@/", $name)) {
$varName = mb_substr($name, 1);
$inputs[$varName] = $value;
}
}
return $inputs;
}
private function checkSuccessulRun(RunScriptTask $task)
{
if ($task->status != RunScriptTask::STATUS_EXECUTED)
{
throw new ExecuteTaskFailedError($task);
}
if ($task->stderr !== '')
{
throw new StderrNotEmptyError($task->stderr);
}
}
}