2016-01-12 08:09:18 +00:00
|
|
|
#include <python2.7/Python.h>
|
|
|
|
#include "runner.h"
|
|
|
|
|
2016-01-12 10:23:56 +00:00
|
|
|
|
2016-01-12 11:04:16 +00:00
|
|
|
static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
|
2016-01-12 10:23:56 +00:00
|
|
|
struct config config;
|
2016-01-23 07:36:34 +00:00
|
|
|
struct result result = {0, 0, 0, 0, 0};
|
2016-01-22 02:11:16 +00:00
|
|
|
PyObject *args_list = NULL, *env_list = NULL, *use_sandbox = NULL, *next = NULL, *args_iter = NULL, *env_iter = NULL;
|
2016-01-12 15:51:11 +00:00
|
|
|
int count = 0;
|
2016-01-22 02:01:08 +00:00
|
|
|
static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time",
|
|
|
|
"max_memory", "args", "env", "use_sandbox", NULL};
|
2016-01-12 15:51:11 +00:00
|
|
|
|
2016-01-12 10:23:56 +00:00
|
|
|
config.path = config.in_file = config.out_file = NULL;
|
2016-01-22 02:01:08 +00:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssii|OOO", kwargs_list, &(config.path), &(config.in_file),
|
2016-01-12 15:51:11 +00:00
|
|
|
&(config.out_file), &(config.max_cpu_time), &(config.max_memory),
|
2016-01-22 02:01:08 +00:00
|
|
|
&args_list, &env_list, &use_sandbox)) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Invalid args and kwargs");
|
2016-01-12 08:09:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-12 11:04:16 +00:00
|
|
|
if (config.max_cpu_time <= 1) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Max cpu time can not less than 1 ms");
|
2016-01-12 10:23:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-12 11:04:16 +00:00
|
|
|
if (config.max_memory < 16 * 1024 * 1024) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Max memory can not be less than 16M");
|
2016-01-12 11:04:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (access(config.path, F_OK) == -1) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Exec file does not exist");
|
2016-01-12 11:04:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (access(config.in_file, F_OK) == -1) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Input file does not exist");
|
2016-01-12 10:23:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-23 10:56:45 +00:00
|
|
|
config.args[count++] = config.path;
|
2016-01-20 12:13:48 +00:00
|
|
|
if (args_list != NULL) {
|
|
|
|
if (!PyList_Check(args_list)) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "args must be a list");
|
2016-01-20 12:13:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
args_iter = PyObject_GetIter(args_list);
|
|
|
|
while (1) {
|
|
|
|
next = PyIter_Next((args_iter));
|
|
|
|
if (!next) {
|
|
|
|
break;
|
|
|
|
}
|
2016-01-23 13:50:22 +00:00
|
|
|
if (!PyString_Check(next) && !PyUnicode_Check(next)) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "arg must be string");
|
2016-01-20 12:13:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
config.args[count] = PyString_AsString(next);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config.args[count] = NULL;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
if (env_list != NULL) {
|
|
|
|
if (!PyList_Check(env_list)) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "env must be a list");
|
2016-01-20 12:13:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
env_iter = PyObject_GetIter(env_list);
|
|
|
|
while (1) {
|
|
|
|
next = PyIter_Next(env_iter);
|
|
|
|
if (!next) {
|
|
|
|
break;
|
|
|
|
}
|
2016-01-23 13:50:22 +00:00
|
|
|
if (!PyString_Check(next) && !PyUnicode_Check(next)) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "env must be string");
|
2016-01-20 12:13:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
config.env[count] = PyString_AsString(next);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config.env[count] = NULL;
|
2016-01-12 10:23:56 +00:00
|
|
|
|
2016-01-22 02:01:08 +00:00
|
|
|
if (use_sandbox != NULL) {
|
|
|
|
if (!PyBool_Check(use_sandbox)) {
|
2016-01-22 07:30:42 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "use sandbox must ba a bool");
|
2016-01-22 02:01:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
config.use_sandbox = PyObject_IsTrue(use_sandbox);
|
|
|
|
}
|
2016-01-23 06:51:37 +00:00
|
|
|
else {
|
|
|
|
config.use_sandbox = 1;
|
|
|
|
}
|
2016-01-22 02:01:08 +00:00
|
|
|
|
2016-01-12 10:23:56 +00:00
|
|
|
run(&config, &result);
|
2016-01-23 07:34:48 +00:00
|
|
|
return Py_BuildValue("{s:i, s:l, s:i, s:i, s:i}",
|
2016-01-12 11:04:16 +00:00
|
|
|
"cpu_time", result.cpu_time, "memory", result.memory, "real_time", result.real_time, "signal",
|
2016-01-23 07:34:48 +00:00
|
|
|
result.signal, "flag", result.flag);
|
2016-01-12 11:04:16 +00:00
|
|
|
|
2016-01-12 08:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyMethodDef judger_methods[] = {
|
2016-01-12 11:04:16 +00:00
|
|
|
{"run", (PyCFunction) judger_run, METH_VARARGS | METH_KEYWORDS, NULL},
|
2016-01-12 08:09:18 +00:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
2016-01-12 10:23:56 +00:00
|
|
|
|
2016-01-13 03:57:31 +00:00
|
|
|
PyMODINIT_FUNC initjudger(void) {
|
2016-01-22 07:30:42 +00:00
|
|
|
Py_InitModule3("judger", judger_methods, NULL);
|
2016-01-18 06:41:57 +00:00
|
|
|
}
|