forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -195,52 +195,41 @@ bool isVenvPython(const FilePath &python)
|
|||||||
return python.parentDir().parentDir().pathAppended("pyvenv.cfg").exists();
|
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);
|
||||||
auto it = cache.find(python);
|
if (it == cache->end()) {
|
||||||
if (it == cache.end()) {
|
const Key key = keyFromString(keyString);
|
||||||
auto store = PersistentCacheStore::byKey(keyFromString("pyVenvIsUsable"));
|
const auto store = PersistentCacheStore::byKey(key);
|
||||||
if (store && store->value(keyFromString(python.toString())).toBool()) {
|
if (store && store->value(keyFromString(python.toString())).toBool()) {
|
||||||
cache.insert(python, true);
|
cache->insert(python, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Process process;
|
Process process;
|
||||||
process.setCommand({python, QStringList{"-m", "venv", "-h"}});
|
process.setCommand({python, QStringList{"-m", commandArg, "-h"}});
|
||||||
process.runBlocking();
|
process.runBlocking();
|
||||||
const bool usable = process.result() == ProcessResult::FinishedWithSuccess;
|
const bool usable = process.result() == ProcessResult::FinishedWithSuccess;
|
||||||
if (usable) {
|
if (usable) {
|
||||||
Store newStore = store.value_or(Store{});
|
Store newStore = store.value_or(Store{});
|
||||||
newStore.insert(keyFromString(python.toString()), true);
|
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;
|
return *it;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool venvIsUsable(const FilePath &python)
|
||||||
|
{
|
||||||
|
static QHash<FilePath, bool> cache;
|
||||||
|
return isUsableHelper(&cache, "pyVenvIsUsable", "venv", python);
|
||||||
|
}
|
||||||
|
|
||||||
bool pipIsUsable(const FilePath &python)
|
bool pipIsUsable(const FilePath &python)
|
||||||
{
|
{
|
||||||
static QHash<FilePath, bool> cache;
|
static QHash<FilePath, bool> cache;
|
||||||
auto it = cache.find(python);
|
return isUsableHelper(&cache, "pyPipIsUsable", "pip", 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString pythonVersion(const FilePath &python)
|
QString pythonVersion(const FilePath &python)
|
||||||
|
Reference in New Issue
Block a user