fgerlits commented on code in PR #1504:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1504#discussion_r1126278246


##########
extensions/python/PythonScriptExecutor.cpp:
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.
+ */
+#include "PythonScriptExecutor.h"
+
+#include <string>
+#include <filesystem>
+#include <vector>
+#include <utility>
+
+#include "PythonScriptEngine.h"
+#include "range/v3/range/conversion.hpp"
+#include "Resource.h"
+
+namespace org::apache::nifi::minifi::extensions::python {
+
+PythonScriptExecutor::PythonScriptExecutor(std::string name, const 
utils::Identifier& uuid) : script::ScriptExecutor(std::move(name), uuid) {}
+
+
+void PythonScriptExecutor::onTrigger(const 
std::shared_ptr<core::ProcessContext>& context, const 
std::shared_ptr<core::ProcessSession>& session) {
+  gsl_Expects(python_script_engine_);
+  gsl_Expects(std::holds_alternative<std::filesystem::path>(script_to_run_) || 
std::holds_alternative<std::string>(script_to_run_));
+
+  if (module_directory_) {
+    
python_script_engine_->setModulePaths(utils::StringUtils::splitAndTrimRemovingEmpty(*module_directory_,
 ",") | ranges::to<std::vector<std::filesystem::path>>());
+  }
+
+  if (std::holds_alternative<std::filesystem::path>(script_to_run_))
+    
python_script_engine_->evalFile(std::get<std::filesystem::path>(script_to_run_));
+  else
+    python_script_engine_->eval(std::get<std::string>(script_to_run_));
+
+  python_script_engine_->onTrigger(context, session);
+}
+
+void PythonScriptExecutor::initialize(std::filesystem::path script_file,
+    std::string script_body,
+    std::optional<std::string> module_directory,
+    size_t /*max_concurrent_engines*/,
+    const core::Relationship& success,
+    const core::Relationship& failure,
+    std::shared_ptr<core::logging::Logger> logger) {
+  if (script_file.empty() == script_body.empty())
+    throw std::runtime_error("Exactly one of these must be non-zero: 
ScriptBody, ScriptFile");

Review Comment:
   ```suggestion
       throw std::runtime_error("Exactly one of these must be non-empty: 
ScriptBody, ScriptFile");
   ```
   here, too



##########
extensions/python/PythonScriptExecutor.cpp:
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.
+ */
+#include "PythonScriptExecutor.h"
+
+#include <string>
+#include <filesystem>
+#include <vector>
+#include <utility>
+
+#include "PythonScriptEngine.h"
+#include "range/v3/range/conversion.hpp"
+#include "Resource.h"
+
+namespace org::apache::nifi::minifi::extensions::python {
+
+PythonScriptExecutor::PythonScriptExecutor(std::string name, const 
utils::Identifier& uuid) : script::ScriptExecutor(std::move(name), uuid) {}
+
+
+void PythonScriptExecutor::onTrigger(const 
std::shared_ptr<core::ProcessContext>& context, const 
std::shared_ptr<core::ProcessSession>& session) {
+  gsl_Expects(python_script_engine_);
+  gsl_Expects(std::holds_alternative<std::filesystem::path>(script_to_run_) || 
std::holds_alternative<std::string>(script_to_run_));
+
+  if (module_directory_) {
+    
python_script_engine_->setModulePaths(utils::StringUtils::splitAndTrimRemovingEmpty(*module_directory_,
 ",") | ranges::to<std::vector<std::filesystem::path>>());
+  }
+
+  if (std::holds_alternative<std::filesystem::path>(script_to_run_))
+    
python_script_engine_->evalFile(std::get<std::filesystem::path>(script_to_run_));
+  else
+    python_script_engine_->eval(std::get<std::string>(script_to_run_));
+
+  python_script_engine_->onTrigger(context, session);
+}
+
+void PythonScriptExecutor::initialize(std::filesystem::path script_file,
+    std::string script_body,
+    std::optional<std::string> module_directory,
+    size_t /*max_concurrent_engines*/,
+    const core::Relationship& success,
+    const core::Relationship& failure,
+    std::shared_ptr<core::logging::Logger> logger) {

Review Comment:
   the `logger` parameter could be `const ... &` to avoid an increase/decrease 
of the refcount



##########
extensions/python/types/PyProcessor.cpp:
##########
@@ -0,0 +1,127 @@
+/**
+ * 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.
+ */
+
+#include "PyProcessor.h"
+#include <string>
+#include "PyException.h"
+#include "Types.h"
+
+extern "C" {
+namespace org::apache::nifi::minifi::extensions::python {
+
+static PyMethodDef PyProcessor_methods[] = {
+    {"setSupportsDynamicProperties", (PyCFunction) 
PyProcessor::setSupportsDynamicProperties, METH_VARARGS, nullptr},
+    {"setDescription", (PyCFunction) PyProcessor::setDescription, 
METH_VARARGS, nullptr},
+    {"addProperty", (PyCFunction) PyProcessor::addProperty, METH_VARARGS, 
nullptr},
+    {}  /* Sentinel */
+};
+
+static PyType_Slot PyProcessorTypeSpecSlots[] = {
+    {Py_tp_dealloc, 
reinterpret_cast<void*>(pythonAllocatedInstanceDealloc<PyProcessor>)},
+    {Py_tp_init, reinterpret_cast<void*>(PyProcessor::init)},
+    {Py_tp_methods, reinterpret_cast<void*>(PyProcessor_methods)},
+    {Py_tp_new, 
reinterpret_cast<void*>(newPythonAllocatedInstance<PyProcessor>)},
+    {}  /* Sentinel */
+};
+
+static PyType_Spec PyProcessorTypeSpec{
+    .name = "minifi_native.Processor",
+    .basicsize = sizeof(PyProcessor),
+    .itemsize = 0,
+    .flags = Py_TPFLAGS_DEFAULT,
+    .slots = PyProcessorTypeSpecSlots
+};
+
+int PyProcessor::init(PyProcessor* self, PyObject* args, PyObject*) {
+  PyObject* weak_ptr_capsule = nullptr;
+  if (!PyArg_ParseTuple(args, "O", &weak_ptr_capsule)) {
+    return -1;
+  }
+
+  auto processor = PyCapsule_GetPointer(weak_ptr_capsule, HeldTypeName);
+  if (!processor)
+    throw PyException();
+  self->processor_ = *static_cast<HeldType*>(processor);
+  return 0;
+}
+
+PyObject* PyProcessor::setSupportsDynamicProperties(PyProcessor* self, 
PyObject*) {
+  auto processor = self->processor_.lock();
+  if (!processor) {
+    PyErr_SetString(PyExc_AttributeError, "tried reading processor outside 
'on_trigger'");
+    Py_RETURN_NONE;
+  }
+
+  processor->setSupportsDynamicProperties();
+  Py_RETURN_NONE;
+}
+
+PyObject* PyProcessor::setDescription(PyProcessor* self, PyObject* args) {
+  auto processor = self->processor_.lock();
+  if (!processor) {
+    PyErr_SetString(PyExc_AttributeError, "tried reading processor outside 
'on_trigger'");
+    Py_RETURN_NONE;
+  }
+
+  const char* description;
+  if (!PyArg_ParseTuple(args, "s", &description)) {
+    throw PyException();
+  }
+  processor->setDescription(std::string(description));
+  Py_RETURN_NONE;
+}
+
+namespace {
+bool getBoolFromTuple(PyObject* tuple, Py_ssize_t location) {
+  auto object = PyTuple_GetItem(tuple, location);
+
+  if (!object)
+    throw PyException();
+
+  if (object == Py_True)
+    return true;
+  if (object == Py_False)
+    return false;
+  throw std::invalid_argument(fmt::format("{} is expected to be Py_True({}) or 
Py_False({})", fmt::ptr(object), fmt::ptr(Py_True), fmt::ptr(Py_False)));

Review Comment:
   Is this going to print 3 memory addresses?  If we have a stack trace, then 
we have access to the addresses, and if we don't, then they aren't very useful.
   
   I would just throw something like "Expected to get Py_True or Py_False, but 
got something else".



##########
extensions/python/PythonScriptEngine.cpp:
##########
@@ -0,0 +1,183 @@
+/**
+ * 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.
+ */
+
+#include <string>
+#include <filesystem>
+
+#include "PythonScriptEngine.h"
+#include "PythonBindings.h"
+#include "types/PyProcessSession.h"
+#include "types/PyProcessContext.h"
+#include "types/PyProcessor.h"
+#include "types/PyLogger.h"
+#include "types/PyRelationship.h"
+
+namespace org::apache::nifi::minifi::extensions::python {
+
+Interpreter* Interpreter::getInterpreter() {
+  static Interpreter interpreter;
+  return &interpreter;
+}
+
+GlobalInterpreterLock::GlobalInterpreterLock() {
+  gil_state_ = PyGILState_Ensure();
+}
+
+GlobalInterpreterLock::~GlobalInterpreterLock() {
+  PyGILState_Release(gil_state_);
+}
+
+namespace {
+// PyEval_InitThreads might be marked deprecated (depending on the version of 
Python.h)
+// Python <= 3.6: This needs to be called manually after Py_Initialize to 
initialize threads
+// Python >= 3.7: Noop function since its functionality is included in 
Py_Initialize
+// Python >= 3.9: Marked as deprecated (still noop)
+// This can be removed if we drop the support for Python 3.6
+void initThreads() {
+#if defined(__clang__)
+  #pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#elif defined(__GNUC__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#elif defined(WIN32)
+  #pragma warning(push)
+#pragma warning(disable: 4996)
+#endif

Review Comment:
   I think it would be simpler to `#ifdef` based on `PY_MAJOR_VERSION` and 
`PY_MINOR_VERSION`



##########
extensions/python/pythonprocessors/examples/GaussianDistributionWithNumpy.py:
##########
@@ -0,0 +1,42 @@
+# 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.
+
+import numpy as np
+
+
+class WriteCallback:
+    def __init__(self, content):
+        self.content = content
+
+    def process(self, output_stream):
+        output_stream.write(self.content.encode('utf-8'))
+        return len(self.content)
+
+
+def describe(processor):
+    processor.setDescription("Draw random samples from a normal (Gaussian) 
distribution.")
+
+
+def onInitialize(processor):
+    processor.setSupportsDynamicProperties()

Review Comment:
   this processor doesn't use dynamic properties; can we remove this?



##########
extensions/python/types/BaseTypes.h:
##########
@@ -0,0 +1,246 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <concepts>
+#include <utility>
+
+#include "Python.h"
+
+namespace org::apache::nifi::minifi::extensions::python {
+
+template<typename T>
+concept convertible_to_object = requires {
+  static_cast<PyObject*>(std::declval<T>());
+};
+
+template<typename T>
+concept custom_type = requires {
+  { T::typeObject() } -> std::same_as<PyTypeObject*>;
+};
+
+template<typename T>
+concept holder_type = requires {
+  typename T::HeldType;
+} && custom_type<T>;
+
+enum class ReferenceType {
+  BORROWED,
+  OWNED,
+};
+
+template<ReferenceType reference_type>
+struct ObjectReference {
+  ObjectReference() = default;
+
+  explicit ObjectReference(PyObject* object)
+      : object_(object) {
+  }
+
+  ~ObjectReference() {
+    decrementRefCount();
+  }
+
+  ObjectReference(const ObjectReference& that)
+      : object_(that.object_) {
+    incrementRefCount();
+  }
+
+  ObjectReference(ObjectReference&& that)
+      : object_(that.object_) {
+    that.object_ = nullptr;
+  }
+
+  ObjectReference& operator=(const ObjectReference& that) {
+    if (this == &that) {
+      return *this;
+    }
+
+    decrementRefCount();
+    object_ = that.object_;
+    incrementRefCount();
+    return *this;
+  }
+
+  ObjectReference& operator=(ObjectReference&& that) {
+    if (this == &that) {
+      return *this;
+    }
+
+    object_ = that.object_;

Review Comment:
   do we need to call `decrementRefCount()` on the old `object_` (potentially) 
owned by `this`?



##########
extensions/python/types/BaseTypes.h:
##########
@@ -0,0 +1,246 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <concepts>
+#include <utility>
+
+#include "Python.h"
+
+namespace org::apache::nifi::minifi::extensions::python {
+
+template<typename T>
+concept convertible_to_object = requires {
+  static_cast<PyObject*>(std::declval<T>());
+};
+
+template<typename T>
+concept custom_type = requires {
+  { T::typeObject() } -> std::same_as<PyTypeObject*>;
+};
+
+template<typename T>
+concept holder_type = requires {
+  typename T::HeldType;
+} && custom_type<T>;
+
+enum class ReferenceType {
+  BORROWED,
+  OWNED,
+};
+
+template<ReferenceType reference_type>
+struct ObjectReference {
+  ObjectReference() = default;
+
+  explicit ObjectReference(PyObject* object)
+      : object_(object) {
+  }
+
+  ~ObjectReference() {
+    decrementRefCount();
+  }
+
+  ObjectReference(const ObjectReference& that)
+      : object_(that.object_) {
+    incrementRefCount();
+  }
+
+  ObjectReference(ObjectReference&& that)
+      : object_(that.object_) {
+    that.object_ = nullptr;
+  }
+
+  ObjectReference& operator=(const ObjectReference& that) {
+    if (this == &that) {
+      return *this;
+    }
+
+    decrementRefCount();
+    object_ = that.object_;
+    incrementRefCount();
+    return *this;
+  }
+
+  ObjectReference& operator=(ObjectReference&& that) {
+    if (this == &that) {
+      return *this;
+    }
+
+    object_ = that.object_;
+    that.object_ = nullptr;
+    return *this;
+  }
+
+  ObjectReference& operator=(PyObject* object) {
+    decrementRefCount();
+    object_ = object;
+    return *this;
+  }

Review Comment:
   do we need an `incrementRefCount()` after the assignment?



##########
extensions/python/types/PyInputStream.cpp:
##########
@@ -0,0 +1,91 @@
+/**
+ *
+ * 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.
+ */
+
+#include "PyInputStream.h"
+#include <vector>
+
+#include "PyException.h"
+#include "Types.h"
+#include "utils/gsl.h"
+
+extern "C" {
+namespace org::apache::nifi::minifi::extensions::python {
+
+static PyMethodDef PyInputStream_methods[] = {
+    {"read", (PyCFunction) PyInputStream::read, METH_VARARGS, nullptr},
+    {}  /* Sentinel */
+};
+
+static PyType_Slot PyInputStreamTypeSpecSlots[] = {
+    {Py_tp_dealloc, 
reinterpret_cast<void*>(pythonAllocatedInstanceDealloc<PyInputStream>)},
+    {Py_tp_init, reinterpret_cast<void*>(PyInputStream::init)},
+    {Py_tp_methods, reinterpret_cast<void*>(PyInputStream_methods)},
+    {Py_tp_new, 
reinterpret_cast<void*>(newPythonAllocatedInstance<PyInputStream>)},
+    {}  /* Sentinel */
+};
+
+static PyType_Spec PyInputStreamTypeSpec{
+    .name = "minifi_native.InputStream",
+    .basicsize = sizeof(PyInputStream),
+    .itemsize = 0,
+    .flags = Py_TPFLAGS_DEFAULT,
+    .slots = PyInputStreamTypeSpecSlots
+};
+
+int PyInputStream::init(PyInputStream* self, PyObject* args, PyObject*) {
+  PyObject* weak_ptr_capsule = nullptr;
+  if (!PyArg_ParseTuple(args, "O", &weak_ptr_capsule)) {
+    return -1;
+  }
+
+  auto input_stream = PyCapsule_GetPointer(weak_ptr_capsule, HeldTypeName);
+  if (!input_stream)
+    throw PyException();
+  self->input_stream_ = *static_cast<HeldType*>(input_stream);
+  return 0;
+}

Review Comment:
   just out of curiosity, why do we sometimes indicate an error by returning 
`-1` (or null), and other times by throwing an exception?



##########
win_build_vs.bat:
##########
@@ -119,7 +119,7 @@ cmake -G %generator% %build_platform_cmd% 
-DINSTALLER_MERGE_MODULES=%installer_m
         -DENABLE_NANOFI=%build_nanofi% -DENABLE_OPENCV=%build_opencv% 
-DENABLE_PROMETHEUS=%build_prometheus% -DENABLE_ELASTICSEARCH=%build_ELASTIC% 
-DUSE_SHARED_LIBS=OFF -DDISABLE_CONTROLLER=ON  ^
         -DENABLE_BUSTACHE=%enable_bustache% -DENABLE_COAP=%enable_coap% 
-DENABLE_ENCRYPT_CONFIG=%enable_encrypt_config% -DENABLE_GPS=%enable_gps% 
-DENABLE_LUA_SCRIPTING=%enable_lua_scripting% ^
         -DENABLE_MQTT=%enable_mqtt% -DENABLE_OPC=%enable_opc% 
-DENABLE_OPENWSMAN=%enable_openwsman% -DENABLE_OPS=%enable_ops% 
-DENABLE_PCAP=%enable_pcap% ^
-        -DENABLE_SCRIPTING=%enable_scripting% 
-DENABLE_SENSORS=%enable_sensors% -DENABLE_TENSORFLOW=%enable_tensorflow% 
-DENABLE_USB_CAMERA=%enable_usb_camera% ^
+        -DENABLE_PYTHON_SCRIPTING=%enable_python_scripting% 
-DENABLE_SENSORS=%enable_sensors% -DENABLE_TENSORFLOW=%enable_tensorflow% 
-DENABLE_USB_CAMERA=%enable_usb_camera% ^

Review Comment:
   please update `Windows.md`, as well (although it was incorrect before this 
PR, too)



##########
extensions/python/tests/TestExecuteScriptProcessorWithPythonScript.cpp:
##########
@@ -0,0 +1,263 @@
+/**
+ *
+ * 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.
+ */
+
+#include <memory>
+#include <string>
+
+#include "SingleProcessorTestController.h"
+#include "TestBase.h"
+#include "Catch.h"
+
+#include "../../script/ExecuteScript.h"
+#include "utils/file/FileUtils.h"
+#include "utils/file/PathUtils.h"
+
+namespace org::apache::nifi::minifi::processors::test {
+
+TEST_CASE("Script engine is not set", "[executescriptMisconfiguration]") {
+  TestController test_controller;
+  auto plan = test_controller.createPlan();
+
+  auto execute_script = plan->addProcessor("ExecuteScript", "executeScript");
+
+  plan->setProperty(execute_script, ExecuteScript::ScriptEngine.getName(), "");
+  plan->setProperty(execute_script, ExecuteScript::ScriptFile.getName(), 
"/path/to/script.py");
+
+  REQUIRE_THROWS_AS(test_controller.runSession(plan, true), minifi::Exception);
+}
+
+TEST_CASE("Neither script body nor script file is set", 
"[executescriptMisconfiguration]") {
+  TestController test_controller;
+  auto plan = test_controller.createPlan();
+
+  auto execute_script = plan->addProcessor("ExecuteScript", "executeScript");
+
+  plan->setProperty(execute_script, ExecuteScript::ScriptEngine.getName(), 
"python");
+
+  REQUIRE_THROWS_AS(test_controller.runSession(plan, true), minifi::Exception);
+}
+
+TEST_CASE("Test both script body and script file set", 
"[executescriptMisconfiguration]") {
+  TestController test_controller;
+  auto plan = test_controller.createPlan();
+
+  auto execute_script = plan->addProcessor("ExecuteScript", "executeScript");
+
+  plan->setProperty(execute_script, ExecuteScript::ScriptEngine.getName(), 
"python");
+  plan->setProperty(execute_script, ExecuteScript::ScriptFile.getName(), 
"/path/to/script.py");
+  plan->setProperty(execute_script, ExecuteScript::ScriptBody.getName(), R"(
+    def onTrigger(context, session):
+      log.info('hello from python')
+  )");
+
+  REQUIRE_THROWS_AS(test_controller.runSession(plan, true), minifi::Exception);
+}
+
+TEST_CASE("Python: Test session get should return None if there are no 
flowfiles in the incoming connections") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<ExecuteScript>();
+
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptBody, R"(
+def onTrigger(context, session):
+  flow_file = session.get()
+
+  if flow_file is not None:
+    raise Exception("Didn't expect flow_file")
+  )");
+  auto result = controller.trigger();
+  REQUIRE(result.at(ExecuteScript::Success).empty());
+  REQUIRE(result.at(ExecuteScript::Failure).empty());
+}
+
+TEST_CASE("Python: Test Read File", "[executescriptPythonRead]") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<ExecuteScript>();
+
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptBody, R"(
+import codecs
+
+class ReadCallback(object):
+  def process(self, input_stream):
+    content = codecs.getreader('utf-8')(input_stream).read()
+    log.info('file content: %s' % content)
+    return len(content)
+
+def onTrigger(context, session):
+  flow_file = session.get()
+
+  if flow_file is not None:
+    log.info('got flow file: %s' % flow_file.getAttribute('filename'))
+    session.read(flow_file, ReadCallback())
+    session.transfer(flow_file, REL_SUCCESS)
+  )");
+
+  auto result = controller.trigger("tempFile");
+  REQUIRE(result.at(ExecuteScript::Success).size() == 1);
+  CHECK(controller.plan->getContent(result.at(ExecuteScript::Success)[0]) == 
"tempFile");
+}
+
+TEST_CASE("Python: Test Write File", "[executescriptPythonWrite]") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<ExecuteScript>();
+
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptBody, R"(
+class WriteCallback(object):
+  def process(self, output_stream):
+    new_content = 'hello 2'.encode('utf-8')
+    output_stream.write(new_content)
+    return len(new_content)
+
+def onTrigger(context, session):
+  flow_file = session.get()
+  if flow_file is not None:
+    log.info('got flow file: %s' % flow_file.getAttribute('filename'))
+    session.write(flow_file, WriteCallback())
+    session.transfer(flow_file, REL_SUCCESS)
+  )");
+
+  auto result = controller.trigger("tempFile");
+  REQUIRE(result.at(ExecuteScript::Success).size() == 1);
+  CHECK(controller.plan->getContent(result.at(ExecuteScript::Success)[0]) == 
"hello 2");
+}
+
+TEST_CASE("Python: Test Create", "[executescriptPythonCreate]") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<ExecuteScript>();
+
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptBody, R"(
+def onTrigger(context, session):
+  flow_file = session.create()
+
+  if flow_file is not None:
+    log.info('created flow file: %s' % flow_file.getAttribute('filename'))
+    session.transfer(flow_file, REL_SUCCESS)
+  )");
+
+
+  auto result = controller.trigger();
+  REQUIRE(result.at(ExecuteScript::Success).size() == 1);
+  REQUIRE(result.at(ExecuteScript::Failure).empty());
+  REQUIRE(LogTestController::getInstance().contains("[info] created flow 
file:"));
+}
+
+TEST_CASE("Python: Test Update Attribute", 
"[executescriptPythonUpdateAttribute]") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<ExecuteScript>();
+
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptBody, R"(
+def onTrigger(context, session):
+  flow_file = session.get()
+
+  if flow_file is not None:
+    log.info('got flow file: %s' % flow_file.getAttribute('filename'))
+    flow_file.addAttribute('test_attr', '1')
+    attr = flow_file.getAttribute('test_attr')
+    log.info('got flow file attr \'test_attr\': %s' % attr)
+    flow_file.updateAttribute('test_attr', str(int(attr) + 1))
+    session.transfer(flow_file, REL_SUCCESS)
+  )");
+
+  auto result = controller.trigger("tempFile");
+  REQUIRE(result.at(ExecuteScript::Success).size() == 1);
+  CHECK(controller.plan->getContent(result.at(ExecuteScript::Success)[0]) == 
"tempFile");
+  CHECK(result.at(ExecuteScript::Success)[0]->getAttribute("test_attr") == 
"2");
+}
+
+TEST_CASE("Python: Test Get Context Property", 
"[executescriptPythonGetContextProperty]") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<ExecuteScript>();
+
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptBody, R"(
+def onTrigger(context, session):
+  script_engine = context.getProperty('Script Engine')
+  log.info('got Script Engine property: %s' % script_engine)
+  )");
+
+  auto result_without_input = controller.trigger();
+  REQUIRE(result_without_input.at(ExecuteScript::Success).empty());
+  REQUIRE(result_without_input.at(ExecuteScript::Failure).empty());
+
+  REQUIRE(LogTestController::getInstance().contains("[info] got Script Engine 
property: python"));
+}
+
+TEST_CASE("Python: Test Module Directory property", 
"[executescriptPythonModuleDirectoryProperty]") {
+  using org::apache::nifi::minifi::utils::file::get_executable_dir;
+
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<ExecuteScript>();
+
+  const auto script_files_directory = 
std::filesystem::path(__FILE__).parent_path() / "test_python_scripts";
+
+
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptFile, 
(script_files_directory / "foo_bar_processor.py").string());
+  execute_script->setProperty(ExecuteScript::ModuleDirectory, 
(script_files_directory / "foo_modules" / "foo.py").string() + "," + 
(script_files_directory / "bar_modules").string());
+
+  auto result = controller.trigger("tempFile");
+  REQUIRE(result.at(ExecuteScript::Success).size() == 1);
+  REQUIRE(result.at(ExecuteScript::Failure).empty());
+
+  REQUIRE(LogTestController::getInstance().contains("foobar"));
+}
+
+TEST_CASE("Python: Non existent script file should throw", 
"[executescriptPythonNonExistentScriptFile]") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<ExecuteScript>();
+
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptFile, 
"/tmp/non-existent-file");
+
+  REQUIRE_THROWS_AS(controller.trigger("tempFile"), minifi::Exception);
+}
+
+TEST_CASE("Python can remove flowfiles", "[ExecuteScript]") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<ExecuteScript>();
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptBody, R"(
+def onTrigger(context, session):
+  flow_file = session.get()
+  session.remove(flow_file);)");
+  REQUIRE_NOTHROW(controller.trigger("hello"));

Review Comment:
   we could also check that both the `Success` and `Failure` outgoing 
connections are empty



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to