Skip to content

Commit

Permalink
python: add reloc support
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
  • Loading branch information
alltilla committed Sep 22, 2023
1 parent 24be6af commit 64f7b8e
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 2 deletions.
1 change: 1 addition & 0 deletions modules/python-modules/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ EXTRA_DIST += \
modules/python-modules/syslogng/message.py \
modules/python-modules/syslogng/parser.py \
modules/python-modules/syslogng/persist.py \
modules/python-modules/syslogng/reloc.py \
modules/python-modules/syslogng/source.py \
modules/python-modules/syslogng/template.py \
modules/python-modules/syslogng/debuggercli/__init__.py \
Expand Down
1 change: 1 addition & 0 deletions modules/python-modules/syslogng/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
from .logger import Logger
from .persist import Persist
from .confgen import register_config_generator
from .reloc import get_installation_path_for
38 changes: 38 additions & 0 deletions modules/python-modules/syslogng/reloc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#############################################################################
# Copyright (c) 2023 Attila Szakacs
#
# This library 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.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# As an additional exemption you are allowed to compile & link against the
# OpenSSL libraries as published by the OpenSSL project. See the file
# COPYING for details.
#
#############################################################################
# pylint: disable=function-redefined,unused-import


try:
from _syslogng import get_installation_path_for

except ImportError:
import warnings

warnings.warn("You have imported the syslogng package outside of syslog-ng, "
"thus some of the functionality is not available. "
"Defining fake classes for those exported by the underlying syslog-ng code")

def get_installation_path_for(template):
"""Resolve an installation path template"""
return template
2 changes: 2 additions & 0 deletions modules/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ set(PYTHON_SOURCES
compat/compat-python.c
python-flags.h
python-flags.c
python-reloc.h
python-reloc.c
)

