]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
Add Python bindings for librgw
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Fri, 13 May 2011 23:07:08 +0000 (16:07 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 17 May 2011 18:04:12 +0000 (11:04 -0700)
Add some Python bindings for librgw.
Also add some more verbose error logging to librgw.

Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/Makefile.am
src/pybind/rgw.py [new file with mode: 0644]
src/rgw/librgw.cc
src/test/ceph-pybind-rgw-test.py [new file with mode: 0644]

index a5b4229dc729fa97badaedecdfa816b377a4b374..27b440a82c074d8a2306d5c6fe6e63b984586b6b 100644 (file)
@@ -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 (file)
index 0000000..7cbac1c
--- /dev/null
@@ -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
index 5034a8964d85e5eeddb1f36e60a548f599682de4..24e1276f66c34ba50a2c6c8c98c960d94012c3bb 100644 (file)
 #include "include/rados/librgw.h"
 #include "rgw/rgw_acl.h"
 #include "rgw_acl.h"
+#include "common/config.h"
 
 #include <errno.h>
 #include <sstream>
 #include <string.h>
 
+#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 (file)
index 0000000..f7fd02b
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/python
+
+import rgw
+import sys
+
+r = rgw.Rgw()
+
+xml = """<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">\n
+  <Owner>\n
+    <ID>foo</ID>\n
+    <DisplayName>MrFoo</DisplayName>\n
+  </Owner>\n
+  <AccessControlList>\n
+    <Grant>\n
+      <Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"CanonicalUser\">\n
+       <ID>bar</ID>\n
+       <DisplayName>display-name</DisplayName>\n
+      </Grantee>\n
+      <Permission>FULL_CONTROL</Permission>\n
+    </Grant>\n
+  </AccessControlList>\n
+</AccessControlPolicy>"""
+
+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)