Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 08-24-2022, 09:05 AM   #1
Mikowmer
Member
Mikowmer began at the beginning.
 
Posts: 18
Karma: 10
Join Date: Aug 2022
Location: Australia
Device: reMarkable 2
Dealing with non-Python Dependencies

Hello!

I'm currently attempting to troubleshoot someone else's plugin for Calibre. The plugin in question is here: https://github.com/naclander/Calibre...-Driver-Plugin

At the moment, it looks like certain Rust binaries in the Cryptography module aren't importing correctly due to an 'unknown location'.
Code:
ImportError: cannot import name 'asn1' from 'cryptography.hazmat.bindings._rust' (unknown location)
The abi3.so binary files for those specific submodules are present. However, when the plugin is being loaded, those binaries are ignored.


Would it be possible to get some help on including non-Python dependencies in a Calibre plugin?
Mikowmer is offline   Reply With Quote
Old 08-24-2022, 09:16 AM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 44,650
Karma: 24495948
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
The appropriate folders need to be on sys.path before attempting the import.
kovidgoyal is offline   Reply With Quote
Advert
Old 08-24-2022, 09:33 AM   #3
Mikowmer
Member
Mikowmer began at the beginning.
 
Posts: 18
Karma: 10
Join Date: Aug 2022
Location: Australia
Device: reMarkable 2
Quote:
Originally Posted by kovidgoyal View Post
The appropriate folders need to be on sys.path before attempting the import.
... Meaning?

Is that the appropriate folders for the module, or the non-Python code?
Mikowmer is offline   Reply With Quote
Old 08-24-2022, 10:28 AM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 44,650
Karma: 24495948
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
The folder that contains the python package you are importing has to be on sys.path if it is and the abi version of the compiled module is a match it will import.
kovidgoyal is offline   Reply With Quote
Old 08-31-2022, 01:22 AM   #5
Kenivia
Junior Member
Kenivia began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Aug 2022
Device: Remarkable2
I've posted a workaround on GitHub here. You were right, the path added to sys.path was the path to the zip file.

Having the library somewhere else is not the most elegant solution, is there a better way to do this? Maybe the plugin could decompress the zip file temporarily?

Last edited by Kenivia; 08-31-2022 at 01:26 AM.
Kenivia is offline   Reply With Quote
Advert
Old 08-31-2022, 03:12 AM   #6
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 44,650
Karma: 24495948
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Code:
with self:
   import whatever
see the __enter__ method of the Plugin base class
kovidgoyal is offline   Reply With Quote
Old 07-19-2024, 03:24 AM   #7
xdgc
Junior Member
xdgc began at the beginning.
 
Posts: 7
Karma: 10
Join Date: Aug 2019
Device: Kindle DX, Kindle PW, Nook Tab, Nook Glow, reMarkable 2, Kobo Clara BW
I just found this plugin and am having the same trouble, two years later. (It's abandonware so not taking updates.)

The plugin is a zip plugin. All the dependencies are in the zip, and Python can import python text or bytecode from a zip, but it can't import native code from a zip. (Presumably this is because python text/bytecode can be compiled or executed from memory, but the OS loader has to memory-map native DSOs.)

Kovid pointed to the Plugin base class's __enter__ method, which in principle knows how to handle this situation. It scans the zipfile for unimportable files and unpacks if needed. But this is a device plugin and device plugins are not loaded as context managers, so that's inapplicable on the surface.

After some ponderment, what I think Kovid is cryptically suggesting is that the library code's startup method, prior to importing modules that include DSOs, should use itself as a context manager. Indeed this patch allows the pycrypto code to load:

Code:
diff --git a/__init__.py b/__init__.py
index e548b30..71c41bb 100644
--- a/__init__.py
+++ b/__init__.py
@@ -20,12 +20,17 @@ class RemarkablePlugin(DevicePlugin):
     supported_platforms = ["linux", "windows", "osx"]
     version = (1, 2, 3)  # The version number of this plugin
     minimum_calibre_version = (0, 7, 53)
+    seen_device = False

     FORMATS = ["epub", "pdf"]

     MANAGES_DEVICE_PRESENCE = True

     def startup(self):
+        with self:
+            return self._startup()
+
+    def _startup(self):
         # Use the plugins directory that's included with the plugin
         sys.path.append(self.plugin_path)
         global remarkable_fs
Onward now to the next issue: RemarkablePlugin's packaging script uses your system Python, whose compiled output is very likely not to be loadable by Calibre's Python.
xdgc is offline   Reply With Quote
Old 07-19-2024, 09:23 PM   #8
xdgc
Junior Member
xdgc began at the beginning.
 
Posts: 7
Karma: 10
Join Date: Aug 2019
Device: Kindle DX, Kindle PW, Nook Tab, Nook Glow, reMarkable 2, Kobo Clara BW
https://github.com/xdgc/Calibre-Rema...ree/2024-fixes
xdgc is offline   Reply With Quote
Old 07-19-2024, 09:24 PM   #9
xdgc
Junior Member
xdgc began at the beginning.
 
Posts: 7
Karma: 10
Join Date: Aug 2019
Device: Kindle DX, Kindle PW, Nook Tab, Nook Glow, reMarkable 2, Kobo Clara BW
This plugin works for me now, on MacOS.

https://github.com/xdgc/Calibre-Rema...ree/2024-fixes
xdgc is offline   Reply With Quote
Old 07-19-2024, 09:28 PM   #10
xdgc
Junior Member
xdgc began at the beginning.
 
Posts: 7
Karma: 10
Join Date: Aug 2019
Device: Kindle DX, Kindle PW, Nook Tab, Nook Glow, reMarkable 2, Kobo Clara BW
Fixed reMarkable plugin

This plugin works for me now, without any module hacks. On MacOS there is a code-signing issue, but the plugin detects if it's a problem and gives a remediation on the debug log.

I forked and patched it here [github].
xdgc is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Plugin dependencies jpwhiting Development 9 09-10-2021 10:39 AM
Python 2 to Python 3, Polyglot & Plugins DaltonST Calibre 18 04-24-2019 01:10 AM
Calibre Plugin with External Dependencies ClashTheBunny Development 1 06-22-2015 11:41 AM
Dependencies Bada Bing Calibre 3 03-11-2011 08:17 AM
[Rant] New dependencies for 0.6.11? Jellby Calibre 15 10-16-2009 11:17 AM


All times are GMT -4. The time now is 08:27 AM.


MobileRead.com is a privately owned, operated and funded community.