diff options
author | David Schulz <david.schulz@qt.io> | 2022-03-15 08:33:23 +0100 |
---|---|---|
committer | David Schulz <david.schulz@qt.io> | 2022-03-21 11:38:06 +0000 |
commit | 6f520f8783292e6bc31cd96082e4459a61d1a772 (patch) | |
tree | de9d73a1398fb4352f36b38e8c8bb887f8f52c54 /src/plugins/python/pythonlanguageclient.cpp | |
parent | c94564d910f653e97e201c25006e8b78bd07a8d4 (diff) | |
download | qt-creator-6f520f8783292e6bc31cd96082e4459a61d1a772.tar.gz |
Python: extract pip installation task
Make it reusable for pyside and other packages.
Change-Id: If97e65a506d36916aaa61f48b9a2f71c458d4fe9
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'src/plugins/python/pythonlanguageclient.cpp')
-rw-r--r-- | src/plugins/python/pythonlanguageclient.cpp | 106 |
1 files changed, 14 insertions, 92 deletions
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<TextEditor::TextDocument> 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<void>::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<void> m_future; - QFutureWatcher<void> m_watcher; - QtcProcess m_process; - QTimer m_killTimer; - const FilePath m_python; - QPointer<TextEditor::TextDocument> m_document; -}; - void PyLSConfigureAssistant::installPythonLanguageServer(const FilePath &python, QPointer<TextEditor::TextDocument> 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" |