]> git.proxmox.com Git - systemd.git/blob - src/python-systemd/id128.c
Imported Upstream version 204
[systemd.git] / src / python-systemd / id128.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <Python.h>
23
24 #include <systemd/sd-messages.h>
25
26 #include "pyutil.h"
27
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."
32 );
33
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)."
38 );
39
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)."
44 );
45
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)."
50 );
51
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;
56
57 uuid = PyImport_ImportModule("uuid");
58 if (!uuid)
59 return NULL;
60
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)
66 return NULL;
67
68 if (PyDict_SetItemString(kwargs, "bytes", bytes) < 0)
69 return NULL;
70
71 return PyObject_Call(UUID, args, kwargs);
72 }
73
74 #define helper(name) \
75 static PyObject *name(PyObject *self, PyObject *args) { \
76 sd_id128_t id; \
77 int r; \
78 \
79 assert(args == NULL); \
80 \
81 r = sd_id128_##name(&id); \
82 if (r < 0) { \
83 errno = -r; \
84 return PyErr_SetFromErrno(PyExc_IOError); \
85 } \
86 \
87 return make_uuid(id); \
88 }
89
90 helper(randomize)
91 helper(get_machine)
92 helper(get_boot)
93
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 */
99 };
100
101 static int add_id(PyObject *module, const char* name, sd_id128_t id) {
102 PyObject *obj;
103
104 obj = make_uuid(id);
105 if (!obj)
106 return -1;
107
108 return PyModule_AddObject(module, name, obj);
109 }
110
111 #pragma GCC diagnostic push
112 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
113
114 #if PY_MAJOR_VERSION < 3
115
116 PyMODINIT_FUNC initid128(void) {
117 PyObject *m;
118
119 m = Py_InitModule3("id128", methods, module__doc__);
120 if (m == NULL)
121 return;
122
123 /* a series of lines like 'add_id() ;' follow */
124 #define JOINER ;
125 #include "id128-constants.h"
126 #undef JOINER
127 PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
128 }
129
130 #else
131
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 */
137 methods
138 };
139
140 PyMODINIT_FUNC PyInit_id128(void) {
141 PyObject *m;
142
143 m = PyModule_Create(&module);
144 if (m == NULL)
145 return NULL;
146
147 if ( /* a series of lines like 'add_id() ||' follow */
148 #define JOINER ||
149 #include "id128-constants.h"
150 #undef JOINER
151 PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
152 Py_DECREF(m);
153 return NULL;
154 }
155
156 return m;
157 }
158
159 #endif
160
161 #pragma GCC diagnostic pop