Addons: If normal python fails, try 'python'

This commit is contained in:
Daniel Marjamäki 2020-05-18 07:44:46 +02:00
parent 23999fece5
commit a0f2fda53a
1 changed files with 21 additions and 9 deletions

View File

@ -169,31 +169,43 @@ static std::string executeAddon(const AddonInfo &addonInfo,
const std::string &dumpFile) const std::string &dumpFile)
{ {
std::string pythonExe = cmdFileName((addonInfo.python != "") ? addonInfo.python : defaultPythonExe); std::string pythonExe;
// Can python be executed?
const int lastTest = 3;
for (int test = 1; test <= lastTest; ++test) {
if (test == 1 || test == lastTest)
pythonExe = cmdFileName((addonInfo.python != "") ? addonInfo.python : defaultPythonExe);
else
pythonExe = "python";
const std::string cmd = pythonExe + " --version 2>&1";
#ifdef _WIN32
if (pythonExe.find(" ") != std::string::npos) { if (pythonExe.find(" ") != std::string::npos) {
// popen strips the first quote. Needs 2 sets to fully quote. // popen strips the first quote. Needs 2 sets to fully quote.
pythonExe = "\"" + pythonExe + "\""; pythonExe = "\"" + pythonExe + "\"";
} }
// Can python be executed?
{
const std::string cmd = pythonExe + " --version 2>&1";
#ifdef _WIN32
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose); std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);
#else #else
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose); std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
#endif #endif
if (!pipe) if (!pipe) {
if (test < lastTest)
continue;
throw InternalError(nullptr, "popen failed (command: '" + cmd + "')"); throw InternalError(nullptr, "popen failed (command: '" + cmd + "')");
}
char buffer[1024]; char buffer[1024];
std::string result; std::string result;
while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr) while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr)
result += buffer; result += buffer;
if (result.compare(0, 7, "Python ", 0, 7) != 0 || result.size() > 50) if (result.compare(0, 7, "Python ", 0, 7) != 0 || result.size() > 50) {
if (test < lastTest)
continue;
throw InternalError(nullptr, "Failed to execute '" + cmd + "' (" + result + ")"); throw InternalError(nullptr, "Failed to execute '" + cmd + "' (" + result + ")");
} }
}
const std::string cmd = pythonExe + " " + cmdFileName(addonInfo.scriptFile) + " --cli" + addonInfo.args + " " + cmdFileName(dumpFile) + " 2>&1"; const std::string cmd = pythonExe + " " + cmdFileName(addonInfo.scriptFile) + " --cli" + addonInfo.args + " " + cmdFileName(dumpFile) + " 2>&1";