Add python bindings for librpmsign
authorPanu Matilainen <pmatilai@redhat.com>
Wed, 13 Oct 2010 11:29:28 +0000 (14:29 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Wed, 13 Oct 2010 11:29:28 +0000 (14:29 +0300)
- Similarly to the build-bindings, permit import failures as librpmsign
  might not be installed and we dont want to force the dependencies
  into every single installation

python/Makefile.am
python/rpm/__init__.py
python/rpmsmodule.c [new file with mode: 0644]

index 2bd6232..b952743 100644 (file)
@@ -7,7 +7,7 @@ AM_CPPFLAGS += -I$(top_srcdir)/python
 AM_CPPFLAGS += -I$(top_srcdir)/misc
 AM_CPPFLAGS += -I@WITH_PYTHON_INCLUDE@
 
-pkgpyexec_LTLIBRARIES = _rpmmodule.la _rpmbmodule.la
+pkgpyexec_LTLIBRARIES = _rpmmodule.la _rpmbmodule.la _rpmsmodule.la
 pkgpyexec_DATA = rpm/__init__.py rpm/transaction.py
 
 _rpmmodule_la_LDFLAGS = -module -avoid-version -shared
@@ -38,3 +38,13 @@ _rpmbmodule_la_LIBADD = \
 
 _rpmbmodule_la_SOURCES = rpmbmodule.c rpmsystem-py.h \
        spec-py.c spec-py.h
+
+_rpmsmodule_la_LDFLAGS = -module -avoid-version -shared
+_rpmsmodule_la_LIBADD = \
+        $(top_builddir)/lib/librpmsign.la \
+        $(top_builddir)/lib/librpm.la \
+        $(top_builddir)/rpmio/librpmio.la \
+        @WITH_PYTHON_LIB@
+
+_rpmsmodule_la_SOURCES = rpmsmodule.c rpmsystem-py.h
+
index d584a39..d868d19 100644 (file)
@@ -23,6 +23,12 @@ try:
 except ImportError:
     pass
 
+# try to import signing bits but dont require it
+try:
+    from rpm._rpms import *
+except ImportError:
+    pass
+
 # backwards compatibility + give the same class both ways
 ts = TransactionSet
 
diff --git a/python/rpmsmodule.c b/python/rpmsmodule.c
new file mode 100644 (file)
index 0000000..e0b1b66
--- /dev/null
@@ -0,0 +1,97 @@
+#include "rpmsystem-py.h"
+
+#include <rpm/rpmsign.h>
+
+#include "debug.h"
+
+static char rpms__doc__[] =
+"";
+
+static PyObject * addSign(PyObject * self, PyObject * args, PyObject *kwds)
+{
+    const char *path = NULL;
+    const char *passPhrase = NULL;
+    char * kwlist[] = { "path", "passPhrase", "keyid", "hashalgo", NULL };
+    struct rpmSignArgs sig, *sigp = NULL;
+
+    memset(&sig, 0, sizeof(sig));
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss|si", kwlist,
+                               &path, &passPhrase, &sig.keyid, &sig.hashalgo))
+       return NULL;
+
+    if (sig.keyid || sig.hashalgo)
+       sigp = &sig;
+
+    return PyBool_FromLong(rpmPkgSign(path, sigp, passPhrase) == 0);
+}
+
+static PyObject * delSign(PyObject * self, PyObject * args, PyObject *kwds)
+{
+    const char *path = NULL;
+    char * kwlist[] = { "path", NULL };
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &path))
+       return NULL;
+
+    return PyBool_FromLong(rpmPkgDelSign(path) == 0);
+}
+
+/*
+  Do any common preliminary work before python 2 vs python 3 module creation:
+*/
+static int prepareInitModule(void)
+{
+    return 1;
+}
+
+static int initModule(PyObject *m)
+{
+    return 1;
+}
+
+static PyMethodDef modMethods[] = {
+    { "addSign", (PyCFunction) addSign, METH_VARARGS|METH_KEYWORDS, NULL },
+    { "delSign", (PyCFunction) delSign, METH_VARARGS|METH_KEYWORDS, NULL },
+    { NULL },
+};
+
+#if PY_MAJOR_VERSION >= 3
+static struct PyModuleDef moduledef = {
+    PyModuleDef_HEAD_INIT,
+    "_rpms",     /* m_name */
+    rpms__doc__, /* m_doc */
+    0,           /* m_size */
+    NULL,        /* m_methods */
+    NULL,        /* m_reload */
+    NULL,        /* m_traverse */
+    NULL,        /* m_clear */
+    NULL         /* m_free */
+};
+
+PyObject * PyInit__rpm(void);  /* XXX eliminate gcc warning */
+PyObject * PyInit__rpm(void)
+{
+    PyObject *m;
+
+    if (!prepareInitModule())
+        return NULL;
+    m = PyModule_Create(&moduledef);
+    if (m == NULL || !initModule(m)) {
+        Py_XDECREF(m);
+        m = NULL;
+    }
+    return m;
+}
+#else
+void init_rpms(void);  /* XXX eliminate gcc warning */
+void init_rpms(void)
+{
+    PyObject *m;
+  
+    if (!prepareInitModule())
+        return;
+
+    m = Py_InitModule3("_rpms", modMethods, rpms__doc__);
+    if (m) initModule(m);
+}
+#endif