+ self->cont->write(data, len);
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_getPID(eConsolePy* self)
+{
+ return PyInt_FromLong(self->cont->getPID());
+}
+
+static PyObject *
+eConsolePy_setCWD(eConsolePy* self, PyObject *args)
+{
+ const char *path=0;
+ if (!PyArg_ParseTuple(args, "s", &path))
+ return NULL;
+ self->cont->setCWD(path);
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_kill(eConsolePy* self)
+{
+ self->cont->kill();
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_sendCtrlC(eConsolePy* self)
+{
+ self->cont->sendCtrlC();
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_sendEOF(eConsolePy* self)
+{
+ self->cont->sendEOF();
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_dumpToFile(eConsolePy* self, PyObject *args)
+{
+ char *filename;
+ if (!PyArg_ParseTuple(args, "s", &filename))
+ {
+ PyErr_SetString(PyExc_TypeError,
+ "arg must be a string (filename)");
+ return NULL;
+ }
+ else
+ {
+ int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+ self->cont->setFileFD(1, fd);
+ eDebug("eConsoleAppContainer::dumpToFile open(%s, O_WRONLY|O_CREAT|O_TRUNC, 0644)=%d", filename, fd);
+ }
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+eConsolePy_readFromFile(eConsolePy* self, PyObject *args)
+{
+ char *filename;
+ if (!PyArg_ParseTuple(args, "s", &filename))
+ {
+ PyErr_SetString(PyExc_TypeError,
+ "arg must be a string (filename)");
+ return NULL;
+ }
+ else
+ {
+ int fd = open(filename, O_RDONLY);
+ if (fd >= 0)
+ {
+ char readbuf[32*1024];
+ int rsize = read(fd, readbuf, 32*1024);
+ self->cont->setFileFD(0, fd);
+ eDebug("eConsoleAppContainer::readFromFile open(%s, O_RDONLY)=%d, read: %d", filename, fd, rsize);
+ self->cont->write(readbuf, rsize);
+ }
+ else
+ {
+ eDebug("eConsoleAppContainer::readFromFile %s not exist!", filename);
+ self->cont->setFileFD(0, -1);
+ }
+ }
+ Py_RETURN_NONE;
+}
+
+static PyMethodDef eConsolePy_methods[] = {
+ {"setCWD", (PyCFunction)eConsolePy_setCWD, METH_VARARGS,
+ "set working dir"
+ },
+ {"execute", (PyCFunction)eConsolePy_execute, METH_VARARGS,
+ "execute command"
+ },
+ {"dumpToFile", (PyCFunction)eConsolePy_dumpToFile, METH_VARARGS,
+ "set output file"
+ },
+ {"readFromFile", (PyCFunction)eConsolePy_readFromFile, METH_VARARGS,
+ "set input file"
+ },
+ {"getPID", (PyCFunction)eConsolePy_getPID, METH_NOARGS,
+ "execute command"
+ },
+ {"kill", (PyCFunction)eConsolePy_kill, METH_NOARGS,
+ "kill application"
+ },
+ {"sendCtrlC", (PyCFunction)eConsolePy_sendCtrlC, METH_NOARGS,
+ "send Ctrl-C to application"
+ },
+ {"sendEOF", (PyCFunction)eConsolePy_sendEOF, METH_NOARGS,
+ "send EOF to application"
+ },
+ {"write", (PyCFunction)eConsolePy_write, METH_VARARGS,
+ "write data to application"
+ },
+ {"running", (PyCFunction)eConsolePy_running, METH_NOARGS,
+ "returns the running state"
+ },
+ {NULL} /* Sentinel */
+};
+
+static PyTypeObject eConsolePyType = {
+ PyObject_HEAD_INIT(NULL)
+ 0, /*ob_size*/
+ "eConsoleImpl.eConsoleAppContainer", /*tp_name*/
+ sizeof(eConsolePy), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)eConsolePy_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+ "eConsoleAppContainer objects", /* tp_doc */
+ (traverseproc)eConsolePy_traverse, /* tp_traverse */
+ (inquiry)eConsolePy_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ offsetof(eConsolePy, in_weakreflist), /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ eConsolePy_methods, /* tp_methods */
+ 0, /* tp_members */
+ eConsolePy_getseters, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ eConsolePy_new, /* tp_new */
+};
+
+static PyMethodDef module_methods[] = {
+ {NULL} /* Sentinel */
+};
+
+void eConsoleInit(void)
+{
+ PyObject* m = Py_InitModule3("eConsoleImpl", module_methods,
+ "Module that implements eConsoleAppContainer with working cyclic garbage collection.");
+
+ if (m == NULL)
+ return;
+
+ if (!PyType_Ready(&eConsolePyType))
+ {
+ Org_Py_INCREF((PyObject*)&eConsolePyType);
+ PyModule_AddObject(m, "eConsoleAppContainer", (PyObject*)&eConsolePyType);
+ }
+}