d5f131e42c0df89dee11bb2c770b28d05dc406ea
[tools/librpm-tizen.git] / python / rpmkeyring-py.c
1 #include "rpmsystem-py.h"
2 #include <rpm/rpmkeyring.h>
3 #include "rpmkeyring-py.h"
4
5 struct rpmPubkeyObject_s {
6     PyObject_HEAD
7     PyObject *md_dict;
8     rpmPubkey pubkey;
9 };
10
11 static void rpmPubkey_dealloc(rpmPubkeyObject * s)
12 {
13     s->pubkey = rpmPubkeyFree(s->pubkey);
14     Py_TYPE(s)->tp_free((PyObject *)s);
15 }
16
17 static PyObject *rpmPubkey_new(PyTypeObject *subtype, 
18                            PyObject *args, PyObject *kwds)
19 {
20     PyObject *key;
21     char *kwlist[] = { "key", NULL };
22     rpmPubkey pubkey = NULL;
23     uint8_t *pkt;
24     size_t pktlen;
25
26     if (!PyArg_ParseTupleAndKeywords(args, kwds, "S", kwlist, &key))
27         return NULL;
28
29     if (pgpParsePkts(PyBytes_AsString(key), &pkt, &pktlen) <= 0) {
30         PyErr_SetString(PyExc_ValueError, "invalid pubkey");
31         return NULL;
32     }
33     pubkey = rpmPubkeyNew(pkt, pktlen);
34
35     return rpmPubkey_Wrap(subtype, pubkey);
36 }
37
38 static PyObject * rpmPubkey_Base64(rpmPubkeyObject *s)
39 {
40     char *b64 = rpmPubkeyBase64(s->pubkey);
41     PyObject *res = Py_BuildValue("s", b64);
42     free(b64);
43     return res;
44 }
45
46 static struct PyMethodDef rpmPubkey_methods[] = {
47     { "base64", (PyCFunction) rpmPubkey_Base64, METH_NOARGS, NULL },
48     { NULL }
49 };
50
51 static char rpmPubkey_doc[] = "";
52
53 PyTypeObject rpmPubkey_Type = {
54         PyVarObject_HEAD_INIT(&PyType_Type, 0)
55         "rpm.pubkey",                   /* tp_name */
56         sizeof(rpmPubkeyObject),        /* tp_size */
57         0,                              /* tp_itemsize */
58         (destructor) rpmPubkey_dealloc,/* tp_dealloc */
59         0,                              /* tp_print */
60         (getattrfunc)0,                 /* tp_getattr */
61         0,                              /* tp_setattr */
62         0,                              /* tp_compare */
63         0,                              /* tp_repr */
64         0,                              /* tp_as_number */
65         0,                              /* tp_as_sequence */
66         0,                              /* tp_as_mapping */
67         0,                              /* tp_hash */
68         0,                              /* tp_call */
69         0,                              /* tp_str */
70         PyObject_GenericGetAttr,        /* tp_getattro */
71         PyObject_GenericSetAttr,        /* tp_setattro */
72         0,                              /* tp_as_buffer */
73         Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
74         rpmPubkey_doc,                  /* tp_doc */
75         0,                              /* tp_traverse */
76         0,                              /* tp_clear */
77         0,                              /* tp_richcompare */
78         0,                              /* tp_weaklistoffset */
79         0,                              /* tp_iter */
80         0,                              /* tp_iternext */
81         rpmPubkey_methods,              /* tp_methods */
82         0,                              /* tp_members */
83         0,                              /* tp_getset */
84         0,                              /* tp_base */
85         0,                              /* tp_dict */
86         0,                              /* tp_descr_get */
87         0,                              /* tp_descr_set */
88         0,                              /* tp_dictoffset */
89         0,                              /* tp_init */
90         0,                              /* tp_alloc */
91         rpmPubkey_new,                  /* tp_new */
92         0,                              /* tp_free */
93         0,                              /* tp_is_gc */
94 };
95
96 struct rpmKeyringObject_s {
97     PyObject_HEAD
98     PyObject *md_dict;
99     rpmKeyring keyring;
100 };
101
102 static void rpmKeyring_dealloc(rpmKeyringObject * s)
103 {
104     rpmKeyringFree(s->keyring);
105     Py_TYPE(s)->tp_free((PyObject *)s);
106 }
107
108 static PyObject *rpmKeyring_new(PyTypeObject *subtype, 
109                            PyObject *args, PyObject *kwds)
110 {
111     rpmKeyring keyring = rpmKeyringNew();
112     return rpmKeyring_Wrap(subtype, keyring);
113 }
114
115 static PyObject *rpmKeyring_addKey(rpmKeyringObject *s, PyObject *arg)
116 {
117     rpmPubkeyObject *pubkey = NULL;
118
119     if (!PyArg_Parse(arg, "O!", &rpmPubkey_Type, &pubkey))
120         return NULL;
121
122     return Py_BuildValue("i", rpmKeyringAddKey(s->keyring, pubkey->pubkey));
123 };
124
125 static struct PyMethodDef rpmKeyring_methods[] = {
126     { "addKey", (PyCFunction) rpmKeyring_addKey, METH_O,
127         NULL },
128     {NULL,              NULL}           /* sentinel */
129 };
130
131 static char rpmKeyring_doc[] =
132 "";
133
134 PyTypeObject rpmKeyring_Type = {
135         PyVarObject_HEAD_INIT(&PyType_Type, 0)
136         "rpm.keyring",                  /* tp_name */
137         sizeof(rpmKeyringObject),       /* tp_size */
138         0,                              /* tp_itemsize */
139         (destructor) rpmKeyring_dealloc,/* tp_dealloc */
140         0,                              /* tp_print */
141         0,                              /* tp_getattr */
142         0,                              /* tp_setattr */
143         0,                              /* tp_compare */
144         0,                              /* tp_repr */
145         0,                              /* tp_as_number */
146         0,                              /* tp_as_sequence */
147         0,                              /* tp_as_mapping */
148         0,                              /* tp_hash */
149         0,                              /* tp_call */
150         0,                              /* tp_str */
151         PyObject_GenericGetAttr,        /* tp_getattro */
152         PyObject_GenericSetAttr,        /* tp_setattro */
153         0,                              /* tp_as_buffer */
154         Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
155         rpmKeyring_doc,                 /* tp_doc */
156         0,                              /* tp_traverse */
157         0,                              /* tp_clear */
158         0,                              /* tp_richcompare */
159         0,                              /* tp_weaklistoffset */
160         0,                              /* tp_iter */
161         0,                              /* tp_iternext */
162         rpmKeyring_methods,             /* tp_methods */
163         0,                              /* tp_members */
164         0,                              /* tp_getset */
165         0,                              /* tp_base */
166         0,                              /* tp_dict */
167         0,                              /* tp_descr_get */
168         0,                              /* tp_descr_set */
169         0,                              /* tp_dictoffset */
170         0,                              /* tp_init */
171         0,                              /* tp_alloc */
172         rpmKeyring_new,                 /* tp_new */
173         0,                              /* tp_free */
174         0,                              /* tp_is_gc */
175 };
176
177 PyObject * rpmPubkey_Wrap(PyTypeObject *subtype, rpmPubkey pubkey)
178 {
179     rpmPubkeyObject *s = (rpmPubkeyObject *)subtype->tp_alloc(subtype, 0);
180     if (s == NULL) return NULL;
181
182     s->pubkey = pubkey;
183     return (PyObject*) s;
184 }
185
186 PyObject * rpmKeyring_Wrap(PyTypeObject *subtype, rpmKeyring keyring)
187 {
188     rpmKeyringObject *s = (rpmKeyringObject *)subtype->tp_alloc(subtype, 0);
189     if (s == NULL) return NULL;
190
191     s->keyring = keyring;
192     return (PyObject*) s;
193 }
194
195 int rpmKeyringFromPyObject(PyObject *item, rpmKeyring *keyring)
196 {
197     rpmKeyringObject *kro;
198     if (!PyArg_Parse(item, "O!", &rpmKeyring_Type, &kro))
199         return 0;
200     *keyring = kro->keyring;
201     return 1;
202 }