Browse Source

Clear python cache conditionally

Clear the cache artifacts on plugin install and plugin uninstall events.
adaptive-webui-19844
Chocobo1 7 years ago
parent
commit
8c32302377
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 32
      src/base/search/searchpluginmanager.cpp

32
src/base/search/searchpluginmanager.cpp

@ -33,6 +33,7 @@
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QDirIterator>
#include <QDomDocument> #include <QDomDocument>
#include <QDomElement> #include <QDomElement>
#include <QDomNode> #include <QDomNode>
@ -54,10 +55,29 @@
namespace namespace
{ {
void removePythonScript(const QString &path) void clearPythonCache(const QString &path)
{ {
Utils::Fs::forceRemove(path); // remove python cache artifacts in `path` and subdirs
Utils::Fs::forceRemove(path + 'c'); // python2 pyc files
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)); qDebug("Asked to install plugin at %s", qUtf8Printable(source));
clearPythonCache(engineLocation());
if (Utils::Misc::isUrl(source)) { if (Utils::Misc::isUrl(source)) {
using namespace Net; using namespace Net;
DownloadHandler *handler = DownloadManager::instance()->downloadUrl(source, true); DownloadHandler *handler = DownloadManager::instance()->downloadUrl(source, true);
@ -209,7 +231,7 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString
if (QFile::exists(destPath)) { if (QFile::exists(destPath)) {
// Backup in case install fails // Backup in case install fails
QFile::copy(destPath, destPath + ".bak"); QFile::copy(destPath, destPath + ".bak");
removePythonScript(destPath); Utils::Fs::forceRemove(destPath);
updated = true; updated = true;
} }
// Copy the plugin // Copy the plugin
@ -241,6 +263,8 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString
bool SearchPluginManager::uninstallPlugin(const QString &name) bool SearchPluginManager::uninstallPlugin(const QString &name)
{ {
clearPythonCache(engineLocation());
// remove it from hard drive // remove it from hard drive
QDir pluginsFolder(pluginsLocation()); QDir pluginsFolder(pluginsLocation());
QStringList filters; QStringList filters;

Loading…
Cancel
Save