mirror of
https://github.com/noDRM/DeDRM_tools.git
synced 2024-08-22 00:56:09 +06:00
By default, the wineprefix passed to WineGetKeys is "". Unfortunately,
os.path.abspath(os.path.expanduser(os.path.expandvars("")))
returns the path to the working directory, which depends on the
directory from which calibre was invoked. Hence under current
behaviour the wineprefix becomes that path, no longer being the empty
string. This means that the `cmdline` that's run is always
`WINEPREFIX=/some/path/ wine python.exe [...]`, rather than `wine
python.exe [...]` even under default conditions, when the wineprefix
hasn't been changed. Unless the user is improbably lucky and invokes
calibre from ~/.wine/ (the default wineprefix), this causes automatic
retrieval of the keys to always fail.
The bug was introduced in f2190a6755
.
Checking for "" allows for correct behaviour in the default case,
while keeping the nice behaviour of expanding `~`.
76 lines
3.2 KiB
Python
76 lines
3.2 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import with_statement
|
|
|
|
__license__ = 'GPL v3'
|
|
|
|
# Standard Python modules.
|
|
import os, sys, re, hashlib
|
|
from calibre_plugins.dedrm.__init__ import PLUGIN_NAME, PLUGIN_VERSION
|
|
|
|
def WineGetKeys(scriptpath, extension, wineprefix=""):
|
|
import subprocess
|
|
from subprocess import Popen, PIPE, STDOUT
|
|
|
|
import subasyncio
|
|
from subasyncio import Process
|
|
|
|
if extension == u".k4i":
|
|
import json
|
|
|
|
basepath, script = os.path.split(scriptpath)
|
|
print u"{0} v{1}: Running {2} under Wine".format(PLUGIN_NAME, PLUGIN_VERSION, script)
|
|
|
|
outdirpath = os.path.join(basepath, u"winekeysdir")
|
|
if not os.path.exists(outdirpath):
|
|
os.makedirs(outdirpath)
|
|
|
|
if wineprefix != "":
|
|
wineprefix = os.path.abspath(os.path.expanduser(os.path.expandvars(wineprefix)))
|
|
|
|
if wineprefix != "" and os.path.exists(wineprefix):
|
|
cmdline = u"WINEPREFIX=\"{2}\" wine python.exe \"{0}\" \"{1}\"".format(scriptpath,outdirpath,wineprefix)
|
|
else:
|
|
cmdline = u"wine python.exe \"{0}\" \"{1}\"".format(scriptpath,outdirpath)
|
|
print u"{0} v{1}: Command line: '{2}'".format(PLUGIN_NAME, PLUGIN_VERSION, cmdline)
|
|
|
|
try:
|
|
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
|
p2 = Process(cmdline, shell=True, bufsize=1, stdin=None, stdout=sys.stdout, stderr=STDOUT, close_fds=False)
|
|
result = p2.wait("wait")
|
|
except Exception, e:
|
|
print u"{0} v{1}: Wine subprocess call error: {2}".format(PLUGIN_NAME, PLUGIN_VERSION, e.args[0])
|
|
if wineprefix != "" and os.path.exists(wineprefix):
|
|
cmdline = u"WINEPREFIX=\"{2}\" wine C:\\Python27\\python.exe \"{0}\" \"{1}\"".format(scriptpath,outdirpath,wineprefix)
|
|
else:
|
|
cmdline = u"wine C:\\Python27\\python.exe \"{0}\" \"{1}\"".format(scriptpath,outdirpath)
|
|
print u"{0} v{1}: Command line: “{2}”".format(PLUGIN_NAME, PLUGIN_VERSION, cmdline)
|
|
|
|
try:
|
|
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
|
p2 = Process(cmdline, shell=True, bufsize=1, stdin=None, stdout=sys.stdout, stderr=STDOUT, close_fds=False)
|
|
result = p2.wait("wait")
|
|
except Exception, e:
|
|
print u"{0} v{1}: Wine subprocess call error: {2}".format(PLUGIN_NAME, PLUGIN_VERSION, e.args[0])
|
|
|
|
# try finding winekeys anyway, even if above code errored
|
|
winekeys = []
|
|
# 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:
|
|
if extension == u".k4i":
|
|
new_key_value = json.loads(keyfile.read())
|
|
else:
|
|
new_key_value = keyfile.read()
|
|
winekeys.append(new_key_value)
|
|
except:
|
|
print u"{0} v{1}: Error loading file {2}".format(PLUGIN_NAME, PLUGIN_VERSION, filename)
|
|
traceback.print_exc()
|
|
os.remove(fpath)
|
|
print u"{0} v{1}: Found and decrypted {2} {3}".format(PLUGIN_NAME, PLUGIN_VERSION, len(winekeys), u"key file" if len(winekeys) == 1 else u"key files")
|
|
return winekeys
|