--- /dev/null
+
+#@@CALIBRE_COMPAT_CODE_START@@
+import sys, os
+
+# Explicitly allow importing the parent folder
+if os.path.dirname(os.path.dirname(os.path.abspath(__file__))) not in sys.path:
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+# Bugfix for Calibre < 5:
+if "calibre" in sys.modules and sys.version_info[0] == 2:
+ from calibre.utils.config import config_dir
+ if os.path.join(config_dir, "plugins", "DeDRM.zip") not in sys.path:
+ sys.path.insert(0, os.path.join(config_dir, "plugins", "DeDRM.zip"))
+#@@CALIBRE_COMPAT_CODE_END@@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
+from __future__ import print_function
+
# __init__.py for DeDRM_plugin
# Copyright © 2008-2020 Apprentice Harper et al.
# Copyright © 2021 NoDRM
import time
import traceback
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
-
+#@@CALIBRE_COMPAT_CODE@@
class DeDRMError(Exception):
pass
def cli_main(self, data):
- from standalone import main
+ from .standalone import main
main(data)
def initialize(self):
from PyQt5 import Qt as QtGui
from zipfile import ZipFile
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+
+#@@CALIBRE_COMPAT_CODE@@
# calibre modules and constants.
import sys, re
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile, traceback
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
-
+#@@CALIBRE_COMPAT_CODE@@
# Wrap a stream so that output gets flushed immediately
# and also make sure that any unicode strings get
from struct import pack
from struct import unpack
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+#@@CALIBRE_COMPAT_CODE@@
class TpzDRMError(Exception):
import html.entities
import json
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+#@@CALIBRE_COMPAT_CODE@@
class DrmException(Exception):
from io import BytesIO
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+
+#@@CALIBRE_COMPAT_CODE@@
+
from ion import DrmIon, DrmIonVoucher
import os, sys
import traceback
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+
+#@@CALIBRE_COMPAT_CODE@@
from calibre.utils.config import JSONConfig
import sys
import os
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+
+#@@CALIBRE_COMPAT_CODE@@
import re
# CLI interface for the DeDRM plugin (useable without Calibre, too)
+from __future__ import absolute_import, print_function
+
# Copyright © 2021 NoDRM
OPT_SHORT_TO_LONG = [
["f", "force"]
]
-import sys, os
-IS_CALIBRE = False
-if "calibre" in sys.modules:
- IS_CALIBRE = True
+#@@CALIBRE_COMPAT_CODE@@
-# Explicitly allow importing the parent folder
-sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Explicitly set the package identifier so we are allowed to import stuff ...
__package__ = "DeDRM_plugin"
+import os, sys
global _additional_data
print("Based on DeDRM Calibre plugin by Apprentice Harper, Apprentice Alf and others.")
print("See https://github.com/noDRM/DeDRM_tools for more information.")
print()
- if IS_CALIBRE:
+ if "calibre" in sys.modules:
print("This plugin can be run through Calibre - like you are doing right now - ")
print("but it can also be executed with a standalone Python interpreter.")
else:
if arg == "--help":
print_help()
- exit(0)
+ sys.exit(0)
elif arg == "--credits":
print_credits()
- exit(0)
+ sys.exit(0)
elif arg in ["--username", "--password"]:
used_up = 1
if next is None:
print_err_header()
print("Missing parameter for argument " + arg)
- exit(1)
+ sys.exit(1)
else:
_additional_params.append(next[0])
else:
print_err_header()
print("Unknown argument: " + arg)
- exit(1)
+ sys.exit(1)
# Used up 0 additional arguments
skip_opts = False
# First element is always the ZIP name, remove that.
- if not arguments[0].lower().endswith(".zip") and not IS_CALIBRE:
+ if not arguments[0].lower().endswith(".zip") and not "calibre" in sys.modules:
print("Warning: File name does not end in .zip ...")
print(arguments)
arguments.pop(0)
import sys
import os, csv, getopt
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+#@@CALIBRE_COMPAT_CODE@@
+
import zlib, zipfile, tempfile, shutil
import traceback
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
-import sys, os
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
-
+#@@CALIBRE_COMPAT_CODE@@
from ignoblekeyGenPassHash import generate_key
# Standard Python modules.
import os, sys, re, hashlib, traceback
-from calibre_plugins.dedrm.__init__ import PLUGIN_NAME, PLUGIN_VERSION
+
+#@@CALIBRE_COMPAT_CODE@@
+
+from __init__ import PLUGIN_NAME, PLUGIN_VERSION
class NoWinePython3Exception(Exception):
import sys, os
-# Calibre stuff - so we can import from our ZIP without absolute module name
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+#@@CALIBRE_COMPAT_CODE@@
import zlib
import zipfilerugged
books = [lib.books[num - 1]]
except (ValueError, IndexError):
print("Invalid choice. Exiting...")
- exit()
+ sys.exit()
results = [decrypt_book(book, lib) for book in books]
lib.close()
books = [lib.books[num - 1]]
except (ValueError, IndexError):
print("Invalid choice. Exiting...")
- exit()
+ sys.exit()
results = [decrypt_book(book, lib) for book in books]
lib.close()
DEDRM_SRC_DIR = 'DeDRM_plugin'
+DEDRM_SRC_TMP_DIR = 'DeDRM_plugin_temp'
DEDRM_README= 'DeDRM_plugin_ReadMe.txt'
OBOK_SRC_DIR = 'Obok_plugin'
OBOK_README = 'obok_plugin_ReadMe.txt'
RELEASE_DIR = 'release'
+def patch_file(filepath):
+ f = open(filepath, "rb")
+ fn = open(filepath + ".tmp", "wb")
+ patch = open(os.path.join(DEDRM_SRC_DIR, "__calibre_compat_code.py"), "rb")
+ patchdata = patch.read()
+ patch.close()
+
+ while True:
+ line = f.readline()
+ if len(line) == 0:
+ break
+
+ if line.strip().startswith(b"#@@CALIBRE_COMPAT_CODE@@"):
+ fn.write(patchdata)
+ else:
+ fn.write(line)
+
+ f.close()
+ fn.close()
+ shutil.move(filepath + ".tmp", filepath)
+
+
def make_release(version):
try:
shutil.rmtree(RELEASE_DIR)
except:
pass
+ try:
+ shutil.rmtree(DEDRM_SRC_TMP_DIR)
+ except:
+ pass
+
os.mkdir(RELEASE_DIR)
- shutil.make_archive(DEDRM_SRC_DIR, 'zip', DEDRM_SRC_DIR)
+
+ # Copy folder
+ shutil.copytree(DEDRM_SRC_DIR, DEDRM_SRC_TMP_DIR)
+
+ # Modify folder
+ try:
+ shutil.rmtree(os.path.join(os.path.abspath(DEDRM_SRC_TMP_DIR), "__pycache__"))
+ except:
+ pass
+
+ # Patch file to add compat code.
+ for root, dirs, files in os.walk(DEDRM_SRC_TMP_DIR):
+ for name in files:
+ if name.endswith(".py"):
+ patch_file(os.path.join(root, name))
+
+
+ # Package
+ shutil.make_archive(DEDRM_SRC_DIR, 'zip', DEDRM_SRC_TMP_DIR)
shutil.make_archive(OBOK_SRC_DIR, 'zip', OBOK_SRC_DIR)
shutil.move(DEDRM_SRC_DIR+'.zip', RELEASE_DIR)
shutil.move(OBOK_SRC_DIR+'.zip', RELEASE_DIR)
shutil.copy(OBOK_README, RELEASE_DIR)
shutil.copy("ReadMe_Overview.txt", RELEASE_DIR)
+ # Remove temp folder:
+ shutil.rmtree(DEDRM_SRC_TMP_DIR)
+
if version is not None:
release_name = 'DeDRM_tools_{}'.format(version)
else: