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 <bal...@linux.vnet.ibm.com>
---
 configure.in                  |   14 ++++++++++++++
 include/libcgroup/config.h    |    2 ++
 include/libcgroup/error.h     |    2 ++
 include/libcgroup/groups.h    |    2 ++
 include/libcgroup/init.h      |    2 ++
 include/libcgroup/iterators.h |    2 ++
 include/libcgroup/tasks.h     |    2 ++
 src/Makefile.am               |    6 +++++-
 src/bindings/Makefile.am      |   15 +++++++++++++++
 src/bindings/libcgroup.p      |   29 +++++++++++++++++++++++++++++
 src/bindings/tests/stat.py    |   35 +++++++++++++++++++++++++++++++++++
 11 files changed, 110 insertions(+), 1 deletions(-)
 create mode 100644 src/bindings/Makefile.am
 create mode 100644 src/bindings/libcgroup.p
 create mode 100644 src/bindings/tests/stat.py

diff --git a/configure.in b/configure.in
index 936f113..fb982d3 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 dc4719d..3865603 100644
--- a/include/libcgroup/config.h
+++ b/include/libcgroup/config.h
@@ -5,7 +5,9 @@
 #error "Only <libcgroup.h> should be included directly."
 #endif
 
+#ifndef SWIG
 #include <features.h>
+#endif
 
 __BEGIN_DECLS
 
diff --git a/include/libcgroup/error.h b/include/libcgroup/error.h
index 5d20537..a78473a 100644
--- a/include/libcgroup/error.h
+++ b/include/libcgroup/error.h
@@ -5,7 +5,9 @@
 #error "Only <libcgroup.h> should be included directly."
 #endif
 
+#ifndef SWIG
 #include <features.h>
+#endif
 
 __BEGIN_DECLS
 
diff --git a/include/libcgroup/groups.h b/include/libcgroup/groups.h
index c87805f..1060641 100644
--- a/include/libcgroup/groups.h
+++ b/include/libcgroup/groups.h
@@ -5,9 +5,11 @@
 #error "Only <libcgroup.h> should be included directly."
 #endif
 
+#ifndef SWIG
 #include <features.h>
 #include <sys/types.h>
 #include <stdbool.h>
+#endif
 
 __BEGIN_DECLS
 
diff --git a/include/libcgroup/init.h b/include/libcgroup/init.h
index 41eafcc..881b830 100644
--- a/include/libcgroup/init.h
+++ b/include/libcgroup/init.h
@@ -5,7 +5,9 @@
 #error "Only <libcgroup.h> should be included directly."
 #endif
 
+#ifndef SWIG
 #include <features.h>
+#endif
 
 __BEGIN_DECLS
 
diff --git a/include/libcgroup/iterators.h b/include/libcgroup/iterators.h
index ed4e6de..8ecc53f 100644
--- a/include/libcgroup/iterators.h
+++ b/include/libcgroup/iterators.h
@@ -5,9 +5,11 @@
 #error "Only <libcgroup.h> should be included directly."
 #endif
 
+#ifndef SWIG
 #include <sys/types.h>
 #include <stdio.h>
 #include <features.h>
+#endif
 
 __BEGIN_DECLS
 
diff --git a/include/libcgroup/tasks.h b/include/libcgroup/tasks.h
index 60ad383..fb728f4 100644
--- a/include/libcgroup/tasks.h
+++ b/include/libcgroup/tasks.h
@@ -7,8 +7,10 @@
 
 #include <libcgroup/groups.h>
 
+#ifndef SWIG
 #include <features.h>
 #include <stdbool.h>
+#endif
 
 __BEGIN_DECLS
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 1046c25..18a2ef0 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 0000000..31feb2a
--- /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 0000000..ebbbcdc
--- /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 0000000..3cfdb24
--- /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)


------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and, 
should the need arise, upgrade to a full multi-node Oracle RAC database 
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to