python/Makefile | 3
python/sanlock.c | 180 -------------------------------------------
python/sanlock.py | 16 +++
python/sanlockmod.c | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++++
python/setup.py | 9 +-
sanlock.spec | 22 +++++
6 files changed, 263 insertions(+), 182 deletions(-)
New commits:
commit 10489d2e5904f57541fdd92e8b99f4b88e8be411
Author: Federico Simoncelli <fsimonce(a)redhat.com>
Date: Wed Apr 13 11:00:25 2011 +0100
python: pass a lockspace copy to str_to_lockspace
sanlock_str_to_lockspace is destructive
diff --git a/python/sanlockmod.c b/python/sanlockmod.c
index a782b7a..65ead2a 100644
--- a/python/sanlockmod.c
+++ b/python/sanlockmod.c
@@ -35,20 +35,33 @@ py_register(PyObject *self, PyObject *args)
static int
__parse_lockspace(PyObject *args, struct sanlk_lockspace *ret_ls)
{
- char *lockspace;
+ char *lockspace, *lockspace_arg;
/* parse python tuple */
if (!PyArg_ParseTuple(args, "s", &lockspace)) {
return -1;
}
+ /* sanlock_str_to_lockspace is destructive */
+ lockspace_arg = strdup(lockspace);
+
+ if (lockspace_arg == NULL) {
+ PyErr_SetString(sanlockmod_exception, "SANLock extension memory
error");
+ return -1;
+ }
+
/* convert lockspace string to structure */
- if (sanlock_str_to_lockspace(lockspace, ret_ls) != 0) {
+ if (sanlock_str_to_lockspace(lockspace_arg, ret_ls) != 0) {
PyErr_SetString(sanlockmod_exception, "Invalid SANLock lockspace");
- return -1;
+ goto exit_fail;
}
+ free(lockspace_arg);
return 0;
+
+exit_fail:
+ free(lockspace_arg);
+ return -1;
}
static PyObject *
commit 6506dc4cd68a2585bbae2077e6dc58c9dcfdafa4
Author: Federico Simoncelli <fsimonce(a)redhat.com>
Date: Wed Apr 13 11:00:24 2011 +0100
rpm: add python binding package
diff --git a/python/Makefile b/python/Makefile
index e48a72d..c72c253 100644
--- a/python/Makefile
+++ b/python/Makefile
@@ -7,6 +7,9 @@
all:
python setup.py build
+install:
+ python setup.py install --root=$(DESTDIR)
+
clean:
rm -rf build
diff --git a/python/setup.py b/python/setup.py
index f8b34df..843216c 100644
--- a/python/setup.py
+++ b/python/setup.py
@@ -6,7 +6,11 @@
from distutils.core import setup, Extension
-sanlockmod = Extension('sanlockmod', sources=['sanlockmod.c'],
libraries=['sanlock'])
+sanlockmod = Extension(name = 'sanlockmod',
+ sources = ['sanlockmod.c'],
+ include_dirs = ['../src'],
+ library_dirs = ['../src'],
+ libraries = ['sanlock'])
setup(name = 'SANLock',
version = '1.0',
diff --git a/sanlock.spec b/sanlock.spec
index d936756..5eadafe 100644
--- a/sanlock.spec
+++ b/sanlock.spec
@@ -1,3 +1,5 @@
+%{!?python_sitearch: %global python_sitearch %(%{__python} -c "from
distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}
+
Name: sanlock
Version: 1.1.0
Release: 3%{?dist}
@@ -26,6 +28,7 @@ access to the shared disks.
# upstream does not support _smp_mflags
CFLAGS=$RPM_OPT_FLAGS make -C wdmd
CFLAGS=$RPM_OPT_FLAGS make -C src
+CFLAGS=$RPM_OPT_FLAGS make -C python
%install
rm -rf $RPM_BUILD_ROOT
@@ -35,6 +38,9 @@ make -C src \
make -C wdmd \
install LIB_LIBDIR=%{_libdir} \
DESTDIR=$RPM_BUILD_ROOT
+make -C python \
+ install LIB_LIBDIR=%{_libdir} \
+ DESTDIR=$RPM_BUILD_ROOT
install -D -m 755 init.d/sanlock $RPM_BUILD_ROOT/%{_initddir}/sanlock
install -D -m 755 init.d/wdmd $RPM_BUILD_ROOT/%{_initddir}/wdmd
@@ -85,6 +91,22 @@ access to the shared disks.
%{_libdir}/libsanlock.so.*
%{_libdir}/libwdmd.so.*
+%package python
+Summary: Python bindings for the sanlock library
+Group: Development/Libraries
+
+%description python
+The %{name}-python package contains a module that permits applications
+written in the Python programming language to use the interface
+supplied by the sanlock library.
+
+%files python
+%defattr(-,root,root,-)
+%doc COPYING
+%{python_sitearch}/SANLock-1.0-py2.6.egg-info
+%{python_sitearch}/sanlock.py*
+%{python_sitearch}/sanlockmod.so
+
%package devel
Summary: Development files for %{name}
Group: Development/Libraries
commit d5ac787ec2f255a06506f4c513fe9a4052969399
Author: Federico Simoncelli <fsimonce(a)redhat.com>
Date: Wed Apr 13 11:00:23 2011 +0100
python: wrap sanlock extension with a module
diff --git a/python/sanlock.c b/python/sanlock.c
deleted file mode 100644
index ed11d79..0000000
--- a/python/sanlock.c
+++ /dev/null
@@ -1,202 +0,0 @@
-/*
- * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU General Public License v.2.
- */
-
-#include <Python.h>
-#include <sanlock.h>
-#include <sanlock_resource.h>
-#include <sanlock_admin.h>
-
-int __sanlock_fd = -1;
-PyObject *py_module;
-
-/* SANLock exception */
-static PyObject *sanlock_exception;
-
-static PyObject *
-py_register(PyObject *self, PyObject *args)
-{
- Py_BEGIN_ALLOW_THREADS
- __sanlock_fd = sanlock_register();
- Py_END_ALLOW_THREADS
-
- if (__sanlock_fd < 0) {
- PyErr_SetString(sanlock_exception, "SANLock registration failed");
- return NULL;
- }
-
- Py_RETURN_NONE;
-}
-
-static int
-__parse_lockspace(PyObject *args, struct sanlk_lockspace *ret_ls)
-{
- char *lockspace;
-
- /* parse python tuple */
- if (!PyArg_ParseTuple(args, "s", &lockspace)) {
- return -1;
- }
-
- /* convert lockspace string to structure */
- if (sanlock_str_to_lockspace(lockspace, ret_ls) != 0) {
- PyErr_SetString(sanlock_exception, "Invalid SANLock lockspace");
- return -1;
- }
-
- return 0;
-}
-
-static PyObject *
-py_add_lockspace(PyObject *self, PyObject *args)
-{
- int rv;
- struct sanlk_lockspace ls;
-
- if (__parse_lockspace(args, &ls) != 0) {
- return NULL;
- }
-
- /* add sanlock lockspace (gil disabled) */
- Py_BEGIN_ALLOW_THREADS
- rv = sanlock_add_lockspace(&ls, 0 );
- Py_END_ALLOW_THREADS
-
- if (rv != 0) {
- PyErr_SetString(sanlock_exception, "SANLock lockspace add failure");
- return NULL;
- }
-
- Py_RETURN_NONE;
-}
-
-static PyObject *
-py_rem_lockspace(PyObject *self, PyObject *args)
-{
- int rv;
- struct sanlk_lockspace ls;
-
- if (__parse_lockspace(args, &ls) != 0) {
- return NULL;
- }
-
- /* remove sanlock lockspace (gil disabled) */
- Py_BEGIN_ALLOW_THREADS
- rv = sanlock_rem_lockspace(&ls, 0);
- Py_END_ALLOW_THREADS
-
- if (rv != 0) {
- PyErr_SetString(sanlock_exception, "SANLock lockspace remove
failure");
- return NULL;
- }
-
- Py_RETURN_NONE;
-}
-
-static int
-__parse_resource(PyObject *args, struct sanlk_resource **ret_res)
-{
- char *resource;
-
- /* parse python tuple */
- if (!PyArg_ParseTuple(args, "s", &resource)) {
- return -1;
- }
-
- /* convert resource string to structure */
- if (sanlock_str_to_res(resource, ret_res) != 0) {
- PyErr_SetString(sanlock_exception, "Invalid SANLock resource");
- return -1;
- }
-
- return 0;
-}
-
-static PyObject *
-py_acquire(PyObject *self, PyObject *args)
-{
- int rv;
- struct sanlk_resource *res;
-
- if (__parse_resource(args, &res) != 0) {
- return NULL;
- }
-
- /* acquire resource (gil disabled) */
- Py_BEGIN_ALLOW_THREADS
- rv = sanlock_acquire(__sanlock_fd, -1, 0, 1, &res, 0);
- Py_END_ALLOW_THREADS
-
- if (rv != 0) {
- PyErr_SetString(sanlock_exception, "SANLock resource not acquired");
- goto exit_fail;
- }
-
- free(res);
- Py_RETURN_NONE;
-
-exit_fail:
- free(res);
- return NULL;
-}
-
-static PyObject *
-py_release(PyObject *self, PyObject *args)
-{
- int rv;
- struct sanlk_resource *res;
-
- if (__parse_resource(args, &res) != 0) {
- return NULL;
- }
-
- /* release resource (gil disabled)*/
- Py_BEGIN_ALLOW_THREADS
- rv = sanlock_release(__sanlock_fd, -1, 0, 1, &res);
- Py_END_ALLOW_THREADS
-
- if (rv != 0) {
- PyErr_SetString(sanlock_exception, "SANLock resource not released");
- goto exit_fail;
- }
-
- free(res);
- Py_RETURN_NONE;
-
-exit_fail:
- free(res);
- return NULL;
-}
-
-static PyMethodDef
-sanlock_methods[] = {
- {"register", py_register, METH_NOARGS, "Register to sanlock
daemon."},
- {"add_lockspace", py_add_lockspace, METH_VARARGS,
- "Add a lockspace, acquiring a host_id in it."},
- {"rem_lockspace", py_rem_lockspace, METH_VARARGS,
- "Remove a lockspace, releasing our host_id in it."},
- {"acquire", py_acquire, METH_VARARGS,
- "Acquire leases for the current process."},
- {"release", py_release, METH_VARARGS,
- "Release leases for the current process."},
- {NULL, NULL, 0, NULL}
-};
-
-PyMODINIT_FUNC
-initsanlock(void)
-{
- py_module = Py_InitModule("sanlock", sanlock_methods);
-
- /* Python's module loader doesn't support clean recovery from errors */
- if (py_module == NULL)
- return;
-
- /* Initializing sanlock exception */
- sanlock_exception = PyErr_NewException("sanlock.exception", NULL, NULL);
- Py_INCREF(sanlock_exception);
- PyModule_AddObject(py_module, "exception", sanlock_exception);
-}
diff --git a/python/sanlock.py b/python/sanlock.py
new file mode 100644
index 0000000..35ccb3b
--- /dev/null
+++ b/python/sanlock.py
@@ -0,0 +1,16 @@
+# Copyright (C) 2010-2011 Red Hat, Inc. All rights reserved.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions
+# of the GNU General Public License v.2.
+
+import sys
+import sanlockmod
+
+SANLOCK_FUNCTIONS = ('register', 'add_lockspace',
+ 'rem_lockspace', 'acquire', 'release')
+
+for skfun in SANLOCK_FUNCTIONS:
+ setattr(sys.modules[__name__], skfun, getattr(sanlockmod, skfun))
+del skfun
+
diff --git a/python/sanlockmod.c b/python/sanlockmod.c
new file mode 100644
index 0000000..a782b7a
--- /dev/null
+++ b/python/sanlockmod.c
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License v.2.
+ */
+
+#include <Python.h>
+#include <sanlock.h>
+#include <sanlock_resource.h>
+#include <sanlock_admin.h>
+
+int __sanlockmod_fd = -1;
+PyObject *py_module;
+
+/* SANLock exception */
+static PyObject *sanlockmod_exception;
+
+static PyObject *
+py_register(PyObject *self, PyObject *args)
+{
+ Py_BEGIN_ALLOW_THREADS
+ __sanlockmod_fd = sanlock_register();
+ Py_END_ALLOW_THREADS
+
+ if (__sanlockmod_fd < 0) {
+ PyErr_SetString(sanlockmod_exception, "SANLock registration failed");
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+
+static int
+__parse_lockspace(PyObject *args, struct sanlk_lockspace *ret_ls)
+{
+ char *lockspace;
+
+ /* parse python tuple */
+ if (!PyArg_ParseTuple(args, "s", &lockspace)) {
+ return -1;
+ }
+
+ /* convert lockspace string to structure */
+ if (sanlock_str_to_lockspace(lockspace, ret_ls) != 0) {
+ PyErr_SetString(sanlockmod_exception, "Invalid SANLock lockspace");
+ return -1;
+ }
+
+ return 0;
+}
+
+static PyObject *
+py_add_lockspace(PyObject *self, PyObject *args)
+{
+ int rv;
+ struct sanlk_lockspace ls;
+
+ if (__parse_lockspace(args, &ls) != 0) {
+ return NULL;
+ }
+
+ /* add sanlock lockspace (gil disabled) */
+ Py_BEGIN_ALLOW_THREADS
+ rv = sanlock_add_lockspace(&ls, 0 );
+ Py_END_ALLOW_THREADS
+
+ if (rv != 0) {
+ PyErr_SetString(sanlockmod_exception, "SANLock lockspace add
failure");
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+py_rem_lockspace(PyObject *self, PyObject *args)
+{
+ int rv;
+ struct sanlk_lockspace ls;
+
+ if (__parse_lockspace(args, &ls) != 0) {
+ return NULL;
+ }
+
+ /* remove sanlock lockspace (gil disabled) */
+ Py_BEGIN_ALLOW_THREADS
+ rv = sanlock_rem_lockspace(&ls, 0);
+ Py_END_ALLOW_THREADS
+
+ if (rv != 0) {
+ PyErr_SetString(sanlockmod_exception, "SANLock lockspace remove
failure");
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+
+static int
+__parse_resource(PyObject *args, struct sanlk_resource **ret_res)
+{
+ char *resource;
+
+ /* parse python tuple */
+ if (!PyArg_ParseTuple(args, "s", &resource)) {
+ return -1;
+ }
+
+ /* convert resource string to structure */
+ if (sanlock_str_to_res(resource, ret_res) != 0) {
+ PyErr_SetString(sanlockmod_exception, "Invalid SANLock resource");
+ return -1;
+ }
+
+ return 0;
+}
+
+static PyObject *
+py_acquire(PyObject *self, PyObject *args)
+{
+ int rv;
+ struct sanlk_resource *res;
+
+ if (__parse_resource(args, &res) != 0) {
+ return NULL;
+ }
+
+ /* acquire resource (gil disabled) */
+ Py_BEGIN_ALLOW_THREADS
+ rv = sanlock_acquire(__sanlockmod_fd, -1, 0, 1, &res, 0);
+ Py_END_ALLOW_THREADS
+
+ if (rv != 0) {
+ PyErr_SetString(sanlockmod_exception, "SANLock resource not
acquired");
+ goto exit_fail;
+ }
+
+ free(res);
+ Py_RETURN_NONE;
+
+exit_fail:
+ free(res);
+ return NULL;
+}
+
+static PyObject *
+py_release(PyObject *self, PyObject *args)
+{
+ int rv;
+ struct sanlk_resource *res;
+
+ if (__parse_resource(args, &res) != 0) {
+ return NULL;
+ }
+
+ /* release resource (gil disabled)*/
+ Py_BEGIN_ALLOW_THREADS
+ rv = sanlock_release(__sanlockmod_fd, -1, 0, 1, &res);
+ Py_END_ALLOW_THREADS
+
+ if (rv != 0) {
+ PyErr_SetString(sanlockmod_exception, "SANLock resource not
released");
+ goto exit_fail;
+ }
+
+ free(res);
+ Py_RETURN_NONE;
+
+exit_fail:
+ free(res);
+ return NULL;
+}
+
+static PyMethodDef
+sanlockmod_methods[] = {
+ {"register", py_register, METH_NOARGS, "Register to SANLock
daemon."},
+ {"add_lockspace", py_add_lockspace, METH_VARARGS,
+ "Add a lockspace, acquiring a host_id in it."},
+ {"rem_lockspace", py_rem_lockspace, METH_VARARGS,
+ "Remove a lockspace, releasing our host_id in it."},
+ {"acquire", py_acquire, METH_VARARGS,
+ "Acquire leases for the current process."},
+ {"release", py_release, METH_VARARGS,
+ "Release leases for the current process."},
+ {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC
+initsanlockmod(void)
+{
+ py_module = Py_InitModule("sanlockmod", sanlockmod_methods);
+
+ /* Python's module loader doesn't support clean recovery from errors */
+ if (py_module == NULL)
+ return;
+
+ /* Initializing sanlock exception */
+ sanlockmod_exception = PyErr_NewException("sanlockmod.exception", NULL,
NULL);
+ Py_INCREF(sanlockmod_exception);
+ PyModule_AddObject(py_module, "exception", sanlockmod_exception);
+}
diff --git a/python/setup.py b/python/setup.py
index acf3810..f8b34df 100644
--- a/python/setup.py
+++ b/python/setup.py
@@ -6,9 +6,10 @@
from distutils.core import setup, Extension
-module1 = Extension('sanlock', sources=['sanlock.c'],
libraries=['sanlock'])
+sanlockmod = Extension('sanlockmod', sources=['sanlockmod.c'],
libraries=['sanlock'])
setup(name = 'SANLock',
version = '1.0',
description = 'SANLock python package',
- ext_modules = [module1])
+ ext_modules = [sanlockmod],
+ py_modules = ['sanlock'])
commit 6550399984a1af9ba58903660c655237ed60e810
Author: Federico Simoncelli <fsimonce(a)redhat.com>
Date: Wed Apr 13 11:00:22 2011 +0100
python: release the gil during sanlock operations
diff --git a/python/sanlock.c b/python/sanlock.c
index 7e6f46f..ed11d79 100644
--- a/python/sanlock.c
+++ b/python/sanlock.c
@@ -20,7 +20,9 @@ static PyObject *sanlock_exception;
static PyObject *
py_register(PyObject *self, PyObject *args)
{
+ Py_BEGIN_ALLOW_THREADS
__sanlock_fd = sanlock_register();
+ Py_END_ALLOW_THREADS
if (__sanlock_fd < 0) {
PyErr_SetString(sanlock_exception, "SANLock registration failed");
@@ -42,7 +44,7 @@ __parse_lockspace(PyObject *args, struct sanlk_lockspace *ret_ls)
/* convert lockspace string to structure */
if (sanlock_str_to_lockspace(lockspace, ret_ls) != 0) {
- PyErr_SetString(sanlock_exception, "Invalid sanlock resource");
+ PyErr_SetString(sanlock_exception, "Invalid SANLock lockspace");
return -1;
}
@@ -52,14 +54,19 @@ __parse_lockspace(PyObject *args, struct sanlk_lockspace *ret_ls)
static PyObject *
py_add_lockspace(PyObject *self, PyObject *args)
{
+ int rv;
struct sanlk_lockspace ls;
if (__parse_lockspace(args, &ls) != 0) {
return NULL;
}
- /* add sanlock lockspace */
- if (sanlock_add_lockspace(&ls, 0 ) != 0) {
+ /* add sanlock lockspace (gil disabled) */
+ Py_BEGIN_ALLOW_THREADS
+ rv = sanlock_add_lockspace(&ls, 0 );
+ Py_END_ALLOW_THREADS
+
+ if (rv != 0) {
PyErr_SetString(sanlock_exception, "SANLock lockspace add failure");
return NULL;
}
@@ -70,14 +77,19 @@ py_add_lockspace(PyObject *self, PyObject *args)
static PyObject *
py_rem_lockspace(PyObject *self, PyObject *args)
{
+ int rv;
struct sanlk_lockspace ls;
if (__parse_lockspace(args, &ls) != 0) {
return NULL;
}
- /* remove sanlock lockspace */
- if (sanlock_rem_lockspace(&ls, 0 ) != 0) {
+ /* remove sanlock lockspace (gil disabled) */
+ Py_BEGIN_ALLOW_THREADS
+ rv = sanlock_rem_lockspace(&ls, 0);
+ Py_END_ALLOW_THREADS
+
+ if (rv != 0) {
PyErr_SetString(sanlock_exception, "SANLock lockspace remove
failure");
return NULL;
}
@@ -97,7 +109,7 @@ __parse_resource(PyObject *args, struct sanlk_resource **ret_res)
/* convert resource string to structure */
if (sanlock_str_to_res(resource, ret_res) != 0) {
- PyErr_SetString(sanlock_exception, "Invalid sanlock resource");
+ PyErr_SetString(sanlock_exception, "Invalid SANLock resource");
return -1;
}
@@ -107,14 +119,19 @@ __parse_resource(PyObject *args, struct sanlk_resource **ret_res)
static PyObject *
py_acquire(PyObject *self, PyObject *args)
{
+ int rv;
struct sanlk_resource *res;
if (__parse_resource(args, &res) != 0) {
return NULL;
}
- /* acquire resource */
- if (sanlock_acquire(__sanlock_fd, -1, 0, 1, &res, 0) != 0) {
+ /* acquire resource (gil disabled) */
+ Py_BEGIN_ALLOW_THREADS
+ rv = sanlock_acquire(__sanlock_fd, -1, 0, 1, &res, 0);
+ Py_END_ALLOW_THREADS
+
+ if (rv != 0) {
PyErr_SetString(sanlock_exception, "SANLock resource not acquired");
goto exit_fail;
}
@@ -130,14 +147,19 @@ exit_fail:
static PyObject *
py_release(PyObject *self, PyObject *args)
{
+ int rv;
struct sanlk_resource *res;
if (__parse_resource(args, &res) != 0) {
return NULL;
}
- /* release resource */
- if (sanlock_release(__sanlock_fd, -1, 0, 1, &res) != 0) {
+ /* release resource (gil disabled)*/
+ Py_BEGIN_ALLOW_THREADS
+ rv = sanlock_release(__sanlock_fd, -1, 0, 1, &res);
+ Py_END_ALLOW_THREADS
+
+ if (rv != 0) {
PyErr_SetString(sanlock_exception, "SANLock resource not released");
goto exit_fail;
}