From: Balbir Singh Date: Sun, 2 Jan 2011 10:03:32 +0000 (+0530) Subject: Add bindings for python X-Git-Tag: v0.37.1~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=45417a3cdaae0bd42488b7e5f7316a19770e8b6d;p=thirdparty%2Flibcgroup.git Add bindings for python The added bindings depend on swig and hence are disabled by default. If you know what you are doing, enable the bindings for python. To test the bindings, ensure _libcgroup.so is copied in the same directory as libcgroup.py. There after libcgroup API's can be invoked from python. Please use --enable-bindings after installing swig to compile with the changes Signed-off-by: Balbir Singh --- diff --git a/configure.in b/configure.in index 936f1135..fb982d30 100644 --- a/configure.in +++ b/configure.in @@ -32,6 +32,19 @@ AC_DISABLE_STATIC AC_CONFIG_SRCDIR([src]) AC_CONFIG_HEADER([config.h]) +AC_ARG_ENABLE([bindings], + [AC_HELP_STRING([--enable-bindings], + [enable python bindings [default=no]])], + [ + if test "x$enableval" = xno; then + with_bindings=false + else + with_bindings=true + fi + ], + [with_bindings = false]) +AM_CONDITIONAL([WITH_BINDINGS], [test x$with_bindings = xtrue]) + # Process command line options AC_ARG_ENABLE([debug], [AC_HELP_STRING([--enable-debug], @@ -181,6 +194,7 @@ AC_CONFIG_FILES([Makefile src/daemon/Makefile src/tools/Makefile src/pam/Makefile + src/bindings/Makefile scripts/Makefile scripts/init.d/cgconfig scripts/init.d/cgred diff --git a/include/libcgroup/config.h b/include/libcgroup/config.h index dc4719d8..38656038 100644 --- a/include/libcgroup/config.h +++ b/include/libcgroup/config.h @@ -5,7 +5,9 @@ #error "Only should be included directly." #endif +#ifndef SWIG #include +#endif __BEGIN_DECLS diff --git a/include/libcgroup/error.h b/include/libcgroup/error.h index 5d20537a..a78473a4 100644 --- a/include/libcgroup/error.h +++ b/include/libcgroup/error.h @@ -5,7 +5,9 @@ #error "Only should be included directly." #endif +#ifndef SWIG #include +#endif __BEGIN_DECLS diff --git a/include/libcgroup/groups.h b/include/libcgroup/groups.h index c87805ff..10606414 100644 --- a/include/libcgroup/groups.h +++ b/include/libcgroup/groups.h @@ -5,9 +5,11 @@ #error "Only should be included directly." #endif +#ifndef SWIG #include #include #include +#endif __BEGIN_DECLS diff --git a/include/libcgroup/init.h b/include/libcgroup/init.h index 41eafcc2..881b8302 100644 --- a/include/libcgroup/init.h +++ b/include/libcgroup/init.h @@ -5,7 +5,9 @@ #error "Only should be included directly." #endif +#ifndef SWIG #include +#endif __BEGIN_DECLS diff --git a/include/libcgroup/iterators.h b/include/libcgroup/iterators.h index ed4e6de2..8ecc53f5 100644 --- a/include/libcgroup/iterators.h +++ b/include/libcgroup/iterators.h @@ -5,9 +5,11 @@ #error "Only should be included directly." #endif +#ifndef SWIG #include #include #include +#endif __BEGIN_DECLS diff --git a/include/libcgroup/tasks.h b/include/libcgroup/tasks.h index 60ad383d..fb728f44 100644 --- a/include/libcgroup/tasks.h +++ b/include/libcgroup/tasks.h @@ -7,8 +7,10 @@ #include +#ifndef SWIG #include #include +#endif __BEGIN_DECLS diff --git a/src/Makefile.am b/src/Makefile.am index 1046c25b..18a2ef02 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,8 @@ -SUBDIRS = . daemon pam tools +if WITH_BINDINGS +BINDINGS_SUBDIR = bindings +endif + +SUBDIRS = . daemon pam tools $(BINDINGS_SUBDIR) # generate parse.h from parse.y AM_YFLAGS = -d diff --git a/src/bindings/Makefile.am b/src/bindings/Makefile.am new file mode 100644 index 00000000..31feb2ab --- /dev/null +++ b/src/bindings/Makefile.am @@ -0,0 +1,15 @@ +SUBDIRS = . +INCLUDES = -I$(top_srcdir)/include + +lib_LTLIBRARIES = _libcgroup.la +_libcgroup_la_SOURCES = libcgroup.c +_libcgroup_la_LDFLAGS = $(shell python-config --ldflags) -module -avoid-version +_libcgroup_la_CFLAGS = $(shell python-config --cflags) +_libcgroup_la_LIBADD = $(top_builddir)/src/.libs/libcgroup.la +SWIG=swig + + +libcgroup.c: libcgroup.p $(top_srcdir)/include/libcgroup.h + cp libcgroup.p libcgroup.i + $(CC) $(INCLUDES) -DSWIG -nostdinc -E $(top_srcdir)/include/libcgroup.h >> libcgroup.i + $(SWIG) -python -o libcgroup.c libcgroup.i diff --git a/src/bindings/libcgroup.p b/src/bindings/libcgroup.p new file mode 100644 index 00000000..ebbbcdc2 --- /dev/null +++ b/src/bindings/libcgroup.p @@ -0,0 +1,29 @@ +%module libcgroup +%{ +#include "libcgroup.h" +%} + +#ifdef __cplusplus +#define __BEGIN_DECLS extern "C" { +#define __END_DECLS } +#else +#define __BEGIN_DECLS +#define __END_DECLS +#endif + +%include typemaps.i +%include cpointer.i +%pointer_functions(int, intp); +%typemap (in) void** (void *temp) { + void *arg; + if ((arg = PyCObject_AsVoidPtr($input)) != NULL) { + $1 = &arg; + } else + $1 = &temp; +} + +%typemap (argout) void** { + PyObject *obj = PyCObject_FromVoidPtr(*$1, NULL); + $result = PyTuple_Pack(2, $result, obj); +} + diff --git a/src/bindings/tests/stat.py b/src/bindings/tests/stat.py new file mode 100644 index 00000000..3cfdb246 --- /dev/null +++ b/src/bindings/tests/stat.py @@ -0,0 +1,35 @@ +#!/usr/bin/python + +from libcgroup import * +from ctypes import * + +ret = cgroup_init() +if (ret != 0): + print cgroup_strerror(ret) + exit(1) + +# +# Add the correct controllers based on the mount point +# +cg_stat = cgroup_stat() +tree_handle = c_void_p() +info = cgroup_file_info() +lvl = new_intp() +ret1, tree_handle = cgroup_walk_tree_begin("memory", "/", 0, tree_handle, info, lvl) +root_len = len(info.full_path) - 1 +while ret1 != ECGEOF: + if (info.type == CGROUP_FILE_TYPE_DIR): + dir = info.full_path[root_len:] + print "\nDirectory %s\n" %(dir) + + p = c_void_p() + ret, p = cgroup_read_stats_begin("memory", dir, p, cg_stat) + while ret != ECGEOF: + print "%s:%s" %(cg_stat.name, cg_stat.value.strip('\n')) + ret, p = cgroup_read_stats_next(p, cg_stat) + + cgroup_read_stats_end(p) + ret1, tree_handle = cgroup_walk_tree_next(0, tree_handle, info, intp_value(lvl)) + +cgroup_walk_tree_end(tree_handle) +delete_intp(lvl)