mirror of
https://github.com/systemd/systemd.git
synced 2025-03-09 12:58:26 +03:00
python: add systemd.id128 module
uuid.UUIDs are utilized to hold UUID values.
This commit is contained in:
parent
89f603f5d8
commit
927e96326c
30
Makefile.am
30
Makefile.am
@ -3380,7 +3380,8 @@ EXTRA_DIST += \
|
||||
# ------------------------------------------------------------------------------
|
||||
if HAVE_PYTHON_DEVEL
|
||||
pkgpyexec_LTLIBRARIES = \
|
||||
_journal.la
|
||||
_journal.la \
|
||||
id128.la
|
||||
|
||||
_journal_la_SOURCES = \
|
||||
src/python-systemd/_journal.c
|
||||
@ -3400,9 +3401,36 @@ _journal_la_LIBADD = \
|
||||
$(PYTHON_LIBS) \
|
||||
libsystemd-journal.la
|
||||
|
||||
id128_la_SOURCES = \
|
||||
src/python-systemd/id128.c \
|
||||
src/python-systemd/id128-constants.h
|
||||
|
||||
id128_la_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
-fvisibility=default \
|
||||
$(PYTHON_CFLAGS) \
|
||||
-I$(top_builddir)/src/python-systemd
|
||||
|
||||
id128_la_LDFLAGS = \
|
||||
$(AM_LDFLAGS) \
|
||||
-shared \
|
||||
-module \
|
||||
-avoid-version
|
||||
|
||||
id128_la_LIBADD = \
|
||||
$(PYTHON_LIBS) \
|
||||
libsystemd-id128.la
|
||||
|
||||
dist_pkgpyexec_PYTHON = \
|
||||
src/python-systemd/journal.py \
|
||||
src/python-systemd/__init__.py
|
||||
|
||||
src/python-systemd/id128-constants.h: src/systemd/sd-messages.h Makefile
|
||||
$(AM_V_at)$(MKDIR_P) $(dir $@)
|
||||
$(AM_V_GEN)$(SED) -n -r 's/,//g; s/#define (SD_MESSAGE_[A-Z0-9_]+)\s.*/add_id(m, "\1", \1);/p' <$< >$@
|
||||
|
||||
BUILT_SOURCES += \
|
||||
src/python-systemd/id128-constants.h
|
||||
endif
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
1
src/python-systemd/.gitignore
vendored
Normal file
1
src/python-systemd/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/id128-constants.h
|
@ -3,7 +3,7 @@
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright 2012 David Strauss
|
||||
Copyright 2012 David Strauss <david@davidstrauss.net>
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
|
157
src/python-systemd/id128.c
Normal file
157
src/python-systemd/id128.c
Normal file
@ -0,0 +1,157 @@
|
||||
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright 2013 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <systemd/sd-messages.h>
|
||||
|
||||
#define _cleanup_Py_DECREF_ __attribute__((cleanup(cleanup_Py_DECREFp)))
|
||||
|
||||
static void cleanup_Py_DECREFp(PyObject **p) {
|
||||
if (!*p)
|
||||
return;
|
||||
|
||||
Py_DECREF(*p);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(module__doc__,
|
||||
"Python interface to the libsystemd-id128 library.\n\n"
|
||||
"Provides SD_MESSAGE_* constants and functions to query and generate\n"
|
||||
"128bit unique identifiers."
|
||||
);
|
||||
|
||||
PyDoc_STRVAR(randomize__doc__,
|
||||
"randomize() -> UUID\n\n"
|
||||
"Return a new random 128bit unique identifier.\n"
|
||||
"Wraps sd_id128_randomize(3)."
|
||||
);
|
||||
|
||||
PyDoc_STRVAR(get_machine__doc__,
|
||||
"get_machine() -> UUID\n\n"
|
||||
"Return a 128bit unique identifier for this machine.\n"
|
||||
"Wraps sd_id128_get_machine(3)."
|
||||
);
|
||||
|
||||
PyDoc_STRVAR(get_boot__doc__,
|
||||
"get_boot() -> UUID\n\n"
|
||||
"Return a 128bit unique identifier for this boot.\n"
|
||||
"Wraps sd_id128_get_boot(3)."
|
||||
);
|
||||
|
||||
static PyObject* make_uuid(sd_id128_t id) {
|
||||
PyObject _cleanup_Py_DECREF_
|
||||
*uuid = NULL, *UUID = NULL, *bytes = NULL,
|
||||
*args = NULL, *kwargs = NULL, *obj = NULL;
|
||||
|
||||
uuid = PyImport_ImportModule("uuid");
|
||||
if (!uuid)
|
||||
return NULL;
|
||||
|
||||
UUID = PyObject_GetAttrString(uuid, "UUID");
|
||||
bytes = PyBytes_FromStringAndSize((const char*) &id.bytes, sizeof(id.bytes));
|
||||
args = Py_BuildValue("()");
|
||||
kwargs = PyDict_New();
|
||||
if (!UUID || !bytes || !args || !kwargs)
|
||||
return NULL;
|
||||
|
||||
if (PyDict_SetItemString(kwargs, "bytes", bytes) < 0)
|
||||
return NULL;
|
||||
|
||||
return PyObject_Call(UUID, args, kwargs);
|
||||
}
|
||||
|
||||
#define helper(name) \
|
||||
static PyObject *name(PyObject *self, PyObject *args) { \
|
||||
sd_id128_t id; \
|
||||
int r; \
|
||||
\
|
||||
assert(args == NULL); \
|
||||
\
|
||||
r = sd_id128_##name(&id); \
|
||||
if (r < 0) { \
|
||||
errno = -r; \
|
||||
return PyErr_SetFromErrno(PyExc_IOError); \
|
||||
} \
|
||||
\
|
||||
return make_uuid(id); \
|
||||
}
|
||||
|
||||
helper(randomize)
|
||||
helper(get_machine)
|
||||
helper(get_boot)
|
||||
|
||||
static PyMethodDef methods[] = {
|
||||
{ "randomize", randomize, METH_NOARGS, randomize__doc__},
|
||||
{ "get_machine", get_machine, METH_NOARGS, get_machine__doc__},
|
||||
{ "get_boot", get_boot, METH_NOARGS, get_boot__doc__},
|
||||
{ NULL, NULL, 0, NULL } /* Sentinel */
|
||||
};
|
||||
|
||||
static int add_id(PyObject *module, const char* name, sd_id128_t id) {
|
||||
PyObject _cleanup_Py_DECREF_ *obj;
|
||||
|
||||
obj = make_uuid(id);
|
||||
if (!obj)
|
||||
return -1;
|
||||
|
||||
return PyObject_SetAttrString(module, name, obj);
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
|
||||
PyMODINIT_FUNC initid128(void) {
|
||||
PyObject *m;
|
||||
|
||||
m = Py_InitModule3("id128", methods, module__doc__);
|
||||
if (m == NULL)
|
||||
return;
|
||||
|
||||
#include "id128-constants.h"
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static struct PyModuleDef module = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"id128", /* name of module */
|
||||
module__doc__, /* module documentation, may be NULL */
|
||||
0, /* size of per-interpreter state of the module */
|
||||
methods
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC PyInit_id128(void) {
|
||||
PyObject *m;
|
||||
|
||||
m = PyModule_Create(&module);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
|
||||
#include "id128-constants.h"
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#pragma GCC diagnostic pop
|
@ -1,8 +1,10 @@
|
||||
# -*- Mode: python; indent-tabs-mode: nil -*- */
|
||||
# -*- Mode: python; coding:utf-8; indent-tabs-mode: nil -*- */
|
||||
#
|
||||
# This file is part of systemd.
|
||||
#
|
||||
# Copyright 2012 David Strauss
|
||||
# Copyright 2012 David Strauss <david@davidstrauss.net>
|
||||
# Copyright 2012 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
|
||||
# Copyright 2012 Marti Raudsepp <marti@juffo.org>
|
||||
#
|
||||
# systemd is free software; you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Lesser General Public License as published by
|
||||
@ -120,7 +122,7 @@ class JournalHandler(_logging.Handler):
|
||||
"""Journal handler class for the Python logging framework.
|
||||
|
||||
Please see the Python logging module documentation for an
|
||||
overview: http://docs.python.org/library/logging.html
|
||||
overview: http://docs.python.org/library/logging.html.
|
||||
|
||||
To create a custom logger whose messages go only to journal:
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user