Skip to content

Commit e63c7c0

Browse files
committed
Review update
1 parent ca6a73f commit e63c7c0

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

extensions/python/PythonDependencyInstaller.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "PyException.h"
2525
#include "types/Types.h"
2626
#include "utils/OptionalUtils.h"
27+
#include "utils/ConfigurationUtils.h"
2728

2829
namespace org::apache::nifi::minifi::extensions::python {
2930

@@ -56,17 +57,22 @@ std::string encapsulateCommandInQuotesIfNeeded(const std::string& command) {
5657
#define pclose _pclose
5758
#endif
5859

59-
std::pair<int, std::string> executeProcess(const std::string& command) {
60-
std::array<char, 256> buffer{};
60+
struct CommandResult {
61+
int exit_code;
62+
std::string output;
63+
};
64+
65+
CommandResult executeProcess(const std::string& command) {
66+
std::array<char, utils::configuration::DEFAULT_BUFFER_SIZE> buffer{};
6167

6268
FILE* pipe = popen(encapsulateCommandInQuotesIfNeeded(command).c_str(), "r");
6369
if (!pipe) {
6470
return {1, fmt::format("Failed to open pipe for command: {}", command)};
6571
}
6672

67-
std::string result;
73+
std::ostringstream result;
6874
while (fgets(buffer.data(), gsl::narrow<int>(buffer.size()), pipe) != nullptr) {
69-
result += buffer.data();
75+
result << buffer.data();
7076
}
7177

7278
int status = pclose(pipe);
@@ -81,7 +87,7 @@ std::pair<int, std::string> executeProcess(const std::string& command) {
8187
}
8288
#endif
8389

84-
return {exit_code, result};
90+
return {exit_code, result.str()};
8591
}
8692

8793
} // namespace
@@ -130,9 +136,9 @@ void PythonDependencyInstaller::createVirtualEnvIfSpecified() const {
130136
logger_->log_info("Creating python virtual env at: {}", virtualenv_path_.string());
131137
auto venv_command = "\"" + python_binary_ + "\" -m venv \"" + virtualenv_path_.string() + "\" 2>&1";
132138
auto result = executeProcess(venv_command);
133-
if (result.first != 0) {
134-
logger_->log_error("The following command creating python virtual env failed: '{}'\nSetup process output:\n{}", venv_command, result.second);
135-
throw PythonScriptException(fmt::format("The following command creating python virtual env failed: '{}'\nSetup process output:\n{}", venv_command, result.second));
139+
if (result.exit_code != 0) {
140+
logger_->log_error("The following command creating python virtual env failed: '{}'\nSetup process output:\n{}", venv_command, result.output);
141+
throw PythonScriptException(fmt::format("The following command creating python virtual env failed: '{}'\nSetup process output:\n{}", venv_command, result.output));
136142
}
137143
}
138144
}
@@ -147,11 +153,11 @@ void PythonDependencyInstaller::runInstallCommandInVirtualenv(const std::string&
147153
command_with_virtualenv.append(install_command);
148154

149155
auto result = executeProcess(command_with_virtualenv + " 2>&1");
150-
if (result.first != 0) {
151-
logger_->log_error("Failed to install python packages to virtualenv. Install process output:\n{}", result.second);
152-
throw PythonScriptException(fmt::format("Failed to install python packages to virtualenv. Install process output:\n{}", result.second));
156+
if (result.exit_code != 0) {
157+
logger_->log_error("Failed to install python packages to virtualenv. Install process output:\n{}", result.output);
158+
throw PythonScriptException(fmt::format("Failed to install python packages to virtualenv. Install process output:\n{}", result.output));
153159
} else {
154-
logger_->log_info("Python packages installed successfully with command: '{}'.\nInstall process output:\n{}", command_with_virtualenv, result.second);
160+
logger_->log_info("Python packages installed successfully with command: '{}'.\nInstall process output:\n{}", command_with_virtualenv, result.output);
155161
}
156162
}
157163

0 commit comments

Comments
 (0)