DeDRM_tools/DeDRM_calibre_plugin/DeDRM_plugin/zipfix.py

61 lines
2.3 KiB
Python
Raw Normal View History

2010-11-11 15:11:36 -07:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2010-11-11 15:11:36 -07:00
2013-04-05 10:44:48 -06:00
from __future__ import with_statement
__license__ = 'GPL v3'
2010-11-11 15:11:36 -07:00
2013-04-05 10:44:48 -06:00
# Standard Python modules.
import os, sys, re, hashlib
from calibre_plugins.dedrm.__init__ import PLUGIN_NAME, PLUGIN_VERSION
2010-11-11 15:11:36 -07:00
2013-04-05 10:44:48 -06:00
def WineGetKeys(scriptpath, extension, wineprefix=""):
import subprocess
from subprocess import Popen, PIPE, STDOUT
2010-11-11 15:11:36 -07:00
2013-04-05 10:44:48 -06:00
import subasyncio
from subasyncio import Process
2010-11-11 15:11:36 -07:00
2013-04-05 10:44:48 -06:00
if extension == u".k4i":
import json
2010-11-11 15:11:36 -07:00
2013-04-05 10:44:48 -06:00
basepath, script = os.path.split(scriptpath)
print u"{0} v{1}: Running {2} under Wine".format(PLUGIN_NAME, PLUGIN_VERSION, script)
2010-11-11 15:11:36 -07:00
2013-04-05 10:44:48 -06:00
outdirpath = os.path.join(basepath, u"winekeysdir")
if not os.path.exists(outdirpath):
os.mkdir(outdirpath)
2012-11-07 06:14:25 -07:00
2013-04-05 10:44:48 -06:00
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)
2010-11-11 15:11:36 -07:00
try:
2013-04-05 10:44:48 -06:00
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")
2010-11-11 15:11:36 -07:00
except Exception, e:
2013-04-05 10:44:48 -06:00
print u"{0} v{1}: Wine subprocess call error: {2}".format(PLUGIN_NAME, PLUGIN_VERSION, e.args[0])
return []
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