]> xmof Git - DeDRM.git/commitdiff
Bugfixes in standalone code for Calibre < 5 / Python 2
authorNoDRM <no_drm123@protonmail.com>
Wed, 29 Dec 2021 10:36:59 +0000 (11:36 +0100)
committerNoDRM <no_drm123@protonmail.com>
Wed, 29 Dec 2021 10:39:48 +0000 (11:39 +0100)
17 files changed:
DeDRM_plugin/__calibre_compat_code.py [new file with mode: 0644]
DeDRM_plugin/__init__.py
DeDRM_plugin/config.py
DeDRM_plugin/erdr2pml.py
DeDRM_plugin/genbook.py
DeDRM_plugin/k4mobidedrm.py
DeDRM_plugin/kfxdedrm.py
DeDRM_plugin/prefs.py
DeDRM_plugin/scriptinterface.py
DeDRM_plugin/standalone/__init__.py
DeDRM_plugin/topazextract.py
DeDRM_plugin/utilities.py
DeDRM_plugin/wineutils.py
DeDRM_plugin/zipfix.py
Obok_plugin/obok/obok.py
Other_Tools/Kobo/obok.py
make_release.py

diff --git a/DeDRM_plugin/__calibre_compat_code.py b/DeDRM_plugin/__calibre_compat_code.py
new file mode 100644 (file)
index 0000000..4896dd6
--- /dev/null
@@ -0,0 +1,14 @@
+
+#@@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@@
index 80f7f288ffe7327b9af9ae3e0ef631d28cf6632a..ac11d0fc5956c1a8de48eeb9b7e7dd5cf1be99ac 100644 (file)
@@ -1,6 +1,8 @@
 #!/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
@@ -97,9 +99,7 @@ import sys, os
 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
@@ -162,7 +162,7 @@ class DeDRM(FileTypePlugin):
 
 
     def cli_main(self, data):
-        from standalone import main
+        from .standalone import main
         main(data)
     
     def initialize(self):
index 4cdb2eabf282a60e0df458ff6004030bf3dc02ef..79e7c4d5f17dc66d9bc0f97caddf0598b905d0bd 100755 (executable)
@@ -16,8 +16,8 @@ from PyQt5.Qt import (Qt, QWidget, QHBoxLayout, QVBoxLayout, QLabel, QLineEdit,
 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.
index 70d7b46924b46699ac585df6d0042de13d1aea6b..26c947d426ee0ae488908f84187f40129bfdef7e 100755 (executable)
@@ -71,9 +71,7 @@ __version__='1.00'
 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
index a0e0d0d6d7a6181510568b32f8bbe3708fb1d42b..21186b8c650cc1ef8e97d6e767145181789148c2 100644 (file)
@@ -35,8 +35,7 @@ import getopt
 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):
index 1a6465bbf2eceea60209f6006f1eac158ac5e4ac..ec108d4dccc99a19e5cb59d035a0851804e1cb4c 100644 (file)
@@ -72,8 +72,7 @@ import time
 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):
index 0674d630ea2732ac75b2821f5081a699300a9584..cec764c2098ff3268b390cac3b1b85b7494072d1 100644 (file)
@@ -15,8 +15,9 @@ import zipfile
 
 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
 
index ae44213282f1f8cdd6e8694cd217bc9efa5be40d..4db9618e85add05cf488aa9354c1778dc88b4425 100755 (executable)
@@ -8,8 +8,8 @@ __license__ = 'GPL v3'
 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
index 3351ee724f7d80d45ebf7d03526200b8fe89ac9d..e65f4263d1c1f91ae84255ee0d32d55eadd4244b 100644 (file)
@@ -6,8 +6,8 @@
 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
index fceda3b4c53b2b567ddfced2f170db0416897ec1..dbf0cab68a9273a67fc7a0a8105653bde1161c7c 100644 (file)
@@ -3,6 +3,8 @@
 
 # CLI interface for the DeDRM plugin (useable without Calibre, too)
 
+from __future__ import absolute_import, print_function
+
 # Copyright © 2021 NoDRM
 
 OPT_SHORT_TO_LONG = [
@@ -16,15 +18,11 @@ 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
@@ -46,7 +44,7 @@ def print_help():
     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:
@@ -81,11 +79,11 @@ def handle_single_argument(arg, next):
     
     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
@@ -93,7 +91,7 @@ def handle_single_argument(arg, next):
         if next is None: 
             print_err_header()
             print("Missing parameter for argument " + arg)
-            exit(1)
+            sys.exit(1)
         else:
             _additional_params.append(next[0])
 
@@ -104,7 +102,7 @@ def handle_single_argument(arg, next):
     else:
         print_err_header()
         print("Unknown argument: " + arg)
-        exit(1)
+        sys.exit(1)
     
     
     # Used up 0 additional arguments
@@ -132,7 +130,7 @@ def main(argv):
     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)
index 4db507234c15499b50f43aa97e2127aa01287be8..8be96a240f45fcd192e4f202b51e453430bbc5f6 100644 (file)
@@ -14,8 +14,8 @@ __version__ = '6.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
index 1efd8ce5a4442c6ff520c9a715ee709ac67480ce..bd008b597a88456a002027fbc345158ea5a5872b 100644 (file)
@@ -1,10 +1,7 @@
 #!/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
 
index 071e0da12bbef6709aa32e4c2db103e84cab675f..cd79b4c47a8773fa2cd3c0d7a3a5adcbfc41adec 100644 (file)
@@ -5,7 +5,10 @@ __license__ = 'GPL v3'
 
 # 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):
index 079e49acb82cac1685533bffb2dc49bbf0c94373..3fbfbcea4058ab75c02ba6ae51213b7067d9f295 100644 (file)
@@ -22,8 +22,7 @@ __version__ = "1.1"
 
 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
index f680341fa1995cc1293fcd2b4a9664b99a22113b..e80e9aed0b4ab9eb07c4ade50ddfb323a4443aaf 100644 (file)
@@ -788,7 +788,7 @@ def cli_main():
                 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()
index 6f871149d5a7baa79c4c23e2200c9c6b1ffac829..ae088008949b2cc83d19c64d95f9d317670f71a3 100644 (file)
@@ -739,7 +739,7 @@ def cli_main():
                 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()
index e600af8a42761cbce8079b4ca0e0e52357f3a5d6..95bebf66b45b0c36a7d26b8f1e2d211f6fa9bc06 100755 (executable)
@@ -14,19 +14,65 @@ import shutil
 
 
 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)
@@ -34,6 +80,9 @@ def make_release(version):
     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: