Skip to content

Commit

Permalink
Fix #13017 (GUI: Problems to execute python scripts)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Aug 14, 2024
1 parent a25bc07 commit 50b1995
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion gui/checkthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
#include <QCharRef>
#endif

static QString unquote(QString s) {
if (s.startsWith("\""))
s = s.mid(1, s.size() - 2);
return s;
}

// NOLINTNEXTLINE(performance-unnecessary-value-param) - used as callback so we need to preserve the signature
int CheckThread::executeCommand(std::string exe, std::vector<std::string> args, std::string redirect, std::string &output) // cppcheck-suppress passedByValue
{
Expand All @@ -64,7 +70,24 @@ int CheckThread::executeCommand(std::string exe, std::vector<std::string> args,
args2 << QString::fromStdString(arg);

QProcess process;
process.start(QString::fromStdString(exe), args2);

QString e = unquote(QString::fromStdString(exe));

if (e.toLower().replace("\\", "/").endsWith("/python.exe") && !args.empty()) {
const QString path = e.left(e.size()-11);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("PYTHONPATH", path + "/Lib/site-packages");
env.insert("PYTHONHOME", path);
process.setProcessEnvironment(env);

const QString pythonScript = unquote(args2[0]);
if (pythonScript.endsWith(".py")) {
const QString path2 = pythonScript.left(QString(pythonScript).replace('\\', '/').lastIndexOf("/"));
process.setWorkingDirectory(path2);
}
}

process.start(e, args2);
process.waitForFinished();

if (redirect == "2>&1") {
Expand Down

0 comments on commit 50b1995

Please sign in to comment.