Move related functions to Utils::Python

This commit is contained in:
Chocobo1 2018-06-19 20:04:54 +08:00
parent 383a354700
commit 51e82762c5
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
11 changed files with 198 additions and 116 deletions

View file

@ -237,106 +237,8 @@ QPoint Utils::Misc::screenCenter(const QWidget *w)
QRect r = desktop->availableGeometry(scrn);
return QPoint(r.x() + (r.width() - w->frameSize().width()) / 2, r.y() + (r.height() - w->frameSize().height()) / 2);
}
#endif
/**
* Detects the python version.
*/
int Utils::Misc::pythonVersion()
{
static int version = -1;
if (version < 0) {
QString versionComplete = pythonVersionComplete().trimmed();
QStringList splitted = versionComplete.split('.');
if (splitted.size() > 1) {
int highVer = splitted.at(0).toInt();
if ((highVer == 2) || (highVer == 3))
version = highVer;
}
}
return version;
}
/**
* Detects the python executable by calling "python --version".
*/
QString Utils::Misc::pythonExecutable()
{
static QString executable;
if (executable.isEmpty()) {
QProcess pythonProc;
#if defined(Q_OS_UNIX)
/*
* On Unix-Like Systems python2 and python3 should always exist
* http://legacy.python.org/dev/peps/pep-0394/
*/
pythonProc.start("python3", {"--version"}, QIODevice::ReadOnly);
if (pythonProc.waitForFinished() && (pythonProc.exitCode() == 0)) {
executable = "python3";
return executable;
}
pythonProc.start("python2", {"--version"}, QIODevice::ReadOnly);
if (pythonProc.waitForFinished() && (pythonProc.exitCode() == 0)) {
executable = "python2";
return executable;
}
#endif
// Look for "python" in Windows and in UNIX if "python2" and "python3" are
// not detected.
pythonProc.start("python", {"--version"}, QIODevice::ReadOnly);
if (pythonProc.waitForFinished() && (pythonProc.exitCode() == 0))
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::Misc::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
// 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;
}
QString Utils::Misc::unitString(Utils::Misc::SizeUnit unit)
{
return QCoreApplication::translate("misc",