diff options
Diffstat (limited to 'src/plugins/python/pythonsettings.cpp')
-rw-r--r-- | src/plugins/python/pythonsettings.cpp | 64 |
1 files changed, 61 insertions, 3 deletions
diff --git a/src/plugins/python/pythonsettings.cpp b/src/plugins/python/pythonsettings.cpp index d9ab47093c..acb7a50ddd 100644 --- a/src/plugins/python/pythonsettings.cpp +++ b/src/plugins/python/pythonsettings.cpp @@ -211,6 +211,7 @@ public: InterpreterOptionsPage(); void setInterpreter(const QList<Interpreter> &interpreters) { m_interpreters = interpreters; } + void addInterpreter(const Interpreter &interpreter) { m_interpreters << interpreter; } QList<Interpreter> interpreters() const { return m_interpreters; } void setDefaultInterpreter(const QString &defaultId) { m_defaultInterpreterId = defaultId; } @@ -278,6 +279,7 @@ Interpreter::Interpreter(const FilePath &python, const QString &defaultName, boo { SynchronousProcess pythonProcess; pythonProcess.setProcessChannelMode(QProcess::MergedChannels); + pythonProcess.setTimeoutS(1); SynchronousProcessResponse response = pythonProcess.runBlocking( CommandLine(python, {"--version"})); if (response.result == SynchronousProcessResponse::Finished) @@ -465,11 +467,21 @@ void PythonSettings::init() void PythonSettings::setInterpreter(const QList<Interpreter> &interpreters, const QString &defaultId) { + if (defaultId == interpreterOptionsPage().defaultInterpreter().id + && interpreters == interpreterOptionsPage().interpreters()) { + return; + } interpreterOptionsPage().setInterpreter(interpreters); interpreterOptionsPage().setDefaultInterpreter(defaultId); - toSettings(Core::ICore::settings(), {interpreters, defaultId}); - if (QTC_GUARD(settingsInstance)) - emit settingsInstance->interpretersChanged(interpreters, defaultId); + saveSettings(); +} + +void PythonSettings::addInterpreter(const Interpreter &interpreter, bool isDefault) +{ + interpreterOptionsPage().addInterpreter(interpreter); + if (isDefault) + interpreterOptionsPage().setDefaultInterpreter(interpreter.id); + saveSettings(); } PythonSettings *PythonSettings::instance() @@ -478,6 +490,52 @@ PythonSettings *PythonSettings::instance() return settingsInstance; } +QList<Interpreter> PythonSettings::detectPythonVenvs(const FilePath &path) +{ + QList<Interpreter> result; + QDir dir = path.toFileInfo().isDir() ? QDir(path.toString()) : path.toFileInfo().dir(); + if (dir.exists()) { + const QString venvPython = HostOsInfo::withExecutableSuffix("python"); + const QString activatePath = HostOsInfo::isWindowsHost() ? QString{"Scripts"} + : QString{"bin"}; + do { + for (const QString &directory : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { + if (dir.cd(directory)) { + if (dir.cd(activatePath)) { + if (dir.exists("activate") && dir.exists(venvPython)) { + FilePath python = FilePath::fromString(dir.absoluteFilePath(venvPython)); + dir.cdUp(); + const QString defaultName = QString("Python (%1 Virtual Environment)") + .arg(dir.dirName()); + Interpreter interpreter + = Utils::findOrDefault(PythonSettings::interpreters(), + Utils::equal(&Interpreter::command, python)); + if (interpreter.command.isEmpty()) { + interpreter = Interpreter(python, defaultName); + PythonSettings::addInterpreter(interpreter); + } + result << interpreter; + } else { + dir.cdUp(); + } + } + dir.cdUp(); + } + } + } while (dir.cdUp()); + } + return result; +} + +void PythonSettings::saveSettings() +{ + const QList<Interpreter> &interpreters = interpreterOptionsPage().interpreters(); + const QString &defaultId = interpreterOptionsPage().defaultInterpreter().id; + toSettings(Core::ICore::settings(), {interpreters, defaultId}); + if (QTC_GUARD(settingsInstance)) + emit settingsInstance->interpretersChanged(interpreters, defaultId); +} + QList<Interpreter> PythonSettings::interpreters() { return interpreterOptionsPage().interpreters(); |