2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2019-10-01 13:16:17 +02:00
|
|
|
|
|
|
|
|
#include "pythonutils.h"
|
|
|
|
|
|
|
|
|
|
#include "pythonproject.h"
|
|
|
|
|
#include "pythonsettings.h"
|
2022-07-15 11:49:34 +02:00
|
|
|
#include "pythontr.h"
|
2019-10-01 13:16:17 +02:00
|
|
|
|
2021-12-13 14:19:30 +01:00
|
|
|
#include <coreplugin/messagemanager.h>
|
2019-10-01 13:16:17 +02:00
|
|
|
|
2021-12-13 14:19:30 +01:00
|
|
|
#include <projectexplorer/project.h>
|
2019-10-01 13:16:17 +02:00
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
|
|
2021-12-13 14:19:30 +01:00
|
|
|
#include <utils/algorithm.h>
|
2022-02-23 17:11:20 +01:00
|
|
|
#include <utils/mimeutils.h>
|
2022-01-24 12:20:37 +01:00
|
|
|
#include <utils/qtcprocess.h>
|
2019-10-01 13:16:17 +02:00
|
|
|
|
2022-04-13 12:26:54 +02:00
|
|
|
using namespace ProjectExplorer;
|
2019-10-01 13:16:17 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2022-07-15 11:49:34 +02:00
|
|
|
namespace Python::Internal {
|
2019-10-01 13:16:17 +02:00
|
|
|
|
2023-01-13 06:55:05 +01:00
|
|
|
static QHash<FilePath, FilePath> &userDefinedPythonsForDocument()
|
|
|
|
|
{
|
|
|
|
|
static QHash<FilePath, FilePath> userDefines;
|
|
|
|
|
return userDefines;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 14:19:30 +01:00
|
|
|
FilePath detectPython(const FilePath &documentPath)
|
2019-10-01 13:16:17 +02:00
|
|
|
{
|
2022-06-09 15:29:11 +02:00
|
|
|
Project *project = documentPath.isEmpty() ? nullptr
|
|
|
|
|
: SessionManager::projectForFile(documentPath);
|
2019-10-01 13:16:17 +02:00
|
|
|
if (!project)
|
2022-06-09 15:29:11 +02:00
|
|
|
project = SessionManager::startupProject();
|
|
|
|
|
|
|
|
|
|
Environment env = Environment::systemEnvironment();
|
2019-10-01 13:16:17 +02:00
|
|
|
|
|
|
|
|
if (project) {
|
|
|
|
|
if (auto target = project->activeTarget()) {
|
2022-04-13 12:26:54 +02:00
|
|
|
if (auto runConfig = target->activeRunConfiguration()) {
|
|
|
|
|
if (auto interpreter = runConfig->aspect<InterpreterAspect>())
|
2022-06-09 15:21:35 +02:00
|
|
|
return interpreter->currentInterpreter().command;
|
2022-06-09 15:29:11 +02:00
|
|
|
if (auto environmentAspect = runConfig->aspect<EnvironmentAspect>())
|
|
|
|
|
env = environmentAspect->environment();
|
2019-10-01 13:16:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 06:55:05 +01:00
|
|
|
const FilePath userDefined = userDefinedPythonsForDocument().value(documentPath);
|
|
|
|
|
if (userDefined.exists())
|
|
|
|
|
return userDefined;
|
|
|
|
|
|
2019-10-25 09:15:59 +02:00
|
|
|
// check whether this file is inside a python virtual environment
|
2022-06-09 15:21:35 +02:00
|
|
|
const QList<Interpreter> venvInterpreters = PythonSettings::detectPythonVenvs(documentPath);
|
|
|
|
|
if (!venvInterpreters.isEmpty())
|
|
|
|
|
return venvInterpreters.first().command;
|
2019-10-01 13:16:17 +02:00
|
|
|
|
2022-06-09 15:21:35 +02:00
|
|
|
auto defaultInterpreter = PythonSettings::defaultInterpreter().command;
|
|
|
|
|
if (defaultInterpreter.exists())
|
|
|
|
|
return defaultInterpreter;
|
2019-10-01 13:16:17 +02:00
|
|
|
|
2022-06-16 15:09:39 +02:00
|
|
|
auto pythonFromPath = [=](const QString toCheck) {
|
|
|
|
|
for (const FilePath &python : env.findAllInPath(toCheck)) {
|
|
|
|
|
// Windows creates empty redirector files that may interfere
|
|
|
|
|
if (python.exists() && python.osType() == OsTypeWindows && python.fileSize() != 0)
|
|
|
|
|
return python;
|
|
|
|
|
}
|
|
|
|
|
return FilePath();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const FilePath fromPath3 = pythonFromPath("python3");
|
|
|
|
|
if (fromPath3.exists())
|
|
|
|
|
return fromPath3;
|
2022-06-09 15:29:11 +02:00
|
|
|
|
2022-06-16 15:09:39 +02:00
|
|
|
const FilePath fromPath = pythonFromPath("python");
|
|
|
|
|
if (fromPath.exists())
|
|
|
|
|
return fromPath;
|
2022-06-09 15:29:11 +02:00
|
|
|
|
2022-06-09 15:21:35 +02:00
|
|
|
return PythonSettings::interpreters().value(0).command;
|
2019-10-01 13:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-13 06:55:05 +01:00
|
|
|
void definePythonForDocument(const FilePath &documentPath, const FilePath &python)
|
|
|
|
|
{
|
|
|
|
|
userDefinedPythonsForDocument()[documentPath] = python;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 16:26:20 +01:00
|
|
|
static QStringList replImportArgs(const FilePath &pythonFile, ReplType type)
|
|
|
|
|
{
|
|
|
|
|
using MimeTypes = QList<MimeType>;
|
|
|
|
|
const MimeTypes mimeTypes = pythonFile.isEmpty() || type == ReplType::Unmodified
|
|
|
|
|
? MimeTypes()
|
|
|
|
|
: mimeTypesForFileName(pythonFile.toString());
|
|
|
|
|
const bool isPython = Utils::anyOf(mimeTypes, [](const MimeType &mt) {
|
|
|
|
|
return mt.inherits("text/x-python") || mt.inherits("text/x-python3");
|
|
|
|
|
});
|
|
|
|
|
if (type == ReplType::Unmodified || !isPython)
|
|
|
|
|
return {};
|
|
|
|
|
const auto import = type == ReplType::Import
|
2021-06-04 07:59:00 +02:00
|
|
|
? QString("import %1").arg(pythonFile.completeBaseName())
|
|
|
|
|
: QString("from %1 import *").arg(pythonFile.completeBaseName());
|
2020-03-10 16:26:20 +01:00
|
|
|
return {"-c", QString("%1; print('Running \"%1\"')").arg(import)};
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 11:23:31 +01:00
|
|
|
void openPythonRepl(QObject *parent, const FilePath &file, ReplType type)
|
2020-03-10 16:26:20 +01:00
|
|
|
{
|
|
|
|
|
static const auto workingDir = [](const FilePath &file) {
|
|
|
|
|
if (file.isEmpty()) {
|
2022-04-13 12:26:54 +02:00
|
|
|
if (Project *project = SessionManager::startupProject())
|
2021-08-02 18:02:10 +02:00
|
|
|
return project->projectDirectory();
|
2022-11-21 12:49:33 +01:00
|
|
|
return FilePath::currentWorkingPath();
|
2020-03-10 16:26:20 +01:00
|
|
|
}
|
2022-01-05 09:58:42 +01:00
|
|
|
return file.absolutePath();
|
2020-03-10 16:26:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto args = QStringList{"-i"} + replImportArgs(file, type);
|
2022-02-10 19:25:03 +01:00
|
|
|
auto process = new QtcProcess(parent);
|
2022-02-18 00:56:14 +01:00
|
|
|
process->setTerminalMode(TerminalMode::On);
|
2020-03-10 16:26:20 +01:00
|
|
|
const FilePath pythonCommand = detectPython(file);
|
|
|
|
|
process->setCommand({pythonCommand, args});
|
|
|
|
|
process->setWorkingDirectory(workingDir(file));
|
2022-01-20 17:36:28 +01:00
|
|
|
const QString commandLine = process->commandLine().toUserOutput();
|
2022-04-06 16:54:56 +02:00
|
|
|
QObject::connect(process, &QtcProcess::done, process, [process, commandLine] {
|
|
|
|
|
if (process->error() != QProcess::UnknownError) {
|
2022-07-15 11:49:34 +02:00
|
|
|
Core::MessageManager::writeDisrupting(Tr::tr(
|
2022-04-06 16:54:56 +02:00
|
|
|
(process->error() == QProcess::FailedToStart)
|
|
|
|
|
? "Failed to run Python (%1): \"%2\"."
|
|
|
|
|
: "Error while running Python (%1): \"%2\".")
|
|
|
|
|
.arg(commandLine, process->errorString()));
|
|
|
|
|
}
|
|
|
|
|
process->deleteLater();
|
|
|
|
|
});
|
2020-03-10 16:26:20 +01:00
|
|
|
process->start();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 09:28:01 +01:00
|
|
|
QString pythonName(const FilePath &pythonPath)
|
|
|
|
|
{
|
|
|
|
|
static QHash<FilePath, QString> nameForPython;
|
|
|
|
|
if (!pythonPath.exists())
|
|
|
|
|
return {};
|
|
|
|
|
QString name = nameForPython.value(pythonPath);
|
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
|
QtcProcess pythonProcess;
|
|
|
|
|
pythonProcess.setTimeoutS(2);
|
|
|
|
|
pythonProcess.setCommand({pythonPath, {"--version"}});
|
|
|
|
|
pythonProcess.runBlocking();
|
|
|
|
|
if (pythonProcess.result() != ProcessResult::FinishedWithSuccess)
|
|
|
|
|
return {};
|
|
|
|
|
name = pythonProcess.allOutput().trimmed();
|
|
|
|
|
nameForPython[pythonPath] = name;
|
|
|
|
|
}
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 07:40:20 +01:00
|
|
|
PythonProject *pythonProjectForFile(const FilePath &pythonFile)
|
|
|
|
|
{
|
2022-04-13 12:26:54 +02:00
|
|
|
for (Project *project : SessionManager::projects()) {
|
2022-03-23 07:40:20 +01:00
|
|
|
if (auto pythonProject = qobject_cast<PythonProject *>(project)) {
|
|
|
|
|
if (pythonProject->isKnownFile(pythonFile))
|
|
|
|
|
return pythonProject;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 11:49:34 +02:00
|
|
|
} // Python::Internal
|