mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 18:24:30 -04:00
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:
parent
5105557e5d
commit
ed2d33a584
@ -119,11 +119,34 @@ class Plugin(object):
|
|||||||
|
|
||||||
def __enter__(self, *args):
|
def __enter__(self, *args):
|
||||||
if self.plugin_path is not None:
|
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):
|
def __exit__(self, *args):
|
||||||
if self.plugin_path in sys.path:
|
ip, it = getattr(self, '_sys_insertion_path', None), getattr(self,
|
||||||
sys.path.remove(self.plugin_path)
|
'_sys_insertion_tdir', None)
|
||||||
|
if ip in sys.path:
|
||||||
|
sys.path.remove(ip)
|
||||||
|
if hasattr(it, '__exit__'):
|
||||||
|
it.__exit__(*args)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class FileTypePlugin(Plugin):
|
class FileTypePlugin(Plugin):
|
||||||
@ -183,6 +206,7 @@ class MetadataReaderPlugin(Plugin):
|
|||||||
type = _('Metadata reader')
|
type = _('Metadata reader')
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
print 11111, args, kwargs
|
||||||
Plugin.__init__(self, *args, **kwargs)
|
Plugin.__init__(self, *args, **kwargs)
|
||||||
self.quick = False
|
self.quick = False
|
||||||
|
|
||||||
|
@ -401,7 +401,7 @@ def initialize_plugins():
|
|||||||
plugin = load_plugin(zfp) if not isinstance(zfp, type) else zfp
|
plugin = load_plugin(zfp) if not isinstance(zfp, type) else zfp
|
||||||
except PluginNotFound:
|
except PluginNotFound:
|
||||||
continue
|
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)
|
_initialized_plugins.append(plugin)
|
||||||
except:
|
except:
|
||||||
print 'Failed to initialize plugin...'
|
print 'Failed to initialize plugin...'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user