1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <systemd/sd-messages.h>
28 PyDoc_STRVAR(module__doc__,
29 "Python interface to the libsystemd-id128 library.\n\n"
30 "Provides SD_MESSAGE_* constants and functions to query and generate\n"
31 "128-bit unique identifiers."
34 PyDoc_STRVAR(randomize__doc__,
35 "randomize() -> UUID\n\n"
36 "Return a new random 128-bit unique identifier.\n"
37 "Wraps sd_id128_randomize(3)."
40 PyDoc_STRVAR(get_machine__doc__,
41 "get_machine() -> UUID\n\n"
42 "Return a 128-bit unique identifier for this machine.\n"
43 "Wraps sd_id128_get_machine(3)."
46 PyDoc_STRVAR(get_boot__doc__,
47 "get_boot() -> UUID\n\n"
48 "Return a 128-bit unique identifier for this boot.\n"
49 "Wraps sd_id128_get_boot(3)."
52 static PyObject* make_uuid(sd_id128_t id) {
53 _cleanup_Py_DECREF_ PyObject
54 *uuid = NULL, *UUID = NULL, *bytes = NULL,
55 *args = NULL, *kwargs = NULL;
57 uuid = PyImport_ImportModule("uuid");
61 UUID = PyObject_GetAttrString(uuid, "UUID");
62 bytes = PyBytes_FromStringAndSize((const char*) &id.bytes, sizeof(id.bytes));
63 args = Py_BuildValue("()");
64 kwargs = PyDict_New();
65 if (!UUID || !bytes || !args || !kwargs)
68 if (PyDict_SetItemString(kwargs, "bytes", bytes) < 0)
71 return PyObject_Call(UUID, args, kwargs);
74 #define helper(name) \
75 static PyObject *name(PyObject *self, PyObject *args) { \
79 assert(args == NULL); \
81 r = sd_id128_##name(&id); \
84 return PyErr_SetFromErrno(PyExc_IOError); \
87 return make_uuid(id); \
94 static PyMethodDef methods[] = {
95 { "randomize", randomize, METH_NOARGS, randomize__doc__},
96 { "get_machine", get_machine, METH_NOARGS, get_machine__doc__},
97 { "get_boot", get_boot, METH_NOARGS, get_boot__doc__},
98 { NULL, NULL, 0, NULL } /* Sentinel */
101 static int add_id(PyObject *module, const char* name, sd_id128_t id) {
108 return PyModule_AddObject(module, name, obj);
111 #pragma GCC diagnostic push
112 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
114 #if PY_MAJOR_VERSION < 3
116 PyMODINIT_FUNC initid128(void) {
119 m = Py_InitModule3("id128", methods, module__doc__);
123 /* a series of lines like 'add_id() ;' follow */
125 #include "id128-constants.h"
127 PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
132 static struct PyModuleDef module = {
133 PyModuleDef_HEAD_INIT,
134 "id128", /* name of module */
135 module__doc__, /* module documentation, may be NULL */
136 -1, /* size of per-interpreter state of the module */
140 PyMODINIT_FUNC PyInit_id128(void) {
143 m = PyModule_Create(&module);
147 if ( /* a series of lines like 'add_id() ||' follow */
149 #include "id128-constants.h"
151 PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
161 #pragma GCC diagnostic pop