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/>.
26 #include <systemd/sd-messages.h>
30 PyDoc_STRVAR(module__doc__,
31 "Python interface to the libsystemd-id128 library.\n\n"
32 "Provides SD_MESSAGE_* constants and functions to query and generate\n"
33 "128-bit unique identifiers."
36 PyDoc_STRVAR(randomize__doc__,
37 "randomize() -> UUID\n\n"
38 "Return a new random 128-bit unique identifier.\n"
39 "Wraps sd_id128_randomize(3)."
42 PyDoc_STRVAR(get_machine__doc__,
43 "get_machine() -> UUID\n\n"
44 "Return a 128-bit unique identifier for this machine.\n"
45 "Wraps sd_id128_get_machine(3)."
48 PyDoc_STRVAR(get_boot__doc__,
49 "get_boot() -> UUID\n\n"
50 "Return a 128-bit unique identifier for this boot.\n"
51 "Wraps sd_id128_get_boot(3)."
54 static PyObject* make_uuid(sd_id128_t id) {
55 _cleanup_Py_DECREF_ PyObject
56 *uuid = NULL, *UUID = NULL, *bytes = NULL,
57 *args = NULL, *kwargs = NULL;
59 uuid = PyImport_ImportModule("uuid");
63 UUID = PyObject_GetAttrString(uuid, "UUID");
64 bytes = PyBytes_FromStringAndSize((const char*) &id.bytes, sizeof(id.bytes));
65 args = Py_BuildValue("()");
66 kwargs = PyDict_New();
67 if (!UUID || !bytes || !args || !kwargs)
70 if (PyDict_SetItemString(kwargs, "bytes", bytes) < 0)
73 return PyObject_Call(UUID, args, kwargs);
76 #define helper(name) \
77 static PyObject *name(PyObject *self, PyObject *args) { \
81 assert(args == NULL); \
83 r = sd_id128_##name(&id); \
86 return PyErr_SetFromErrno(PyExc_IOError); \
89 return make_uuid(id); \
96 static PyMethodDef methods[] = {
97 { "randomize", randomize, METH_NOARGS, randomize__doc__},
98 { "get_machine", get_machine, METH_NOARGS, get_machine__doc__},
99 { "get_boot", get_boot, METH_NOARGS, get_boot__doc__},
100 { NULL, NULL, 0, NULL } /* Sentinel */
103 static int add_id(PyObject *module, const char* name, sd_id128_t id) {
110 return PyModule_AddObject(module, name, obj);
113 #pragma GCC diagnostic push
114 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
116 #if PY_MAJOR_VERSION < 3
118 PyMODINIT_FUNC initid128(void) {
121 m = Py_InitModule3("id128", methods, module__doc__);
125 /* a series of lines like 'add_id() ;' follow */
127 #include "id128-constants.h"
133 static struct PyModuleDef module = {
134 PyModuleDef_HEAD_INIT,
135 "id128", /* name of module */
136 module__doc__, /* module documentation, may be NULL */
137 -1, /* size of per-interpreter state of the module */
141 PyMODINIT_FUNC PyInit_id128(void) {
144 m = PyModule_Create(&module);
148 if ( /* a series of lines like 'add_id() ||' follow */
150 #include "id128-constants.h"
162 #pragma GCC diagnostic pop