Add bindings for rpm keyring and pubkey types
authorPanu Matilainen <pmatilai@redhat.com>
Fri, 9 Oct 2009 06:02:19 +0000 (09:02 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Fri, 9 Oct 2009 06:02:19 +0000 (09:02 +0300)
python/Makefile.am
python/rpmkeyring-py.c [new file with mode: 0644]
python/rpmkeyring-py.h [new file with mode: 0644]
python/rpmmodule.c

index 2b91392..f50c103 100644 (file)
@@ -22,6 +22,7 @@ _rpmmodule_la_SOURCES = rpmmodule.c rpmsystem-py.h \
        rpmds-py.c rpmds-py.h \
        rpmfd-py.c rpmfd-py.h \
        rpmfi-py.c rpmfi-py.h \
+       rpmkeyring-py.c rpmkeyring-py.h \
        rpmmi-py.c rpmmi-py.h \
        rpmps-py.c rpmps-py.h \
        rpmmacro-py.c rpmmacro-py.h \
diff --git a/python/rpmkeyring-py.c b/python/rpmkeyring-py.c
new file mode 100644 (file)
index 0000000..9fa4f48
--- /dev/null
@@ -0,0 +1,204 @@
+#include "rpmsystem-py.h"
+#include <rpm/rpmkeyring.h>
+#include "rpmkeyring-py.h"
+
+struct rpmPubkeyObject_s {
+    PyObject_HEAD
+    PyObject *md_dict;
+    rpmPubkey pubkey;
+};
+
+static void rpmPubkey_dealloc(rpmPubkeyObject * s)
+{
+    s->pubkey = rpmPubkeyFree(s->pubkey);
+    s->ob_type->tp_free((PyObject *)s);
+}
+
+static PyObject *rpmPubkey_new(PyTypeObject *subtype, 
+                          PyObject *args, PyObject *kwds)
+{
+    PyObject *key;
+    char *kwlist[] = { "key", NULL };
+    rpmPubkey pubkey = NULL;
+    uint8_t *pkt;
+    size_t pktlen;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "S", kwlist, &key))
+       return NULL;
+
+    if (pgpParsePkts(PyString_AsString(key), &pkt, &pktlen) <= 0) {
+       PyErr_SetString(PyExc_ValueError, "invalid pubkey");
+       return NULL;
+    }
+    pubkey = rpmPubkeyNew(pkt, pktlen);
+
+    return rpmPubkey_Wrap(subtype, pubkey);
+}
+
+static PyObject * rpmPubkey_Base64(rpmPubkeyObject *s)
+{
+    char *b64 = rpmPubkeyBase64(s->pubkey);
+    PyObject *res = Py_BuildValue("s", b64);
+    free(b64);
+    return res;
+}
+
+static struct PyMethodDef rpmPubkey_methods[] = {
+    { "base64", (PyCFunction) rpmPubkey_Base64, METH_NOARGS, NULL },
+    { NULL }
+};
+
+static char rpmPubkey_doc[] = "";
+
+PyTypeObject rpmPubkey_Type = {
+       PyObject_HEAD_INIT(&PyType_Type)
+       0,                              /* ob_size */
+       "rpm.pubkey",                   /* tp_name */
+       sizeof(rpmPubkeyObject),        /* tp_size */
+       0,                              /* tp_itemsize */
+       (destructor) rpmPubkey_dealloc,/* tp_dealloc */
+       0,                              /* tp_print */
+       (getattrfunc)0,                 /* tp_getattr */
+       0,                              /* tp_setattr */
+       0,                              /* tp_compare */
+       0,                              /* tp_repr */
+       0,                              /* tp_as_number */
+       0,                              /* tp_as_sequence */
+       0,                              /* tp_as_mapping */
+       0,                              /* tp_hash */
+       0,                              /* tp_call */
+       0,                              /* tp_str */
+       PyObject_GenericGetAttr,        /* tp_getattro */
+       PyObject_GenericSetAttr,        /* tp_setattro */
+       0,                              /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
+       rpmPubkey_doc,                  /* tp_doc */
+       0,                              /* tp_traverse */
+       0,                              /* tp_clear */
+       0,                              /* tp_richcompare */
+       0,                              /* tp_weaklistoffset */
+       0,                              /* tp_iter */
+       0,                              /* tp_iternext */
+       rpmPubkey_methods,              /* tp_methods */
+       0,                              /* tp_members */
+       0,                              /* tp_getset */
+       0,                              /* tp_base */
+       0,                              /* tp_dict */
+       0,                              /* tp_descr_get */
+       0,                              /* tp_descr_set */
+       0,                              /* tp_dictoffset */
+       0,                              /* tp_init */
+       0,                              /* tp_alloc */
+       rpmPubkey_new,                  /* tp_new */
+       0,                              /* tp_free */
+       0,                              /* tp_is_gc */
+};
+
+struct rpmKeyringObject_s {
+    PyObject_HEAD
+    PyObject *md_dict;
+    rpmKeyring keyring;
+};
+
+static void rpmKeyring_dealloc(rpmKeyringObject * s)
+{
+    rpmKeyringFree(s->keyring);
+    s->ob_type->tp_free((PyObject *)s);
+}
+
+static PyObject *rpmKeyring_new(PyTypeObject *subtype, 
+                          PyObject *args, PyObject *kwds)
+{
+    rpmKeyring keyring = rpmKeyringNew();
+    return rpmKeyring_Wrap(subtype, keyring);
+}
+
+static PyObject *rpmKeyring_addKey(rpmKeyringObject *s, PyObject *arg)
+{
+    rpmPubkeyObject *pubkey = NULL;
+
+    if (!PyArg_Parse(arg, "O!", &rpmPubkey_Type, &pubkey))
+       return NULL;
+
+    return Py_BuildValue("i", rpmKeyringAddKey(s->keyring, pubkey->pubkey));
+};
+
+static struct PyMethodDef rpmKeyring_methods[] = {
+    { "addKey", (PyCFunction) rpmKeyring_addKey, METH_O,
+        NULL },
+    {NULL,             NULL}           /* sentinel */
+};
+
+static char rpmKeyring_doc[] =
+"";
+
+PyTypeObject rpmKeyring_Type = {
+       PyObject_HEAD_INIT(&PyType_Type)
+       0,                              /* ob_size */
+       "rpm.keyring",                  /* tp_name */
+       sizeof(rpmKeyringObject),       /* tp_size */
+       0,                              /* tp_itemsize */
+       (destructor) rpmKeyring_dealloc,/* tp_dealloc */
+       0,                              /* tp_print */
+       0,                              /* tp_getattr */
+       0,                              /* tp_setattr */
+       0,                              /* tp_compare */
+       0,                              /* tp_repr */
+       0,                              /* tp_as_number */
+       0,                              /* tp_as_sequence */
+       0,                              /* tp_as_mapping */
+       0,                              /* tp_hash */
+       0,                              /* tp_call */
+       0,                              /* tp_str */
+       PyObject_GenericGetAttr,        /* tp_getattro */
+       PyObject_GenericSetAttr,        /* tp_setattro */
+       0,                              /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
+       rpmKeyring_doc,                 /* tp_doc */
+       0,                              /* tp_traverse */
+       0,                              /* tp_clear */
+       0,                              /* tp_richcompare */
+       0,                              /* tp_weaklistoffset */
+       0,                              /* tp_iter */
+       0,                              /* tp_iternext */
+       rpmKeyring_methods,             /* tp_methods */
+       0,                              /* tp_members */
+       0,                              /* tp_getset */
+       0,                              /* tp_base */
+       0,                              /* tp_dict */
+       0,                              /* tp_descr_get */
+       0,                              /* tp_descr_set */
+       0,                              /* tp_dictoffset */
+       0,                              /* tp_init */
+       0,                              /* tp_alloc */
+       rpmKeyring_new,                 /* tp_new */
+       0,                              /* tp_free */
+       0,                              /* tp_is_gc */
+};
+
+PyObject * rpmPubkey_Wrap(PyTypeObject *subtype, rpmPubkey pubkey)
+{
+    rpmPubkeyObject *s = (rpmPubkeyObject *)subtype->tp_alloc(subtype, 0);
+    if (s == NULL) return PyErr_NoMemory();
+
+    s->pubkey = pubkey;
+    return (PyObject*) s;
+}
+
+PyObject * rpmKeyring_Wrap(PyTypeObject *subtype, rpmKeyring keyring)
+{
+    rpmKeyringObject *s = (rpmKeyringObject *)subtype->tp_alloc(subtype, 0);
+    if (s == NULL) return PyErr_NoMemory();
+
+    s->keyring = keyring;
+    return (PyObject*) s;
+}
+
+int rpmKeyringFromPyObject(PyObject *item, rpmKeyring *keyring)
+{
+    rpmKeyringObject *kro;
+    if (!PyArg_Parse(item, "O!", &rpmKeyring_Type, &kro))
+       return 0;
+    *keyring = kro->keyring;
+    return 1;
+}
diff --git a/python/rpmkeyring-py.h b/python/rpmkeyring-py.h
new file mode 100644 (file)
index 0000000..4ba017c
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef H_RPMKEYRING_PY
+#define H_RPMKEYRING_PY
+
+#include <rpm/rpmtypes.h>
+
+typedef struct rpmPubkeyObject_s rpmPubkeyObject;
+typedef struct rpmKeyringObject_s rpmKeyringObject;
+
+extern PyTypeObject rpmKeyring_Type;
+extern PyTypeObject rpmPubkey_Type;
+
+PyObject * rpmPubkey_Wrap(PyTypeObject *subtype, rpmPubkey pubkey);
+PyObject * rpmKeyring_Wrap(PyTypeObject *subtype, rpmKeyring keyring);
+
+int rpmKeyringFromPyObject(PyObject *item, rpmKeyring *keyring);
+#endif
index 96b395d..0706551 100644 (file)
@@ -9,6 +9,7 @@
 #include "header-py.h"
 #include "rpmds-py.h"
 #include "rpmfi-py.h"
