2020-09-27 04:54:49 -06:00
|
|
|
#!/usr/bin/env python3
|
2013-10-02 12:59:40 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
__license__ = 'GPL v3'
|
|
|
|
|
|
|
|
# Standard Python modules.
|
2020-01-20 05:52:54 -07:00
|
|
|
import os, sys, re, hashlib, traceback
|
2021-12-29 03:36:59 -07:00
|
|
|
|
|
|
|
#@@CALIBRE_COMPAT_CODE@@
|
|
|
|
|
|
|
|
from __init__ import PLUGIN_NAME, PLUGIN_VERSION
|
2013-10-02 12:59:40 -06:00
|
|
|
|
|
|
|
|
2020-11-09 16:51:13 -07:00
|
|
|
class NoWinePython3Exception(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class WinePythonCLI:
|
|
|
|
py3_test = "import sys; sys.exit(0 if (sys.version_info.major==3) else 1)"
|
|
|
|
def __init__(self, wineprefix=""):
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
if wineprefix != "":
|
|
|
|
wineprefix = os.path.abspath(os.path.expanduser(os.path.expandvars(wineprefix)))
|
|
|
|
|
|
|
|
if wineprefix != "" and os.path.exists(wineprefix):
|
|
|
|
self.wineprefix = wineprefix
|
|
|
|
else:
|
|
|
|
self.wineprefix = None
|
|
|
|
|
|
|
|
candidate_execs = [
|
|
|
|
["wine", "py.exe", "-3"],
|
|
|
|
["wine", "python3.exe"],
|
|
|
|
["wine", "python.exe"],
|
|
|
|
["wine", "C:\\Python27\\python.exe"], # Should likely be removed
|
|
|
|
]
|
|
|
|
for e in candidate_execs:
|
|
|
|
self.python_exec = e
|
|
|
|
try:
|
|
|
|
self.check_call(["-c", self.py3_test])
|
|
|
|
print("{0} v{1}: Python3 exec found as {2}".format(
|
|
|
|
PLUGIN_NAME, PLUGIN_VERSION, " ".join(self.python_exec)
|
|
|
|
))
|
|
|
|
return None
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
if e.returncode == 1:
|
|
|
|
print("{0} v{1}: {2} is not python3".format(
|
|
|
|
PLUGIN_NAME, PLUGIN_VERSION, " ".join(self.python_exec)
|
|
|
|
))
|
|
|
|
elif e.returncode == 53:
|
|
|
|
print("{0} v{1}: {2} does not exist".format(
|
|
|
|
PLUGIN_NAME, PLUGIN_VERSION, " ".join(self.python_exec)
|
|
|
|
))
|
|
|
|
raise NoWinePython3Exception("Could not find python3 executable on specified wine prefix")
|
|
|
|
|
|
|
|
|
|
|
|
def check_call(self, cli_args):
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
env_dict = os.environ
|
|
|
|
env_dict["PYTHONPATH"] = ""
|
|
|
|
if self.wineprefix is not None:
|
|
|
|
env_dict["WINEPREFIX"] = self.wineprefix
|
|
|
|
|
|
|
|
subprocess.check_call(self.python_exec + cli_args, env=env_dict,
|
|
|
|
stdin=None, stdout=sys.stdout,
|
|
|
|
stderr=subprocess.STDOUT, close_fds=False,
|
|
|
|
bufsize=1)
|
|
|
|
|
|
|
|
|
|
|
|
def WineGetKeys(scriptpath, extension, wineprefix=""):
|
2013-10-02 12:59:40 -06:00
|
|
|
|
2020-09-27 04:54:49 -06:00
|
|
|
if extension == ".k4i":
|
2013-10-02 12:59:40 -06:00
|
|
|
import json
|
|
|
|
|
2020-11-09 16:51:13 -07:00
|
|
|
try:
|
|
|
|
pyexec = WinePythonCLI(wineprefix)
|
|
|
|
except NoWinePython3Exception:
|
|
|
|
print('{0} v{1}: Unable to find python3 executable in WINEPREFIX="{2}"'.format(PLUGIN_NAME, PLUGIN_VERSION, wineprefix))
|
2022-03-19 09:02:33 -06:00
|
|
|
return [], []
|
2020-11-09 16:51:13 -07:00
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
basepath, script = os.path.split(scriptpath)
|
2020-09-27 04:54:49 -06:00
|
|
|
print("{0} v{1}: Running {2} under Wine".format(PLUGIN_NAME, PLUGIN_VERSION, script))
|
2013-10-02 12:59:40 -06:00
|
|
|
|
2020-09-27 04:54:49 -06:00
|
|
|
outdirpath = os.path.join(basepath, "winekeysdir")
|
2013-10-02 12:59:40 -06:00
|
|
|
if not os.path.exists(outdirpath):
|
|
|
|
os.makedirs(outdirpath)
|
|
|
|
|
2019-06-12 13:13:25 -06:00
|
|
|
if wineprefix != "":
|
|
|
|
wineprefix = os.path.abspath(os.path.expanduser(os.path.expandvars(wineprefix)))
|
|
|
|
|
2013-10-02 12:59:40 -06:00
|
|
|
try:
|
2020-11-09 16:51:13 -07:00
|
|
|
result = pyexec.check_call([scriptpath, outdirpath])
|
2020-09-26 14:22:47 -06:00
|
|
|
except Exception as e:
|
2020-09-27 04:54:49 -06:00
|
|
|
print("{0} v{1}: Wine subprocess call error: {2}".format(PLUGIN_NAME, PLUGIN_VERSION, e.args[0]))
|
2016-01-10 23:44:44 -07:00
|
|
|
|
|
|
|
# try finding winekeys anyway, even if above code errored
|
2013-10-02 12:59:40 -06:00
|
|
|
winekeys = []
|
2021-11-15 02:47:09 -07:00
|
|
|
winekey_names = []
|
2013-10-02 12:59:40 -06:00
|
|
|
# get any files with extension in the output dir
|
|
|
|
files = [f for f in os.listdir(outdirpath) if f.endswith(extension)]
|
|
|
|
for filename in files:
|
|
|
|
try:
|
|
|
|
fpath = os.path.join(outdirpath, filename)
|
|
|
|
with open(fpath, 'rb') as keyfile:
|
2020-09-27 04:54:49 -06:00
|
|
|
if extension == ".k4i":
|
2013-10-02 12:59:40 -06:00
|
|
|
new_key_value = json.loads(keyfile.read())
|
|
|
|
else:
|
|
|
|
new_key_value = keyfile.read()
|
|
|
|
winekeys.append(new_key_value)
|
2021-11-15 02:47:09 -07:00
|
|
|
winekey_names.append(filename)
|
2013-10-02 12:59:40 -06:00
|
|
|
except:
|
2020-09-27 04:54:49 -06:00
|
|
|
print("{0} v{1}: Error loading file {2}".format(PLUGIN_NAME, PLUGIN_VERSION, filename))
|
2013-10-02 12:59:40 -06:00
|
|
|
traceback.print_exc()
|
|
|
|
os.remove(fpath)
|
2020-09-27 04:54:49 -06:00
|
|
|
print("{0} v{1}: Found and decrypted {2} {3}".format(PLUGIN_NAME, PLUGIN_VERSION, len(winekeys), "key file" if len(winekeys) == 1 else "key files"))
|
2021-11-15 02:47:09 -07:00
|
|
|
return winekeys, winekey_names
|