Browse Source

Reduce queries to python version

Instead of doing at least 2 queries for python infos, now requires only
1 query (in ideal condition), and the result is cached.
adaptive-webui-19844
Chocobo1 6 years ago
parent
commit
3e6c8a05dd
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 2
      src/base/search/searchdownloadhandler.cpp
  2. 2
      src/base/search/searchhandler.cpp
  3. 8
      src/base/search/searchpluginmanager.cpp
  4. 133
      src/base/utils/foreignapps.cpp
  5. 17
      src/base/utils/foreignapps.h
  6. 7
      src/base/utils/version.h
  7. 47
      src/gui/mainwindow.cpp
  8. 2
      src/gui/search/searchwidget.cpp

2
src/base/search/searchdownloadhandler.cpp

@ -48,7 +48,7 @@ SearchDownloadHandler::SearchDownloadHandler(const QString &siteUrl, const QStri
url url
}; };
// Launch search // Launch search
m_downloadProcess->start(Utils::ForeignApps::Python::pythonExecutable(), params, QIODevice::ReadOnly); m_downloadProcess->start(Utils::ForeignApps::pythonInfo().executableName, params, QIODevice::ReadOnly);
} }
void SearchDownloadHandler::downloadProcessFinished(int exitcode) void SearchDownloadHandler::downloadProcessFinished(int exitcode)

2
src/base/search/searchhandler.cpp

@ -70,7 +70,7 @@ SearchHandler::SearchHandler(const QString &pattern, const QString &category, co
}; };
// Launch search // Launch search
m_searchProcess->setProgram(Utils::ForeignApps::Python::pythonExecutable()); m_searchProcess->setProgram(Utils::ForeignApps::pythonInfo().executableName);
m_searchProcess->setArguments(params + m_pattern.split(" ")); m_searchProcess->setArguments(params + m_pattern.split(" "));
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))

8
src/base/search/searchpluginmanager.cpp

