Plugins: When loading a plugin zip file extract to temp dir and add to sys.path, if the zip file contains binay code (pyd/dll/so/dylib), instead of just adding the zip file to the path, as python cannot load compiled code from a zip file

This commit is contained in:
Kovid Goyal 2010-02-28 18:48:58 -07:00
parent 5105557e5d
commit ed2d33a584
2 changed files with 28 additions and 4 deletions

View File

@ -119,11 +119,34 @@ class Plugin(object):
def __enter__(self, *args):
if self.plugin_path is not None:
sys.path.insert(0, self.plugin_path)
from calibre.utils.zipfile import ZipFile
zf = ZipFile(self.plugin_path)
extensions = set([x.rpartition('.')[-1].lower() for x in
zf.namelist()])
zip_safe = True
for ext in ('pyd', 'so', 'dll', 'dylib'):
if ext in extensions:
zip_safe = False
if zip_safe:
sys.path.insert(0, self.plugin_path)
self._sys_insertion_path = self.plugin_path
else:
from calibre.ptempfile import TemporaryDirectory
self._sys_insertion_tdir = TemporaryDirectory('plugin_unzip')
self._sys_insertion_path = self._sys_insertion_tdir.__enter__(*args)
zf.extractall(self._sys_insertion_path)
sys.path.insert(0, self._sys_insertion_path)
zf.close()
def __exit__(self, *args):
if self.plugin_path in sys.path:
sys.path.remove(self.plugin_path)
ip, it = getattr(self, '_sys_insertion_path', None), getattr(self,
'_sys_insertion_tdir', None)
if ip in sys.path:
sys.path.remove(ip)
if hasattr(it, '__exit__'):
it.__exit__(*args)
class FileTypePlugin(Plugin):
@ -183,6 +206,7 @@ class MetadataReaderPlugin(Plugin):
type = _('Metadata reader')
def __init__(self, *args, **kwargs):
print 11111, args, kwargs
Plugin.__init__(self, *args, **kwargs)
self.quick = False

View File

@ -401,7 +401,7 @@ def initialize_plugins():
plugin = load_plugin(zfp) if not isinstance(zfp, type) else zfp
except PluginNotFound:
continue
plugin = initialize_plugin(plugin, zfp if not isinstance(zfp, type) else zfp)
plugin = initialize_plugin(plugin, None if isinstance(zfp, type) else zfp)
_initialized_plugins.append(plugin)
except:
print 'Failed to initialize plugin...'