return object;
}
+static PyObject* Pakfire_import_key(PakfireObject* self, PyObject* args) {
+ struct pakfire_key* key = NULL;
+ PyObject* object = NULL;
+ PyObject* file = NULL;
+ int r;
+
+ // Parse arguments
+ if (!PyArg_ParseTuple(args, "O", &file))
+ return NULL;
+
+ // Treat the object as a file
+ FILE* f = PyObject_AsFileHandle(file, "r");
+ if (!f)
+ return NULL;
+
+ // Import the key
+ r = pakfire_key_import(&key, self->pakfire, f);
+ if (r) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ goto ERROR;
+ }
+
+ // Convert the key into a Key object
+ object = new_key(&KeyType, key);
+ if (!object)
+ goto ERROR;
+
+ERROR:
+ if (key)
+ pakfire_key_unref(key);
+ if (f)
+ fclose(f);
+
+ return object;
+}
+
static PyObject* Pakfire_whatprovides(PakfireObject* self, PyObject* args) {
const char* provides = NULL;
struct pakfire_packagelist* list = NULL;
METH_VARARGS,
NULL
},
+ {
+ "import_key",
+ (PyCFunction)Pakfire_import_key,
+ METH_VARARGS,
+ NULL,
+ },
{
"install",
(PyCFunction)Pakfire_install,
#include <datetime.h>
#include <errno.h>
+#include <stdio.h>
#include <pakfire/filelist.h>
#include <pakfire/package.h>
0
);
}
+
+FILE* PyObject_AsFileHandle(PyObject* object, const char* mode) {
+ // Get a file descriptor
+ int fd = PyObject_AsFileDescriptor(object);
+ if (fd < 0)
+ return NULL;
+
+ // Duplicate the file handle
+ fd = dup(fd);
+
+ // Convert to file handle
+ FILE* f = fdopen(fd, mode);
+ if (!f) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+
+ return f;
+}
#define PYTHON_PAKFIRE_UTIL_H
#include <Python.h>
+#include <stdio.h>
#include <pakfire/filelist.h>
#include <pakfire/packagelist.h>
PyObject* PyDateTime_FromTime_t(const time_t* t);
+FILE* PyObject_AsFileHandle(PyObject* object, const char* mode);
+
#endif /* PYTHON_PAKFIRE_UTIL_H */
# Check that the algorithm matches
self.assertEqual(key.algorithm, "Ed255919")
+ def _import(self, path):
+ path = os.path.join(
+ os.environ.get("TEST_DATA_DIR"), path,
+ )
+
+ with open(path, "rb") as f:
+ return self.pakfire.import_key(f)
+
+ def test_import(self):
+ # Import a public key
+ key = self._import("keys/key1.pub")
+
+ # Import a secret key
+ key = self._import("keys/key1.sec")
+
if __name__ == "__main__":
unittest.main()