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


##########
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:
   Yes it looks strange, but there is an issue with std::system on Windows that 
needs the additional quotes on the whole command to work correctly. If I 
remember well it passes the system command as a parameter to the `cmd.exe /C` 
command and the additional quotes are needed to parse the system command's 
arguments correctly.



-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to