+#include "rpmkeyring-py.h"
 #include "rpmmi-py.h"
 #include "rpmps-py.h"
 #include "rpmmacro-py.h"
@@ -208,9 +209,11 @@ void init_rpm(void)
     if (PyType_Ready(&hdr_Type) < 0) return;
     if (PyType_Ready(&rpmds_Type) < 0) return;
     if (PyType_Ready(&rpmfi_Type) < 0) return;
+    if (PyType_Ready(&rpmKeyring_Type) < 0) return;
     if (PyType_Ready(&rpmmi_Type) < 0) return;
     if (PyType_Ready(&rpmProblem_Type) < 0) return;
     if (PyType_Ready(&rpmps_Type) < 0) return;
+    if (PyType_Ready(&rpmPubkey_Type) < 0) return;
     if (PyType_Ready(&rpmtd_Type) < 0) return;
     if (PyType_Ready(&rpmte_Type) < 0) return;
     if (PyType_Ready(&rpmts_Type) < 0) return;
@@ -243,6 +246,9 @@ void init_rpm(void)
     Py_INCREF(&rpmfi_Type);
     PyModule_AddObject(m, "fi", (PyObject *) &rpmfi_Type);
 
+    Py_INCREF(&rpmKeyring_Type);
+    PyModule_AddObject(m, "keyring", (PyObject *) &rpmKeyring_Type);
+
     Py_INCREF(&rpmmi_Type);
     PyModule_AddObject(m, "mi", (PyObject *) &rpmmi_Type);
 
@@ -252,6 +258,9 @@ void init_rpm(void)
     Py_INCREF(&rpmps_Type);
     PyModule_AddObject(m, "ps", (PyObject *) &rpmps_Type);
 
+    Py_INCREF(&rpmPubkey_Type);
+    PyModule_AddObject(m, "pubkey", (PyObject *) &rpmPubkey_Type);
+
     Py_INCREF(&rpmtd_Type);
     PyModule_AddObject(m, "td", (PyObject *) &rpmtd_Type);