2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2022 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
|
2022-03-15 08:33:23 +01:00
|
|
|
|
|
|
|
|
#include "pipsupport.h"
|
|
|
|
|
|
2022-07-15 11:49:34 +02:00
|
|
|
#include "pythontr.h"
|
2022-03-15 08:33:23 +01:00
|
|
|
|
|
|
|
|
#include <coreplugin/messagemanager.h>
|
|
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
|
|
|
|
|
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/mimeutils.h>
|
|
|
|
|
#include <utils/qtcprocess.h>
|
|
|
|
|
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
2022-07-15 11:49:34 +02:00
|
|
|
namespace Python::Internal {
|
2022-03-15 08:33:23 +01:00
|
|
|
|
2022-07-15 11:49:34 +02:00
|
|
|
const char pipInstallTaskId[] = "Python::pipInstallTask";
|
2022-03-15 08:33:23 +01:00
|
|
|
|
2022-06-20 16:32:36 +02:00
|
|
|
PipInstallTask::PipInstallTask(const FilePath &python)
|
2022-03-15 08:33:23 +01:00
|
|
|
: m_python(python)
|
|
|
|
|
{
|
2022-06-20 16:32:36 +02:00
|
|
|
connect(&m_process, &QtcProcess::done, this, &PipInstallTask::handleDone);
|
|
|
|
|
connect(&m_process, &QtcProcess::readyReadStandardError, this, &PipInstallTask::handleError);
|
|
|
|
|
connect(&m_process, &QtcProcess::readyReadStandardOutput, this, &PipInstallTask::handleOutput);
|
|
|
|
|
connect(&m_killTimer, &QTimer::timeout, this, &PipInstallTask::cancel);
|
|
|
|
|
connect(&m_watcher, &QFutureWatcher<void>::canceled, this, &PipInstallTask::cancel);
|
2022-07-05 13:07:37 +02:00
|
|
|
m_watcher.setFuture(m_future.future());
|
2022-03-15 08:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PipInstallTask::setPackage(const PipPackage &package)
|
|
|
|
|
{
|
|
|
|
|
m_package = package;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PipInstallTask::run()
|
|
|
|
|
{
|
|
|
|
|
if (m_package.packageName.isEmpty()) {
|
|
|
|
|
emit finished(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-07-15 11:49:34 +02:00
|
|
|
const QString taskTitle = Tr::tr("Install %1").arg(m_package.displayName);
|
2022-03-15 08:33:23 +01:00
|
|
|
Core::ProgressManager::addTask(m_future.future(), taskTitle, pipInstallTaskId);
|
|
|
|
|
QString package = m_package.packageName;
|
|
|
|
|
if (!m_package.version.isEmpty())
|
|
|
|
|
package += "==" + m_package.version;
|
|
|
|
|
QStringList arguments = {"-m", "pip", "install", package};
|
|
|
|
|
|
|
|
|
|
// add --user to global pythons, but skip it for venv pythons
|
|
|
|
|
if (!QDir(m_python.parentDir().toString()).exists("activate"))
|
|
|
|
|
arguments << "--user";
|
|
|
|
|
|
|
|
|
|
m_process.setCommand({m_python, arguments});
|
|
|
|
|
m_process.start();
|
|
|
|
|
|
|
|
|
|
Core::MessageManager::writeDisrupting(
|
2022-07-15 11:49:34 +02:00
|
|
|
Tr::tr("Running \"%1\" to install %2.")
|
2022-03-15 08:33:23 +01:00
|
|
|
.arg(m_process.commandLine().toUserOutput(), m_package.displayName));
|
|
|
|
|
|
|
|
|
|
m_killTimer.setSingleShot(true);
|
|
|
|
|
m_killTimer.start(5 /*minutes*/ * 60 * 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PipInstallTask::cancel()
|
|
|
|
|
{
|
2022-06-16 10:17:33 +02:00
|
|
|
m_process.stop();
|
|
|
|
|
m_process.waitForFinished();
|
2022-03-15 08:33:23 +01:00
|
|
|
Core::MessageManager::writeFlashing(
|
2022-07-15 11:49:34 +02:00
|
|
|
Tr::tr("The %1 installation was canceled by %2.")
|
|
|
|
|
.arg(m_package.displayName, m_killTimer.isActive() ? Tr::tr("user") : Tr::tr("time out")));
|
2022-03-15 08:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 16:32:36 +02:00
|
|
|
void PipInstallTask::handleDone()
|
2022-03-15 08:33:23 +01:00
|
|
|
{
|
|
|
|
|
m_future.reportFinished();
|
|
|
|
|
const bool success = m_process.result() == ProcessResult::FinishedWithSuccess;
|
|
|
|
|
if (!success) {
|
2022-07-15 11:49:34 +02:00
|
|
|
Core::MessageManager::writeFlashing(Tr::tr("Installing the %1 failed with exit code %2")
|
2022-03-28 13:34:53 +02:00
|
|
|
.arg(m_package.displayName).arg(m_process.exitCode()));
|
2022-03-15 08:33:23 +01:00
|
|
|
}
|
|
|
|
|
emit finished(success);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PipInstallTask::handleOutput()
|
|
|
|
|
{
|
2023-01-05 17:55:04 +01:00
|
|
|
const QString &stdOut = QString::fromLocal8Bit(m_process.readAllRawStandardOutput().trimmed());
|
2022-03-15 08:33:23 +01:00
|
|
|
if (!stdOut.isEmpty())
|
|
|
|
|
Core::MessageManager::writeSilently(stdOut);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 09:00:03 +01:00
|
|
|
void PipInstallTask::handleError()
|
2022-03-15 08:33:23 +01:00
|
|
|
{
|
2023-01-05 17:55:04 +01:00
|
|
|
const QString &stdErr = QString::fromLocal8Bit(m_process.readAllRawStandardError().trimmed());
|
2022-03-15 08:33:23 +01:00
|
|
|
if (!stdErr.isEmpty())
|
|
|
|
|
Core::MessageManager::writeSilently(stdErr);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 16:32:36 +02:00
|
|
|
PipPackageInfo PipPackage::info(const FilePath &python) const
|
2022-04-08 12:58:19 +02:00
|
|
|
{
|
|
|
|
|
PipPackageInfo result;
|
|
|
|
|
|
|
|
|
|
QtcProcess pip;
|
|
|
|
|
pip.setCommand(CommandLine(python, {"-m", "pip", "show", "-f", packageName}));
|
|
|
|
|
pip.runBlocking();
|
|
|
|
|
QString fieldName;
|
|
|
|
|
QStringList data;
|
|
|
|
|
const QString pipOutput = pip.allOutput();
|
|
|
|
|
for (const QString &line : pipOutput.split('\n')) {
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
if (line.front().isSpace()) {
|
|
|
|
|
data.append(line.trimmed());
|
|
|
|
|
} else {
|
|
|
|
|
result.parseField(fieldName, data);
|
|
|
|
|
if (auto colonPos = line.indexOf(':'); colonPos >= 0) {
|
|
|
|
|
fieldName = line.left(colonPos);
|
|
|
|
|
data = QStringList(line.mid(colonPos + 1).trimmed());
|
|
|
|
|
} else {
|
|
|
|
|
fieldName.clear();
|
|
|
|
|
data.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.parseField(fieldName, data);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PipPackageInfo::parseField(const QString &field, const QStringList &data)
|
|
|
|
|
{
|
|
|
|
|
if (field.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
if (field == "Name") {
|
|
|
|
|
name = data.value(0);
|
|
|
|
|
} else if (field == "Version") {
|
|
|
|
|
version = data.value(0);
|
|
|
|
|
} else if (field == "Summary") {
|
|
|
|
|
summary = data.value(0);
|
|
|
|
|
} else if (field == "Home-page") {
|
|
|
|
|
homePage = QUrl(data.value(0));
|
|
|
|
|
} else if (field == "Author") {
|
|
|
|
|
author = data.value(0);
|
|
|
|
|
} else if (field == "Author-email") {
|
|
|
|
|
authorEmail = data.value(0);
|
|
|
|
|
} else if (field == "License") {
|
|
|
|
|
license = data.value(0);
|
|
|
|
|
} else if (field == "Location") {
|
|
|
|
|
location = FilePath::fromUserInput(data.value(0)).normalizedPathName();
|
|
|
|
|
} else if (field == "Requires") {
|
|
|
|
|
requiresPackage = data.value(0).split(',', Qt::SkipEmptyParts);
|
|
|
|
|
} else if (field == "Required-by") {
|
|
|
|
|
requiredByPackage = data.value(0).split(',', Qt::SkipEmptyParts);
|
|
|
|
|
} else if (field == "Files") {
|
|
|
|
|
for (const QString &fileName : data) {
|
|
|
|
|
if (!fileName.isEmpty())
|
|
|
|
|
files.append(FilePath::fromUserInput(fileName.trimmed()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 11:49:34 +02:00
|
|
|
} // Python::Internal
|