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