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 <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2022-03-28 09:27:54 +02:00
parent d8589e0222
commit d0c8cc20f5
2 changed files with 37 additions and 12 deletions

View File

@@ -229,7 +229,7 @@ void InterpreterOptionsWidget::detailsChanged()
void InterpreterOptionsWidget::addItem() void InterpreterOptionsWidget::addItem()
{ {
const QModelIndex &index = m_model.indexForItem( 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); QTC_ASSERT(index.isValid(), return);
m_view.setCurrentIndex(index); 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()); 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) : id(_id)
, name(_name) , name(_name)
, command(_command) , command(_command)
, autoDetected(_autoDetected)
{} {}
static InterpreterOptionsPage &interpreterOptionsPage() static InterpreterOptionsPage &interpreterOptionsPage()
@@ -371,15 +375,29 @@ static SavedSettings fromSettings(QSettings *settings)
QList<Interpreter> pythons; QList<Interpreter> pythons;
settings->beginGroup(settingsGroupKey); settings->beginGroup(settingsGroupKey);
const QVariantList interpreters = settings->value(interpreterKey).toList(); const QVariantList interpreters = settings->value(interpreterKey).toList();
QList<Interpreter> oldSettings;
for (const QVariant &interpreterVar : interpreters) { for (const QVariant &interpreterVar : interpreters) {
auto interpreterList = interpreterVar.toList(); auto interpreterList = interpreterVar.toList();
if (interpreterList.size() != 3) const Interpreter interpreter{interpreterList.value(0).toString(),
continue; interpreterList.value(1).toString(),
pythons << Interpreter{interpreterList.value(0).toString(), FilePath::fromVariant(interpreterList.value(2)),
interpreterList.value(1).toString(), interpreterList.value(3, true).toBool()};
FilePath::fromVariant(interpreterList.value(2))}; 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(); const QString defaultId = settings->value(defaultKey).toString();
settings->endGroup(); settings->endGroup();
@@ -390,10 +408,15 @@ static SavedSettings fromSettings(QSettings *settings)
static void toSettings(QSettings *settings, const SavedSettings &savedSettings) static void toSettings(QSettings *settings, const SavedSettings &savedSettings)
{ {
settings->beginGroup(settingsGroupKey); settings->beginGroup(settingsGroupKey);
const QVariantList interpretersVar QVariantList interpretersVar;
= Utils::transform(savedSettings.pythons, [](const Interpreter &interpreter) { for (const Interpreter &interpreter : savedSettings.pythons) {
return QVariant({interpreter.id, interpreter.name, interpreter.command.toVariant()}); 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(interpreterKey, interpretersVar);
settings->setValue(defaultKey, savedSettings.defaultId); settings->setValue(defaultKey, savedSettings.defaultId);
settings->endGroup(); settings->endGroup();

View File

@@ -42,7 +42,8 @@ public:
bool windowedSuffix = false); bool windowedSuffix = false);
Interpreter(const QString &id, Interpreter(const QString &id,
const QString &name, const QString &name,
const Utils::FilePath &command); const Utils::FilePath &command,
bool autoDetected = true);
inline bool operator==(const Interpreter &other) const inline bool operator==(const Interpreter &other) const
{ {
@@ -52,6 +53,7 @@ public:
QString id = QUuid::createUuid().toString(); QString id = QUuid::createUuid().toString();
QString name; QString name;
Utils::FilePath command; Utils::FilePath command;
bool autoDetected = true;
}; };
class PythonSettings : public QObject class PythonSettings : public QObject