From b02e708b6df6cfe8d01f34d887c589bff570f8b8 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Sun, 21 Jan 2024 12:27:38 +0100 Subject: [PATCH] PythonUtils: Remove code repetition Introduce isUsableHelper() and use it from venvIsUsable() and pipIsUsable(). Change-Id: I29c869f544e28d0962bc0e357399db66f48ba3d1 Reviewed-by: Reviewed-by: David Schulz --- src/plugins/python/pythonutils.cpp | 45 +++++++++++------------------- 1 file 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 *cache, const QString &keyString, + const QString &commandArg, const FilePath &python) { - static QHash 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 cache; + return isUsableHelper(&cache, "pyVenvIsUsable", "venv", python); +} + bool pipIsUsable(const FilePath &python) { static QHash 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)