Python: move python name cache to utils

Change-Id: I7f6d13a465be6de90aea64e7f19c92bca3ee6c19
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2022-03-16 09:28:01 +01:00
parent b8c8e0ccae
commit ff686f5679
3 changed files with 20 additions and 19 deletions

View File

@@ -77,25 +77,6 @@ struct PythonLanguageServerState
FilePath pylsModulePath;
};
static QString pythonName(const FilePath &pythonPath)
{
static QHash<FilePath, QString> nameForPython;
if (!pythonPath.exists())
return {};
QString name = nameForPython.value(pythonPath);
if (name.isEmpty()) {
QtcProcess pythonProcess;
pythonProcess.setTimeoutS(2);
pythonProcess.setCommand({pythonPath, {"--version"}});
pythonProcess.runBlocking();
if (pythonProcess.result() != ProcessResult::FinishedWithSuccess)
return {};
name = pythonProcess.allOutput().trimmed();
nameForPython[pythonPath] = name;
}
return name;
}
FilePath getPylsModulePath(CommandLine pylsCommand)
{
static QMutex mutex; // protect the access to the cache

View File

@@ -127,5 +127,24 @@ void openPythonRepl(QObject *parent, const FilePath &file, ReplType type)
process->start();
}
QString pythonName(const FilePath &pythonPath)
{
static QHash<FilePath, QString> nameForPython;
if (!pythonPath.exists())
return {};
QString name = nameForPython.value(pythonPath);
if (name.isEmpty()) {
QtcProcess pythonProcess;
pythonProcess.setTimeoutS(2);
pythonProcess.setCommand({pythonPath, {"--version"}});
pythonProcess.runBlocking();
if (pythonProcess.result() != ProcessResult::FinishedWithSuccess)
return {};
name = pythonProcess.allOutput().trimmed();
nameForPython[pythonPath] = name;
}
return name;
}
} // namespace Internal
} // namespace Python

View File

@@ -33,6 +33,7 @@ namespace Internal {
enum class ReplType { Unmodified, Import, ImportToplevel };
void openPythonRepl(QObject *parent, const Utils::FilePath &file, ReplType type);
Utils::FilePath detectPython(const Utils::FilePath &documentPath);
QString pythonName(const Utils::FilePath &pythonPath);
} // namespace Internal
} // namespace Python