From 36fb0846a862287222b673c4e967b29ce82869a4 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Fri, 13 May 2011 16:07:08 -0700 Subject: [PATCH] Add Python bindings for librgw Add some Python bindings for librgw. Also add some more verbose error logging to librgw. Signed-off-by: Colin McCabe --- src/Makefile.am | 6 ++++-- src/pybind/rgw.py | 36 ++++++++++++++++++++++++++++++++ src/rgw/librgw.cc | 20 ++++++++++++++---- src/test/ceph-pybind-rgw-test.py | 35 +++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/pybind/rgw.py create mode 100644 src/test/ceph-pybind-rgw-test.py diff --git a/src/Makefile.am b/src/Makefile.am index a5b4229dc729f..27b440a82c074 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -743,10 +743,12 @@ libclient_a_SOURCES = \ dist-hook: $(srcdir)/check_version $(srcdir)/.git_version -python_PYTHON = pybind/rados.py +python_PYTHON = pybind/rados.py \ + pybind/rgw.py if WITH_DEBUG -dist_bin_SCRIPTS += test/ceph-pybind-test.py +dist_bin_SCRIPTS += test/ceph-pybind-test.py \ + test/ceph-pybind-rgw-test.py endif # headers... and everything else we want to include in a 'make dist' diff --git a/src/pybind/rgw.py b/src/pybind/rgw.py new file mode 100644 index 0000000000000..7cbac1c8a5558 --- /dev/null +++ b/src/pybind/rgw.py @@ -0,0 +1,36 @@ +"""librgw Python ctypes wrapper +Copyright 2011, New Dream Network +""" +from ctypes import CDLL, c_char_p, c_size_t, c_void_p,\ + create_string_buffer, byref, Structure, c_uint64, c_ubyte, c_byte,\ + pointer, c_int +import ctypes +import datetime +import errno +import time + +class Rgw(object): + """librgw python wrapper""" + def __init__(self): + self.lib = CDLL('librgw.so') + def acl_bin2xml(self, blob): + blob_buf = ctypes.create_string_buffer(blob[:]) + xml = c_char_p(0) + ret = self.lib.librgw_acl_bin2xml(byref(blob_buf), len(blob), byref(xml)) + if (ret != 0): + raise Exception(ret, "acl_bin2xml failed with error %d" % ret) + retstr = str(xml) + self.lib.librgw_free_xml(xml) + return retstr + def acl_xml2bin(self, xml): + blen = c_int(0) + blob = c_void_p(0) + print "WATERMELON 1" + ret = self.lib.librgw_acl_xml2bin(c_char_p(xml), byref(blob), byref(blen)) + if (ret != 0): + raise Exception(ret, "acl_bin2xml failed with error %d" % ret) + print "WATERMELON 2" + retblob = ctypes.cast(blob, ctypes.POINTER(ctypes.c_ubyte * blen.value)) + rets = str(retblob) + self.lib.librgw_free_bin(blob) + return rets diff --git a/src/rgw/librgw.cc b/src/rgw/librgw.cc index 5034a8964d85e..24e1276f66c34 100644 --- a/src/rgw/librgw.cc +++ b/src/rgw/librgw.cc @@ -16,11 +16,14 @@ #include "include/rados/librgw.h" #include "rgw/rgw_acl.h" #include "rgw_acl.h" +#include "common/config.h" #include #include #include +#define RGW_LOG(x) pdout(x, g_conf.rgw_log) + int librgw_acl_bin2xml(const char *bin, int bin_len, char **xml) { try { @@ -43,7 +46,12 @@ int librgw_acl_bin2xml(const char *bin, int bin_len, char **xml) return -ENOBUFS; return 0; } + catch (const std::exception &e) { + RGW_LOG(-1) << "librgw_acl_bin2xml: caught exception " << e.what() << dendl; + return -2000; + } catch (...) { + RGW_LOG(-1) << "librgw_acl_bin2xml: caught unknown exception " << dendl; return -2000; } } @@ -83,12 +91,16 @@ int librgw_acl_xml2bin(const char *xml, char **bin, int *bin_len) *bin_len = bin_len_; return 0; } + catch (const std::exception &e) { + RGW_LOG(-1) << "librgw_acl_bin2xml: caught exception " << e.what() << dendl; + } catch (...) { - if (!bin_) - free(bin_); - bin_ = NULL; - return -2000; + RGW_LOG(-1) << "librgw_acl_bin2xml: caught unknown exception " << dendl; } + if (!bin_) + free(bin_); + bin_ = NULL; + return -2000; } void librgw_free_bin(char *bin) diff --git a/src/test/ceph-pybind-rgw-test.py b/src/test/ceph-pybind-rgw-test.py new file mode 100644 index 0000000000000..f7fd02be25eb5 --- /dev/null +++ b/src/test/ceph-pybind-rgw-test.py @@ -0,0 +1,35 @@ +#!/usr/bin/python + +import rgw +import sys + +r = rgw.Rgw() + +xml = """\n + \n + foo\n + MrFoo\n + \n + \n + \n + \n + bar\n + display-name\n + \n + FULL_CONTROL\n + \n + \n +""" + +print "converting %s to binary..." % xml +blob = r.acl_xml2bin(xml) +print "got blob of length %d" % len(blob) + +xml2 = r.acl_bin2xml(blob) + +blob2 = r.acl_xml2bin(xml2) + +if (blob != blob2): + raise "blob differed from blob2!" + +sys.exit(0) -- 2.39.5