aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/python/pythonutils.cpp
diff options
context:
space:
mode:
authorJarek Kobus <jaroslaw.kobus@qt.io>2024-01-21 12:27:38 +0100
committerJarek Kobus <jaroslaw.kobus@qt.io>2024-01-22 07:40:28 +0000
commitb02e708b6df6cfe8d01f34d887c589bff570f8b8 (patch)
treebd674df06d4678f423a68721d0c2412c03a534e8 /src/plugins/python/pythonutils.cpp
parentc3d7b4de3e30cbc81ff13de30397860ed40afdb2 (diff)
PythonUtils: Remove code repetition
Introduce isUsableHelper() and use it from venvIsUsable() and pipIsUsable(). Change-Id: I29c869f544e28d0962bc0e357399db66f48ba3d1 Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/python/pythonutils.cpp')
-rw-r--r--src/plugins/python/pythonutils.cpp45
1 files changed, 17 insertions, 28 deletions
diff --git a/src/plugins/python/pythonutils.cpp b/src/plugins/python/pythonutils.cpp
index 63d1f7a7c30..a786a559053 100644
--- a/src/plugins/python/pythonutils.cpp
+++ b/src/plugins/python/pythonutils.cpp
@@ -195,52 +195,41 @@ bool isVenvPython(const FilePath &python)
return python.parentDir().parentDir().pathAppended("pyvenv.cfg").exists();
}
-bool venvIsUsable(const FilePath &python)
+static bool isUsableHelper(QHash<FilePath, bool> *cache, const QString &keyString,
+ const QString &commandArg, const FilePath &python)
{
- static QHash<FilePath, bool> cache;
- auto it = cache.find(python);
- if (it == cache.end()) {
- auto store = PersistentCacheStore::byKey(keyFromString("pyVenvIsUsable"));
+ auto it = cache->find(python);
+ if (it == cache->end()) {
+ const Key key = keyFromString(keyString);
+ const auto store = PersistentCacheStore::byKey(key);
if (store && store->value(keyFromString(python.toString())).toBool()) {
- cache.insert(python, true);
+ cache->insert(python, true);
return true;
}
Process process;
- process.setCommand({python, QStringList{"-m", "venv", "-h"}});
+ process.setCommand({python, QStringList{"-m", commandArg, "-h"}});
process.runBlocking();
const bool usable = process.result() == ProcessResult::FinishedWithSuccess;
if (usable) {
Store newStore = store.value_or(Store{});
newStore.insert(keyFromString(python.toString()), true);
- PersistentCacheStore::write(keyFromString("pyVenvIsUsable"), newStore);
+ PersistentCacheStore::write(key, newStore);
}
- it = cache.insert(python, usable);
+ it = cache->insert(python, usable);
}
return *it;
}
+bool venvIsUsable(const FilePath &python)
+{
+ static QHash<FilePath, bool> cache;
+ return isUsableHelper(&cache, "pyVenvIsUsable", "venv", python);
+}
+
bool pipIsUsable(const FilePath &python)
{
static QHash<FilePath, bool> cache;
- auto it = cache.find(python);
- if (it == cache.end()) {
- auto store = PersistentCacheStore::byKey(keyFromString("pyPipIsUsable"));
- if (store && store->value(keyFromString(python.toString())).toBool()) {
- cache.insert(python, true);
- return true;
- }
- Process process;
- process.setCommand({python, QStringList{"-m", "pip", "-h"}});
- process.runBlocking();
- const bool usable = process.result() == ProcessResult::FinishedWithSuccess;
- if (usable) {
- Store newStore = store.value_or(Store{});
- newStore.insert(keyFromString(python.toString()), true);
- PersistentCacheStore::write(keyFromString("pyPipIsUsable"), newStore);
- }
- it = cache.insert(python, usable);
- }
- return *it;
+ return isUsableHelper(&cache, "pyPipIsUsable", "pip", python);
}
QString pythonVersion(const FilePath &python)