From 115a409d921cab5c7c692e1c1a8c62c43177cb19 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 28 Jun 2018 17:11:18 +0800 Subject: [PATCH] Clear python cache conditionally Clear the cache artifacts on plugin install and plugin uninstall events. --- src/base/search/searchpluginmanager.cpp | 32 +++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/base/search/searchpluginmanager.cpp b/src/base/search/searchpluginmanager.cpp index 483f3d7b6..a72eb0472 100644 --- a/src/base/search/searchpluginmanager.cpp +++ b/src/base/search/searchpluginmanager.cpp @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -54,10 +55,29 @@ namespace { - void removePythonScript(const QString &path) + void clearPythonCache(const QString &path) { - Utils::Fs::forceRemove(path); - Utils::Fs::forceRemove(path + 'c'); // python2 pyc files + // remove python cache artifacts in `path` and subdirs + + QStringList dirs = {path}; + QDirIterator iter {path, (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories}; + while (iter.hasNext()) + dirs += iter.next(); + + for (const QString &dir : qAsConst(dirs)) { + // python 3: remove "__pycache__" folders + if (dir.endsWith("/__pycache__")) { + Utils::Fs::removeDirRecursive(dir); + continue; + } + + // python 2: remove "*.pyc" files + const QStringList files = QDir(dir).entryList(QDir::Files); + for (const QString file : files) { + if (file.endsWith(".pyc")) + Utils::Fs::forceRemove(file); + } + } } } @@ -169,6 +189,8 @@ void SearchPluginManager::installPlugin(const QString &source) { qDebug("Asked to install plugin at %s", qUtf8Printable(source)); + clearPythonCache(engineLocation()); + if (Utils::Misc::isUrl(source)) { using namespace Net; DownloadHandler *handler = DownloadManager::instance()->download(DownloadRequest(source).saveToFile(true)); @@ -209,7 +231,7 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString if (QFile::exists(destPath)) { // Backup in case install fails QFile::copy(destPath, destPath + ".bak"); - removePythonScript(destPath); + Utils::Fs::forceRemove(destPath); updated = true; } // Copy the plugin @@ -241,6 +263,8 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString bool SearchPluginManager::uninstallPlugin(const QString &name) { + clearPythonCache(engineLocation()); + // remove it from hard drive QDir pluginsFolder(pluginsLocation()); QStringList filters;