Python: export Interpreter generator

Change-Id: I591bd6c14706e1699f028a9a3a6dfd9b89eec66a
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2019-10-24 14:28:58 +02:00
parent 01df962d6a
commit 95bd328c31
2 changed files with 23 additions and 11 deletions

View File

@@ -272,15 +272,14 @@ static bool alreadyRegistered(const QList<Interpreter> &pythons, const FilePath
}); });
} }
Interpreter interpreterForPythonExecutable(const FilePath &python, Interpreter::Interpreter(const FilePath &python, const QString &defaultName, bool windowedSuffix)
const QString &defaultName, : id(QUuid::createUuid().toString())
bool windowedSuffix = false) , command(python)
{ {
SynchronousProcess pythonProcess; SynchronousProcess pythonProcess;
pythonProcess.setProcessChannelMode(QProcess::MergedChannels); pythonProcess.setProcessChannelMode(QProcess::MergedChannels);
SynchronousProcessResponse response = pythonProcess.runBlocking( SynchronousProcessResponse response = pythonProcess.runBlocking(
CommandLine(python, {"--version"})); CommandLine(python, {"--version"}));
QString name;
if (response.result == SynchronousProcessResponse::Finished) if (response.result == SynchronousProcessResponse::Finished)
name = response.stdOut().trimmed(); name = response.stdOut().trimmed();
if (name.isEmpty()) if (name.isEmpty())
@@ -290,9 +289,14 @@ Interpreter interpreterForPythonExecutable(const FilePath &python,
QDir pythonDir(python.parentDir().toString()); QDir pythonDir(python.parentDir().toString());
if (pythonDir.exists() && pythonDir.exists("activate") && pythonDir.cdUp()) if (pythonDir.exists() && pythonDir.exists("activate") && pythonDir.cdUp())
name += QString(" (%1 Virtual Environment)").arg(pythonDir.dirName()); name += QString(" (%1 Virtual Environment)").arg(pythonDir.dirName());
return Interpreter{QUuid::createUuid().toString(), name, python};
} }
Interpreter::Interpreter(const QString &_id, const QString &_name, const FilePath &_command)
: id(_id)
, name(_name)
, command(_command)
{}
static InterpreterOptionsPage &interpreterOptionsPage() static InterpreterOptionsPage &interpreterOptionsPage()
{ {
static InterpreterOptionsPage page; static InterpreterOptionsPage page;
@@ -388,11 +392,11 @@ static void addPythonsFromRegistry(QList<Interpreter> &pythons)
const FilePath &path = FilePath::fromUserInput(regVal.toString()); const FilePath &path = FilePath::fromUserInput(regVal.toString());
const FilePath &python = path.pathAppended(HostOsInfo::withExecutableSuffix("python")); const FilePath &python = path.pathAppended(HostOsInfo::withExecutableSuffix("python"));
if (python.exists() && !alreadyRegistered(pythons, python)) if (python.exists() && !alreadyRegistered(pythons, python))
pythons << interpreterForPythonExecutable(python, "Python " + versionGroup); pythons << Interpreter(python, "Python " + versionGroup);
const FilePath &pythonw = path.pathAppended( const FilePath &pythonw = path.pathAppended(
HostOsInfo::withExecutableSuffix("pythonw")); HostOsInfo::withExecutableSuffix("pythonw"));
if (pythonw.exists() && !alreadyRegistered(pythons, pythonw)) if (pythonw.exists() && !alreadyRegistered(pythons, pythonw))
pythons << interpreterForPythonExecutable(pythonw, "Python " + versionGroup, true); pythons << Interpreter(pythonw, "Python " + versionGroup, true);
} }
pythonRegistry.endGroup(); pythonRegistry.endGroup();
} }
@@ -405,11 +409,11 @@ static void addPythonsFromPath(QList<Interpreter> &pythons)
if (HostOsInfo::isWindowsHost()) { if (HostOsInfo::isWindowsHost()) {
for (const FilePath &executable : env.findAllInPath("python")) { for (const FilePath &executable : env.findAllInPath("python")) {
if (executable.exists() && !alreadyRegistered(pythons, executable)) if (executable.exists() && !alreadyRegistered(pythons, executable))
pythons << interpreterForPythonExecutable(executable, "Python from Path"); pythons << Interpreter(executable, "Python from Path");
} }
for (const FilePath &executable : env.findAllInPath("pythonw")) { for (const FilePath &executable : env.findAllInPath("pythonw")) {
if (executable.exists() && !alreadyRegistered(pythons, executable)) if (executable.exists() && !alreadyRegistered(pythons, executable))
pythons << interpreterForPythonExecutable(executable, "Python from Path", true); pythons << Interpreter(executable, "Python from Path", true);
} }
} else { } else {
const QStringList filters = {"python", const QStringList filters = {"python",
@@ -421,7 +425,7 @@ static void addPythonsFromPath(QList<Interpreter> &pythons)
for (const QFileInfo &fi : dir.entryInfoList(filters)) { for (const QFileInfo &fi : dir.entryInfoList(filters)) {
const FilePath executable = Utils::FilePath::fromFileInfo(fi); const FilePath executable = Utils::FilePath::fromFileInfo(fi);
if (executable.exists() && !alreadyRegistered(pythons, executable)) if (executable.exists() && !alreadyRegistered(pythons, executable))
pythons << interpreterForPythonExecutable(executable, "Python from Path"); pythons << Interpreter(executable, "Python from Path");
} }
} }
} }

View File

@@ -35,7 +35,15 @@ namespace Internal {
class Interpreter class Interpreter
{ {
public: public:
QString id; Interpreter() = default;
Interpreter(const Utils::FilePath &python,
const QString &defaultName,
bool windowedSuffix = false);
Interpreter(const QString &id,
const QString &name,
const Utils::FilePath &command);
QString id = QUuid::createUuid().toString();
QString name; QString name;
Utils::FilePath command; Utils::FilePath command;
}; };