Added basic Cutter python bindings

This commit is contained in:
xarkes 2018-02-11 18:59:23 +01:00 committed by xarkes
parent 0a9fba0677
commit abd9cca091
5 changed files with 66 additions and 6 deletions

View File

@ -32,6 +32,14 @@ unix:exists(/usr/local/include/libr) {
INCLUDEPATH += /usr/local/include/libr
}
# Libraries
include(lib_radare2.pri)
# TODO Not portable
unix {
LIBS += /usr/lib/libpython3.6m.so
INCLUDEPATH += /usr/include/python3.6m
}
SOURCES += \
main.cpp \
cutter.cpp \
@ -93,7 +101,8 @@ SOURCES += \
widgets/VTablesWidget.cpp \
CutterApplication.cpp \
utils/JupyterConnection.cpp \
widgets/JupyterWidget.cpp
widgets/JupyterWidget.cpp \
utils/PythonAPI.cpp
HEADERS += \
cutter.h \
@ -156,7 +165,8 @@ HEADERS += \
CutterApplication.h \
widgets/VTablesWidget.h \
utils/JupyterConnection.h \
widgets/JupyterWidget.h
widgets/JupyterWidget.h \
utils/PythonAPI.h
FORMS += \
dialogs/AboutDialog.ui \
@ -204,10 +214,6 @@ RESOURCES += \
DISTFILES += cutter.astylerc
include(lib_radare2.pri)
# 'make install' for AppImage
unix {
isEmpty(PREFIX) {

View File

@ -2,6 +2,7 @@ import sys
import threading
import time
from notebook.notebookapp import *
import cutter
class CutterNotebookApp(NotebookApp):
@ -47,4 +48,7 @@ def start_jupyter():
app = CutterNotebookApp()
app.initialize()
app.start()
print('TODO: Export cutter bindings to any kernel')
print(cutter.version())
print(cutter.cmd('?e That is executed from radare2'))
return app

View File

@ -9,6 +9,7 @@
#include <QFile>
#include "JupyterConnection.h"
#include "PythonAPI.h"
JupyterConnection::JupyterConnection(QObject *parent) : QObject(parent)
{
@ -30,6 +31,7 @@ JupyterConnection::~JupyterConnection()
void JupyterConnection::start()
{
PyImport_AppendInittab("cutter", &PyInit_api);
Py_Initialize();
PyEval_InitThreads();

41
src/utils/PythonAPI.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "PythonAPI.h"
#include "cutter.h"
/* Return the number of arguments of the application command line */
PyObject *api_version(PyObject *self, PyObject *null)
{
Q_UNUSED(self)
Q_UNUSED(null)
return PyUnicode_FromString("Cutter version " CUTTER_VERSION);
}
PyObject *api_cmd(PyObject *self, PyObject *args)
{
Q_UNUSED(self);
char *command;
char *result = (char*) "";
if (PyArg_ParseTuple(args, "s:command", &command))
{
result = Core()->cmd(command).toUtf8().data();
}
return PyUnicode_FromString(result);
}
PyMethodDef CutterMethods[] = {
{"version", api_version, METH_NOARGS,
"Returns Cutter current version"},
{"cmd", api_cmd, METH_VARARGS,
"Execute a command inside Cutter"},
{NULL, NULL, 0, NULL}
};
PyModuleDef CutterModule = {
PyModuleDef_HEAD_INIT, "cutter", NULL, -1, CutterMethods,
NULL, NULL, NULL, NULL
};
PyObject *PyInit_api()
{
return PyModule_Create(&CutterModule);
}

7
src/utils/PythonAPI.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef PYTHONAPI_H
#define PYTHONAPI_H
#include <Python.h>
PyObject *PyInit_api();
#endif // PYTHONAPI_H