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

MINIFICPP-2484 Find and load libpython automatically #1889

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions extensions/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ endif()

include(${CMAKE_SOURCE_DIR}/extensions/ExtensionHeader.txt)

add_minifi_library(minifi-python-lib-loader-extension SHARED pythonlibloader/PythonLibLoader.cpp)
target_link_libraries(minifi-python-lib-loader-extension PRIVATE ${LIBMINIFI})

file(GLOB SOURCES "*.cpp" "types/*.cpp" "pythonloader/PyProcLoader.cpp")

add_minifi_library(minifi-python-script-extension SHARED ${SOURCES})
Expand All @@ -38,9 +41,9 @@ endif()
target_compile_definitions(minifi-python-script-extension PUBLIC PY_SSIZE_T_CLEAN)

target_sources(minifi-python-script-extension PRIVATE ${PY_SOURCES})
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(minifi-python-script-extension PUBLIC -Wl,--no-as-needed ${Python_LIBRARIES} -Wl,--as-needed)
else()

# On Linux the python library is loaded dynamically in the minifi-python-loader extension before the python script extension is loaded
if (WIN32 OR APPLE)
szaszm marked this conversation as resolved.
Show resolved Hide resolved
target_link_libraries(minifi-python-script-extension PUBLIC ${Python_LIBRARIES})
endif()

Expand Down Expand Up @@ -81,5 +84,6 @@ else()
)
endif()

register_extension(minifi-python-lib-loader-extension "PYTHON LIB LOADER" PYTHON-LIB-LOADER-EXTENSIONS "This enables library that loads python library for python symbols")
register_extension(minifi-python-script-extension "PYTHON SCRIPTING ENGINE" PYTHON-SCRIPTING-EXTENSIONS "This enables python script engine" "extensions/python/tests")
register_extension_linter(minifi-python-script-extension-linter)
2 changes: 2 additions & 0 deletions extensions/python/PYTHON.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,5 @@ By default the `python3` command is used on Unix systems and `python` command is

# in minifi.properties
nifi.python.env.setup.binary=python3

On Linux the python binary set in the property is also used to find the associated libpython library that will be dynamically loaded and used by the MiNiFi C++ python bindings.
96 changes: 96 additions & 0 deletions extensions/python/pythonlibloader/PythonLibLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if !defined(WIN32) && !defined(__APPLE__)
#include <dlfcn.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <array>
#include "utils/StringUtils.h"
#include "core/logging/LoggerConfiguration.h"
#endif
#include "core/extension/Extension.h"

namespace minifi = org::apache::nifi::minifi;

#if !defined(WIN32) && !defined(__APPLE__)
martinzink marked this conversation as resolved.
Show resolved Hide resolved
class PythonLibLoader {
public:
explicit PythonLibLoader(const std::shared_ptr<minifi::Configure>& config) {
std::string python_command = "python3";
if (auto python_binary = config->get(minifi::Configure::nifi_python_env_setup_binary)) {
python_command = python_binary.value();
}
szaszm marked this conversation as resolved.
Show resolved Hide resolved
std::string command = python_command +
" -c \"import sysconfig, os, glob; print(min(glob.glob(os.path.join(sysconfig.get_config_var('LIBDIR'), f\\\"libpython{sysconfig.get_config_var('VERSION')}.so*\\\")), key=len, default=''))\"";
auto lib_python_path = execCommand(command);
if (lib_python_path.empty()) {
lordgamez marked this conversation as resolved.
Show resolved Hide resolved
logger_->log_error("Failed to find libpython path from specified python binary: {}", python_command);
throw std::runtime_error("Failed to find libpython path");
}

lib_python_handle_ = dlopen(lib_python_path.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (!lib_python_handle_) {
logger_->log_error("Failed to load libpython from path '{}' with error: {}", lib_python_path, dlerror());
throw std::runtime_error("Failed to load libpython");
}
logger_->log_info("Loaded libpython from path '{}'", lib_python_path);
}

PythonLibLoader(PythonLibLoader&&) = delete;
PythonLibLoader(const PythonLibLoader&) = delete;
PythonLibLoader& operator=(PythonLibLoader&&) = delete;
PythonLibLoader& operator=(const PythonLibLoader&) = delete;

~PythonLibLoader() {
if (lib_python_handle_) {
dlclose(lib_python_handle_);
}
}

private:
static std::string execCommand(const std::string& cmd) {
std::array<char, 128> buffer{};
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
return "";
}
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) {
result += buffer.data();
}
return minifi::utils::string::trim(result);
}

void* lib_python_handle_ = nullptr;
std::shared_ptr<minifi::core::logging::Logger> logger_ = minifi::core::logging::LoggerFactory<PythonLibLoader>::getLogger();
};
#endif

static bool init(const std::shared_ptr<minifi::Configure>& config) {
#if !defined(WIN32) && !defined(__APPLE__)
static PythonLibLoader python_lib_loader(config);
#else
(void)config;
#endif
return true;
lordgamez marked this conversation as resolved.
Show resolved Hide resolved
}

static void deinit() {}

REGISTER_EXTENSION("PythonLibLoaderExtension", init, deinit);