add python bindings
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -8,3 +8,5 @@ nethogs.project
|
||||
libnethogs.so.*
|
||||
libnethogs.a
|
||||
results/
|
||||
build/
|
||||
nethogs.egg-info
|
||||
|
||||
8
pyproject.toml
Normal file
8
pyproject.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[build-system]
|
||||
requires = [
|
||||
"setuptools>=42",
|
||||
"wheel",
|
||||
"pybind11>=2.8.0",
|
||||
]
|
||||
|
||||
build-backend = "setuptools.build_meta"
|
||||
62
setup.py
Normal file
62
setup.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import glob
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from pybind11 import get_cmake_dir
|
||||
# Available at setup time due to pyproject.toml
|
||||
from pybind11.setup_helpers import Pybind11Extension, build_ext
|
||||
from setuptools import setup
|
||||
|
||||
_version_info = subprocess.run(['bash', "./determineVersion.sh"], stdout=subprocess.PIPE)
|
||||
__version__ = _version_info.stdout.decode("utf-8").rstrip("\n").split("-")[0] if _version_info else "0.0.0"
|
||||
|
||||
OBJS = [
|
||||
"src/bindings.cpp",
|
||||
"src/libnethogs.cpp",
|
||||
"src/packet.cpp",
|
||||
"src/connection.cpp",
|
||||
"src/process.cpp",
|
||||
"src/decpcap.c",
|
||||
"src/inode2prog.cpp",
|
||||
"src/conninode.cpp",
|
||||
"src/devices.cpp"
|
||||
]
|
||||
|
||||
FLAGS = [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wno-missing-field-initializers",
|
||||
"--std=c++0x",
|
||||
"-O3",
|
||||
"-fPIC",
|
||||
'-DVERSION="{}"'.format(__version__)
|
||||
]
|
||||
# The main interface is through Pybind11Extension.
|
||||
# * You can add cxx_std=11/14/17, and then build_ext can be removed.
|
||||
# * You can set include_pybind11=false to add the include directory yourself,
|
||||
# say from a submodule.
|
||||
#
|
||||
# Note:
|
||||
# Sort input source files if you glob sources to ensure bit-for-bit
|
||||
# reproducible builds (https://github.com/pybind/python_example/pull/53)
|
||||
|
||||
ext_modules = [
|
||||
Pybind11Extension(
|
||||
"nethogs",
|
||||
sources = OBJS,
|
||||
extra_compile_args = FLAGS,
|
||||
libraries = ["pcap"]
|
||||
),
|
||||
]
|
||||
|
||||
setup(
|
||||
name="nethogs",
|
||||
version=__version__,
|
||||
author="raboof",
|
||||
url="https://github.com/raboof/nethogs",
|
||||
description="Nethogs python bindings",
|
||||
ext_modules=ext_modules,
|
||||
cmdclass={"build_ext": build_ext},
|
||||
zip_safe=False,
|
||||
python_requires=">=3.6",
|
||||
)
|
||||
68
src/bindings.cpp
Normal file
68
src/bindings.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <pybind11/functional.h>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
|
||||
#include "libnethogs.h"
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
//--- for some reason this is a global defined in main.cpp
|
||||
std::set<pid_t> pidsToWatch;
|
||||
|
||||
//--- hacky way to get callbacks working and handle signals
|
||||
std::function<void(int, NethogsMonitorRecord const *)> empty_callback;
|
||||
std::function<void(int, NethogsMonitorRecord const *)> loop_callback;
|
||||
void loop_callback_wrapper(int arg1, NethogsMonitorRecord const *arg2){
|
||||
py::gil_scoped_acquire acquire;
|
||||
if (PyErr_CheckSignals() != 0) {
|
||||
nethogsmonitor_breakloop();
|
||||
PyErr_Clear();
|
||||
}
|
||||
else if (loop_callback) {
|
||||
loop_callback(arg1, arg2);
|
||||
}
|
||||
}
|
||||
|
||||
int nethogsmonitor_loop_py(
|
||||
std::function<void(int, NethogsMonitorRecord const *)> &cb,
|
||||
char *filter,
|
||||
int to_ms)
|
||||
{
|
||||
int retval;
|
||||
loop_callback = cb;
|
||||
{
|
||||
py::gil_scoped_release release;
|
||||
retval = nethogsmonitor_loop(loop_callback_wrapper, filter, to_ms);
|
||||
}
|
||||
loop_callback = empty_callback;
|
||||
return retval;
|
||||
}
|
||||
|
||||
//--- python module binding
|
||||
PYBIND11_MODULE(nethogs, m) {
|
||||
py::class_<NethogsMonitorRecord>(m, "NethogsMonitorRecord")
|
||||
.def_readwrite("record_id", &NethogsMonitorRecord::record_id)
|
||||
.def_readwrite("name", &NethogsMonitorRecord::name)
|
||||
.def_readwrite("pid", &NethogsMonitorRecord::pid)
|
||||
.def_readwrite("uid", &NethogsMonitorRecord::uid)
|
||||
.def_readwrite("device_name", &NethogsMonitorRecord::device_name)
|
||||
.def_readwrite("sent_bytes", &NethogsMonitorRecord::sent_bytes)
|
||||
.def_readwrite("recv_bytes", &NethogsMonitorRecord::recv_bytes)
|
||||
.def_readwrite("sent_kbs", &NethogsMonitorRecord::sent_kbs)
|
||||
.def_readwrite("recv_kbs", &NethogsMonitorRecord::recv_kbs);
|
||||
|
||||
m.def("nethogsmonitor_loop", &nethogsmonitor_loop_py, R"pbdoc(
|
||||
Nethogs monitor loop
|
||||
)pbdoc");
|
||||
m.def("nethogsmonitor_breakloop", &nethogsmonitor_breakloop, R"pbdoc(
|
||||
Nethogs monitor loop break
|
||||
)pbdoc");
|
||||
|
||||
#ifdef VERSION
|
||||
m.attr("__version__") = VERSION;
|
||||
#else
|
||||
m.attr("__version__") = "unknown";
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -440,4 +440,3 @@ void remove_timed_out_processes() {
|
||||
}
|
||||
|
||||
void garbage_collect_processes() { garbage_collect_inodeproc(); }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user