add_module(
Expand Down
4 changes: 3 additions & 1 deletion modules/python/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ modules_python_libmod_python_la_SOURCES = \
modules/python/python-options.h \
modules/python/python-options.c \
modules/python/python-flags.h \
modules/python/python-flags.c
modules/python/python-flags.c \
modules/python/python-reloc.h \
modules/python/python-reloc.c


modules_python_libmod_python_la_LDFLAGS = \
Expand Down
52 changes: 52 additions & 0 deletions modules/python/python-reloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023 Attila Szakacs
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*/

#include "python-reloc.h"
#include "python-types.h"
#include "reloc.h"

static PyObject *
py_get_installation_path_for(PyObject *self, PyObject *args, PyObject *kwargs)
{
const gchar *template_str;
static const gchar *kwlist[] = { "template", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", (gchar **) kwlist, &template_str))
return NULL;

const gchar *result_str = get_installation_path_for(template_str);
if (!result_str)
Py_RETURN_NONE;

return py_string_from_string(result_str, -1);
}

static PyMethodDef py_reloc_methods[] =
{
{ "get_installation_path_for", (PyCFunction) py_get_installation_path_for, METH_VARARGS | METH_KEYWORDS, "Resolve an installation path template" },
{ NULL }
};

void
py_reloc_global_init(void)
{
PyObject *module = PyImport_AddModule("_syslogng");
PyModule_AddFunctions(module, py_reloc_methods);
}
29 changes: 29 additions & 0 deletions modules/python/python-reloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 Attila Szakacs
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*/

#ifndef _SNG_PYTHON_RELOC_H
#define _SNG_PYTHON_RELOC_H

#include "python-module.h"

void py_reloc_global_init(void);

#endif
2 changes: 2 additions & 0 deletions modules/python/python-startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "python-confgen.h"
#include "python-global-code-loader.h"
#include "python-types.h"
#include "python-reloc.h"

#include "reloc.h"

Expand Down Expand Up @@ -412,6 +413,7 @@ _py_initialize_builtin_modules(void)
py_persist_global_init();
py_bookmark_global_init();
py_ack_tracker_global_init();
py_reloc_global_init();
py_global_code_loader_global_init();
py_logger_global_init();
}
Expand Down
7 changes: 7 additions & 0 deletions modules/python/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ add_unit_test(LIBTEST CRITERION
DEPENDS mod-python "${PYTHON_LIBRARIES}")

set_property(TEST test_python_options APPEND PROPERTY ENVIRONMENT "PYTHONMALLOC=malloc_debug")

add_unit_test(LIBTEST CRITERION
TARGET test_python_reloc
INCLUDES "${PYTHON_INCLUDE_DIR}" "${PYTHON_INCLUDE_DIRS}"
DEPENDS mod-python "${PYTHON_LIBRARIES}")

set_property(TEST test_python_reloc APPEND PROPERTY ENVIRONMENT "PYTHONMALLOC=malloc_debug")
9 changes: 8 additions & 1 deletion modules/python/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ modules_python_tests_TESTS = \
modules/python/tests/test_python_persist \
modules/python/tests/test_python_bookmark \
modules/python/tests/test_python_ack_tracker \
modules/python/tests/test_python_options
modules/python/tests/test_python_options \
modules/python/tests/test_python_reloc

modules_python_tests_test_python_logmsg_CFLAGS = $(TEST_CFLAGS) $(PYTHON_CFLAGS) -I$(top_srcdir)/modules/python
modules_python_tests_test_python_logmsg_LDADD = $(TEST_LDADD) \
Expand Down Expand Up @@ -61,4 +62,10 @@ modules_python_tests_test_python_options_LDADD = $(TEST_LDADD) \
-dlpreopen $(top_builddir)/modules/python/libmod-python.la \
$(PYTHON_LIBS)

modules_python_tests_test_python_reloc_CFLAGS = $(TEST_CFLAGS) $(PYTHON_CFLAGS) \
-I$(top_srcdir)/modules/python
modules_python_tests_test_python_reloc_LDADD = $(TEST_LDADD) \
-dlpreopen $(top_builddir)/modules/python/libmod-python.la \
$(PYTHON_LIBS)

EXTRA_DIST += modules/python/tests/CMakeLists.txt
90 changes: 90 additions & 0 deletions modules/python/tests/test_python_reloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2023 Attila Szakacs
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*/

#include "python-module.h"
#include "python-helpers.h"
#include "python-main.h"
#include "python-startup.h"
#include "python-global.h"
#include "python-types.h"

#include "apphook.h"
#include "cfg.h"
#include "reloc.h"

#include <criterion/criterion.h>

static PyObject *_python_main;
static PyObject *_python_main_dict;

static void
_init_python_main(void)
{
PyGILState_STATE gstate = PyGILState_Ensure();
{
PythonConfig *pc = python_config_get(configuration);
_python_main = _py_get_main_module(pc);
_python_main_dict = PyModule_GetDict(_python_main);
}
PyGILState_Release(gstate);
}

void
setup(void)
{
app_startup();

configuration = cfg_new_snippet();
_py_init_interpreter(FALSE);
_init_python_main();
}

void
teardown(void)
{
cfg_free(configuration);
app_shutdown();
}

TestSuite(python_reloc, .init = setup, .fini = teardown);

Test(python_reloc, test_get_installation_path_for)
{
const gchar *c_resolution_1 = get_installation_path_for("/foo/bar");
const gchar *c_resolution_2 = get_installation_path_for("${prefix}/foo/bar");

const gchar *script = "from _syslogng import get_installation_path_for\n"
"assert get_installation_path_for(r'/foo/bar') == c_resolution_1\n"
"assert get_installation_path_for(r'${prefix}/foo/bar') == c_resolution_2\n";

PyGILState_STATE gstate = PyGILState_Ensure();
{
PyDict_SetItemString(_python_main_dict, "c_resolution_1", py_string_from_string(c_resolution_1, -1));
PyDict_SetItemString(_python_main_dict, "c_resolution_2", py_string_from_string(c_resolution_2, -1));

if (!PyRun_String(script, Py_file_input, _python_main_dict, _python_main_dict))
{
PyErr_Print();
cr_assert(FALSE, "Error running Python script >>>%s<<<", script);
}
}
PyGILState_Release(gstate);
};
3 changes: 3 additions & 0 deletions tests/copyright/policy
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ modules/python-modules/syslogng/dest\.py
modules/python-modules/syslogng/logger\.py
modules/python-modules/syslogng/message\.py
modules/python-modules/syslogng/parser\.py
modules/python-modules/syslogng/reloc\.py
modules/python-modules/syslogng/template\.py
modules/python/python-confgen\.[ch]
lib/tests/test_logscheduler\.c
Expand Down Expand Up @@ -186,9 +187,11 @@ modules/grpc/otel/tests/test-syslog-ng-otlp\.cpp$
modules/examples/inner-destinations/tls-test-validation
modules/examples/sources/random-choice-generator
modules/python/python-options.(c|h)$
modules/python/python-reloc\.(c|h)$
modules/python/python-binding.(c|h)$
modules/python/python-flags.(c|h)$
modules/python/tests/test_python_options.c$
modules/python/tests/test_python_reloc\.c$
scl/fortigate/.*\.conf$
scl/cee/.*\.conf$
scl/logscale/logscale\.conf$
Expand Down

0 comments on commit 64f7b8e

Please sign in to comment.