python/Makefile | 26 ++---
python/python-sync_manager.spec.in | 52 ----------
python/sanlock.c | 180 +++++++++++++++++++++++++++++++++++++
python/setup.py | 19 ++-
python/syncManager.py | 111 ----------------------
5 files changed, 204 insertions(+), 184 deletions(-)
New commits:
commit 9924949c58a35e685b7cdf3a5c5063c7886e94fe
Author: Federico Simoncelli <fsimonce(a)redhat.com>
Date: Tue Apr 12 19:37:18 2011 +0100
python: add python binding
diff --git a/python/Makefile b/python/Makefile
new file mode 100644
index 0000000..e48a72d
--- /dev/null
+++ b/python/Makefile
@@ -0,0 +1,12 @@
+# 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.
+
+all:
+ python setup.py build
+
+clean:
+ rm -rf build
+
diff --git a/python/sanlock.c b/python/sanlock.c
new file mode 100644
index 0000000..7e6f46f
--- /dev/null
+++ b/python/sanlock.c
@@ -0,0 +1,180 @@
+/*
+ * 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)
+{
+ __sanlock_fd = sanlock_register();
+
+ 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 resource");
+ return -1;
+ }
+
+ return 0;
+}
+
+static PyObject *
+py_add_lockspace(PyObject *self, PyObject *args)
+{
+ struct sanlk_lockspace ls;
+
+ if (__parse_lockspace(args, &ls) != 0) {
+ return NULL;
+ }
+
+ /* add sanlock lockspace */
+ if (sanlock_add_lockspace(&ls, 0 ) != 0) {
+ PyErr_SetString(sanlock_exception, "SANLock lockspace add failure");
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+py_rem_lockspace(PyObject *self, PyObject *args)
+{
+ struct sanlk_lockspace ls;
+
+ if (__parse_lockspace(args, &ls) != 0) {
+ return NULL;
+ }
+
+ /* remove sanlock lockspace */
+ if (sanlock_rem_lockspace(&ls, 0 ) != 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)
+{
+ 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) {
+ 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)
+{
+ struct sanlk_resource *res;
+
+ if (__parse_resource(args, &res) != 0) {
+ return NULL;
+ }
+
+ /* release resource */
+ if (sanlock_release(__sanlock_fd, -1, 0, 1, &res) != 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/setup.py b/python/setup.py
new file mode 100644
index 0000000..acf3810
--- /dev/null
+++ b/python/setup.py
@@ -0,0 +1,14 @@
+# 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.
+
+from distutils.core import setup, Extension
+
+module1 = Extension('sanlock', sources=['sanlock.c'],
libraries=['sanlock'])
+
+setup(name = 'SANLock',
+ version = '1.0',
+ description = 'SANLock python package',
+ ext_modules = [module1])
commit bee107aa3b690158767d37ec079d754ccff63ac9
Author: Federico Simoncelli <fsimonce(a)redhat.com>
Date: Tue Apr 12 19:37:17 2011 +0100
python: remove unused python code
diff --git a/python/Makefile b/python/Makefile
deleted file mode 100644
index 3f1f49c..0000000
--- a/python/Makefile
+++ /dev/null
@@ -1,18 +0,0 @@
-
-INSTALL=$(shell which install)
-RPMTOP=$(shell bash -c "pwd -P")/rpmbuild
-rpmversion:=0.1
-rpmrelease:=1.$(shell date +%Y%m%d)git
-TARBALL=python-sync-manger-$(rpmversion)-$(rpmrelease).tar.gz
-
-rpm:
- rm -rf $(RPMTOP)
- mkdir $(RPMTOP)
- mkdir $(RPMTOP)/BUILD $(RPMTOP)/BUILDROOT $(RPMTOP)/RPMS $(RPMTOP)/SOURCES
$(RPMTOP)/SPECS $(RPMTOP)/SRPMS
- tar zcf $(TARBALL) `git ls-files`
- mv $(TARBALL) $(RPMTOP)/SOURCES
- sed 's/^Version:.*/Version: $(rpmversion)/;s/^Release:.*/Release:
$(rpmrelease)%{?dist}/;s/^Source0:.*/Source0: $(TARBALL)/' \
- python-sync_manager.spec.in > python-sync_manager.spec
- rpmbuild -v -ba --define="_topdir $(RPMTOP)" \
- --define="_sourcedir $(RPMTOP)/SOURCES" \
- ./python-sync_manager.spec
diff --git a/python/python-sync_manager.spec.in b/python/python-sync_manager.spec.in
deleted file mode 100644
index f05b91f..0000000
--- a/python/python-sync_manager.spec.in
+++ /dev/null
@@ -1,52 +0,0 @@
-
-
-Name: python-sync-manager
-Version: 0.1
-Release: 1.19700101git%{?dist}
-Summary: Python bindings for sync-manager
-
-Group: Development/Tools
-License: GPLv2+
-URL:
http://http://cleopatra.tlv.redhat.com/qumrawiki/Sync%20Manager
-Source0: %{name}-%{version}.tar.gz
-BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}
-
-BuildRequires: python-devel
-Requires: python >= 2.6
-Requires: sync-manager = %{version}-%{release}
-
-%description
-Sync Manager is a program to manage disk paxos based leases.
-It helps synchronizing access to shared resource in a distributed system.
-This is the python bindings for it.
-
-%prep
-%setup -q -c
-
-%build
-FLAGS="$RPM_OPT_FLAGS" %{__python} setup.py build
-
-%install
-rm -rf $RPM_BUILD_ROOT
-%{__python} setup.py install -O1 --skip-build --root $RPM_BUILD_ROOT
-
-
-%clean
-rm -rf $RPM_BUILD_ROOT
-
-
-%files
-%define egg_name %(echo %{name} | sed "s/-/_/g")
-
-%defattr(-,root,root,-)
-%{python_sitelib}/syncManager.py
-%{python_sitelib}/syncManager.pyc
-%{python_sitelib}/syncManager.pyo
-%{python_sitelib}/%{egg_name}-%{version}-py?.?.egg-info
-
-%doc
-
-
-
-%changelog
-
diff --git a/python/setup.py b/python/setup.py
deleted file mode 100644
index 1fb9bce..0000000
--- a/python/setup.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from distutils.core import setup
-setup(name='python-sync-manager',
- version='0.1',
- py_modules=['syncManager'],
- )
diff --git a/python/syncManager.py b/python/syncManager.py
deleted file mode 100644
index e0080ea..0000000
--- a/python/syncManager.py
+++ /dev/null
@@ -1,111 +0,0 @@
-import subprocess
-import SocketServer
-import os
-import sys
-import logging
-from select import select
-from StringIO import StringIO
-
-SYNCMANAGER_PATH = "/usr/libexec/sync_manager"
-
-class SyncManagerException(Exception):
- def __init__(self, msg, errno, stdout, stderr):
- Exception.__init__(self, msg)
- self.errno = errno
- self.stdout = stdout
- self.stderr = stderr
-
-class SyncManager(object):
- _log = logging.getLogger("SyncManager");
- def __init__(self, name):
- self.name = name
- self.syncManagerPath = SYNCMANAGER_PATH
-
- def initStorage(self, leases, numberOfHosts, maximumNumberOfHosts = None):
- self._log.debug("Initializing leases on '%s'", leases)
- numberOfHosts = int(numberOfHosts)
- args = ["-n", self.name, "-h", str(numberOfHosts)]
-
- if not maximumNumberOfHosts is None:
- args.extend(["-H", str(maximumNumberOfHosts)])
-
- args.extend(self._compileLeaseArgs(leases))
- self._runTool("init", args)
-
- def acquireLeases(self, leases):
- args = ["-n", self.name]
-
- args.extend(self._compileLeaseArgs(leases))
- self._runTool("acquire", args)
-
- def setHostID(self, hostID):
- args = ["-n", self.name, "-i", str(hostID)]
- self._runTool("set_host_id", args)
-
- def releaseLeases(self, resources):
- args = ["-n", self.name]
-
- for resource in resources:
- args.extend(["-r", resource])
-
- self._runTool("release", args)
-
- def _compileLeaseArgs(self, leases):
- args = []
- for lease, disks in leases:
- mangledDisks = ["%s:%d" %
(os.path.abspath(disk).replace("\\", "\\\\").replace(":",
"\\:"), offset) for (disk, offset) in disks]
- args.extend(["-l", "%s:%s" % (lease,
":".join(mangledDisks))])
-
- return args
-
- def _runToolAsync(self, command, args):
- cmd = [self.syncManagerPath] + [command, "-D"] + args
- self._log.debug("Running syncmanager CMD:'%s'",
subprocess.list2cmdline(cmd))
- mngr = subprocess.Popen(cmd,
- stdin = subprocess.PIPE,
- stdout = subprocess.PIPE,
- stderr = subprocess.PIPE
- )
- return mngr
-
- def _runTool(self, command, args):
- mngr = self._runToolAsync(command, args)
- stdout = StringIO()
- stderr = StringIO()
- empty = False
- while (mngr.poll() is None) or (not empty):
- readyObjects = select([mngr.stdout, mngr.stderr], [], [], 1)[0]
- for obj in readyObjects:
- line = obj.readline().replace("\n", "")
- if line == "":
- empty = True
- continue
- if obj is mngr.stdout:
- stdout.write(line)
- stdout.write("\n")
- else:
- stderr.write(line)
- stderr.write("\n")
-
- empty = False
-
- self._log.debug("synctool - %s" % line)
-
- rc = mngr.returncode
- stdout.seek(0)
- stderr.seek(0)
- stdout = stdout.read()
- stderr = stderr.read()
- if rc != 0:
- cmd = ["sync_manager", command] + args;
- cmd = subprocess.list2cmdline(cmd)
-
- raise SyncManagerException("SyncManager failed (CMD:%s) (RC:%d) (OUT:%s)
(ERR:%s)" %
- (cmd, rc, stdout, stderr),
- errno = rc,
- stdout = stdout,
- stderr = stderr)
-
-
- return (stdout, stderr)
-