mirror of
https://github.com/ceph/ceph
synced 2025-02-24 03:27:10 +00:00
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 <colin.mccabe@dreamhost.com>
This commit is contained in:
parent
8ed372c903
commit
36fb0846a8
@ -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'
|
||||
|
36
src/pybind/rgw.py
Normal file
36
src/pybind/rgw.py
Normal file
@ -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
|
@ -16,11 +16,14 @@
|
||||
#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 (...) {
|
||||
if (!bin_)
|
||||
free(bin_);
|
||||
bin_ = NULL;
|
||||
return -2000;
|
||||
catch (const std::exception &e) {
|
||||
RGW_LOG(-1) << "librgw_acl_bin2xml: caught exception " << e.what() << dendl;
|
||||
}
|
||||
catch (...) {
|
||||
RGW_LOG(-1) << "librgw_acl_bin2xml: caught unknown exception " << dendl;
|
||||
}
|
||||
if (!bin_)
|
||||
free(bin_);
|
||||
bin_ = NULL;
|
||||
return -2000;
|
||||
}
|
||||
|
||||
void librgw_free_bin(char *bin)
|
||||
|
35
src/test/ceph-pybind-rgw-test.py
Normal file
35
src/test/ceph-pybind-rgw-test.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user