From d0c8cc20f5e3ce396e965465d1f29337275aac0c Mon Sep 17 00:00:00 2001 From: David Schulz Date: Mon, 28 Mar 2022 09:27:54 +0200 Subject: [PATCH] Python: automatically purge outdated autodetected interpreters Save whether an interpreter was automatically detected. Use this information on startup and check whether the path still exists to remove the interpreters that are gone. Fixes: QTCREATORBUG-27253 Change-Id: I094e573122f2800f643a2708b924a7a9d7e25ae1 Reviewed-by: Christian Stenger --- src/plugins/python/pythonsettings.cpp | 45 ++++++++++++++++++++------- src/plugins/python/pythonsettings.h | 4 ++- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/plugins/python/pythonsettings.cpp b/src/plugins/python/pythonsettings.cpp index 7ca25a63f9e..7deea9b34d8 100644 --- a/src/plugins/python/pythonsettings.cpp +++ b/src/plugins/python/pythonsettings.cpp @@ -229,7 +229,7 @@ void InterpreterOptionsWidget::detailsChanged() void InterpreterOptionsWidget::addItem() { const QModelIndex &index = m_model.indexForItem( - m_model.appendItem({QUuid::createUuid().toString(), QString("Python"), FilePath()})); + m_model.appendItem({QUuid::createUuid().toString(), QString("Python"), FilePath(), false})); QTC_ASSERT(index.isValid(), return); m_view.setCurrentIndex(index); } @@ -330,10 +330,14 @@ Interpreter::Interpreter(const FilePath &python, const QString &defaultName, boo name += QString(" (%1 Virtual Environment)").arg(pythonDir.dirName()); } -Interpreter::Interpreter(const QString &_id, const QString &_name, const FilePath &_command) +Interpreter::Interpreter(const QString &_id, + const QString &_name, + const FilePath &_command, + bool _autoDetected) : id(_id) , name(_name) , command(_command) + , autoDetected(_autoDetected) {} static InterpreterOptionsPage &interpreterOptionsPage() @@ -371,15 +375,29 @@ static SavedSettings fromSettings(QSettings *settings) QList pythons; settings->beginGroup(settingsGroupKey); const QVariantList interpreters = settings->value(interpreterKey).toList(); + QList oldSettings; for (const QVariant &interpreterVar : interpreters) { auto interpreterList = interpreterVar.toList(); - if (interpreterList.size() != 3) - continue; - pythons << Interpreter{interpreterList.value(0).toString(), - interpreterList.value(1).toString(), - FilePath::fromVariant(interpreterList.value(2))}; + const Interpreter interpreter{interpreterList.value(0).toString(), + interpreterList.value(1).toString(), + FilePath::fromVariant(interpreterList.value(2)), + interpreterList.value(3, true).toBool()}; + if (interpreterList.size() == 3) + oldSettings << interpreter; + else if (interpreterList.size() == 4) + pythons << interpreter; } + for (const Interpreter &interpreter : qAsConst(oldSettings)) { + if (Utils::anyOf(pythons, Utils::equal(&Interpreter::id, interpreter.id))) + continue; + pythons << interpreter; + } + + pythons = Utils::filtered(pythons, [](const Interpreter &interpreter){ + return !interpreter.autoDetected || interpreter.command.isExecutableFile(); + }); + const QString defaultId = settings->value(defaultKey).toString(); settings->endGroup(); @@ -390,10 +408,15 @@ static SavedSettings fromSettings(QSettings *settings) static void toSettings(QSettings *settings, const SavedSettings &savedSettings) { settings->beginGroup(settingsGroupKey); - const QVariantList interpretersVar - = Utils::transform(savedSettings.pythons, [](const Interpreter &interpreter) { - return QVariant({interpreter.id, interpreter.name, interpreter.command.toVariant()}); - }); + QVariantList interpretersVar; + for (const Interpreter &interpreter : savedSettings.pythons) { + QVariantList interpreterVar{interpreter.id, + interpreter.name, + interpreter.command.toVariant()}; + interpretersVar.append(QVariant(interpreterVar)); // old settings + interpreterVar.append(interpreter.autoDetected); + interpretersVar.append(QVariant(interpreterVar)); // new settings + } settings->setValue(interpreterKey, interpretersVar); settings->setValue(defaultKey, savedSettings.defaultId); settings->endGroup(); diff --git a/src/plugins/python/pythonsettings.h b/src/plugins/python/pythonsettings.h index 7f6ce314475..565a80d4e7e 100644 --- a/src/plugins/python/pythonsettings.h +++ b/src/plugins/python/pythonsettings.h @@ -42,7 +42,8 @@ public: bool windowedSuffix = false); Interpreter(const QString &id, const QString &name, - const Utils::FilePath &command); + const Utils::FilePath &command, + bool autoDetected = true); inline bool operator==(const Interpreter &other) const { @@ -52,6 +53,7 @@ public: QString id = QUuid::createUuid().toString(); QString name; Utils::FilePath command; + bool autoDetected = true; }; class PythonSettings : public QObject