szaszm commented on code in PR #1721:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1721#discussion_r1511271227


##########
docker/python-verify/conda.Dockerfile:
##########
@@ -31,14 +31,12 @@ USER root
 RUN wget https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh 
-P /tmp \
     && echo "6c8a4abb36fbb711dc055b7049a23bbfd61d356de9468b41c5140f8a11abd851 
/tmp/Anaconda3-2023.09-0-Linux-x86_64.sh" | sha256sum -c \
     && bash /tmp/Anaconda3-2023.09-0-Linux-x86_64.sh -b -p /opt/conda  \
-    && chown -R ${USER}:${USER} /opt/conda \
-    && mkdir /home/${USER}  \
-    && chown -R ${USER}:${USER} /home/${USER}
+    && chown -R ${USER}:${USER} /opt/conda
 
 USER ${USER}
 
 RUN ${CONDA_HOME}/bin/conda init bash
-RUN ${CONDA_HOME}/bin/conda install langchain -c conda-forge
+RUN ${CONDA_HOME}/bin/conda install "langchain<=0.17.0" -c conda-forge

Review Comment:
   What happened in langchain >0.17?



##########
extensions/python/PythonScriptEngine.cpp:
##########
@@ -68,6 +67,73 @@ void initThreads() {
 #pragma warning(pop)
 #endif
 }
+
+std::string encapsulateCommandInQuotesIfNeeded(const std::string& command) {
+#if WIN32
+    return "\"" + command + "\"";
+#else
+    return command;
+#endif
+}
+
+std::vector<std::filesystem::path> getRequirementsFilePaths(const 
std::shared_ptr<Configure> &configuration) {
+  std::vector<std::filesystem::path> paths;
+  if (auto python_processor_path = 
configuration->get(minifi::Configuration::nifi_python_processor_dir)) {
+    for (const auto& entry : 
std::filesystem::recursive_directory_iterator(std::filesystem::path{*python_processor_path}))
 {
+      if (std::filesystem::is_regular_file(entry.path()) && 
entry.path().filename() == "requirements.txt") {
+        paths.push_back(entry.path());
+      }
+    }
+  }
+  return paths;
+}
+
+std::string getPythonBinary(const std::shared_ptr<Configure> &configuration) {
+#if WIN32
+  std::string python_binary = "python";
+#else
+  std::string python_binary = "python3";
+#endif
+  if (auto binary = 
configuration->get(minifi::Configuration::nifi_python_env_setup_binary)) {
+    python_binary = *binary;
+  }
+  return python_binary;
+}
+
+void createVirtualEnvIfSpecified(const std::shared_ptr<Configure> 
&configuration) {
+  if (auto path = 
configuration->get(minifi::Configuration::nifi_python_virtualenv_directory)) {
+    PythonConfigState::getInstance().virtualenv_path = *path;
+    if 
(!std::filesystem::exists(PythonConfigState::getInstance().virtualenv_path) || 
!std::filesystem::is_empty(PythonConfigState::getInstance().virtualenv_path)) {
+      auto venv_command = "\"" + 
PythonConfigState::getInstance().python_binary + "\" -m venv \"" + 
PythonConfigState::getInstance().virtualenv_path.string() + "\"";
+      auto return_value = 
std::system(encapsulateCommandInQuotesIfNeeded(venv_command).c_str());
+      if (return_value != 0) {
+        throw PythonScriptException(fmt::format("The following command 
creating python virtual env failed: '{}'", venv_command));
+      }
+    }
+  }
+}
+
+void installPythonPackagesIfRequested(const std::shared_ptr<Configure> 
&configuration, const std::shared_ptr<core::logging::Logger>& logger) {
+  std::string automatic_install_str;
+  if (!PythonConfigState::getInstance().isPackageInstallationNeeded()) {
+    return;
+  }
+  auto requirement_file_paths = getRequirementsFilePaths(configuration);
+  for (const auto& requirements_file_path : requirement_file_paths) {
+    logger->log_info("Installing python packages from the following 
requirements.txt file: {}", requirements_file_path.string());
+    std::string pip_command;
+#if WIN32
+    
pip_command.append("\"").append((PythonConfigState::getInstance().virtualenv_path
 / "Scripts" / "activate.bat").string()).append("\" && ");
+#else
+    pip_command.append(". 
\"").append((PythonConfigState::getInstance().virtualenv_path / "bin" / 
"activate").string()).append("\" && ");
+#endif
+    
pip_command.append("\"").append(PythonConfigState::getInstance().python_binary).append("\"
 -m pip install --no-cache-dir -r 
\"").append(requirements_file_path.string()).append("\"");
+    auto return_value = 
std::system(encapsulateCommandInQuotesIfNeeded(pip_command).c_str());

Review Comment:
   Could you add this explanation into a code comment, maybe above the quoting 
function? I had the very same question when reading the code.



##########
encrypt-config/tests/ConfigFileEncryptorTests.cpp:
##########
@@ -77,7 +77,7 @@ TEST_CASE("ConfigFileEncryptor can encrypt the sensitive 
properties", "[encrypt-
     uint32_t num_properties_encrypted = 
encryptSensitivePropertiesInFile(test_file, KEY);
 
     REQUIRE(num_properties_encrypted == 1);
-    REQUIRE(test_file.size() == 110);
+    REQUIRE(test_file.size() == 115);

Review Comment:
   what's the increase?



##########
extensions/python/PythonConfigState.h:
##########
@@ -0,0 +1,50 @@
+/**
+ *
+ * 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 <filesystem>
+#include <string>
+
+namespace org::apache::nifi::minifi::extensions::python {
+
+struct PythonConfigState {
+ public:
+  PythonConfigState(PythonConfigState&&) = delete;
+  PythonConfigState(const PythonConfigState&) = delete;
+  PythonConfigState& operator=(PythonConfigState&&) = delete;
+  PythonConfigState& operator=(const PythonConfigState&) = delete;
+
+  bool isPackageInstallationNeeded() const {
+    return install_python_packages_automatically && !virtualenv_path.empty();
+  }
+
+  static PythonConfigState& getInstance() {
+    static PythonConfigState config;
+    return config;
+  }

Review Comment:
   I'd prefer no new singletons. Would it be possible? Is there any reason for 
this being a singleton?



-- 
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