From 6f520f8783292e6bc31cd96082e4459a61d1a772 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Tue, 15 Mar 2022 08:33:23 +0100 Subject: Python: extract pip installation task Make it reusable for pyside and other packages. Change-Id: If97e65a506d36916aaa61f48b9a2f71c458d4fe9 Reviewed-by: Christian Stenger --- src/plugins/python/pythonlanguageclient.cpp | 106 ++++------------------------ 1 file changed, 14 insertions(+), 92 deletions(-) (limited to 'src/plugins/python/pythonlanguageclient.cpp') diff --git a/src/plugins/python/pythonlanguageclient.cpp b/src/plugins/python/pythonlanguageclient.cpp index 7e7827faf6..5525d9ae04 100644 --- a/src/plugins/python/pythonlanguageclient.cpp +++ b/src/plugins/python/pythonlanguageclient.cpp @@ -25,6 +25,7 @@ #include "pythonlanguageclient.h" +#include "pipsupport.h" #include "pythonconstants.h" #include "pythonplugin.h" #include "pythonproject.h" @@ -586,95 +587,6 @@ static Client *registerLanguageServer(const FilePath &python) return client; } -class PythonLSInstallHelper : public QObject -{ - Q_OBJECT -public: - PythonLSInstallHelper(const FilePath &python, QPointer document) - : m_python(python) - , m_document(document) - { - m_watcher.setFuture(m_future.future()); - } - - void run() - { - Core::ProgressManager::addTask(m_future.future(), "Install PyLS", installPylsTaskId); - connect(&m_process, &QtcProcess::finished, this, &PythonLSInstallHelper::installFinished); - connect(&m_process, - &QtcProcess::readyReadStandardError, - this, - &PythonLSInstallHelper::errorAvailable); - connect(&m_process, - &QtcProcess::readyReadStandardOutput, - this, - &PythonLSInstallHelper::outputAvailable); - - connect(&m_killTimer, &QTimer::timeout, this, &PythonLSInstallHelper::cancel); - connect(&m_watcher, &QFutureWatcher::canceled, this, &PythonLSInstallHelper::cancel); - - QStringList arguments = {"-m", "pip", "install", "python-lsp-server[all]"}; - - // 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( - tr("Running \"%1\" to install Python language server.") - .arg(m_process.commandLine().toUserOutput())); - - m_killTimer.setSingleShot(true); - m_killTimer.start(5 /*minutes*/ * 60 * 1000); - } - -private: - void cancel() - { - m_process.stopProcess(); - Core::MessageManager::writeFlashing( - tr("The Python language server installation was canceled by %1.") - .arg(m_killTimer.isActive() ? tr("user") : tr("time out"))); - } - - void installFinished() - { - m_future.reportFinished(); - if (m_process.result() == ProcessResult::FinishedWithSuccess) { - if (Client *client = registerLanguageServer(m_python)) - LanguageClientManager::openDocumentWithClient(m_document, client); - } else { - Core::MessageManager::writeFlashing( - tr("Installing the Python language server failed with exit code %1") - .arg(m_process.exitCode())); - } - deleteLater(); - } - - void outputAvailable() - { - const QString &stdOut = QString::fromLocal8Bit(m_process.readAllStandardOutput().trimmed()); - if (!stdOut.isEmpty()) - Core::MessageManager::writeSilently(stdOut); - } - - void errorAvailable() - { - const QString &stdErr = QString::fromLocal8Bit(m_process.readAllStandardError().trimmed()); - if (!stdErr.isEmpty()) - Core::MessageManager::writeSilently(stdErr); - } - - QFutureInterface m_future; - QFutureWatcher m_watcher; - QtcProcess m_process; - QTimer m_killTimer; - const FilePath m_python; - QPointer m_document; -}; - void PyLSConfigureAssistant::installPythonLanguageServer(const FilePath &python, QPointer document) { @@ -685,7 +597,19 @@ void PyLSConfigureAssistant::installPythonLanguageServer(const FilePath &python, for (TextEditor::TextDocument *additionalDocument : m_infoBarEntries[python]) additionalDocument->infoBar()->removeInfo(installPylsInfoBarId); - auto install = new PythonLSInstallHelper(python, document); + auto install = new PipInstallTask(python); + + connect(install, &PipInstallTask::finished, this, [=](const bool success) { + if (success) { + if (Client *client = registerLanguageServer(python)) { + if (document) + LanguageClientManager::openDocumentWithClient(document, client); + } + } + install->deleteLater(); + }); + + install->setPackage(PipPackage{"python-lsp-server[all]", "Python Language Server"}); install->run(); } @@ -839,5 +763,3 @@ PyLSConfigureAssistant::PyLSConfigureAssistant(QObject *parent) } // namespace Internal } // namespace Python - -#include "pythonlanguageclient.moc" -- cgit v1.2.1