24
24
#include " PyException.h"
25
25
#include " types/Types.h"
26
26
#include " utils/OptionalUtils.h"
27
+ #include " utils/ConfigurationUtils.h"
27
28
28
29
namespace org ::apache::nifi::minifi::extensions::python {
29
30
@@ -56,17 +57,22 @@ std::string encapsulateCommandInQuotesIfNeeded(const std::string& command) {
56
57
#define pclose _pclose
57
58
#endif
58
59
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{};
61
67
62
68
FILE* pipe = popen (encapsulateCommandInQuotesIfNeeded (command).c_str (), " r" );
63
69
if (!pipe) {
64
70
return {1 , fmt::format (" Failed to open pipe for command: {}" , command)};
65
71
}
66
72
67
- std::string result;
73
+ std::ostringstream result;
68
74
while (fgets (buffer.data (), gsl::narrow<int >(buffer.size ()), pipe) != nullptr ) {
69
- result += buffer.data ();
75
+ result << buffer.data ();
70
76
}
71
77
72
78
int status = pclose (pipe);
@@ -81,7 +87,7 @@ std::pair<int, std::string> executeProcess(const std::string& command) {
81
87
}
82
88
#endif
83
89
84
- return {exit_code, result};
90
+ return {exit_code, result. str () };
85
91
}
86
92
87
93
} // namespace
@@ -130,9 +136,9 @@ void PythonDependencyInstaller::createVirtualEnvIfSpecified() const {
130
136
logger_->log_info (" Creating python virtual env at: {}" , virtualenv_path_.string ());
131
137
auto venv_command = " \" " + python_binary_ + " \" -m venv \" " + virtualenv_path_.string () + " \" 2>&1" ;
132
138
auto result = executeProcess (venv_command);
133
- if (result.first != 0 ) {
134
- logger_->log_error (" The following command creating python virtual env failed: '{}'\n Setup process output:\n {}" , venv_command, result.second );
135
- throw PythonScriptException (fmt::format (" The following command creating python virtual env failed: '{}'\n Setup 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: '{}'\n Setup process output:\n {}" , venv_command, result.output );
141
+ throw PythonScriptException (fmt::format (" The following command creating python virtual env failed: '{}'\n Setup process output:\n {}" , venv_command, result.output ));
136
142
}
137
143
}
138
144
}
@@ -147,11 +153,11 @@ void PythonDependencyInstaller::runInstallCommandInVirtualenv(const std::string&
147
153
command_with_virtualenv.append (install_command);
148
154
149
155
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 ));
153
159
} else {
154
- logger_->log_info (" Python packages installed successfully with command: '{}'.\n Install process output:\n {}" , command_with_virtualenv, result.second );
160
+ logger_->log_info (" Python packages installed successfully with command: '{}'.\n Install process output:\n {}" , command_with_virtualenv, result.output );
155
161
}
156
162
}
157
163
0 commit comments