From e34cc79dad10ea39fd01f03d4e2de5a6b680b9f9 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Fri, 27 Feb 2015 14:00:00 +0100 Subject: [PATCH 1/5] Use python3 and python2 instead of python on Linux Prefer python3 over python2 when both are available. Both python2 and python3 should always exists. More info at: http://legacy.python.org/dev/peps/pep-0394/ --- src/core/utils/misc.cpp | 36 +++++++++++++++++++++++++++++ src/core/utils/misc.h | 1 + src/searchengine/searchengine.cpp | 4 ++-- src/searchengine/supportedengines.h | 3 ++- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/core/utils/misc.cpp b/src/core/utils/misc.cpp index 7860f774c..2647b41f2 100644 --- a/src/core/utils/misc.cpp +++ b/src/core/utils/misc.cpp @@ -232,6 +232,26 @@ int Utils::Misc::pythonVersion() static int version = -1; if (version < 0) { QProcess python_proc; +#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) + /* + * On Unix-Like Systems python2 and python3 should always exist + * http://legacy.python.org/dev/peps/pep-0394/ + */ + python_proc.start("python3", QStringList() << "--version", QIODevice::ReadOnly); + if (python_proc.waitForFinished()) { + if (python_proc.exitCode() == 0) { + version = 3; + return 3; + } + } + python_proc.start("python2", QStringList() << "--version", QIODevice::ReadOnly); + if (python_proc.waitForFinished()) { + if (python_proc.exitCode() == 0) { + version = 2; + return 2; + } + } +#else python_proc.start("python", QStringList() << "--version", QIODevice::ReadOnly); if (!python_proc.waitForFinished()) return -1; if (python_proc.exitCode() < 0) return -1; @@ -244,10 +264,26 @@ int Utils::Misc::pythonVersion() version = 3; else version = 2; +#endif } return version; } +QString misc::pythonExecutable() +{ +#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) + /* + * On Unix-Like Systems python2 and python3 should always exist + * http://legacy.python.org/dev/peps/pep-0394/ + */ + if (pythonVersion() == 3) + return "python3"; + if (pythonVersion() == 2) + return "python2"; +#endif + return "python"; +} + // return best userfriendly storage unit (B, KiB, MiB, GiB, TiB) // use Binary prefix standards from IEC 60027-2 // see http://en.wikipedia.org/wiki/Kilobyte diff --git a/src/core/utils/misc.h b/src/core/utils/misc.h index c69061c20..dc003674f 100644 --- a/src/core/utils/misc.h +++ b/src/core/utils/misc.h @@ -56,6 +56,7 @@ namespace Utils QPoint screenCenter(QWidget *win); #endif int pythonVersion(); + QString pythonExecutable(); // return best userfriendly storage unit (B, KiB, MiB, GiB, TiB) // use Binary prefix standards from IEC 60027-2 // see http://en.wikipedia.org/wiki/Kilobyte diff --git a/src/searchengine/searchengine.cpp b/src/searchengine/searchengine.cpp index 5aa947091..70955e779 100644 --- a/src/searchengine/searchengine.cpp +++ b/src/searchengine/searchengine.cpp @@ -232,7 +232,7 @@ void SearchEngine::on_search_button_clicked() { // Changing the text of the current label currentSearchTab->getCurrentLabel()->setText(tr("Results")+" (0):"); // Launch search - searchProcess->start("python", params, QIODevice::ReadOnly); + searchProcess->start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly); searchTimeout->start(180000); // 3min } @@ -272,7 +272,7 @@ void SearchEngine::downloadTorrent(QString engine_url, QString torrent_url) { params << engine_url; params << torrent_url; // Launch search - downloadProcess->start("python", params, QIODevice::ReadOnly); + downloadProcess->start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly); } } diff --git a/src/searchengine/supportedengines.h b/src/searchengine/supportedengines.h index a03db1b41..defe3531b 100644 --- a/src/searchengine/supportedengines.h +++ b/src/searchengine/supportedengines.h @@ -42,6 +42,7 @@ #include #include "core/utils/fs.h" +#include "core/utils/misc.h" #include "core/preferences.h" class SearchCategories: public QObject, public QHash { @@ -150,7 +151,7 @@ public slots: QStringList params; params << Utils::Fs::toNativePath(Utils::Fs::searchEngineLocation()+"/nova2.py"); params << "--capabilities"; - nova.start("python", params, QIODevice::ReadOnly); + nova.start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly); nova.waitForStarted(); nova.waitForFinished(); QString capabilities = QString(nova.readAll()); From 0c23d22472c3244f9fad4a4ede450681cec50675 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Fri, 27 Feb 2015 14:00:00 +0100 Subject: [PATCH 2/5] Show notification if Python is not found and a search is started Also, don't bother starting a search if it's known that Python is not available. --- src/core/utils/misc.cpp | 2 +- src/searchengine/searchengine.cpp | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core/utils/misc.cpp b/src/core/utils/misc.cpp index 2647b41f2..56c6cab8b 100644 --- a/src/core/utils/misc.cpp +++ b/src/core/utils/misc.cpp @@ -269,7 +269,7 @@ int Utils::Misc::pythonVersion() return version; } -QString misc::pythonExecutable() +QString Utils::Misc::pythonExecutable() { #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) /* diff --git a/src/searchengine/searchengine.cpp b/src/searchengine/searchengine.cpp index 70955e779..f4d606da9 100644 --- a/src/searchengine/searchengine.cpp +++ b/src/searchengine/searchengine.cpp @@ -180,6 +180,11 @@ void SearchEngine::giveFocusToSearchInput() { // Function called when we click on search button void SearchEngine::on_search_button_clicked() { + if (Utils::Misc::pythonVersion() < 0) { + mp_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Please install Python to use the Search Engine.")); + return; + } + if (searchProcess->state() != QProcess::NotRunning) { #ifdef Q_OS_WIN searchProcess->kill(); From a8276dd70f523192b5ba1b4d04629c51d143b307 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Sun, 1 Mar 2015 21:00:00 +0100 Subject: [PATCH 3/5] Update link to the Windows Python installer From v2.7.3 to v3.4.3. --- src/gui/mainwindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 5c56a74da..f04f45bc8 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -1568,7 +1568,7 @@ void MainWindow::installPython() { setCursor(QCursor(Qt::WaitCursor)); // Download python - Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl("http://python.org/ftp/python/2.7.3/python-2.7.3.msi"); + Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl("https://www.python.org/ftp/python/3.4.3/python-3.4.3.msi"); connect(handler, SIGNAL(downloadFinished(QString, QString)), this, SLOT(pythonDownloadSuccess(QString, QString))); connect(handler, SIGNAL(downloadFailed(QString, QString)), this, SLOT(pythonDownloadFailure(QString, QString))); } From f4c44ce128412a37e4bbaead3f5a5012a59c0997 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Sun, 1 Mar 2015 21:00:00 +0100 Subject: [PATCH 4/5] Don't specify the Python version required in the notification Both python2 and python3 are supported. --- src/gui/mainwindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index f04f45bc8..337d64c56 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -1380,7 +1380,7 @@ void MainWindow::on_actionSearch_engine_triggered() has_python = true; } else if (QMessageBox::question(this, tr("Missing Python Interpreter"), - tr("Python 2.x is required to use the search engine but it does not seem to be installed.\nDo you want to install it now?"), + tr("Python is required to use the search engine but it does not seem to be installed.\nDo you want to install it now?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) { // Download and Install Python installPython(); From 6609d3e89fb7d7cbbcc2f3900972c70d36044ce6 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Sun, 1 Mar 2015 21:00:00 +0100 Subject: [PATCH 5/5] Don't use list of versions for the Python fallback detection on Windows Always pick the newest versions among those installed. --- src/core/preferences.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/core/preferences.cpp b/src/core/preferences.cpp index 438b2f5b3..7841ab0dd 100644 --- a/src/core/preferences.cpp +++ b/src/core/preferences.cpp @@ -1763,11 +1763,12 @@ QString Preferences::getPythonPath() return path; // Fallback: Detect python from default locations - QStringList supported_versions; - supported_versions << "34" << "33" << "32" << "31" << "30" << "27" << "26" << "25"; - foreach (const QString &v, supported_versions) - if (QFile::exists("C:/Python" + v + "/python.exe")) - return "C:/Python" + v; + const QStringList dirs = QDir("C:/").entryList(QStringList("Python*"), QDir::Dirs, QDir::Name | QDir::Reversed); + foreach (const QString &dir, dirs) { + const QString path("C:/" + dir + "/python.exe"); + if (QFile::exists(path)) + return path; + } return QString(); }