@ -64,7 +64,7 @@ namespace
QPointer<SearchPluginManager> SearchPluginManager::m_instance = nullptr; QPointer<SearchPluginManager> SearchPluginManager::m_instance = nullptr;
SearchPluginManager::SearchPluginManager() SearchPluginManager::SearchPluginManager()
: m_updateUrl(QString("http://searchplugins.qbittorrent.org/%1/engines/").arg(Utils::ForeignApps::Python::pythonVersion() >= 3 ? "nova3" : "nova")) : m_updateUrl(QString("http://searchplugins.qbittorrent.org/%1/engines/").arg(Utils::ForeignApps::pythonInfo().version.majorNumber() >= 3 ? "nova3" : "nova"))
{ {
Q_ASSERT(!m_instance); // only one instance is allowed Q_ASSERT(!m_instance); // only one instance is allowed
m_instance = this; m_instance = this;
@ -323,7 +323,7 @@ QString SearchPluginManager::pluginsLocation()
QString SearchPluginManager::engineLocation() QString SearchPluginManager::engineLocation()
{ {
QString folder = "nova"; QString folder = "nova";
if (Utils::ForeignApps::Python::pythonVersion() >= 3) if (Utils::ForeignApps::pythonInfo().version.majorNumber() >= 3)
folder = "nova3"; folder = "nova3";
const QString location = Utils::Fs::expandPathAbs(specialFolderLocation(SpecialFolder::Data) + folder); const QString location = Utils::Fs::expandPathAbs(specialFolderLocation(SpecialFolder::Data) + folder);
QDir locationDir(location); QDir locationDir(location);
@ -371,7 +371,7 @@ void SearchPluginManager::updateNova()
// create nova directory if necessary // create nova directory if necessary
QDir searchDir(engineLocation()); QDir searchDir(engineLocation());
QString novaFolder = Utils::ForeignApps::Python::pythonVersion() >= 3 ? "searchengine/nova3" : "searchengine/nova"; QString novaFolder = Utils::ForeignApps::pythonInfo().version.majorNumber() >= 3 ? "searchengine/nova3" : "searchengine/nova";
QFile packageFile(searchDir.absoluteFilePath("__init__.py")); QFile packageFile(searchDir.absoluteFilePath("__init__.py"));
packageFile.open(QIODevice::WriteOnly | QIODevice::Text); packageFile.open(QIODevice::WriteOnly | QIODevice::Text);
packageFile.close(); packageFile.close();
@ -437,7 +437,7 @@ void SearchPluginManager::update()
QStringList params; QStringList params;
params << Utils::Fs::toNativePath(engineLocation() + "/nova2.py"); params << Utils::Fs::toNativePath(engineLocation() + "/nova2.py");
params << "--capabilities"; params << "--capabilities";
nova.start(Utils::ForeignApps::Python::pythonExecutable(), params, QIODevice::ReadOnly); nova.start(Utils::ForeignApps::pythonInfo().executableName, params, QIODevice::ReadOnly);
nova.waitForStarted(); nova.waitForStarted();
nova.waitForFinished(); nova.waitForFinished();

133
src/base/utils/foreignapps.cpp

@ -35,99 +35,66 @@
#include "base/logger.h" #include "base/logger.h"
/** using namespace Utils::ForeignApps;
* Detects the python version.
*/ namespace
int Utils::ForeignApps::Python::pythonVersion()
{ {
static int version = -1; bool testPythonInstallation(const QString &exeName, PythonInfo &info)
if (version < 0) { {
QString versionComplete = pythonVersionComplete().trimmed(); QProcess proc;
QStringList splitted = versionComplete.split('.'); proc.start(exeName, {"--version"}, QIODevice::ReadOnly);
if (splitted.size() > 1) { if (proc.waitForFinished() && (proc.exitCode() == QProcess::NormalExit)) {
int highVer = splitted.at(0).toInt(); QByteArray procOutput = proc.readAllStandardOutput();
if ((highVer == 2) || (highVer == 3)) if (procOutput.isEmpty())
version = highVer; procOutput = proc.readAllStandardError();
procOutput = procOutput.simplified();
// Software 'Anaconda' installs its own python interpreter
// and `python --version` returns a string like this:
// "Python 3.4.3 :: Anaconda 2.3.0 (64-bit)"
const QList<QByteArray> outputSplit = procOutput.split(' ');
if (outputSplit.size() <= 1)
return false;
try {
info = {exeName, outputSplit[1]};
}
catch (const std::runtime_error &err) {
return false;
}
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Python detected, version: %1").arg(info.version), Log::INFO);
return true;
} }
return false;
} }
return version;
} }
/** bool Utils::ForeignApps::PythonInfo::isValid() const
* Detects the python executable by calling "python --version". {
*/ return (!executableName.isEmpty() && version.isValid());
QString Utils::ForeignApps::Python::pythonExecutable() }
PythonInfo Utils::ForeignApps::pythonInfo()
{ {
static QString executable; static PythonInfo pyInfo;
if (executable.isEmpty()) { if (!pyInfo.isValid()) {
QProcess pythonProc;
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX)
/* // On Unix-Like Systems python2 and python3 should always exist
* On Unix-Like Systems python2 and python3 should always exist // https://legacy.python.org/dev/peps/pep-0394/
* http://legacy.python.org/dev/peps/pep-0394/ if (testPythonInstallation("python3", pyInfo))
*/ return pyInfo;
pythonProc.start("python3", {"--version"}, QIODevice::ReadOnly); if (testPythonInstallation("python2", pyInfo))
if (pythonProc.waitForFinished() && (pythonProc.exitCode() == 0)) { return pyInfo;
executable = "python3";
return executable;
}
pythonProc.start("python2", {"--version"}, QIODevice::ReadOnly);
if (pythonProc.waitForFinished() && (pythonProc.exitCode() == 0)) {
executable = "python2";
return executable;
}
#endif #endif
// Look for "python" in Windows and in UNIX if "python2" and "python3" are // Look for "python" in Windows and in UNIX if "python2" and "python3" are
// not detected. // not detected.
pythonProc.start("python", {"--version"}, QIODevice::ReadOnly); if (testPythonInstallation("python", pyInfo))
if (pythonProc.waitForFinished() && (pythonProc.exitCode() == 0)) return pyInfo;
executable = "python";
else
Logger::instance()->addMessage(QCoreApplication::translate("misc", "Python not detected"), Log::INFO);
}
return executable;
}
/**
* Returns the complete python version
* eg 2.7.9
* Make sure to have setup python first
*/
QString Utils::ForeignApps::Python::pythonVersionComplete()
{
static QString version;
if (version.isEmpty()) {
if (pythonExecutable().isEmpty())
return version;
QProcess pythonProc;
pythonProc.start(pythonExecutable(), {"--version"}, QIODevice::ReadOnly);
if (pythonProc.waitForFinished() && (pythonProc.exitCode() == 0)) {
QByteArray output = pythonProc.readAllStandardOutput();
if (output.isEmpty())
output = pythonProc.readAllStandardError();
// Software 'Anaconda' installs its own python interpreter LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Python not detected"), Log::INFO);
// and `python --version` returns a string like this:
// `Python 3.4.3 :: Anaconda 2.3.0 (64-bit)`
const QList<QByteArray> outSplit = output.split(' ');
if (outSplit.size() > 1) {
version = outSplit.at(1).trimmed();
Logger::instance()->addMessage(QCoreApplication::translate("misc", "Python version: %1").arg(version), Log::INFO);
}
// If python doesn't report a 3-piece version e.g. 3.6.1
// then fill the missing pieces with zero
const QStringList verSplit = version.split('.', QString::SkipEmptyParts);
if (verSplit.size() < 3) {
for (int i = verSplit.size(); i < 3; ++i) {
if (version.endsWith('.'))
version.append('0');
else
version.append(".0");
}
Logger::instance()->addMessage(QCoreApplication::translate("misc", "Normalized Python version: %1").arg(version), Log::INFO);
}
}
} }
return version;
return pyInfo;
} }

17
src/base/utils/foreignapps.h

@ -31,15 +31,22 @@
#include <QString> #include <QString>
#include "base/utils/version.h"
namespace Utils namespace Utils
{ {
namespace ForeignApps namespace ForeignApps
{ {
namespace Python struct PythonInfo
{ {
int pythonVersion(); using Version = Utils::Version<quint8, 3, 1>;
QString pythonExecutable();
QString pythonVersionComplete(); bool isValid() const;
}
QString executableName;
Version version;
};
PythonInfo pythonInfo();
} }
} }

7
src/base/utils/version.h

@ -58,7 +58,7 @@ namespace Utils
template <typename ... Other> template <typename ... Other>
constexpr Version(Other ... components) constexpr Version(Other ... components)
: m_components {{components ...}} : m_components {{static_cast<T>(components) ...}}
{ {
} }
@ -129,6 +129,11 @@ namespace Utils
return res; return res;
} }
constexpr bool isValid() const
{
return (*this != ThisType {});
}
constexpr bool operator==(const ThisType &other) const constexpr bool operator==(const ThisType &other) const
{ {
return (m_components == other.m_components); return (m_components == other.m_components);

47
src/gui/mainwindow.cpp

@ -1743,45 +1743,42 @@ void MainWindow::on_actionRSSReader_triggered()
void MainWindow::on_actionSearchWidget_triggered() void MainWindow::on_actionSearchWidget_triggered()
{ {
if (!m_hasPython && m_ui->actionSearchWidget->isChecked()) { if (!m_hasPython && m_ui->actionSearchWidget->isChecked()) {
int pythonVersion = Utils::ForeignApps::Python::pythonVersion(); int majorVersion = Utils::ForeignApps::pythonInfo().version.majorNumber();
// Check if python is already in PATH // Check if python is already in PATH
if (pythonVersion > 0) if (majorVersion > 0) {
// Prevent translators from messing with PATH // Prevent translators from messing with PATH
Logger::instance()->addMessage(tr("Python found in %1: %2", "Python found in PATH: /usr/local/bin:/usr/bin:/etc/bin") Logger::instance()->addMessage(tr("Python found in %1: %2", "Python found in PATH: /usr/local/bin:/usr/bin:/etc/bin")
.arg("PATH", qgetenv("PATH").constData()), Log::INFO); .arg("PATH", qgetenv("PATH").constData()), Log::INFO);
}
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
else if (addPythonPathToEnv()) else if (addPythonPathToEnv()) {
pythonVersion = Utils::ForeignApps::Python::pythonVersion(); majorVersion = Utils::ForeignApps::pythonInfo().version.majorNumber();
}
#endif #endif
else {
QMessageBox::information(this, tr("Undetermined Python version"), tr("Couldn't determine your Python version. Search engine disabled."));
m_ui->actionSearchWidget->setChecked(false);
Preferences::instance()->setSearchEnabled(false);
return;
}
bool res = false; bool res = false;
if ((pythonVersion == 2) || (pythonVersion == 3)) { if ((majorVersion == 2) || (majorVersion == 3)) {
// Check Python minimum requirement: 2.7.9 / 3.3.0 // Check Python minimum requirement: 2.7.9 / 3.3.0
QString version = Utils::ForeignApps::Python::pythonVersionComplete(); using Version = Utils::ForeignApps::PythonInfo::Version;
QStringList splitted = version.split('.'); const Version pyVersion = Utils::ForeignApps::pythonInfo().version;
if (splitted.size() > 2) {
int middleVer = splitted.at(1).toInt(); if (((majorVersion == 2) && (pyVersion < Version {2, 7, 9}))
int lowerVer = splitted.at(2).toInt(); || ((majorVersion == 3) && (pyVersion < Version {3, 3, 0}))) {
if (((pythonVersion == 2) && (middleVer < 7)) QMessageBox::information(this, tr("Old Python Interpreter"), tr("Your Python version (%1) is outdated. Please upgrade to latest version for search engines to work.\nMinimum requirement: 2.7.9 / 3.3.0.").arg(pyVersion));
|| ((pythonVersion == 2) && (middleVer == 7) && (lowerVer < 9))
|| ((pythonVersion == 3) && (middleVer < 3))) {
QMessageBox::information(this, tr("Old Python Interpreter"), tr("Your Python version (%1) is outdated. Please upgrade to latest version for search engines to work.\nMinimum requirement: 2.7.9 / 3.3.0.").arg(version));
m_ui->actionSearchWidget->setChecked(false);
Preferences::instance()->setSearchEnabled(false);
return;
}
else {
res = true;
}
}
else {
QMessageBox::information(this, tr("Undetermined Python version"), tr("Couldn't determine your Python version (%1). Search engine disabled.").arg(version));
m_ui->actionSearchWidget->setChecked(false); m_ui->actionSearchWidget->setChecked(false);
Preferences::instance()->setSearchEnabled(false); Preferences::instance()->setSearchEnabled(false);
return; return;
} }
res = true;
} }
if (res) { if (res) {
@ -2086,7 +2083,7 @@ void MainWindow::pythonDownloadSuccess(const QString &url, const QString &filePa
m_hasPython = addPythonPathToEnv(); m_hasPython = addPythonPathToEnv();
if (m_hasPython) { if (m_hasPython) {
// Make it print the version to Log // Make it print the version to Log
Utils::ForeignApps::Python::pythonVersion(); Utils::ForeignApps::pythonInfo();
m_ui->actionSearchWidget->setChecked(true); m_ui->actionSearchWidget->setChecked(true);
displaySearchTab(true); displaySearchTab(true);
} }

2
src/gui/search/searchwidget.cpp

@ -285,7 +285,7 @@ void SearchWidget::giveFocusToSearchInput()
// Function called when we click on search button // Function called when we click on search button
void SearchWidget::on_searchButton_clicked() void SearchWidget::on_searchButton_clicked()
{ {
if (Utils::ForeignApps::Python::pythonVersion() < 0) { if (Utils::ForeignApps::pythonInfo().version.majorNumber() <= 0) {
m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Please install Python to use the Search Engine.")); m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Please install Python to use the Search Engine."));
return; return;
} }

Loading…
Cancel
Save