Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python #2135

Merged
merged 23 commits into from Aug 7, 2018
Merged

Python #2135

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmake/ElektraCache.cmake
Expand Up @@ -387,6 +387,14 @@ set (DISCLAMER "

mark_as_advanced (FORCE # The following settings might be relevant to a few users:
GTEST_ROOT
ANTLR4CPP_DIR
BUILD_GMOCK
BUILD_GTEST
INSTALL_GMOCK
INSTALL_GTEST
LIBFA_INCLUDE_DIR
gmock_build_tests
gtest_hide_internal_symbols
COVERAGE_PREFIX
CMAKE_PIC_FLAGS
CMAKE_STATIC_FLAGS
Expand Down
2 changes: 1 addition & 1 deletion doc/news/_preparation_next_release.md
Expand Up @@ -175,7 +175,7 @@ Thanks to Daniel Bugl.
### Interpreter Plugins

- The plugins ruby, python and jni can now also be mounted as global plugin.
- Fix crash in python plugin. *(Markus Raab)*
- Fix crash in python plugin by using pluginprocess. *(Markus Raab)*

### JNI

Expand Down
3 changes: 3 additions & 0 deletions scripts/jenkins/Jenkinsfile
Expand Up @@ -854,6 +854,9 @@ export LD_LIBRARY_PATH=${WORKSPACE}/system/lib:$LD_LIBRARY_PATH
export PATH=${WORKSPACE}/system/bin:$PATH
export DBUS_SESSION_BUS_ADDRESS=`dbus-daemon --session --fork --print-address`
export LUA_CPATH="${WORKSPACE}/system/lib/lua/5.2/?.so;"

env

kdb run_all
kill `pidof dbus-daemon`
'''
Expand Down
7 changes: 0 additions & 7 deletions src/include/kdbconfig.h.in
Expand Up @@ -10,13 +10,6 @@

@DISCLAMER@

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the BSD License (revised). *
* *
***************************************************************************/

#ifndef KDBCONFIG_H
#define KDBCONFIG_H

Expand Down
47 changes: 30 additions & 17 deletions src/plugins/python/CMakeLists.txt
@@ -1,15 +1,17 @@
include (LibAddBinding)

if (DEPENDENCY_PHASE)
find_package (PythonInterp 3)
find_package (PythonLibs 3 QUIET)
find_package (Pluginprocess)
find_swig ()
check_binding_included ("swig_python" BINDING_WAS_ADDED SUBDIRECTORY "swig/python" SILENT)

if (PYTHONLIBS_FOUND AND SWIG_FOUND AND BINDING_WAS_ADDED)
if (PYTHONLIBS_FOUND AND SWIG_FOUND AND BINDING_WAS_ADDED AND PLUGINPROCESS_FOUND)
include (LibAddMacros)

add_custom_command (OUTPUT runtime.h
COMMAND ${SWIG_EXECUTABLE} -c++ -python -py3 -external-runtime runtime.h)
add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.h
COMMAND ${SWIG_EXECUTABLE} -c++ -python -py3 -external-runtime ${CMAKE_CURRENT_BINARY_DIR}/runtime.h)

# we call this SWIG_COMPILE_FLAGS because we have the same variable in our swig bindings
set (SWIG_COMPILE_FLAGS "-Wno-shadow -Wno-old-style-cast -Wno-unused-variable -Wno-missing-field-initializers")
Expand All @@ -19,10 +21,21 @@ if (DEPENDENCY_PHASE)
endif ()

set_source_files_properties ("python.cpp" PROPERTIES COMPILE_FLAGS "${SWIG_COMPILE_FLAGS}")

set (PYTHON_PLUGIN_FOLDER "${CMAKE_INSTALL_PREFIX}/${TARGET_TEST_DATA_FOLDER}/python")
set (PYTHON_GET_MODULES_DIR_COMMAND
"from distutils.sysconfig import get_python_lib; print(get_python_lib(True, prefix='${CMAKE_INSTALL_PREFIX}'))")
execute_process (COMMAND ${PYTHON_EXECUTABLE} -c "${PYTHON_GET_MODULES_DIR_COMMAND}"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
OUTPUT_STRIP_TRAILING_WHITESPACE)

configure_file (config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
elseif (NOT PYTHONLIBS_FOUND)
remove_plugin (python "python 3 libs (libpython3-dev) not found")
elseif (NOT BINDING_WAS_ADDED)
remove_plugin (python "swig_python binding is required")
elseif (NOT PLUGINPROCESS_FOUND)
remove_plugin (python "Elektra's pluginprocess library is required")
else ()
remove_plugin (python "swig not found")
endif ()
Expand All @@ -33,30 +46,30 @@ add_plugin (python
SOURCES python.hpp
python.cpp
${CMAKE_CURRENT_BINARY_DIR}/runtime.h
${CMAKE_CURRENT_BINARY_DIR}/config.h
INCLUDE_DIRECTORIES ${PYTHON_INCLUDE_DIR}
LINK_LIBRARIES ${PYTHON_LIBRARIES}
LINK_ELEKTRA elektra-pluginprocess
COMPILE_DEFINITIONS SWIG_TYPE_TABLE=kdb
SWIG_RUNTIME=\"runtime.h\"
PYTHON_PLUGIN_NAME=python
PYTHON_PLUGIN_SYMBOL_NAME=Python)

if (ADDTESTING_PHASE)
if (ENABLE_BROKEN_TESTS)

# bindings are required for tests
check_binding_was_added ("swig_python" BINDING_WAS_ADDED)
if (BUILD_TESTING AND BINDING_WAS_ADDED)
# bindings are required for tests
check_binding_was_added ("swig_python" BINDING_WAS_ADDED)
if (BUILD_TESTING AND BINDING_WAS_ADDED)

# test environment clears env so setting PYTHONPATH is no option we workaround this by changing the current
# directory to our bindings output directory + our test adds the current directory to pythons sys.path
add_plugintest (python MEMLEAK WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../../bindings/swig/python/)
# test environment clears env so setting PYTHONPATH is no option we workaround this by changing the current directory to our
# bindings output directory + our test adds the current directory to pythons sys.path
add_plugintest (python MEMLEAK WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../../bindings/swig/python/)

if (INSTALL_TESTING)
install (DIRECTORY python DESTINATION ${TARGET_TEST_DATA_FOLDER})
endif ()
if (INSTALL_TESTING)

else ()
message (WARNING "BUILD_STATIC or BUILD_FULL and swig_python bindings are required for testing, test deactivated")
install (DIRECTORY ${CMAKE_SOURCE_DIR}/src/plugins/python/python/ DESTINATION "${TARGET_TEST_DATA_FOLDER}/python")
endif ()
endif (ENABLE_BROKEN_TESTS)

else ()
message (WARNING "The swig_python binding is required for testing, test deactivated")
endif ()
endif ()
11 changes: 6 additions & 5 deletions src/plugins/python/README.md
Expand Up @@ -9,7 +9,7 @@

## Introduction

The plugin uses Python to do magic things. It basically allows to call plugins written in Python.
The plugin uses Python to do magic things. It allows plugins to be written in Python.

What a Python script can do is not really limited by design, so any kind of plugin may be
implemented. The python plugin is especially useful to write filter and logging scripts.
Expand All @@ -24,19 +24,20 @@ python script. The mount command would look like
if the **ini** plugin should be used for storage and the python plugin only serves to invoke the
filter script.

For a Python script that serves as (JSON) storage plugin itself, one could also use
For a Python script that serves as INI storage plugin itself, one uses

kdb mount file.json /python python script=/path/to/json_plugin.py
kdb mount file.json /python python script=python_configparser.py

### Plugin Configuration

The python plugin supports following optional configuration values/flags:

- `script` (string): The script to be executed.
- `print` (flag): Make the plugin print engine errors, triggered by the calls of
this plugin, to stderr. Mainly intended for diagnostic. Please note that the
Python engine itself will print script errors to stderr regardless of this flag.
- `shutdown` (value, 0 or 1): If enabled, the last call to `kdbClose()` will also
shutdown Pythons engine. Default is 0.
- `python/path` (string): Extends sys.path by this entry. Better then PYTHONPATH
it is always available, regardless of the context where a binary is executed.

### Python Scripts

Expand Down
20 changes: 20 additions & 0 deletions src/plugins/python/config.h.in
@@ -0,0 +1,20 @@
/**
* @file
*
* @brief Build system configuration for python plugins
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

// clang-format off

@DISCLAMER@

#ifndef ELEKTRA_PYTHON_CONFIG_H
#define ELEKTRA_PYTHON_CONFIG_H

#define ELEKTRA_PYTHON_SITE_PACKAGES "@PYTHON_SITE_PACKAGES@"

#define ELEKTRA_PYTHON_PLUGIN_FOLDER "@PYTHON_PLUGIN_FOLDER@"

#endif