mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-21 05:43:32 -07:00
Improve checks for python. Print python version and path to log.
Conflicts: src/core/misc.cpp src/core/utils/misc.h src/gui/mainwindow.cpp
This commit is contained in:
parent
6787623ff3
commit
86d1dc300c
4 changed files with 106 additions and 12 deletions
|
@ -29,6 +29,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "core/unicodestrings.h"
|
#include "core/unicodestrings.h"
|
||||||
|
#include "core/logger.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
@ -279,6 +280,11 @@ int misc::pythonVersion()
|
||||||
python_proc.start("python3", QStringList() << "--version", QIODevice::ReadOnly);
|
python_proc.start("python3", QStringList() << "--version", QIODevice::ReadOnly);
|
||||||
if (python_proc.waitForFinished()) {
|
if (python_proc.waitForFinished()) {
|
||||||
if (python_proc.exitCode() == 0) {
|
if (python_proc.exitCode() == 0) {
|
||||||
|
QByteArray output = python_proc.readAllStandardOutput();
|
||||||
|
if (output.isEmpty())
|
||||||
|
output = python_proc.readAllStandardError();
|
||||||
|
const QByteArray version_str = output.split(' ').last();
|
||||||
|
Logger::instance()->addMessage(QCoreApplication::translate("misc", "Python version: %1").arg(QString(version_str)), Log::INFO);
|
||||||
version = 3;
|
version = 3;
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
@ -286,6 +292,11 @@ int misc::pythonVersion()
|
||||||
python_proc.start("python2", QStringList() << "--version", QIODevice::ReadOnly);
|
python_proc.start("python2", QStringList() << "--version", QIODevice::ReadOnly);
|
||||||
if (python_proc.waitForFinished()) {
|
if (python_proc.waitForFinished()) {
|
||||||
if (python_proc.exitCode() == 0) {
|
if (python_proc.exitCode() == 0) {
|
||||||
|
QByteArray output = python_proc.readAllStandardOutput();
|
||||||
|
if (output.isEmpty())
|
||||||
|
output = python_proc.readAllStandardError();
|
||||||
|
const QByteArray version_str = output.split(' ').last();
|
||||||
|
Logger::instance()->addMessage(QCoreApplication::translate("misc", "Python version: %1").arg(QString(version_str)), Log::INFO);
|
||||||
version = 2;
|
version = 2;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
@ -298,7 +309,7 @@ int misc::pythonVersion()
|
||||||
if (output.isEmpty())
|
if (output.isEmpty())
|
||||||
output = python_proc.readAllStandardError();
|
output = python_proc.readAllStandardError();
|
||||||
const QByteArray version_str = output.split(' ').last();
|
const QByteArray version_str = output.split(' ').last();
|
||||||
qDebug() << "Python version is:" << version_str.trimmed();
|
Logger::instance()->addMessage(QCoreApplication::translate("misc", "Python version: %1").arg(QString(version_str)), Log::INFO);
|
||||||
if (version_str.startsWith("3."))
|
if (version_str.startsWith("3."))
|
||||||
version = 3;
|
version = 3;
|
||||||
else
|
else
|
||||||
|
@ -323,6 +334,32 @@ QString misc::pythonExecutable()
|
||||||
return "python";
|
return "python";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the complete python version
|
||||||
|
* eg 2.7.9
|
||||||
|
* Make sure to have setup python first
|
||||||
|
*/
|
||||||
|
QString misc::pythonVersionComplete() {
|
||||||
|
static QString version;
|
||||||
|
|
||||||
|
if (version.isEmpty()) {
|
||||||
|
if (pythonVersion() < 0)
|
||||||
|
return QString();
|
||||||
|
|
||||||
|
QProcess python_proc;
|
||||||
|
python_proc.start(pythonExecutable(), QStringList() << "--version", QIODevice::ReadOnly);
|
||||||
|
if (!python_proc.waitForFinished()) return QString();
|
||||||
|
if (python_proc.exitCode() < 0) return QString();
|
||||||
|
QByteArray output = python_proc.readAllStandardOutput();
|
||||||
|
if (output.isEmpty())
|
||||||
|
output = python_proc.readAllStandardError();
|
||||||
|
const QByteArray version_str = output.split(' ').last();
|
||||||
|
version = version_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
|
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
|
||||||
// use Binary prefix standards from IEC 60027-2
|
// use Binary prefix standards from IEC 60027-2
|
||||||
// see http://en.wikipedia.org/wiki/Kilobyte
|
// see http://en.wikipedia.org/wiki/Kilobyte
|
||||||
|
|
|
@ -83,6 +83,7 @@ namespace misc
|
||||||
#endif
|
#endif
|
||||||
int pythonVersion();
|
int pythonVersion();
|
||||||
QString pythonExecutable();
|
QString pythonExecutable();
|
||||||
|
QString pythonVersionComplete();
|
||||||
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
|
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
|
||||||
// use Binary prefix standards from IEC 60027-2
|
// use Binary prefix standards from IEC 60027-2
|
||||||
// see http://en.wikipedia.org/wiki/Kilobyte
|
// see http://en.wikipedia.org/wiki/Kilobyte
|
||||||
|
|
|
@ -108,9 +108,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
, m_posInitialized(false)
|
, m_posInitialized(false)
|
||||||
, force_exit(false)
|
, force_exit(false)
|
||||||
, unlockDlgShowing(false)
|
, unlockDlgShowing(false)
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
, has_python(false)
|
, has_python(false)
|
||||||
#endif
|
|
||||||
{
|
{
|
||||||
setupUi(this);
|
setupUi(this);
|
||||||
|
|
||||||
|
@ -1439,19 +1437,73 @@ void MainWindow::on_actionRSS_Reader_triggered()
|
||||||
|
|
||||||
void MainWindow::on_actionSearch_engine_triggered()
|
void MainWindow::on_actionSearch_engine_triggered()
|
||||||
{
|
{
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
if (!has_python && actionSearch_engine->isChecked()) {
|
if (!has_python && actionSearch_engine->isChecked()) {
|
||||||
bool res = false;
|
int pythonVersion = misc::pythonVersion();
|
||||||
|
|
||||||
// Check if python is already in PATH
|
// Check if python is already in PATH
|
||||||
if (misc::pythonVersion() > 0)
|
if (pythonVersion > 0)
|
||||||
res = true;
|
Logger::instance()->addMessage(tr("Python found in %1").arg("PATH"), Log::INFO); // Prevent translators from messing with PATH
|
||||||
else
|
#ifdef Q_OS_WIN
|
||||||
res = addPythonPathToEnv();
|
else if (addPythonPathToEnv())
|
||||||
|
pythonVersion = misc::pythonVersion();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool res = false;
|
||||||
|
|
||||||
|
if (pythonVersion == 2) {
|
||||||
|
// Check if python 2.7.x or later
|
||||||
|
QString version = misc::pythonVersionComplete().trimmed();
|
||||||
|
QStringList splitted = version.split('.');
|
||||||
|
if (splitted.size() > 1) {
|
||||||
|
int middleVer = splitted.at(1).toInt();
|
||||||
|
if (middleVer < 7) {
|
||||||
|
QMessageBox::information(this, tr("Old Python Interpreter"), tr("Your Python version is %1, which is too old. You need at least version 2.7.0 for python2 or 3.3.0 for python3.").arg(version));
|
||||||
|
actionSearch_engine->setChecked(false);
|
||||||
|
Preferences::instance()->setSearchEnabled(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
QMessageBox::information(this, tr("Undetermined Python version"), tr("Couldn't decode your Python version: %1").arg(version));
|
||||||
|
actionSearch_engine->setChecked(false);
|
||||||
|
Preferences::instance()->setSearchEnabled(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (pythonVersion == 3) {
|
||||||
|
// Check if python 3.3.x or later
|
||||||
|
QString version = misc::pythonVersionComplete().trimmed();
|
||||||
|
QStringList splitted = version.split('.');
|
||||||
|
if (splitted.size() > 1) {
|
||||||
|
int middleVer = splitted.at(1).toInt();
|
||||||
|
if (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. Minimum requirement: 2.7.0/3.3.0.").arg(version));
|
||||||
|
actionSearch_engine->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));
|
||||||
|
actionSearch_engine->setChecked(false);
|
||||||
|
Preferences::instance()->setSearchEnabled(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
has_python = true;
|
has_python = true;
|
||||||
}
|
}
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
else if (QMessageBox::question(this, tr("Missing Python Interpreter"),
|
else if (QMessageBox::question(this, tr("Missing Python Interpreter"),
|
||||||
tr("Python 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) {
|
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) {
|
||||||
|
@ -1461,13 +1513,16 @@ void MainWindow::on_actionSearch_engine_triggered()
|
||||||
Preferences::instance()->setSearchEnabled(false);
|
Preferences::instance()->setSearchEnabled(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
else {
|
else {
|
||||||
|
#ifndef Q_OS_WIN
|
||||||
|
QMessageBox::information(this, tr("Missing Python Interpreter"), tr("Python is required to use the search engine but it does not seem to be installed."));
|
||||||
|
#endif
|
||||||
actionSearch_engine->setChecked(false);
|
actionSearch_engine->setChecked(false);
|
||||||
Preferences::instance()->setSearchEnabled(false);
|
Preferences::instance()->setSearchEnabled(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
displaySearchTab(actionSearch_engine->isChecked());
|
displaySearchTab(actionSearch_engine->isChecked());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1627,6 +1682,7 @@ bool MainWindow::addPythonPathToEnv()
|
||||||
return true;
|
return true;
|
||||||
QString python_path = Preferences::getPythonPath();
|
QString python_path = Preferences::getPythonPath();
|
||||||
if (!python_path.isEmpty()) {
|
if (!python_path.isEmpty()) {
|
||||||
|
Logger::instance()->addMessage(tr("Python found in %1").arg(fsutils::toNativePath(python_path)), Log::INFO);
|
||||||
// Add it to PATH envvar
|
// Add it to PATH envvar
|
||||||
QString path_envar = QString::fromLocal8Bit(qgetenv("PATH").constData());
|
QString path_envar = QString::fromLocal8Bit(qgetenv("PATH").constData());
|
||||||
if (path_envar.isNull())
|
if (path_envar.isNull())
|
||||||
|
@ -1669,6 +1725,8 @@ void MainWindow::pythonDownloadSuccess(QString url, QString file_path)
|
||||||
// Reload search engine
|
// Reload search engine
|
||||||
has_python = addPythonPathToEnv();
|
has_python = addPythonPathToEnv();
|
||||||
if (has_python) {
|
if (has_python) {
|
||||||
|
// Make it print the version to Log
|
||||||
|
misc::pythonVersion();
|
||||||
actionSearch_engine->setChecked(true);
|
actionSearch_engine->setChecked(true);
|
||||||
displaySearchTab(true);
|
displaySearchTab(true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -202,9 +202,7 @@ private:
|
||||||
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
||||||
QTimer programUpdateTimer;
|
QTimer programUpdateTimer;
|
||||||
#endif
|
#endif
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
bool has_python;
|
bool has_python;
|
||||||
#endif
|
|
||||||
QMenu* toolbarMenu;
|
QMenu* toolbarMenu;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue