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 08:09:18 +00:00
|
|
|
static PyObject *error;
|
|
|
|
|
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-12 11:22:18 +00:00
|
|
|
struct result result = {0, 0, 0, 0, 0};
|
2016-01-12 10:23:56 +00:00
|
|
|
config.path = config.in_file = config.out_file = NULL;
|
2016-01-12 08:09:18 +00:00
|
|
|
static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time", "max_memory", NULL};
|
2016-01-12 11:04:16 +00:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssii", kwargs_list, &(config.path), &(config.in_file),
|
|
|
|
&(config.out_file), &(config.max_cpu_time), &(config.max_memory))) {
|
2016-01-12 08:09:18 +00:00
|
|
|
PyErr_SetString(error, "Invalid args and kwargs");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-12 11:04:16 +00:00
|
|
|
if (config.max_cpu_time <= 1) {
|
2016-01-12 10:23:56 +00:00
|
|
|
PyErr_SetString(error, "Max cpu time can not less than 1 ms");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-12 11:04:16 +00:00
|
|
|
if (config.max_memory < 16 * 1024 * 1024) {
|
|
|
|
PyErr_SetString(error, "Max memory can not be less than 16M");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (access(config.path, F_OK) == -1) {
|
|
|
|
PyErr_SetString(error, "Exec file does not exist");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (access(config.in_file, F_OK) == -1) {
|
|
|
|
PyErr_SetString(error, "Input file does not exist");
|
2016-01-12 10:23:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-12 11:22:18 +00:00
|
|
|
// printf("%s %s %s %d %d", config.path, config.in_file, config.out_file, config.max_cpu_time, config.max_memory);
|
2016-01-12 10:23:56 +00:00
|
|
|
run(&config, &result);
|
2016-01-12 11:04:16 +00:00
|
|
|
return Py_BuildValue("{s: i, s:l, s:i, s:i, s:i, s:i}",
|
|
|
|
"cpu_time", result.cpu_time, "memory", result.memory, "real_time", result.real_time, "signal",
|
|
|
|
result.signal, "flag", result.flag, "error", result.error);
|
|
|
|
|
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-12 11:04:16 +00:00
|
|
|
PyMODINIT_FUNC initjudger() {
|
2016-01-12 08:09:18 +00:00
|
|
|
PyObject *module = Py_InitModule3("judger", judger_methods, NULL);
|
|
|
|
error = PyErr_NewException("judger.error", NULL, NULL);
|
|
|
|
Py_INCREF(error);
|
|
|
|
PyModule_AddObject(module, "error", error);
|
|
|
|
}
|