From 120a7dd42ec51d338945d38b7f49dae2d0ab67d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 12 Oct 2017 22:24:45 +0200 Subject: [PATCH] GUI: Refactoring pythonCmd lookup --- gui/checkthread.cpp | 35 +++++++++++++++++++++++++++++------ gui/checkthread.h | 19 ++++++++++--------- gui/mainwindow.cpp | 1 - gui/threadhandler.cpp | 1 - gui/threadhandler.h | 5 ----- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/gui/checkthread.cpp b/gui/checkthread.cpp index 522f4d380..7cca41d68 100644 --- a/gui/checkthread.cpp +++ b/gui/checkthread.cpp @@ -258,8 +258,12 @@ void CheckThread::runAddonsAndTools(const ImportProject::FileSettings *fileSetti parseClangErrors(addon, fileName, errout); } else { - const QString a = CheckThread::getAddonFilePath(mDataDir, addon + ".py"); - if (a.isEmpty()) + const QString python = CheckThread::pythonCmd(); + if (python.isEmpty()) + continue; + + const QString addonFilePath = CheckThread::getAddonFilePath(mDataDir, addon + ".py"); + if (addonFilePath.isEmpty()) continue; if (dumpFile.isEmpty()) { @@ -281,15 +285,14 @@ void CheckThread::runAddonsAndTools(const ImportProject::FileSettings *fileSetti mCppcheck.settings().buildDir = buildDir; } - const QString python = mPythonPath.isEmpty() ? QString("python") : mPythonPath; QStringList args; - args << a << dumpFile; + args << addonFilePath << dumpFile; qDebug() << python << args; QProcess process; QProcessEnvironment env = process.processEnvironment(); - if (!env.contains("PYTHONHOME") && !mPythonPath.isEmpty()) { - env.insert("PYTHONHOME", QFileInfo(mPythonPath).canonicalPath()); + if (!env.contains("PYTHONHOME") && !python.startsWith("python")) { + env.insert("PYTHONHOME", QFileInfo(python).canonicalPath()); process.setProcessEnvironment(env); } process.start(python, args); @@ -474,6 +477,26 @@ QString CheckThread::clangTidyCmd() return QString(); } +QString CheckThread::pythonCmd() +{ + QString path = QSettings().value(SETTINGS_PYTHON_PATH).toString(); + if (!path.isEmpty()) + return path; + + path = "python"; +#ifdef Q_OS_WIN + path += ".exe"; +#endif + + QProcess process; + process.start(path, QStringList() << "--version"); + process.waitForFinished(); + if (process.exitCode() == 0) + return path; + + return QString(); +} + QString CheckThread::getAddonFilePath(const QString &dataDir, const QString &addonFile) { const QStringList paths = QStringList() << "/" << "/addons/" << "/../addons/"; diff --git a/gui/checkthread.h b/gui/checkthread.h index 482e29131..6e5364979 100644 --- a/gui/checkthread.h +++ b/gui/checkthread.h @@ -56,10 +56,6 @@ public: mAddonsAndTools = addonsAndTools; } - void setPythonPath(const QString &p) { - mPythonPath = p; - } - void setDataDir(const QString &dataDir) { mDataDir = dataDir; } @@ -81,17 +77,23 @@ public: void stop(); /** - * Loop for clang and return path - * \return path to clang if found, empty if it is not found + * Determine command to run clang + * \return Command to run clang, empty if it is not found */ static QString clangCmd(); /** - * Loop for clang-tidy and return path - * \return path to clang-tidy if found, empty if it is not found + * Determine command to run clang-tidy + * \return Command to run clang-tidy, empty if it is not found */ static QString clangTidyCmd(); + /** + * Determine command to run python + * \return Command to run python, empty if it is not found + */ + static QString pythonCmd(); + /** * Look for addon and return path * \return path to addon if found, empty if it is not found @@ -143,7 +145,6 @@ private: QStringList mFiles; bool mAnalyseWholeProgram; QStringList mAddonsAndTools; - QString mPythonPath; QString mDataDir; QStringList mClangIncludePaths; QStringList mSuppressions; diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 6fe285d8f..fab4c6ab8 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -457,7 +457,6 @@ void MainWindow::doAnalyzeProject(ImportProject p) //mThread->SetanalyzeProject(true); if (mProjectFile) { mThread->setAddonsAndTools(mProjectFile->getAddonsAndTools()); - mThread->setPythonPath(mSettings->value(SETTINGS_PYTHON_PATH).toString()); QString clangHeaders = mSettings->value(SETTINGS_VS_INCLUDE_PATHS).toString(); mThread->setClangIncludePaths(clangHeaders.split(";")); mThread->setSuppressions(mProjectFile->getSuppressions()); diff --git a/gui/threadhandler.cpp b/gui/threadhandler.cpp index 3b631c19d..fc2bdc0be 100644 --- a/gui/threadhandler.cpp +++ b/gui/threadhandler.cpp @@ -94,7 +94,6 @@ void ThreadHandler::check(const Settings &settings) for (int i = 0; i < mRunningThreadCount; i++) { mThreads[i]->setAddonsAndTools(mAddonsAndTools); - mThreads[i]->setPythonPath(mPythonPath); mThreads[i]->setSuppressions(mSuppressions); mThreads[i]->setClangIncludePaths(mClangIncludePaths); mThreads[i]->setDataDir(mDataDir); diff --git a/gui/threadhandler.h b/gui/threadhandler.h index 7e4389413..216b416f5 100644 --- a/gui/threadhandler.h +++ b/gui/threadhandler.h @@ -79,10 +79,6 @@ public: mSuppressions = s; } - void setPythonPath(const QString &p) { - mPythonPath = p; - } - void setClangIncludePaths(const QStringList &s) { mClangIncludePaths = s; } @@ -259,7 +255,6 @@ protected: QStringList mAddonsAndTools; QStringList mSuppressions; - QString mPythonPath; QStringList mClangIncludePaths; QString mDataDir;