From 21644b25b251714d332284b51b94d74c7756a098 Mon Sep 17 00:00:00 2001 From: Florian Bach Date: Wed, 15 Dec 2021 10:09:52 +0100 Subject: [PATCH] Update some dependencies --- .gitignore | 3 +- bundle_calibre_plugin.sh | 2 + calibre-plugin/__init__.py | 90 +++++++++++++++++++++++++++++--------- calibre-plugin/config.py | 5 +-- calibre-plugin/fulfill.py | 2 +- package_modules.sh | 10 ++--- 6 files changed, 82 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 7753923..2efa203 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /calibre-plugin/*.zip -/calibre-plugin/keyextract/*.exe \ No newline at end of file +/calibre-plugin/keyextract/*.exe +/calibre-plugin/module_id.txt \ No newline at end of file diff --git a/bundle_calibre_plugin.sh b/bundle_calibre_plugin.sh index 61d61ad..daad259 100755 --- a/bundle_calibre_plugin.sh +++ b/bundle_calibre_plugin.sh @@ -14,6 +14,8 @@ make popd +echo -n "2021-12-15-01" > module_id.txt + zip -r ../calibre-plugin.zip * popd diff --git a/calibre-plugin/__init__.py b/calibre-plugin/__init__.py index b98dbf7..736c968 100644 --- a/calibre-plugin/__init__.py +++ b/calibre-plugin/__init__.py @@ -45,7 +45,7 @@ __version__ = PLUGIN_VERSION = ".".join([str(x)for x in PLUGIN_VERSION_TUPLE]) from calibre.utils.config import config_dir # type: ignore from calibre.constants import isosx, iswindows, islinux # type: ignore -import os, shutil, traceback, sys, time, io +import os, shutil, traceback, sys, time, io, random import zipfile from lxml import etree @@ -95,32 +95,82 @@ class DeACSM(FileTypePlugin): if not os.path.exists(self.moddir): os.mkdir(self.moddir) - names = ["cryptography.zip", "rsa.zip", "oscrypto.zip", "asn1crypto.zip", "pyasn1.zip"] - - lib_dict = self.load_resources(names) - # print("{0} v{1}: Copying needed library files from plugin zip".format(PLUGIN_NAME, PLUGIN_VERSION)) + # Check if we have a module id: + # Modules will only be extracted if this has changed. + # This A) saves time because we don't extract every time, + # and B) prevents a race condition. + # The compiling scripts need to be adapted to modify + # the module_id.txt in the plugin ZIP every time the + # modules change - for entry, data in lib_dict.items(): - file_path = os.path.join(self.moddir, entry) - try: - with zipfile.ZipFile(io.BytesIO(data), 'r') as ref: - ref.extractall(self.moddir) + try: + ts_file = os.path.join(self.moddir, "module_id.txt") + f = open(ts_file, "r") + id = f.readline().strip() + f.close() + except: + # No timestamp found, probably upgrading from an older version. + id = None - except: - print("{0} v{1}: Exception when copying needed library files".format(PLUGIN_NAME, PLUGIN_VERSION)) - traceback.print_exc() - pass + # Check ID file in the plugin ZIP + try: + ts_dict = self.load_resources( ["module_id.txt"] ) + id_plugin = ts_dict["module_id.txt"].decode("latin-1").split('\n')[0].strip() + except: + # No timestamp found in the plugin ZIP? + # Assume that I made a mistake bundling the plugin, extract anyways. + id_plugin = None - if islinux: - # Also extract EXE files needed for WINE ADE key extraction - names = [ "keyextract/decrypt_win32.exe", "keyextract/decrypt_win64.exe" ] + if id is None or id_plugin is None or id != id_plugin: + print("Module update from \"{0}\" to \"{1}\", extracting ...".format(id, id_plugin)) + # Something changed, extract modules. + + + if os.path.exists(self.moddir): + shutil.rmtree(self.moddir, ignore_errors=True) + + rand_path = self.moddir + str(random.randint(0, 1000000000)) + # Generate random name so we don't get conflicts with multiple instances of the plugin running at once. + # Hack-y solution, but it should work. + + os.mkdir(rand_path) + + names = ["cryptography.zip", "rsa.zip", "oscrypto.zip", "asn1crypto.zip", "pyasn1.zip"] + lib_dict = self.load_resources(names) + for entry, data in lib_dict.items(): - file_path = os.path.join(self.moddir, entry.split('/')[1]) - f = open(file_path, "wb") - f.write(data) + try: + with zipfile.ZipFile(io.BytesIO(data), 'r') as ref: + ref.extractall(rand_path) + + except: + print("{0} v{1}: Exception when copying needed library files".format(PLUGIN_NAME, PLUGIN_VERSION)) + traceback.print_exc() + pass + + if islinux: + # Also extract EXE files needed for WINE ADE key extraction + names = [ "keyextract/decrypt_win32.exe", "keyextract/decrypt_win64.exe" ] + lib_dict = self.load_resources(names) + for entry, data in lib_dict.items(): + file_path = os.path.join(rand_path, entry.split('/')[1]) + f = open(file_path, "wb") + f.write(data) + f.close() + + + # Write module ID + if id_plugin is not None: + mod_file = os.path.join(rand_path, "module_id.txt") + f = open(mod_file, "w") + f.write(id_plugin) f.close() + + # Rename temporary path to actual module path so this will be used next time. + os.rename(rand_path, self.moddir) + sys.path.insert(0, os.path.join(self.moddir, "cryptography")) sys.path.insert(0, os.path.join(self.moddir, "rsa")) sys.path.insert(0, os.path.join(self.moddir, "oscrypto")) diff --git a/calibre-plugin/config.py b/calibre-plugin/config.py index 90246f1..9b8fd47 100644 --- a/calibre-plugin/config.py +++ b/calibre-plugin/config.py @@ -1176,7 +1176,7 @@ class RentedBooksDialog(QDialog): timestamp = datetime.datetime.strptime(book_time_stamp, "%Y-%m-%dT%H:%M:%SZ") currenttime = datetime.datetime.utcnow() except: - print("Invalid book timestamp") + # Invalid book timestano continue @@ -1228,8 +1228,7 @@ class RentedBooksDialog(QDialog): ret, msg = tryReturnBook(Ret_book) if (ret): - print("Book successfully returned:") - print(msg) + print("Book successfully returned.") self.delete_book_entry(nomsg=True) self.populate_list() return info_dialog(None, "Done", "Book successfully returned", show=True, show_copy_button=False) diff --git a/calibre-plugin/fulfill.py b/calibre-plugin/fulfill.py index 6b0e4ee..ee4103e 100644 --- a/calibre-plugin/fulfill.py +++ b/calibre-plugin/fulfill.py @@ -36,7 +36,6 @@ def download(replyData): print (replyData) - metadata_node = adobe_fulfill_response.find("./%s/%s/%s" % (adNS("fulfillmentResult"), adNS("resourceItemInfo"), adNS("metadata"))) download_url = adobe_fulfill_response.find("./%s/%s/%s" % (adNS("fulfillmentResult"), adNS("resourceItemInfo"), adNS("src"))).text resource_id = adobe_fulfill_response.find("./%s/%s/%s" % (adNS("fulfillmentResult"), adNS("resourceItemInfo"), adNS("resource"))).text license_token_node = adobe_fulfill_response.find("./%s/%s/%s" % (adNS("fulfillmentResult"), adNS("resourceItemInfo"), adNS("licenseToken"))) @@ -50,6 +49,7 @@ def download(replyData): book_name = None try: + metadata_node = adobe_fulfill_response.find("./%s/%s/%s" % (adNS("fulfillmentResult"), adNS("resourceItemInfo"), adNS("metadata"))) book_name = metadata_node.find("./%s" % (adDC("title"))).text except: book_name = "Book" diff --git a/package_modules.sh b/package_modules.sh index b1bdfbc..aa13ca1 100755 --- a/package_modules.sh +++ b/package_modules.sh @@ -2,11 +2,11 @@ pushd calibre-plugin -wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/asn1crypto.zip -O asn1crypto.zip -wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/cryptography.zip -O cryptography.zip -wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/oscrypto.zip -O oscrypto.zip -wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/pyasn1.zip -O pyasn1.zip -wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/rsa.zip -O rsa.zip +wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/asn1crypto_1.4.0.zip -O asn1crypto.zip +wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/cryptography_36.0.1.zip -O cryptography.zip +wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/oscrypto_1.2.1.zip -O oscrypto.zip +wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/pyasn1_0.4.8.zip -O pyasn1.zip +wget https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/rsa_4.8.zip -O rsa.zip popd