mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Have plugins mirror run under python 3 and use python2 only for plugins that dont parse under python3
This commit is contained in:
parent
a1e9b55591
commit
2003641a98
@ -3,9 +3,9 @@
|
|||||||
# License: GPLv3 Copyright: 2013, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPLv3 Copyright: 2013, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
|
|
||||||
# Imports {{{
|
# Imports {{{
|
||||||
|
import argparse
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
import atexit
|
import atexit
|
||||||
import bz2
|
import bz2
|
||||||
@ -308,9 +308,28 @@ def parse_metadata(raw, namelist, zf):
|
|||||||
raise ValueError('Could not find plugin class')
|
raise ValueError('Could not find plugin class')
|
||||||
|
|
||||||
|
|
||||||
def get_plugin_info(raw):
|
def parse_plugin(raw, names, zf):
|
||||||
|
ans = parse_metadata(raw, names, zf)
|
||||||
|
if isinstance(ans, dict):
|
||||||
|
return ans
|
||||||
|
# The plugin is importing its base class from somewhere else, le sigh
|
||||||
|
for mod, _ in ans:
|
||||||
|
mod = mod.split('.')
|
||||||
|
if mod[0] == 'calibre_plugins':
|
||||||
|
mod = mod[2:]
|
||||||
|
mod = '/'.join(mod) + '.py'
|
||||||
|
if mod in names:
|
||||||
|
raw = zf.open(names[mod]).read()
|
||||||
|
ans = parse_metadata(raw, names, zf)
|
||||||
|
if isinstance(ans, dict):
|
||||||
|
return ans
|
||||||
|
|
||||||
|
raise ValueError('Failed to find plugin class')
|
||||||
|
|
||||||
|
|
||||||
|
def get_plugin_info(raw_zip):
|
||||||
metadata = None
|
metadata = None
|
||||||
with zipfile.ZipFile(io.BytesIO(raw)) as zf:
|
with zipfile.ZipFile(io.BytesIO(raw_zip)) as zf:
|
||||||
names = {x.decode('utf-8') if isinstance(x, bytes) else x : x for x in zf.namelist()}
|
names = {x.decode('utf-8') if isinstance(x, bytes) else x : x for x in zf.namelist()}
|
||||||
inits = [x for x in names if x.rpartition('/')[-1] == '__init__.py']
|
inits = [x for x in names if x.rpartition('/')[-1] == '__init__.py']
|
||||||
inits.sort(key=lambda x:x.count('/'))
|
inits.sort(key=lambda x:x.count('/'))
|
||||||
@ -325,22 +344,16 @@ def get_plugin_info(raw):
|
|||||||
if metadata is None:
|
if metadata is None:
|
||||||
raise ValueError('No __init__.py found in plugin')
|
raise ValueError('No __init__.py found in plugin')
|
||||||
raw = zf.open(metadata).read()
|
raw = zf.open(metadata).read()
|
||||||
ans = parse_metadata(raw, names, zf)
|
try:
|
||||||
if isinstance(ans, dict):
|
return parse_plugin(raw, names, zf)
|
||||||
return ans
|
except (SyntaxError, TabError, IndentationError):
|
||||||
# The plugin is importing its base class from somewhere else, le sigh
|
with tempfile.NamedTemporaryFile(suffix='.zip') as f:
|
||||||
for mod, _ in ans:
|
f.write(raw_zip)
|
||||||
mod = mod.split('.')
|
f.flush()
|
||||||
if mod[0] == 'calibre_plugins':
|
p = subprocess.Popen(['python2', __file__, f.name], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||||
mod = mod[2:]
|
stdout = p.communicate(json.dumps((raw, names)).encode('utf-8'))[0]
|
||||||
mod = '/'.join(mod) + '.py'
|
return json.loads(stdout)
|
||||||
if mod in names:
|
raise
|
||||||
raw = zf.open(names[mod]).read()
|
|
||||||
ans = parse_metadata(raw, names, zf)
|
|
||||||
if isinstance(ans, dict):
|
|
||||||
return ans
|
|
||||||
|
|
||||||
raise ValueError('Failed to find plugin class')
|
|
||||||
|
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
@ -597,7 +610,22 @@ def update_stats():
|
|||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
|
||||||
|
def py2_parse(zipfile_path):
|
||||||
|
raw, names = json.loads(sys.stdin.read())
|
||||||
|
with zipfile.ZipFile(zipfile_path) as zf:
|
||||||
|
ans = parse_plugin(raw, names, zf)
|
||||||
|
sys.stdout.write(json.dump(ans, ensure_ascii=True))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
p = argparse.ArgumentParser(
|
||||||
|
description='Mirror calibre plugins from the forums. Or parse a single plugin zip file'
|
||||||
|
' if specified on the command line'
|
||||||
|
)
|
||||||
|
p.add_argument('plugin_path', nargs='?', default='', help='Path to plugin zip file to parse')
|
||||||
|
args = p.parse_args()
|
||||||
|
if args.plugin_path:
|
||||||
|
return py2_parse(args.plugin_path)
|
||||||
try:
|
try:
|
||||||
os.chdir(WORKDIR)
|
os.chdir(WORKDIR)
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user